Skip to content

Commit 3085c36

Browse files
committed
Improve LOH allocation check in MemoryStreamSlim test
Previously, the test only checked for LOH allocation on the first loop iteration, which could miss allocations occurring later. Now, a boolean flag tracks if LOH memory is allocated at any point during the test loops, and a final assertion ensures LOH allocation occurred. This makes the test more robust and reliable.
1 parent 28ca633 commit 3085c36

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

Source/Tst/KZDev.PerfUtils.Memory.Heap.Sequential.UnitTests/UsingMemoryStreamSlimReleaseBuffers.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public void UsingMemoryStreamSlimReleaseBuffers_CopyFullToStream_SettingReleaseM
5151
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
5252
GC.Collect(GC.MaxGeneration);
5353
using TestMetricsMonitor testMetricsMonitor = new TestMetricsMonitor();
54+
bool lohHeapAllocated = false;
5455
// Fill the streams with random bytes
5556
foreach (int testSegmentSize in TestSegmentSizes)
5657
for (int testLoop = 0; testLoop < 1000; testLoop++)
@@ -59,12 +60,16 @@ public void UsingMemoryStreamSlimReleaseBuffers_CopyFullToStream_SettingReleaseM
5960
using MemoryStreamSlim testService = MemoryStreamSlim.Create(options => options.WithZeroBufferBehavior(MemoryStreamSlimZeroBufferOption.OnRelease));
6061
int byteCount = testDataSizes[RandomSource.GetRandomInteger(testDataSizes.Length)];
6162
MemoryTestPrep.FillStreamAndArrayWithRandomBytes(testService, byteCount, testSegmentSize);
62-
// Make sure that the LOH memory usage is non-zero on the first loop.
63-
if (testLoop > 0)
63+
if (lohHeapAllocated)
6464
continue;
65-
testMetricsMonitor.LohSize.Should().BeGreaterThan(0);
65+
if (testMetricsMonitor.LohSize > 0)
66+
{
67+
lohHeapAllocated = true;
68+
}
6669
}
6770

71+
// We should hit at least one instance of a case where the LOH was allocated from
72+
lohHeapAllocated.Should().BeTrue("we should have allocated some LOH memory during the test");
6873
// Clean the GC and capture the current LOH memory usage
6974
GC.Collect(GC.MaxGeneration);
7075
double runCompleteLohMemory = testMetricsMonitor.LohSize;

0 commit comments

Comments
 (0)