CancellationTokenTests.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #if NET_4_0
  29. using System;
  30. using System.Threading;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.Threading
  33. {
  34. [TestFixture]
  35. public class CancellationTokenTests
  36. {
  37. [Test]
  38. public void InitedWithFalseToken ()
  39. {
  40. CancellationToken tk = new CancellationToken (false);
  41. Assert.IsFalse (tk.CanBeCanceled, "#1");
  42. Assert.IsFalse (tk.IsCancellationRequested, "#2");
  43. }
  44. [Test]
  45. public void InitedWithTrueToken ()
  46. {
  47. CancellationToken tk = new CancellationToken (true);
  48. Assert.IsTrue (tk.CanBeCanceled, "#1");
  49. Assert.IsTrue (tk.IsCancellationRequested, "#2");
  50. }
  51. [Test]
  52. public void CancellationSourceNotCanceled ()
  53. {
  54. var src = new CancellationTokenSource ();
  55. var tk = src.Token;
  56. Assert.IsTrue (tk.CanBeCanceled);
  57. Assert.IsFalse (tk.IsCancellationRequested);
  58. }
  59. [Test]
  60. public void CancellationSourceCanceled ()
  61. {
  62. var src = new CancellationTokenSource ();
  63. var tk = src.Token;
  64. src.Cancel ();
  65. Assert.IsTrue (tk.CanBeCanceled, "#1");
  66. Assert.IsTrue (tk.IsCancellationRequested, "#2");
  67. }
  68. [Test]
  69. public void UninitializedToken ()
  70. {
  71. var tk = new CancellationToken ();
  72. Assert.IsFalse (tk.CanBeCanceled);
  73. Assert.IsFalse (tk.IsCancellationRequested);
  74. }
  75. [Test]
  76. public void NoneProperty ()
  77. {
  78. var n = CancellationToken.None;
  79. Assert.IsFalse (n.CanBeCanceled, "#1");
  80. Assert.IsFalse (n.IsCancellationRequested, "#2");
  81. Assert.AreEqual (n, CancellationToken.None, "#3");
  82. n.ThrowIfCancellationRequested ();
  83. n.GetHashCode ();
  84. }
  85. [Test]
  86. public void DefaultCancellationTokenRegistration ()
  87. {
  88. var registration = new CancellationTokenRegistration ();
  89. // shouldn't throw
  90. registration.Dispose ();
  91. }
  92. }
  93. }
  94. #endif