-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add polyfill for
CancellationTokenSource.CancelAsync()
(#5)
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace PolyShim.Tests.Net80; | ||
|
||
public class CancellationTokenSourceTests | ||
{ | ||
[Fact] | ||
public async Task CancelAsync_Test() | ||
{ | ||
// Arrange | ||
using var cts = new CancellationTokenSource(); | ||
|
||
// Act | ||
await cts.CancelAsync(); | ||
|
||
// Assert | ||
cts.IsCancellationRequested.Should().BeTrue(); | ||
} | ||
} |
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,30 @@ | ||
#if FEATURE_TASK | ||
#if (NETCOREAPP && !NET8_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD) | ||
#nullable enable | ||
// ReSharper disable RedundantUsingDirective | ||
// ReSharper disable CheckNamespace | ||
// ReSharper disable InconsistentNaming | ||
// ReSharper disable PartialTypeWithSinglePart | ||
|
||
using System.Collections; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
internal static partial class PolyfillExtensions | ||
{ | ||
// https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource.cancelasync | ||
public static Task CancelAsync(this CancellationTokenSource cts) | ||
{ | ||
#if (NETFRAMEWORK && !NET45_OR_GREATER) | ||
return Task.Factory.StartNew(() => cts.Cancel()); | ||
#elif (NETFRAMEWORK && !NET46_OR_GREATER) || (NETSTANDARD && !NETSTANDARD1_3_OR_GREATER) | ||
cts.Cancel(); | ||
return Task.FromResult<object?>(null); | ||
#else | ||
cts.Cancel(); | ||
return Task.CompletedTask; | ||
#endif | ||
} | ||
} | ||
#endif | ||
#endif |