Skip to content

Commit dfb9f93

Browse files
committed
Add CancellationTokenTest
1 parent 6360409 commit dfb9f93

3 files changed

Lines changed: 207 additions & 8 deletions

File tree

Lines changed: 200 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,224 @@
11
using System;
2+
using System.Diagnostics;
3+
using System.Diagnostics.CodeAnalysis;
24
using System.Threading;
5+
using System.Threading.Tasks;
36

47
namespace ThreadingTest;
58

6-
internal sealed class CancellationTokenTest
9+
public static class CancellationTokenTest
710
{
811

912
#region Constants & Statics
1013

11-
public static void Cancel_Test()
14+
public static async Task Cancel_OperationCanceledException_Test()
15+
{
16+
using var cts = new CancellationTokenSource();
17+
var cancellToken = cts.Token;
18+
19+
var task = Task.Factory
20+
.StartNew(
21+
() =>
22+
{
23+
Thread.Sleep(1000);
24+
cancellToken.ThrowIfCancellationRequested();
25+
Trace.WriteLine("end");
26+
},
27+
cancellToken);
28+
29+
await Task.Delay(500);
30+
cts.Cancel();
31+
32+
try
33+
{
34+
await task; // throw OperationCanceledException
35+
36+
//task.Wait(); // throw AggregateException TaskCanceledException
37+
//Task.WaitAll(new[] { task }); // throw AggregateException TaskCanceledException
38+
39+
//await Task.WhenAll(task);// throw OperationCanceledException
40+
}
41+
catch (OperationCanceledException)
42+
{
43+
Console.WriteLine("OperationCanceledException");
44+
}
45+
catch (AggregateException exception)
46+
{
47+
foreach (var inner in exception.InnerExceptions)
48+
{
49+
if (inner is TaskCanceledException)
50+
{
51+
Console.WriteLine("AggregateException TaskCanceledException");
52+
}
53+
}
54+
}
55+
}
56+
57+
public static async Task Cancel_TaskCanceledException_Test()
58+
{
59+
using var cts = new CancellationTokenSource();
60+
var cancellToken = cts.Token;
61+
62+
cts.Cancel(true);
63+
64+
var task = Task.Factory
65+
.StartNew(
66+
() =>
67+
{
68+
Thread.Sleep(1000);
69+
cancellToken.ThrowIfCancellationRequested();
70+
Trace.WriteLine("end");
71+
},
72+
cancellToken);
73+
74+
try
75+
{
76+
await task; // throw TaskCanceledException
77+
78+
//task.Wait(); // throw AggregateException TaskCanceledException
79+
//Task.WaitAll(new[] { task }); // throw AggregateException TaskCanceledException
80+
}
81+
catch (TaskCanceledException)
82+
{
83+
Console.WriteLine("TaskCanceledException");
84+
}
85+
catch (AggregateException exception)
86+
{
87+
foreach (var inner in exception.InnerExceptions)
88+
{
89+
if (inner is TaskCanceledException)
90+
{
91+
Console.WriteLine("AggregateException TaskCanceledException");
92+
}
93+
}
94+
}
95+
}
96+
97+
[SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "<Pending>")]
98+
public static async Task Cancel_ThrowFirst_Test()
1299
{
13100
using var cts = new CancellationTokenSource();
14101
Console.WriteLine($"start: {Environment.CurrentManagedThreadId}");
15102

16-
_ = cts.Token
17-
.Register(() =>
103+
var token = cts.Token;
104+
105+
var t1 = Task.Run(
106+
async () =>
107+
{
108+
Console.WriteLine($"run: 1-{Environment.CurrentManagedThreadId}");
109+
while (!token.IsCancellationRequested)
110+
{
111+
await Task.Delay(100);
112+
}
113+
token.ThrowIfCancellationRequested();
114+
});
115+
116+
var t2 = Task.Run(
117+
async () =>
18118
{
19-
Console.WriteLine($"callback: {Environment.CurrentManagedThreadId}");
20-
Thread.Sleep(5000);
119+
Console.WriteLine($"run: 2-{Environment.CurrentManagedThreadId}");
120+
while (!token.IsCancellationRequested)
121+
{
122+
await Task.Delay(100);
123+
}
124+
token.ThrowIfCancellationRequested();
21125
});
22126

127+
_ = cts.Token
128+
.Register(
129+
() =>
130+
{
131+
Console.WriteLine($"callback: 1-{Environment.CurrentManagedThreadId}");
132+
133+
throw new Exception("1");
134+
});
135+
136+
_ = cts.Token
137+
.Register(
138+
() =>
139+
{
140+
Console.WriteLine($"callback: 2-{Environment.CurrentManagedThreadId}");
141+
throw new Exception("2");
142+
});
143+
144+
await Task.Delay(300);
145+
146+
cts.Cancel(true);
147+
Console.WriteLine($"Canceled: {Environment.CurrentManagedThreadId}");
148+
149+
await Task.WhenAll(t1, t2);
150+
}
151+
152+
public static void Register_Test()
153+
{
154+
using var cts = new CancellationTokenSource();
155+
Console.WriteLine($"start: {Environment.CurrentManagedThreadId}");
156+
157+
_ = cts.Token
158+
.Register(
159+
() =>
160+
{
161+
Console.WriteLine($"callback: {Environment.CurrentManagedThreadId}");
162+
Thread.Sleep(5000);
163+
});
164+
23165
Console.WriteLine($"Canceling: {Environment.CurrentManagedThreadId}");
24166
cts.Cancel();
25167
Console.WriteLine($"Canceled: {Environment.CurrentManagedThreadId}");
26168
}
27169

170+
public static void Register_Twice_Test()
171+
{
172+
using var cts = new CancellationTokenSource();
173+
Console.WriteLine($"start: {Environment.CurrentManagedThreadId}");
174+
175+
_ = cts.Token
176+
.Register(
177+
() =>
178+
{
179+
Console.WriteLine($"callback: 1-{Environment.CurrentManagedThreadId}");
180+
Thread.Sleep(1000);
181+
});
182+
183+
_ = cts.Token
184+
.Register(
185+
() =>
186+
{
187+
Console.WriteLine($"callback: 2-{Environment.CurrentManagedThreadId}");
188+
Thread.Sleep(1000);
189+
});
190+
191+
Console.WriteLine($"Canceling: {Environment.CurrentManagedThreadId}");
192+
cts.Cancel();
193+
Console.WriteLine($"Canceled: {Environment.CurrentManagedThreadId}");
194+
}
195+
196+
public static void Register_Twice_ThrowFirst_Test()
197+
{
198+
using var cts = new CancellationTokenSource();
199+
Console.WriteLine($"start: {Environment.CurrentManagedThreadId}");
200+
201+
_ = cts.Token
202+
.Register(
203+
() =>
204+
{
205+
Console.WriteLine($"callback: 1-{Environment.CurrentManagedThreadId}");
206+
Thread.Sleep(1000);
207+
});
208+
209+
_ = cts.Token
210+
.Register(
211+
() =>
212+
{
213+
Console.WriteLine($"callback: 2-{Environment.CurrentManagedThreadId}");
214+
Thread.Sleep(1000);
215+
});
216+
217+
Console.WriteLine($"Canceling: {Environment.CurrentManagedThreadId}");
218+
cts.Cancel(false);
219+
Console.WriteLine($"Canceled: {Environment.CurrentManagedThreadId}");
220+
}
221+
28222
#endregion
29223

30224
}

src/Tests/ThreadingTest/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ private static async Task Main(string[] args)
2727

2828
// ChannelTest.Reader_Test();
2929

30-
// CancellationTokenTest.Cancel_Test();
30+
//await CancellationTokenTest.Cancel_TaskCanceledException_Test();
31+
await CancellationTokenTest.Cancel_OperationCanceledException_Test();
32+
//CancellationTokenTest.Register_Test();
33+
//CancellationTokenTest.Register_Twice_Test();
34+
//CancellationTokenTest.Register_Twice_ThrowFirst_Test();
35+
//await CancellationTokenTest.Cancel_ThrowFirst_Test();
3136

3237
// InterruptTest.Sleeping_Interrupt_Test();
3338

src/Tests/ThreadingTest/ThreadingTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net9.0</TargetFramework>
66
</PropertyGroup>
77

88
</Project>

0 commit comments

Comments
 (0)