From cb10ed9430762f63a4e9b23a25d142b3ff909879 Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Thu, 28 Apr 2022 20:06:17 -0400 Subject: [PATCH] Ensure TestHandleCollector has enough memory pressure to trigger (#68652) * Ensure TestHandleCollector has enough memory pressure to trigger --- .../Runtime/InteropServices/HandleCollectorTests.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/HandleCollectorTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/HandleCollectorTests.cs index 471c8bf2a84fa..979867bc1efd7 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/HandleCollectorTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/HandleCollectorTests.cs @@ -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)] @@ -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. @@ -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. @@ -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); }