From fb762d10e5df52d24b0dd559f97a14ccf2991d60 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Sat, 29 Jun 2024 12:00:10 +0200 Subject: [PATCH] increase the allocation threshold for tests that need to ensure that 2GB+ does not get pre-allocated (#104151) --- .../System.Formats.Nrbf/tests/AttackTests.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Formats.Nrbf/tests/AttackTests.cs b/src/libraries/System.Formats.Nrbf/tests/AttackTests.cs index e73c8290289ca..d9f7ac05811ad 100644 --- a/src/libraries/System.Formats.Nrbf/tests/AttackTests.cs +++ b/src/libraries/System.Formats.Nrbf/tests/AttackTests.cs @@ -108,12 +108,15 @@ public void CyclicClassReferencesInArraysOfTDoNotCauseStackOverflow() } #if !NETFRAMEWORK + // The tests need to ensure that 2GB+ does not get pre-allocated. + // 200k is enough to get the job done and avoid getting false positives. + const long AllocationThreshold = 200_000; + // GC.GetAllocatedBytesForCurrentThread() is not available on Full Framework. // AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize is available, // but it reports allocations for all threads. Using this API would require // ensuring that it's the only test that is being run at a time. - // Mono either allocates more than expected or the API is not precise enough - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoRuntime))] + [Fact] public void ArraysOfStringsAreNotBeingPreAllocated() { using MemoryStream stream = new(); @@ -136,7 +139,7 @@ public void ArraysOfStringsAreNotBeingPreAllocated() long after = GetAllocatedByteCount(); - Assert.InRange(after, before, before + 5000); + Assert.InRange(after, before, before + AllocationThreshold); Assert.Equal(SerializationRecordType.ArraySingleString, serializationRecord.RecordType); } @@ -162,7 +165,7 @@ public void ArraysOfBytesAreNotBeingPreAllocated() long after = GetAllocatedByteCount(); - Assert.InRange(after, before, before + 200_000); + Assert.InRange(after, before, before + AllocationThreshold); } private static long GetAllocatedByteCount()