2 // CancellationTokenSourceTest.cs
5 // Marek Safar (marek.safar@gmail.com)
6 // Jeremie Laval (jeremie.laval@gmail.com)
8 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Threading;
33 using NUnit.Framework;
34 using System.Threading.Tasks;
35 using MonoTests.System.Threading.Tasks;
37 namespace MonoTests.System.Threading
40 public class CancellationTokenSourceTest
45 public void Ctor_Invalid ()
48 new CancellationTokenSource (-4);
50 } catch (ArgumentException) {
55 public void Ctor_Timeout ()
58 var cts = new CancellationTokenSource (TimeSpan.FromMilliseconds (20));
59 cts.Token.Register (() => called++);
61 Assert.AreEqual (1, called, "#1");
65 public void CancelAfter ()
68 var cts = new CancellationTokenSource ();
69 cts.Token.Register (() => called++);
72 Assert.AreEqual (1, called, "#1");
76 public void CancelAfter_Invalid ()
78 var cts = new CancellationTokenSource ();
82 } catch (ArgumentException) {
87 public void CancelAfter_Disposed ()
90 var cts = new CancellationTokenSource ();
91 cts.Token.Register (() => called++);
95 Assert.AreEqual (0, called, "#1");
103 CancellationTokenSource cts = new CancellationTokenSource ();
104 Assert.IsTrue (cts.Token.CanBeCanceled, "#1");
105 Assert.IsFalse (cts.Token.IsCancellationRequested, "#2");
106 Assert.IsNotNull (cts.Token.WaitHandle, "#3");
110 public void Cancel_NoRegistration ()
112 CancellationTokenSource cts = new CancellationTokenSource ();
117 public void Cancel ()
119 var cts = new CancellationTokenSource ();
122 cts.Token.Register (l => { Assert.AreEqual ("v", l); ++called; }, "v");
124 Assert.AreEqual (1, called, "#1");
127 cts.Token.Register (() => { called += 12; });
129 Assert.AreEqual (12, called, "#2");
134 public void Cancel_Order ()
136 var cts = new CancellationTokenSource ();
138 Action<object> a = x => { Assert.AreEqual(current, x); current++; };
140 cts.Token.Register (a, 2);
141 cts.Token.Register (a, 1);
142 cts.Token.Register (a, 0);
148 public void CancelWithDispose ()
150 CancellationTokenSource cts = new CancellationTokenSource ();
151 CancellationToken c = cts.Token;
162 Assert.AreEqual (1, called, "#1");
166 public void Cancel_SingleException ()
168 var cts = new CancellationTokenSource ();
170 cts.Token.Register (() => { throw new ApplicationException (); });
174 } catch (AggregateException e) {
175 Assert.AreEqual (1, e.InnerExceptions.Count, "#2");
182 public void Cancel_MultipleExceptions ()
184 var cts = new CancellationTokenSource ();
186 cts.Token.Register (() => { throw new ApplicationException ("1"); });
187 cts.Token.Register (() => { throw new ApplicationException ("2"); });
188 cts.Token.Register (() => { throw new ApplicationException ("3"); });
193 } catch (AggregateException e) {
194 Assert.AreEqual (3, e.InnerExceptions.Count, "#2");
200 cts.Token.Register (() => { throw new ApplicationException ("1"); });
202 } catch (ApplicationException) {
209 public void Cancel_ExceptionOrder ()
211 var cts = new CancellationTokenSource ();
213 cts.Token.Register (() => { throw new ApplicationException ("1"); });
214 cts.Token.Register (() => { throw new ApplicationException ("2"); });
215 cts.Token.Register (() => { throw new ApplicationException ("3"); });
219 } catch (AggregateException e) {
220 Assert.AreEqual (3, e.InnerExceptions.Count, "#2");
221 Assert.AreEqual ("3", e.InnerExceptions[0].Message, "#3");
222 Assert.AreEqual ("2", e.InnerExceptions[1].Message, "#4");
223 Assert.AreEqual ("1", e.InnerExceptions[2].Message, "#5");
228 public void Cancel_MultipleException_Recursive ()
230 CancellationTokenSource cts = new CancellationTokenSource ();
231 CancellationToken c = cts.Token;
237 throw new ApplicationException ();
241 throw new NotSupportedException ();
247 } catch (AggregateException e) {
248 Assert.AreEqual (2, e.InnerExceptions.Count, "#2");
253 public void Cancel_MultipleExceptionsFirstThrows ()
255 var cts = new CancellationTokenSource ();
257 cts.Token.Register (() => { throw new ApplicationException ("1"); });
258 cts.Token.Register (() => { throw new ApplicationException ("2"); });
259 cts.Token.Register (() => { throw new ApplicationException ("3"); });
264 } catch (ApplicationException) {
271 public void CreateLinkedTokenSource_InvalidArguments ()
273 var cts = new CancellationTokenSource ();
274 var token = cts.Token;
277 CancellationTokenSource.CreateLinkedTokenSource (null);
279 } catch (ArgumentNullException) {
283 CancellationTokenSource.CreateLinkedTokenSource (new CancellationToken[0]);
285 } catch (ArgumentException) {
290 public void CreateLinkedTokenSource ()
292 var cts = new CancellationTokenSource ();
295 var linked = CancellationTokenSource.CreateLinkedTokenSource (cts.Token);
296 Assert.IsTrue (linked.IsCancellationRequested, "#1");
298 linked = CancellationTokenSource.CreateLinkedTokenSource (new CancellationToken ());
299 Assert.IsFalse (linked.IsCancellationRequested, "#2");
303 public void Dispose ()
305 var cts = new CancellationTokenSource ();
306 var token = cts.Token;
310 var b = cts.IsCancellationRequested;
311 token.ThrowIfCancellationRequested ();
316 } catch (ObjectDisposedException) {
322 } catch (ObjectDisposedException) {
326 token.Register (() => { });
328 } catch (ObjectDisposedException) {
332 var wh = token.WaitHandle;
334 } catch (ObjectDisposedException) {
338 CancellationTokenSource.CreateLinkedTokenSource (token);
340 } catch (ObjectDisposedException) {
347 } catch (ObjectDisposedException) {
353 public void RegisterThenDispose ()
355 var cts1 = new CancellationTokenSource ();
356 var reg1 = cts1.Token.Register (() => { throw new ApplicationException (); });
358 var cts2 = new CancellationTokenSource ();
359 var reg2 = cts2.Token.Register (() => { throw new ApplicationException (); });
361 Assert.AreNotEqual (cts1, cts2, "#1");
362 Assert.AreNotSame (cts1, cts2, "#2");
370 } catch (AggregateException) {
375 public void RegisterWhileCancelling ()
377 var cts = new CancellationTokenSource ();
378 var mre = new ManualResetEvent (false);
379 var mre2 = new ManualResetEvent (false);
382 cts.Token.Register (() => {
383 Assert.IsTrue (cts.IsCancellationRequested, "#10");
384 Assert.IsTrue (cts.Token.WaitHandle.WaitOne (0), "#11");
390 var t = Task.Factory.StartNew (() => { cts.Cancel (); });
392 Assert.IsTrue (mre2.WaitOne (1000), "#0");
393 cts.Token.Register (() => { called++; });
394 Assert.AreEqual (1, called, "#1");
395 Assert.IsFalse (t.IsCompleted, "#2");
398 Assert.IsTrue (t.Wait (1000), "#3");
399 Assert.AreEqual (12, called, "#4");
403 public void ReEntrantRegistrationTest ()
405 bool unregister = false;
406 bool register = false;
407 var source = new CancellationTokenSource ();
408 var token = source.Token;
410 Console.WriteLine ("Test1");
411 var reg = token.Register (() => unregister = true);
412 token.Register (() => reg.Dispose ());
413 token.Register (() => { Console.WriteLine ("Gnyah"); token.Register (() => register = true); });
416 Assert.IsFalse (unregister);
417 Assert.IsTrue (register);
421 public void DisposeAfterRegistrationTest ()
423 var source = new CancellationTokenSource ();
425 var req = source.Token.Register (() => ran = true);
428 Assert.IsFalse (ran);
432 public void CancelLinkedTokenSource ()
434 var cts = new CancellationTokenSource ();
435 bool canceled = false;
436 cts.Token.Register (() => canceled = true);
438 using (var linked = CancellationTokenSource.CreateLinkedTokenSource (cts.Token))
441 Assert.IsFalse (canceled, "#1");
442 Assert.IsFalse (cts.IsCancellationRequested, "#2");
446 Assert.IsTrue (canceled, "#3");
450 public void ConcurrentCancelLinkedTokenSourceWhileDisposing ()
452 ParallelTestHelper.Repeat (delegate {
453 var src = new CancellationTokenSource ();
454 var linked = CancellationTokenSource.CreateLinkedTokenSource (src.Token);
455 var cntd = new CountdownEvent (2);
457 var t1 = new Thread (() => {
462 var t2 = new Thread (() => {
477 public void DisposeRace ()
479 for (int i = 0; i < 1000; ++i) {
480 var c1 = new CancellationTokenSource ();
482 var wh = c1.Token.WaitHandle;