CancellationTokenTests.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // CancellationTokenTests.cs
  3. //
  4. // Authors:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. // Marek Safar ([email protected])
  7. //
  8. // Copyright (c) 2009 Jérémie "Garuma" Laval
  9. // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in
  19. // all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. // THE SOFTWARE.
  28. using System;
  29. using System.Threading;
  30. using NUnit.Framework;
  31. namespace MonoTests.System.Threading
  32. {
  33. [TestFixture]
  34. public class CancellationTokenTests
  35. {
  36. [Test]
  37. public void InitedWithFalseToken ()
  38. {
  39. CancellationToken tk = new CancellationToken (false);
  40. Assert.IsFalse (tk.CanBeCanceled, "#1");
  41. Assert.IsFalse (tk.IsCancellationRequested, "#2");
  42. }
  43. [Test]
  44. public void InitedWithTrueToken ()
  45. {
  46. CancellationToken tk = new CancellationToken (true);
  47. Assert.IsTrue (tk.CanBeCanceled, "#1");
  48. Assert.IsTrue (tk.IsCancellationRequested, "#2");
  49. }
  50. [Test]
  51. public void CancellationSourceNotCanceled ()
  52. {
  53. var src = new CancellationTokenSource ();
  54. var tk = src.Token;
  55. Assert.IsTrue (tk.CanBeCanceled);
  56. Assert.IsFalse (tk.IsCancellationRequested);
  57. }
  58. [Test]
  59. public void CancellationSourceCanceled ()
  60. {
  61. var src = new CancellationTokenSource ();
  62. var tk = src.Token;
  63. src.Cancel ();
  64. Assert.IsTrue (tk.CanBeCanceled, "#1");
  65. Assert.IsTrue (tk.IsCancellationRequested, "#2");
  66. }
  67. [Test]
  68. public void UninitializedToken ()
  69. {
  70. var tk = new CancellationToken ();
  71. Assert.IsFalse (tk.CanBeCanceled);
  72. Assert.IsFalse (tk.IsCancellationRequested);
  73. }
  74. [Test]
  75. public void NoneProperty ()
  76. {
  77. var n = CancellationToken.None;
  78. Assert.IsFalse (n.CanBeCanceled, "#1");
  79. Assert.IsFalse (n.IsCancellationRequested, "#2");
  80. Assert.AreEqual (n, CancellationToken.None, "#3");
  81. n.ThrowIfCancellationRequested ();
  82. n.GetHashCode ();
  83. }
  84. [Test]
  85. public void DefaultCancellationTokenRegistration ()
  86. {
  87. var registration = new CancellationTokenRegistration ();
  88. // shouldn't throw
  89. registration.Dispose ();
  90. }
  91. }
  92. }