-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
75 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Polly.Core.Tests/Utils/CancellationTokenSourcePoolTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Polly.Utils; | ||
|
||
namespace Polly.Core.Tests.Utils; | ||
|
||
public class CancellationTokenSourcePoolTests | ||
{ | ||
[Fact] | ||
public void RentReturn_Reusable_EnsureProperBehavior() | ||
{ | ||
var cts = CancellationTokenSourcePool.Get(); | ||
CancellationTokenSourcePool.Return(cts); | ||
|
||
var cts2 = CancellationTokenSourcePool.Get(); | ||
#if NET6_0_OR_GREATER | ||
cts2.Should().BeSameAs(cts); | ||
#else | ||
cts2.Should().NotBeSameAs(cts); | ||
#endif | ||
} | ||
|
||
[Fact] | ||
public void RentReturn_NotReusable_EnsureProperBehavior() | ||
{ | ||
var cts = CancellationTokenSourcePool.Get(); | ||
cts.Cancel(); | ||
CancellationTokenSourcePool.Return(cts); | ||
|
||
cts.Invoking(c => c.Token).Should().Throw<ObjectDisposedException>(); | ||
|
||
var cts2 = CancellationTokenSourcePool.Get(); | ||
cts2.Token.Should().NotBeNull(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace Polly.Utils | ||
{ | ||
internal static class CancellationTokenSourcePool | ||
{ | ||
#if NET6_0_OR_GREATER | ||
private static readonly ObjectPool<CancellationTokenSource> Pool = new( | ||
static () => new CancellationTokenSource(), | ||
static cts => true); | ||
#endif | ||
public static CancellationTokenSource Get() | ||
{ | ||
#if NET6_0_OR_GREATER | ||
return Pool.Get(); | ||
#else | ||
return new CancellationTokenSource(); | ||
#endif | ||
} | ||
|
||
public static void Return(CancellationTokenSource source) | ||
{ | ||
#if NET6_0_OR_GREATER | ||
if (source.TryReset()) | ||
{ | ||
Pool.Return(source); | ||
return; | ||
} | ||
#endif | ||
source.Dispose(); | ||
} | ||
} | ||
} |