Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace System.Runtime.InteropServices
public class HandleCollectorTests
{
private const int LowLimitSize = 20;
private const int HighLimitSize = 100000;
private const int HighLimitSize = 100_000;

[Theory]
[InlineData(null, 0)]
Expand Down Expand Up @@ -128,6 +128,7 @@ public static void TestHandleCollector()
for (int i = 0; i < LowLimitSize + 1; ++i)
{
HandleLimitTester hlt = new HandleLimitTester(lowLimitCollector);
Assert.True(lowLimitCollector.Count <= i + 1);
}

// HandleLimitTester does the decrement on the HandleCollector during finalization, so we wait for pending finalizers.
Expand All @@ -142,6 +143,7 @@ public static void TestHandleCollector()
for (int i = 0; i < HighLimitSize + 10; ++i)
{
HandleLimitTester hlt = new HandleLimitTester(highLimitCollector);
Assert.True(highLimitCollector.Count <= i + 1);
}

// HandleLimitTester does the decrement on the HandleCollector during finalization, so we wait for pending finalizers.
Expand All @@ -155,11 +157,15 @@ public static void TestHandleCollector()

private sealed class HandleLimitTester
{
private HandleCollector _collector;
private readonly HandleCollector _collector;
private readonly int[] _pressure;

internal HandleLimitTester(HandleCollector collector)
{
_collector = collector;
// Adding an allocation to ensure memory pressure exists so the call to
// GC.Collect() in the Add() method below will have something to do.
_pressure = new int[1_000];
_collector.Add();
GC.KeepAlive(this);
}
Expand Down