TaskCompletionSourceTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #if NET_4_0
  2. //
  3. // TaskCompletionSourceTests.cs
  4. //
  5. // Author:
  6. // Jérémie "Garuma" Laval <[email protected]>
  7. //
  8. // Copyright (c) 2009 Jérémie "Garuma" Laval
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to deal
  12. // in the Software without restriction, including without limitation the rights
  13. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. // THE SOFTWARE.
  27. using System;
  28. using System.Threading;
  29. using System.Threading.Tasks;
  30. using NUnit.Framework;
  31. namespace MonoTests.System.Threading.Tasks
  32. {
  33. [TestFixture]
  34. public class TaskCompletionSourceTests
  35. {
  36. TaskCompletionSource<int> completionSource;
  37. object state;
  38. [SetUp]
  39. public void Setup ()
  40. {
  41. state = new object ();
  42. completionSource = new TaskCompletionSource<int> (state, TaskCreationOptions.LongRunning);
  43. }
  44. [Test]
  45. public void CreationCheckTest ()
  46. {
  47. Assert.IsNotNull (completionSource.Task, "#1");
  48. Assert.AreEqual (TaskCreationOptions.LongRunning, completionSource.Task.CreationOptions, "#2");
  49. }
  50. [Test]
  51. public void SetResultTest ()
  52. {
  53. Assert.IsNotNull (completionSource.Task, "#1");
  54. Assert.IsTrue (completionSource.TrySetResult (42), "#2");
  55. Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#3");
  56. Assert.AreEqual (42, completionSource.Task.Result, "#4");
  57. Assert.IsFalse (completionSource.TrySetResult (43), "#5");
  58. Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#6");
  59. Assert.AreEqual (42, completionSource.Task.Result, "#7");
  60. Assert.IsFalse (completionSource.TrySetCanceled (), "#8");
  61. Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#9");
  62. }
  63. [Test]
  64. public void SetCanceledTest ()
  65. {
  66. Assert.IsNotNull (completionSource.Task, "#1");
  67. Assert.IsTrue (completionSource.TrySetCanceled (), "#2");
  68. Assert.AreEqual (TaskStatus.Canceled, completionSource.Task.Status, "#3");
  69. Assert.IsFalse (completionSource.TrySetResult (42), "#4");
  70. Assert.AreEqual (TaskStatus.Canceled, completionSource.Task.Status, "#5");
  71. }
  72. [Test]
  73. public void SetExceptionTest ()
  74. {
  75. Exception e = new Exception ("foo");
  76. Assert.IsNotNull (completionSource.Task, "#1");
  77. Assert.IsTrue (completionSource.TrySetException (e), "#2");
  78. Assert.AreEqual (TaskStatus.Faulted, completionSource.Task.Status, "#3");
  79. Assert.IsInstanceOfType (typeof (AggregateException), completionSource.Task.Exception, "#4.1");
  80. AggregateException aggr = (AggregateException)completionSource.Task.Exception;
  81. Assert.AreEqual (1, aggr.InnerExceptions.Count, "#4.2");
  82. Assert.AreEqual (e, aggr.InnerExceptions[0], "#4.3");
  83. Assert.IsFalse (completionSource.TrySetResult (42), "#5");
  84. Assert.AreEqual (TaskStatus.Faulted, completionSource.Task.Status, "#6");
  85. Assert.IsFalse (completionSource.TrySetCanceled (), "#8");
  86. Assert.AreEqual (TaskStatus.Faulted, completionSource.Task.Status, "#9");
  87. }
  88. [Test, ExpectedException (typeof (InvalidOperationException))]
  89. public void SetResultExceptionTest ()
  90. {
  91. Assert.IsNotNull (completionSource.Task, "#1");
  92. Assert.IsTrue (completionSource.TrySetResult (42), "#2");
  93. Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#3");
  94. Assert.AreEqual (42, completionSource.Task.Result, "#4");
  95. completionSource.SetResult (43);
  96. }
  97. }
  98. }
  99. #endif