Skip to content

Commit

Permalink
Fix outerloop System.Buffers tests after s_trimBuffers removal (#56068)
Browse files Browse the repository at this point in the history
We deleted this switch.  Turns out there were outerloop tests looking for it.
  • Loading branch information
stephentoub committed Jul 21, 2021
1 parent 94a8430 commit 2ad8c90
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 62 deletions.
16 changes: 2 additions & 14 deletions src/libraries/System.Buffers/tests/ArrayPool/ArrayPoolTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ namespace System.Buffers.ArrayPool.Tests
{
public abstract class ArrayPoolTest
{
protected const string TrimSwitchName = "DOTNET_SYSTEM_BUFFERS_ARRAYPOOL_TRIMSHARED";

protected static class EventIds
{
public const int BufferRented = 1;
Expand All @@ -35,26 +33,16 @@ protected static int RunWithListener(Action body, EventLevel level, Action<Event
}
}

protected static void RemoteInvokeWithTrimming(Action action, bool trim = false)
{
RemoteInvokeOptions options = new RemoteInvokeOptions();
options.StartInfo.UseShellExecute = false;
options.StartInfo.EnvironmentVariables.Add(TrimSwitchName, trim.ToString());

RemoteExecutor.Invoke(action).Dispose();
}

protected static void RemoteInvokeWithTrimming(Action<string> method, bool trim = false, int timeout = RemoteExecutor.FailWaitTimeoutMilliseconds)
protected static void RemoteInvokeWithTrimming(Action method, int timeout = RemoteExecutor.FailWaitTimeoutMilliseconds)
{
var options = new RemoteInvokeOptions
{
TimeOut = timeout
};

options.StartInfo.UseShellExecute = false;
options.StartInfo.EnvironmentVariables.Add(TrimSwitchName, trim.ToString());

RemoteExecutor.Invoke(method, trim.ToString(), options).Dispose();
RemoteExecutor.Invoke(method, options).Dispose();
}
}
}
63 changes: 16 additions & 47 deletions src/libraries/System.Buffers/tests/ArrayPool/CollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,16 @@ namespace System.Buffers.ArrayPool.Tests
public class CollectionTests : ArrayPoolTest
{
[OuterLoop("This is a long running test (over 2 minutes)")]
[ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported)),
InlineData(true),
InlineData(false)]
public void BuffersAreCollectedWhenStale(bool trim)
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void BuffersAreCollectedWhenStale()
{
RemoteInvokeWithTrimming((trimString) =>
RemoteInvokeWithTrimming(() =>
{
// Check that our environment is as we expect
Assert.Equal(trimString, Environment.GetEnvironmentVariable(TrimSwitchName));
const int BufferCount = 8;
const int BufferSize = 1025;
// Get the pool and check our trim setting
var pool = ArrayPool<int>.Shared;
bool parsedTrim = ValidateTrimState(pool, trimString);
List<int[]> rentedBuffers = new List<int[]>();
Expand Down Expand Up @@ -71,27 +65,21 @@ public void BuffersAreCollectedWhenStale(bool trim)
}
// Should only have found a new buffer if we're trimming
Assert.Equal(parsedTrim, foundNewBuffer);
}, trim, 3 * 60 * 1000); // This test has to wait for the buffers to go stale (give it three minutes)
Assert.True(foundNewBuffer);
}, 3 * 60 * 1000); // This test has to wait for the buffers to go stale (give it three minutes)
}

private static bool IsStressModeEnabledAndRemoteExecutorSupported => TestEnvironment.IsStressModeEnabled && RemoteExecutor.IsSupported;

// This test can cause problems for other tests run in parallel (from other assemblies) as
// it pushes the physical memory usage above 80% temporarily.
[ConditionalTheory(nameof(IsStressModeEnabledAndRemoteExecutorSupported)),
InlineData(true),
InlineData(false)]
public unsafe void ThreadLocalIsCollectedUnderHighPressure(bool trim)
[ConditionalFact(nameof(IsStressModeEnabledAndRemoteExecutorSupported))]
public unsafe void ThreadLocalIsCollectedUnderHighPressure()
{
RemoteInvokeWithTrimming((trimString) =>
RemoteInvokeWithTrimming(() =>
{
// Check that our environment is as we expect
Assert.Equal(trimString, Environment.GetEnvironmentVariable(TrimSwitchName));
// Get the pool and check our trim setting
var pool = ArrayPool<byte>.Shared;
bool parsedTrim = ValidateTrimState(pool, trimString);
// Create our buffer, return it, re-rent it and ensure we have the same one
const int BufferSize = 4097;
Expand Down Expand Up @@ -119,40 +107,21 @@ public unsafe void ThreadLocalIsCollectedUnderHighPressure(bool trim)
} while ((int)pressureMethod.Invoke(null, null) != 2);
GC.WaitForPendingFinalizers();
if (parsedTrim)
{
// Should have a new buffer now
Assert.NotSame(buffer, pool.Rent(BufferSize));
}
else
{
// Disabled, should not have trimmed buffer
Assert.Same(buffer, pool.Rent(BufferSize));
}
}, trim);
}
private static bool ValidateTrimState(object pool, string trimString)
{
Assert.StartsWith("TlsOverPerCoreLockedStacksArrayPool", pool.GetType().Name);
bool parsedTrim = bool.Parse(trimString);
var trimField = pool.GetType().GetField("s_trimBuffers", BindingFlags.Static | BindingFlags.NonPublic);
Assert.Equal(parsedTrim, (bool)trimField.GetValue(null));
return parsedTrim;
// Should have a new buffer now
Assert.NotSame(buffer, pool.Rent(BufferSize));
});
}

private static bool IsPreciseGcSupportedAndRemoteExecutorSupported => PlatformDetection.IsPreciseGcSupported && RemoteExecutor.IsSupported;

[ActiveIssue("https://github.com/dotnet/runtime/issues/44037")]
[ConditionalTheory(nameof(IsPreciseGcSupportedAndRemoteExecutorSupported))]
[InlineData(true)]
[InlineData(false)]
public void PollingEventFires(bool trim)
[ConditionalFact(nameof(IsPreciseGcSupportedAndRemoteExecutorSupported))]
public void PollingEventFires()
{
RemoteInvokeWithTrimming((trimString) =>
RemoteInvokeWithTrimming(() =>
{
var pool = ArrayPool<float>.Shared;
bool parsedTrim = ValidateTrimState(pool, trimString);
bool pollEventFired = false;
var buffer = pool.Rent(10);
Expand Down Expand Up @@ -187,8 +156,8 @@ public void PollingEventFires(bool trim)
});
// Polling events should only fire when trimming is enabled
Assert.Equal(parsedTrim, pollEventFired);
}, trim);
Assert.True(pollEventFired);
});
}
}
}
1 change: 0 additions & 1 deletion src/libraries/System.Buffers/tests/ArrayPool/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ public static void RentingGiganticArraySucceeds(int length, bool expectPooled)
{
var options = new RemoteInvokeOptions();
options.StartInfo.UseShellExecute = false;
options.StartInfo.EnvironmentVariables.Add(TrimSwitchName, "false");

RemoteExecutor.Invoke((lengthStr, expectPooledStr) =>
{
Expand Down

0 comments on commit 2ad8c90

Please sign in to comment.