Apply code formatting cleanup across monitor projects
- Reindent appsettings.json and the MSBuild project files - Collapse single-statement guard blocks and tidy using and member order - Normalize XML doc indentation and file endings - Reorder BatteryMonitor test-data helpers and move the exclusion tests into their own region - Carry the same formatting through the boiler config, control, entity and test files
This commit is contained in:
@@ -13,6 +13,60 @@ public class CostAccumulatorTests
|
||||
Month
|
||||
}
|
||||
|
||||
#region Thread safety
|
||||
|
||||
[Fact]
|
||||
public async Task AddCost_ConcurrentAccess_ShouldNotLoseData()
|
||||
{
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
var sut = new CostAccumulator(now);
|
||||
const double incrementPerCall = 0.001;
|
||||
const int callsPerThread = 1000;
|
||||
const int threadCount = 10;
|
||||
|
||||
// Act
|
||||
var tasks = Enumerable.Range(0, threadCount)
|
||||
.Select(_ => Task.Run(() =>
|
||||
{
|
||||
for (var i = 0; i < callsPerThread; i++) sut.AddCost(incrementPerCall, now.AddSeconds(1));
|
||||
}))
|
||||
.ToArray();
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
// Assert
|
||||
var snapshot = sut.GetSnapshot();
|
||||
const double expected = incrementPerCall * callsPerThread * threadCount;
|
||||
snapshot.CostMonthly.Should().BeApproximately(expected, 0.0001);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private static double GetCurrent(CostSnapshot snapshot, CostWindow window)
|
||||
{
|
||||
return window switch
|
||||
{
|
||||
CostWindow.FifteenMinutes => snapshot.Cost15Min,
|
||||
CostWindow.Hour => snapshot.Cost1Hour,
|
||||
CostWindow.Day => snapshot.CostDaily,
|
||||
CostWindow.Month => snapshot.CostMonthly,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(window))
|
||||
};
|
||||
}
|
||||
|
||||
private static double GetPrevious(CostSnapshot snapshot, CostWindow window)
|
||||
{
|
||||
return window switch
|
||||
{
|
||||
CostWindow.FifteenMinutes => snapshot.Previous15Min,
|
||||
CostWindow.Hour => snapshot.Previous1Hour,
|
||||
CostWindow.Day => snapshot.PreviousDaily,
|
||||
CostWindow.Month => snapshot.PreviousMonthly,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(window))
|
||||
};
|
||||
}
|
||||
|
||||
#region AddCost and GetSnapshot
|
||||
|
||||
[Fact]
|
||||
@@ -67,10 +121,7 @@ public class CostAccumulatorTests
|
||||
.ToArray();
|
||||
|
||||
// Act
|
||||
foreach (var increment in increments)
|
||||
{
|
||||
sut.AddCost(increment, now.AddSeconds(1));
|
||||
}
|
||||
foreach (var increment in increments) sut.AddCost(increment, now.AddSeconds(1));
|
||||
|
||||
// Assert
|
||||
var expected = increments.Sum();
|
||||
@@ -142,21 +193,66 @@ public class CostAccumulatorTests
|
||||
|
||||
public static TheoryData<string, CostWindow, DateTime, DateTime, double, double> WindowRolloverCases => new()
|
||||
{
|
||||
{ "15 minute slot ends", CostWindow.FifteenMinutes, new DateTime(2026, 3, 17, 10, 14, 0), new DateTime(2026, 3, 17, 10, 15, 0), 0.10, 0.02 },
|
||||
{ "15 minute slot ends across the hour", CostWindow.FifteenMinutes, new DateTime(2026, 3, 17, 10, 45, 0), new DateTime(2026, 3, 17, 11, 0, 0), 0.10, 0.02 },
|
||||
{ "15 minute slot ends at midnight", CostWindow.FifteenMinutes, new DateTime(2026, 3, 17, 23, 45, 0), new DateTime(2026, 3, 18, 0, 0, 0), 0.10, 0.02 },
|
||||
{ "15 minute slot ends on DST spring forward", CostWindow.FifteenMinutes, new DateTime(2026, 3, 29, 2, 45, 0), new DateTime(2026, 3, 29, 3, 0, 0), 0.10, 0.02 },
|
||||
{ "15 minute slot ends on DST fall back", CostWindow.FifteenMinutes, new DateTime(2026, 10, 25, 2, 45, 0), new DateTime(2026, 10, 25, 3, 0, 0), 0.10, 0.02 },
|
||||
{ "hour ends", CostWindow.Hour, new DateTime(2026, 3, 17, 10, 59, 0), new DateTime(2026, 3, 17, 11, 0, 0), 0.50, 0.01 },
|
||||
{ "hour ends at midnight", CostWindow.Hour, new DateTime(2026, 3, 17, 23, 59, 0), new DateTime(2026, 3, 18, 0, 0, 0), 0.50, 0.01 },
|
||||
{ "hour ends on DST spring forward", CostWindow.Hour, new DateTime(2026, 3, 29, 2, 59, 0), new DateTime(2026, 3, 29, 3, 0, 0), 0.50, 0.01 },
|
||||
{ "hour ends on DST fall back", CostWindow.Hour, new DateTime(2026, 10, 25, 2, 59, 0), new DateTime(2026, 10, 25, 3, 0, 0), 0.50, 0.01 },
|
||||
{ "day changes", CostWindow.Day, new DateTime(2026, 3, 17, 23, 59, 0), new DateTime(2026, 3, 18, 0, 0, 0), 5.00, 0.01 },
|
||||
{ "day changes at month end", CostWindow.Day, new DateTime(2026, 3, 31, 23, 59, 0), new DateTime(2026, 4, 1, 0, 0, 0), 5.00, 0.01 },
|
||||
{ "day changes at year end", CostWindow.Day, new DateTime(2026, 12, 31, 23, 59, 0), new DateTime(2027, 1, 1, 0, 0, 0), 5.00, 0.01 },
|
||||
{ "month changes", CostWindow.Month, new DateTime(2026, 3, 31, 23, 59, 0), new DateTime(2026, 4, 1, 0, 0, 0), 100.00, 0.01 },
|
||||
{ "year changes", CostWindow.Month, new DateTime(2026, 12, 31, 23, 59, 0), new DateTime(2027, 1, 1, 0, 0, 0), 100.00, 0.01 },
|
||||
{ "month changes on leap day", CostWindow.Month, new DateTime(2028, 2, 29, 23, 59, 0), new DateTime(2028, 3, 1, 0, 0, 0), 100.00, 0.01 }
|
||||
{
|
||||
"15 minute slot ends", CostWindow.FifteenMinutes, new DateTime(2026, 3, 17, 10, 14, 0),
|
||||
new DateTime(2026, 3, 17, 10, 15, 0), 0.10, 0.02
|
||||
},
|
||||
{
|
||||
"15 minute slot ends across the hour", CostWindow.FifteenMinutes, new DateTime(2026, 3, 17, 10, 45, 0),
|
||||
new DateTime(2026, 3, 17, 11, 0, 0), 0.10, 0.02
|
||||
},
|
||||
{
|
||||
"15 minute slot ends at midnight", CostWindow.FifteenMinutes, new DateTime(2026, 3, 17, 23, 45, 0),
|
||||
new DateTime(2026, 3, 18, 0, 0, 0), 0.10, 0.02
|
||||
},
|
||||
{
|
||||
"15 minute slot ends on DST spring forward", CostWindow.FifteenMinutes, new DateTime(2026, 3, 29, 2, 45, 0),
|
||||
new DateTime(2026, 3, 29, 3, 0, 0), 0.10, 0.02
|
||||
},
|
||||
{
|
||||
"15 minute slot ends on DST fall back", CostWindow.FifteenMinutes, new DateTime(2026, 10, 25, 2, 45, 0),
|
||||
new DateTime(2026, 10, 25, 3, 0, 0), 0.10, 0.02
|
||||
},
|
||||
{
|
||||
"hour ends", CostWindow.Hour, new DateTime(2026, 3, 17, 10, 59, 0), new DateTime(2026, 3, 17, 11, 0, 0),
|
||||
0.50, 0.01
|
||||
},
|
||||
{
|
||||
"hour ends at midnight", CostWindow.Hour, new DateTime(2026, 3, 17, 23, 59, 0),
|
||||
new DateTime(2026, 3, 18, 0, 0, 0), 0.50, 0.01
|
||||
},
|
||||
{
|
||||
"hour ends on DST spring forward", CostWindow.Hour, new DateTime(2026, 3, 29, 2, 59, 0),
|
||||
new DateTime(2026, 3, 29, 3, 0, 0), 0.50, 0.01
|
||||
},
|
||||
{
|
||||
"hour ends on DST fall back", CostWindow.Hour, new DateTime(2026, 10, 25, 2, 59, 0),
|
||||
new DateTime(2026, 10, 25, 3, 0, 0), 0.50, 0.01
|
||||
},
|
||||
{
|
||||
"day changes", CostWindow.Day, new DateTime(2026, 3, 17, 23, 59, 0), new DateTime(2026, 3, 18, 0, 0, 0),
|
||||
5.00, 0.01
|
||||
},
|
||||
{
|
||||
"day changes at month end", CostWindow.Day, new DateTime(2026, 3, 31, 23, 59, 0),
|
||||
new DateTime(2026, 4, 1, 0, 0, 0), 5.00, 0.01
|
||||
},
|
||||
{
|
||||
"day changes at year end", CostWindow.Day, new DateTime(2026, 12, 31, 23, 59, 0),
|
||||
new DateTime(2027, 1, 1, 0, 0, 0), 5.00, 0.01
|
||||
},
|
||||
{
|
||||
"month changes", CostWindow.Month, new DateTime(2026, 3, 31, 23, 59, 0), new DateTime(2026, 4, 1, 0, 0, 0),
|
||||
100.00, 0.01
|
||||
},
|
||||
{
|
||||
"year changes", CostWindow.Month, new DateTime(2026, 12, 31, 23, 59, 0), new DateTime(2027, 1, 1, 0, 0, 0),
|
||||
100.00, 0.01
|
||||
},
|
||||
{
|
||||
"month changes on leap day", CostWindow.Month, new DateTime(2028, 2, 29, 23, 59, 0),
|
||||
new DateTime(2028, 3, 1, 0, 0, 0), 100.00, 0.01
|
||||
}
|
||||
};
|
||||
|
||||
[Theory]
|
||||
@@ -251,7 +347,9 @@ public class CostAccumulatorTests
|
||||
public static TheoryData<string, DateTime, DateTime, bool> HourBoundaryCases => new()
|
||||
{
|
||||
{ "same hour, same minute", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 0, 0), false },
|
||||
{ "same hour, later minute", new DateTime(2026, 3, 17, 10, 10, 0), new DateTime(2026, 3, 17, 10, 11, 0), false },
|
||||
{
|
||||
"same hour, later minute", new DateTime(2026, 3, 17, 10, 10, 0), new DateTime(2026, 3, 17, 10, 11, 0), false
|
||||
},
|
||||
{ "same hour, last minute", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 59, 59), false },
|
||||
{ "next hour", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 11, 0, 0), true },
|
||||
{ "next hour, late minute", new DateTime(2026, 3, 17, 10, 59, 0), new DateTime(2026, 3, 17, 11, 59, 0), true },
|
||||
@@ -292,7 +390,10 @@ public class CostAccumulatorTests
|
||||
{ "same month, first day", new DateTime(2026, 3, 1, 0, 0, 0), new DateTime(2026, 3, 1, 0, 0, 0), false },
|
||||
{ "same month, last day", new DateTime(2026, 3, 1, 0, 0, 0), new DateTime(2026, 3, 31, 23, 59, 59), false },
|
||||
{ "next month", new DateTime(2026, 3, 31, 23, 59, 0), new DateTime(2026, 4, 1, 0, 0, 0), true },
|
||||
{ "clock moved into previous month", new DateTime(2026, 4, 1, 0, 0, 0), new DateTime(2026, 3, 31, 23, 59, 0), true },
|
||||
{
|
||||
"clock moved into previous month", new DateTime(2026, 4, 1, 0, 0, 0), new DateTime(2026, 3, 31, 23, 59, 0),
|
||||
true
|
||||
},
|
||||
{ "year rollover", new DateTime(2026, 12, 31, 23, 59, 0), new DateTime(2027, 1, 1, 0, 0, 0), true },
|
||||
{ "leap day rollover", new DateTime(2028, 2, 29, 23, 59, 0), new DateTime(2028, 3, 1, 0, 0, 0), true }
|
||||
};
|
||||
@@ -305,55 +406,4 @@ public class CostAccumulatorTests
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Thread safety
|
||||
|
||||
[Fact]
|
||||
public async Task AddCost_ConcurrentAccess_ShouldNotLoseData()
|
||||
{
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
var sut = new CostAccumulator(now);
|
||||
const double incrementPerCall = 0.001;
|
||||
const int callsPerThread = 1000;
|
||||
const int threadCount = 10;
|
||||
|
||||
// Act
|
||||
var tasks = Enumerable.Range(0, threadCount)
|
||||
.Select(_ => Task.Run(() =>
|
||||
{
|
||||
for (var i = 0; i < callsPerThread; i++)
|
||||
{
|
||||
sut.AddCost(incrementPerCall, now.AddSeconds(1));
|
||||
}
|
||||
}))
|
||||
.ToArray();
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
// Assert
|
||||
var snapshot = sut.GetSnapshot();
|
||||
const double expected = incrementPerCall * callsPerThread * threadCount;
|
||||
snapshot.CostMonthly.Should().BeApproximately(expected, 0.0001);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private static double GetCurrent(CostSnapshot snapshot, CostWindow window) => window switch
|
||||
{
|
||||
CostWindow.FifteenMinutes => snapshot.Cost15Min,
|
||||
CostWindow.Hour => snapshot.Cost1Hour,
|
||||
CostWindow.Day => snapshot.CostDaily,
|
||||
CostWindow.Month => snapshot.CostMonthly,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(window))
|
||||
};
|
||||
|
||||
private static double GetPrevious(CostSnapshot snapshot, CostWindow window) => window switch
|
||||
{
|
||||
CostWindow.FifteenMinutes => snapshot.Previous15Min,
|
||||
CostWindow.Hour => snapshot.Previous1Hour,
|
||||
CostWindow.Day => snapshot.PreviousDaily,
|
||||
CostWindow.Month => snapshot.PreviousMonthly,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(window))
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user