-
-
Notifications
You must be signed in to change notification settings - Fork 856
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CancellationToken.WaitUntilCanceled
- Loading branch information
Showing
4 changed files
with
109 additions
and
13 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
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,51 @@ | ||
using Cysharp.Threading.Tasks; | ||
using FluentAssertions; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Channels; | ||
using Cysharp.Threading.Tasks.Linq; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace NetCoreTests | ||
{ | ||
public class CancellationTokenTest | ||
{ | ||
[Fact] | ||
public async Task WaitUntilCanceled() | ||
{ | ||
var cts = new CancellationTokenSource(); | ||
|
||
cts.CancelAfter(TimeSpan.FromSeconds(1.5)); | ||
|
||
var now = DateTime.UtcNow; | ||
|
||
await cts.Token.WaitUntilCanceled(); | ||
|
||
var elapsed = DateTime.UtcNow - now; | ||
|
||
elapsed.Should().BeGreaterThan(TimeSpan.FromSeconds(1)); | ||
} | ||
|
||
[Fact] | ||
public void AlreadyCanceled() | ||
{ | ||
var cts = new CancellationTokenSource(); | ||
|
||
cts.Cancel(); | ||
|
||
cts.Token.WaitUntilCanceled().GetAwaiter().IsCompleted.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void None() | ||
{ | ||
CancellationToken.None.WaitUntilCanceled().GetAwaiter().IsCompleted.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
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