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

Reimplement CancellationTokenSourcePool #1192

Merged
merged 4 commits into from
May 17, 2023
Merged
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions src/Polly.Core.Tests/Utils/CancellationTokenSourcePoolTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Threading;
using Moq;
using Polly.Core.Tests.Helpers;
@@ -14,6 +15,17 @@ public static IEnumerable<object[]> TimeProviders()
yield return new object[] { new FakeTimeProvider() };
}

[Fact]
public void ArgValidation_Ok()
{
var pool = CancellationTokenSourcePool.Create(TimeProvider.System);

Assert.Throws<ArgumentOutOfRangeException>(() => pool.Get(TimeSpan.Zero));
Assert.Throws<ArgumentOutOfRangeException>(() => pool.Get(TimeSpan.FromMilliseconds(-2)));

pool.Get(System.Threading.Timeout.InfiniteTimeSpan).Should().NotBeNull();
}

[MemberData(nameof(TimeProviders))]
[Theory]
public void RentReturn_Reusable_EnsureProperBehavior(object timeProvider)
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ private sealed class DisposableCancellationTokenSourcePool : CancellationTokenSo

public DisposableCancellationTokenSourcePool(TimeProvider timeProvider) => _timeProvider = timeProvider;

public override CancellationTokenSource Get(TimeSpan delay)
protected override CancellationTokenSource GetCore(TimeSpan delay)
{
var source = new CancellationTokenSource();

2 changes: 1 addition & 1 deletion src/Polly.Core/Utils/CancellationTokenSourcePool.Pooled.cs
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ private sealed class PooledCancellationTokenSourcePool : CancellationTokenSource
static cts => true);
private readonly TimeProvider _timeProvider;

public override CancellationTokenSource Get(TimeSpan delay)
protected override CancellationTokenSource GetCore(TimeSpan delay)
{
var source = _pool.Get();

14 changes: 12 additions & 2 deletions src/Polly.Core/Utils/CancellationTokenSourcePool.cs
Original file line number Diff line number Diff line change
@@ -15,9 +15,19 @@ public static CancellationTokenSourcePool Create(TimeProvider timeProvider)
return new DisposableCancellationTokenSourcePool(timeProvider);
}

public abstract CancellationTokenSource Get(TimeSpan delay);
public CancellationTokenSource Get(TimeSpan delay)
{
if (delay <= TimeSpan.Zero && delay != System.Threading.Timeout.InfiniteTimeSpan)
{
throw new ArgumentOutOfRangeException(nameof(delay), "Invalid delay specified.");
martincostello marked this conversation as resolved.
Show resolved Hide resolved
}

return GetCore(delay);
}

protected abstract CancellationTokenSource GetCore(TimeSpan delay);

public abstract void Return(CancellationTokenSource source);

protected static bool IsCancellable(TimeSpan delay) => delay > TimeSpan.Zero;
protected static bool IsCancellable(TimeSpan delay) => delay != System.Threading.Timeout.InfiniteTimeSpan;
}