-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dispose CancellationTokenSource after cancelled.
- Loading branch information
nobody
committed
Dec 7, 2023
1 parent
106f47d
commit 634dfc4
Showing
7 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
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
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
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,59 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace VgcApisTests | ||
{ | ||
[TestClass] | ||
public class Experiments | ||
{ | ||
public Experiments() { } | ||
|
||
[TestMethod] | ||
public void CancellationTokenSourceDisposeTest() | ||
{ | ||
var cts = new CancellationTokenSource(); | ||
var token = cts.Token; | ||
var mre = new ManualResetEventSlim(false); | ||
var task = Task.Run(() => | ||
{ | ||
Assert.IsFalse(token.IsCancellationRequested); | ||
mre.Wait(); | ||
Assert.IsTrue(token.IsCancellationRequested); | ||
}); | ||
|
||
Thread.Sleep(2000); | ||
cts.Cancel(); | ||
cts.Dispose(); | ||
mre.Set(); | ||
task.Wait(); | ||
Assert.IsTrue(token.IsCancellationRequested); | ||
} | ||
|
||
[TestMethod] | ||
public void TimedCancellationTokenSourceDisposeTest() | ||
{ | ||
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2)); | ||
var mre = new ManualResetEventSlim(false); | ||
var token = cts.Token; | ||
token.Register(() => | ||
{ | ||
cts.Dispose(); | ||
mre.Set(); | ||
}); | ||
|
||
var task = Task.Run(() => | ||
{ | ||
Assert.IsFalse(token.IsCancellationRequested); | ||
mre.Wait(); | ||
Assert.IsTrue(token.IsCancellationRequested); | ||
}); | ||
task.Wait(); | ||
Assert.IsTrue(token.IsCancellationRequested); | ||
} | ||
} | ||
} |
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