Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk committed May 17, 2023
1 parent a8067f0 commit 772f598
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
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;
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/Polly.Core/Utils/CancellationTokenSourcePool.Pooled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
14 changes: 12 additions & 2 deletions src/Polly.Core/Utils/CancellationTokenSourcePool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

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;
}

0 comments on commit 772f598

Please sign in to comment.