diff --git a/src/Polly.Core/Utils/CancellationTokenSourcePool.Pooled.cs b/src/Polly.Core/Utils/CancellationTokenSourcePool.Pooled.cs index 4791a8fcc9f..e2a30b7caeb 100644 --- a/src/Polly.Core/Utils/CancellationTokenSourcePool.Pooled.cs +++ b/src/Polly.Core/Utils/CancellationTokenSourcePool.Pooled.cs @@ -3,7 +3,7 @@ namespace Polly.Utils; internal abstract partial class CancellationTokenSourcePool { #if NET6_0_OR_GREATER - private sealed class PooledCancellationTokenSourcePool : CancellationTokenSourcePool + internal sealed class PooledCancellationTokenSourcePool : CancellationTokenSourcePool { public static readonly PooledCancellationTokenSourcePool SystemInstance = new(TimeProvider.System); diff --git a/test/Polly.Core.Tests/Utils/CancellationTokenSourcePoolTests.cs b/test/Polly.Core.Tests/Utils/CancellationTokenSourcePoolTests.cs index aa915c1c23d..da2065c49f1 100644 --- a/test/Polly.Core.Tests/Utils/CancellationTokenSourcePoolTests.cs +++ b/test/Polly.Core.Tests/Utils/CancellationTokenSourcePoolTests.cs @@ -30,7 +30,15 @@ public void ArgValidation_Ok() [Theory] public void RentReturn_Reusable_EnsureProperBehavior(object timeProvider) { + // Use a dedicated pool instance instead of CancellationTokenSourcePool.Create(...), which + // returns a process-wide shared singleton for TimeProvider.System. Using the shared singleton + // makes this test flaky, as other tests running in parallel can rent/return CancellationTokenSource + // instances from the same pool concurrently, changing which instance is returned by Get(). +#if NET6_0_OR_GREATER + var pool = new CancellationTokenSourcePool.PooledCancellationTokenSourcePool(GetTimeProvider(timeProvider)); +#else var pool = CancellationTokenSourcePool.Create(GetTimeProvider(timeProvider)); +#endif var cts = pool.Get(System.Threading.Timeout.InfiniteTimeSpan); pool.Return(cts);