Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix outerloop System.Buffers tests after s_trimBuffers removal #56068

Merged
merged 1 commit into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
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
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