Skip to content

Commit 9c62c61

Browse files
authored
Fix flaky SetupSequence tests (#7051)
1 parent 817f234 commit 9c62c61

File tree

1 file changed

+72
-44
lines changed

1 file changed

+72
-44
lines changed

test/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring.Tests/Windows/WindowsContainerSnapshotProviderTests.cs

Lines changed: 72 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,13 @@ public void SnapshotProvider_EmitsCpuTimeMetric()
198198
updatedAccountingInfo.TotalKernelTime = 2500;
199199
updatedAccountingInfo.TotalUserTime = 1500;
200200

201-
_jobHandleMock.SetupSequence(j => j.GetBasicAccountingInfo())
202-
.Returns(_accountingInfo)
203-
.Returns(_accountingInfo)
204-
.Returns(updatedAccountingInfo)
205-
.Returns(updatedAccountingInfo)
206-
.Throws(new InvalidOperationException("We shouldn't hit here..."));
201+
var callCount = 0;
202+
_jobHandleMock.Setup(j => j.GetBasicAccountingInfo())
203+
.Returns(() =>
204+
{
205+
callCount++;
206+
return callCount <= 2 ? _accountingInfo : updatedAccountingInfo;
207+
});
207208

208209
_sysInfo.NumberOfProcessors = 2;
209210

@@ -231,14 +232,14 @@ public void SnapshotProvider_EmitsCpuTimeMetric()
231232
var snapshot = metricCollector.GetMeasurementSnapshot();
232233
Assert.Equal(2, snapshot.Count);
233234
Assert.Contains(_accountingInfo.TotalKernelTime / (double)TimeSpan.TicksPerSecond, snapshot.Select(m => m.Value));
234-
Assert.Contains(_accountingInfo.TotalKernelTime / (double)TimeSpan.TicksPerSecond, snapshot.Select(m => m.Value));
235+
Assert.Contains(_accountingInfo.TotalUserTime / (double)TimeSpan.TicksPerSecond, snapshot.Select(m => m.Value));
235236

236237
// Step #1 - simulate 1 millisecond passing and collect metrics again:
237238
fakeClock.Advance(TimeSpan.FromMilliseconds(1));
238239
metricCollector.RecordObservableInstruments();
239240
snapshot = metricCollector.GetMeasurementSnapshot();
240241
Assert.Contains(updatedAccountingInfo.TotalKernelTime / (double)TimeSpan.TicksPerSecond, snapshot.Select(m => m.Value));
241-
Assert.Contains(updatedAccountingInfo.TotalKernelTime / (double)TimeSpan.TicksPerSecond, snapshot.Select(m => m.Value));
242+
Assert.Contains(updatedAccountingInfo.TotalUserTime / (double)TimeSpan.TicksPerSecond, snapshot.Select(m => m.Value));
242243

243244
// Step #2 - simulate 1 millisecond passing and collect metrics again:
244245
fakeClock.Advance(TimeSpan.FromMilliseconds(1));
@@ -247,7 +248,7 @@ public void SnapshotProvider_EmitsCpuTimeMetric()
247248

248249
// CPU time should be the same as before, as we're not simulating any CPU usage:
249250
Assert.Contains(updatedAccountingInfo.TotalKernelTime / (double)TimeSpan.TicksPerSecond, snapshot.Select(m => m.Value));
250-
Assert.Contains(updatedAccountingInfo.TotalKernelTime / (double)TimeSpan.TicksPerSecond, snapshot.Select(m => m.Value));
251+
Assert.Contains(updatedAccountingInfo.TotalUserTime / (double)TimeSpan.TicksPerSecond, snapshot.Select(m => m.Value));
251252
}
252253

253254
[Theory]
@@ -262,12 +263,13 @@ public void SnapshotProvider_EmitsCpuMetrics(string instrumentName, bool useZero
262263
updatedAccountingInfo.TotalKernelTime = 2500;
263264
updatedAccountingInfo.TotalUserTime = 1500;
264265

265-
_jobHandleMock.SetupSequence(j => j.GetBasicAccountingInfo())
266-
.Returns(() => _accountingInfo)
267-
.Returns(updatedAccountingInfo)
268-
.Returns(updatedAccountingInfo)
269-
.Returns(updatedAccountingInfo)
270-
.Throws(new InvalidOperationException("We shouldn't hit here..."));
266+
var callCount = 0;
267+
_jobHandleMock.Setup(j => j.GetBasicAccountingInfo())
268+
.Returns(() =>
269+
{
270+
callCount++;
271+
return callCount == 1 ? _accountingInfo : updatedAccountingInfo;
272+
});
271273

272274
_sysInfo.NumberOfProcessors = 2;
273275

@@ -330,15 +332,21 @@ public void SnapshotProvider_EmitsMemoryMetrics(string instrumentName, bool useZ
330332
_appMemoryUsage = 200UL;
331333
ulong updatedAppMemoryUsage = 600UL;
332334

333-
_processInfoMock.SetupSequence(p => p.GetCurrentProcessMemoryUsage())
334-
.Returns(() => _appMemoryUsage)
335-
.Returns(updatedAppMemoryUsage)
336-
.Throws(new InvalidOperationException("We shouldn't hit here..."));
337-
338-
_processInfoMock.SetupSequence(p => p.GetMemoryUsage())
339-
.Returns(() => _appMemoryUsage)
340-
.Returns(updatedAppMemoryUsage)
341-
.Throws(new InvalidOperationException("We shouldn't hit here..."));
335+
var processMemoryCallCount = 0;
336+
_processInfoMock.Setup(p => p.GetCurrentProcessMemoryUsage())
337+
.Returns(() =>
338+
{
339+
processMemoryCallCount++;
340+
return processMemoryCallCount <= 1 ? _appMemoryUsage : updatedAppMemoryUsage;
341+
});
342+
343+
var containerMemoryCallCount = 0;
344+
_processInfoMock.Setup(p => p.GetMemoryUsage())
345+
.Returns(() =>
346+
{
347+
containerMemoryCallCount++;
348+
return containerMemoryCallCount <= 1 ? _appMemoryUsage : updatedAppMemoryUsage;
349+
});
342350

343351
var fakeClock = new FakeTimeProvider();
344352
using var meter = new Meter(nameof(SnapshotProvider_EmitsMemoryMetrics));
@@ -387,15 +395,21 @@ public void SnapshotProvider_TestMemoryMetricsTogether()
387395
ulong updatedAppMemoryUsage = 600UL;
388396
ulong updatedContainerMemoryUsage = 1200UL;
389397

390-
_processInfoMock.SetupSequence(p => p.GetCurrentProcessMemoryUsage())
391-
.Returns(() => _appMemoryUsage)
392-
.Returns(updatedAppMemoryUsage)
393-
.Throws(new InvalidOperationException("We shouldn't hit here..."));
394-
395-
_processInfoMock.SetupSequence(p => p.GetMemoryUsage())
396-
.Returns(() => containerMemoryUsage)
397-
.Returns(updatedContainerMemoryUsage)
398-
.Throws(new InvalidOperationException("We shouldn't hit here..."));
398+
var processMemoryCallCount = 0;
399+
_processInfoMock.Setup(p => p.GetCurrentProcessMemoryUsage())
400+
.Returns(() =>
401+
{
402+
processMemoryCallCount++;
403+
return processMemoryCallCount <= 1 ? _appMemoryUsage : updatedAppMemoryUsage;
404+
});
405+
406+
var containerMemoryCallCount = 0;
407+
_processInfoMock.Setup(p => p.GetMemoryUsage())
408+
.Returns(() =>
409+
{
410+
containerMemoryCallCount++;
411+
return containerMemoryCallCount <= 1 ? containerMemoryUsage : updatedContainerMemoryUsage;
412+
});
399413

400414
var fakeClock = new FakeTimeProvider();
401415
using var meter = new Meter(nameof(SnapshotProvider_TestMemoryMetricsTogether));
@@ -465,17 +479,31 @@ public void SnapshotProvider_EmitsMemoryUsageMetric()
465479
const ulong UpdatedAppMemoryUsage = 600UL;
466480
const ulong UpdatedAppMemoryUsage2 = 300UL;
467481

468-
_processInfoMock.SetupSequence(p => p.GetCurrentProcessMemoryUsage())
469-
.Returns(() => _appMemoryUsage)
470-
.Returns(UpdatedAppMemoryUsage)
471-
.Returns(UpdatedAppMemoryUsage2)
472-
.Throws(new InvalidOperationException("We shouldn't hit here..."));
473-
474-
_processInfoMock.SetupSequence(p => p.GetMemoryUsage())
475-
.Returns(() => _appMemoryUsage)
476-
.Returns(UpdatedAppMemoryUsage)
477-
.Returns(UpdatedAppMemoryUsage2)
478-
.Throws(new InvalidOperationException("We shouldn't hit here..."));
482+
var processMemoryCallCount = 0;
483+
_processInfoMock.Setup(p => p.GetCurrentProcessMemoryUsage())
484+
.Returns(() =>
485+
{
486+
processMemoryCallCount++;
487+
return processMemoryCallCount switch
488+
{
489+
1 => _appMemoryUsage,
490+
2 => UpdatedAppMemoryUsage,
491+
_ => UpdatedAppMemoryUsage2
492+
};
493+
});
494+
495+
var containerMemoryCallCount = 0;
496+
_processInfoMock.Setup(p => p.GetMemoryUsage())
497+
.Returns(() =>
498+
{
499+
containerMemoryCallCount++;
500+
return containerMemoryCallCount switch
501+
{
502+
1 => _appMemoryUsage,
503+
2 => UpdatedAppMemoryUsage,
504+
_ => UpdatedAppMemoryUsage2
505+
};
506+
});
479507

480508
var fakeClock = new FakeTimeProvider();
481509
using var meter = new Meter(nameof(SnapshotProvider_EmitsMemoryMetrics));

0 commit comments

Comments
 (0)