Skip to content

Commit

Permalink
Update TestHandleCollector() test (dotnet#68285)
Browse files Browse the repository at this point in the history
* Update TestHandleCollector() test

Initialize local member in HandleCollector
   since it was the only one not initialized.
Update test to wait for pending finalizers since that is when
   the HandleCollector API will trigger the GC.

Co-authored-by: Elinor Fung <elfung@microsoft.com>
  • Loading branch information
2 people authored and directhex committed Apr 21, 2022
1 parent 3a26b4c commit 600b03f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public HandleCollector(string? name, int initialThreshold, int maximumThreshold)
MaximumThreshold = maximumThreshold;
_threshold = initialThreshold;
_handleCount = 0;
_gcGeneration = 0;
}

public int Count => _handleCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,26 +122,35 @@ public static void Add_Overflows_ThrowsInvalidOperationException()
public static void TestHandleCollector()
{
(int gen0, int gen1, int gen2) initialGcState = (GC.CollectionCount(0), GC.CollectionCount(1), GC.CollectionCount(2));
int initSum = initialGcState.gen0 + initialGcState.gen1 + initialGcState.gen2;

HandleCollector lowLimitCollector = new HandleCollector("LowLimit.Collector", LowLimitSize);
for (int i = 0; i < LowLimitSize + 1; ++i)
{
HandleLimitTester hlt = new HandleLimitTester(lowLimitCollector);
}

(int gen0, int gen1, int gen2) postLowLimitState = (GC.CollectionCount(0),GC.CollectionCount(1), GC.CollectionCount(2));
// HandleLimitTester does the decrement on the HandleCollector during finalization, so we wait for pending finalizers.
GC.WaitForPendingFinalizers();

Assert.True(initialGcState.gen0 + initialGcState.gen1 + initialGcState.gen2 < postLowLimitState.gen0 + postLowLimitState.gen1 + postLowLimitState.gen2, "Low limit handle did not trigger a GC");
(int gen0, int gen1, int gen2) postLowLimitState = (GC.CollectionCount(0), GC.CollectionCount(1), GC.CollectionCount(2));
int postLowLimitSum = postLowLimitState.gen0 + postLowLimitState.gen1 + postLowLimitState.gen2;

Assert.True(initSum < postLowLimitSum, $"Low limit handle did not trigger a GC: {initSum} < {postLowLimitSum}");

HandleCollector highLimitCollector = new HandleCollector("HighLimit.Collector", HighLimitSize);
for (int i = 0; i < HighLimitSize + 10; ++i)
{
HandleLimitTester hlt = new HandleLimitTester(highLimitCollector);
}

// HandleLimitTester does the decrement on the HandleCollector during finalization, so we wait for pending finalizers.
GC.WaitForPendingFinalizers();

(int gen0, int gen1, int gen2) postHighLimitState = (GC.CollectionCount(0), GC.CollectionCount(1), GC.CollectionCount(2));
int postHighLimitSum = postHighLimitState.gen0 + postHighLimitState.gen1 + postHighLimitState.gen2;

Assert.True(postLowLimitState.gen0 + postLowLimitState.gen1 + postLowLimitState.gen2 < postHighLimitState.gen0 + postHighLimitState.gen1 + postHighLimitState.gen2, "High limit handle did not trigger a GC");
Assert.True(postLowLimitSum < postHighLimitSum, $"High limit handle did not trigger a GC: {postLowLimitSum} < {postHighLimitSum}");
}

private sealed class HandleLimitTester
Expand Down

0 comments on commit 600b03f

Please sign in to comment.