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.
33 using System.Threading;
34 using NUnit.Framework;
35 using System.Threading.Tasks;
36 using MonoTests.System.Threading.Tasks;
38 namespace MonoTests.System.Threading
41 public class CancellationTokenSourceTest
46 public void Ctor_Invalid ()
49 new CancellationTokenSource (-4);
51 } catch (ArgumentException) {
56 public void Ctor_Timeout ()
59 var cts = new CancellationTokenSource (TimeSpan.FromMilliseconds (20));
60 cts.Token.Register (() => called++);
62 Assert.AreEqual (1, called, "#1");
66 public void CancelAfter ()
69 var cts = new CancellationTokenSource ();
70 cts.Token.Register (() => called++);
73 Assert.AreEqual (1, called, "#1");
77 public void CancelAfter_Invalid ()
79 var cts = new CancellationTokenSource ();
83 } catch (ArgumentException) {
88 public void CancelAfter_Disposed ()
91 var cts = new CancellationTokenSource ();
92 cts.Token.Register (() => called++);
96 Assert.AreEqual (0, called, "#1");
104 CancellationTokenSource cts = new CancellationTokenSource ();
105 Assert.IsTrue (cts.Token.CanBeCanceled, "#1");
106 Assert.IsFalse (cts.Token.IsCancellationRequested, "#2");
107 Assert.IsNotNull (cts.Token.WaitHandle, "#3");
111 public void Cancel_NoRegistration ()
113 CancellationTokenSource cts = new CancellationTokenSource ();
118 public void Cancel ()
120 var cts = new CancellationTokenSource ();
123 cts.Token.Register (l => { Assert.AreEqual ("v", l); ++called; }, "v");
125 Assert.AreEqual (1, called, "#1");
128 cts.Token.Register (() => { called += 12; });
130 Assert.AreEqual (12, called, "#2");
135 public void CancelWithDispose ()
137 CancellationTokenSource cts = new CancellationTokenSource ();
138 CancellationToken c = cts.Token;
149 Assert.AreEqual (1, called, "#1");
153 public void Cancel_SingleException ()
155 var cts = new CancellationTokenSource ();
157 cts.Token.Register (() => { throw new ApplicationException (); });
161 } catch (AggregateException e) {
162 Assert.AreEqual (1, e.InnerExceptions.Count, "#2");
169 public void Cancel_MultipleExceptions ()
171 var cts = new CancellationTokenSource ();
173 cts.Token.Register (() => { throw new ApplicationException ("1"); });
174 cts.Token.Register (() => { throw new ApplicationException ("2"); });
175 cts.Token.Register (() => { throw new ApplicationException ("3"); });
180 } catch (AggregateException e) {
181 Assert.AreEqual (3, e.InnerExceptions.Count, "#2");
187 cts.Token.Register (() => { throw new ApplicationException ("1"); });
189 } catch (ApplicationException) {
196 public void Cancel_MultipleException_Recursive ()
198 CancellationTokenSource cts = new CancellationTokenSource ();
199 CancellationToken c = cts.Token;
205 throw new ApplicationException ();
209 throw new NotSupportedException ();
215 } catch (AggregateException e) {
216 Assert.AreEqual (2, e.InnerExceptions.Count, "#2");
221 public void Cancel_MultipleExceptionsFirstThrows ()
223 var cts = new CancellationTokenSource ();
225 cts.Token.Register (() => { throw new ApplicationException ("1"); });
226 cts.Token.Register (() => { throw new ApplicationException ("2"); });
227 cts.Token.Register (() => { throw new ApplicationException ("3"); });
232 } catch (ApplicationException) {
239 public void CreateLinkedTokenSource_InvalidArguments ()
241 var cts = new CancellationTokenSource ();
242 var token = cts.Token;
245 CancellationTokenSource.CreateLinkedTokenSource (null);
247 } catch (ArgumentNullException) {
251 CancellationTokenSource.CreateLinkedTokenSource (new CancellationToken[0]);
253 } catch (ArgumentException) {
258 public void CreateLinkedTokenSource ()
260 var cts = new CancellationTokenSource ();
263 var linked = CancellationTokenSource.CreateLinkedTokenSource (cts.Token);
264 Assert.IsTrue (linked.IsCancellationRequested, "#1");
266 linked = CancellationTokenSource.CreateLinkedTokenSource (new CancellationToken ());
267 Assert.IsFalse (linked.IsCancellationRequested, "#2");
271 public void Dispose ()
273 var cts = new CancellationTokenSource ();
274 var token = cts.Token;
278 var b = cts.IsCancellationRequested;
279 token.ThrowIfCancellationRequested ();
284 } catch (ObjectDisposedException) {
290 } catch (ObjectDisposedException) {
294 token.Register (() => { });
296 } catch (ObjectDisposedException) {
300 var wh = token.WaitHandle;
302 } catch (ObjectDisposedException) {
306 CancellationTokenSource.CreateLinkedTokenSource (token);
308 } catch (ObjectDisposedException) {
315 } catch (ObjectDisposedException) {
321 public void RegisterThenDispose ()
323 var cts1 = new CancellationTokenSource ();
324 var reg1 = cts1.Token.Register (() => { throw new ApplicationException (); });
326 var cts2 = new CancellationTokenSource ();
327 var reg2 = cts2.Token.Register (() => { throw new ApplicationException (); });
329 Assert.AreNotEqual (cts1, cts2, "#1");
330 Assert.AreNotSame (cts1, cts2, "#2");
338 } catch (AggregateException) {
343 public void RegisterWhileCancelling ()
345 var cts = new CancellationTokenSource ();
346 var mre = new ManualResetEvent (false);
347 var mre2 = new ManualResetEvent (false);
350 cts.Token.Register (() => {
351 Assert.IsTrue (cts.IsCancellationRequested, "#10");
352 Assert.IsTrue (cts.Token.WaitHandle.WaitOne (0), "#11");
358 var t = Task.Factory.StartNew (() => { cts.Cancel (); });
360 Assert.IsTrue (mre2.WaitOne (1000), "#0");
361 cts.Token.Register (() => { called++; });
362 Assert.AreEqual (1, called, "#1");
363 Assert.IsFalse (t.IsCompleted, "#2");
366 Assert.IsTrue (t.Wait (1000), "#3");
367 Assert.AreEqual (12, called, "#4");
371 public void ReEntrantRegistrationTest ()
373 bool unregister = false;
374 bool register = false;
375 var source = new CancellationTokenSource ();
376 var token = source.Token;
378 var reg = new CancellationTokenRegistration ();
379 Console.WriteLine ("Test1");
380 token.Register (() => reg.Dispose ());
381 reg = token.Register (() => unregister = true);
382 token.Register (() => { Console.WriteLine ("Gnyah"); token.Register (() => register = true); });
385 Assert.IsFalse (unregister);
386 Assert.IsTrue (register);
390 public void DisposeAfterRegistrationTest ()
392 var source = new CancellationTokenSource ();
394 var req = source.Token.Register (() => ran = true);
397 Assert.IsFalse (ran);
401 public void CancelLinkedTokenSource ()
403 var cts = new CancellationTokenSource ();
404 bool canceled = false;
405 cts.Token.Register (() => canceled = true);
407 using (var linked = CancellationTokenSource.CreateLinkedTokenSource (cts.Token))
410 Assert.IsFalse (canceled, "#1");
411 Assert.IsFalse (cts.IsCancellationRequested, "#2");
415 Assert.IsTrue (canceled, "#3");
419 public void ConcurrentCancelLinkedTokenSourceWhileDisposing ()
421 ParallelTestHelper.Repeat (delegate {
422 var src = new CancellationTokenSource ();
423 var linked = CancellationTokenSource.CreateLinkedTokenSource (src.Token);
424 var cntd = new CountdownEvent (2);
426 var t1 = new Thread (() => {
431 var t2 = new Thread (() => {