Skip to content

Commit

Permalink
increase the allocation threshold for tests that need to ensure that …
Browse files Browse the repository at this point in the history
…2GB+ does not get pre-allocated (dotnet#104151)
  • Loading branch information
adamsitnik authored Jun 29, 2024
1 parent 9528c15 commit fb762d1
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/libraries/System.Formats.Nrbf/tests/AttackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}

Expand All @@ -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()
Expand Down

0 comments on commit fb762d1

Please sign in to comment.