TaskCompletionSourceTests.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // TaskCompletionSourceTests.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. //
  7. // Copyright (c) 2009 Jérémie "Garuma" Laval
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if NET_4_0
  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]
  89. public void SetExceptionInvalid ()
  90. {
  91. try {
  92. completionSource.TrySetException (new ApplicationException[0]);
  93. Assert.Fail ("#1");
  94. } catch (ArgumentException) {
  95. }
  96. try {
  97. completionSource.TrySetException (new [] { new ApplicationException (), null });
  98. Assert.Fail ("#2");
  99. } catch (ArgumentException) {
  100. }
  101. Assert.AreEqual (TaskStatus.WaitingForActivation, completionSource.Task.Status, "r1");
  102. }
  103. [Test, ExpectedException (typeof (InvalidOperationException))]
  104. public void SetResultExceptionTest ()
  105. {
  106. Assert.IsNotNull (completionSource.Task, "#1");
  107. Assert.IsTrue (completionSource.TrySetResult (42), "#2");
  108. Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#3");
  109. Assert.AreEqual (42, completionSource.Task.Result, "#4");
  110. completionSource.SetResult (43);
  111. }
  112. [Test]
  113. public void ContinuationTest ()
  114. {
  115. bool result = false;
  116. var t = completionSource.Task.ContinueWith ((p) => { if (p.Result == 2) result = true; });
  117. Assert.AreEqual (TaskStatus.WaitingForActivation, completionSource.Task.Status, "#A");
  118. completionSource.SetResult (2);
  119. t.Wait ();
  120. Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#1");
  121. Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#2");
  122. Assert.IsTrue (result);
  123. }
  124. [Test]
  125. public void FaultedFutureTest ()
  126. {
  127. var thrown = new ApplicationException ();
  128. var source = new TaskCompletionSource<int> ();
  129. source.TrySetException (thrown);
  130. var f = source.Task;
  131. AggregateException ex = null;
  132. try {
  133. f.Wait ();
  134. } catch (AggregateException e) {
  135. ex = e;
  136. }
  137. Assert.IsNotNull (ex);
  138. Assert.AreEqual (thrown, ex.InnerException);
  139. Assert.AreEqual (thrown, f.Exception.InnerException);
  140. Assert.AreEqual (TaskStatus.Faulted, f.Status);
  141. ex = null;
  142. try {
  143. var result = f.Result;
  144. } catch (AggregateException e) {
  145. ex = e;
  146. }
  147. Assert.IsNotNull (ex);
  148. Assert.AreEqual (TaskStatus.Faulted, f.Status);
  149. Assert.AreEqual (thrown, f.Exception.InnerException);
  150. Assert.AreEqual (thrown, ex.InnerException);
  151. }
  152. [Test]
  153. public void WaitingTest ()
  154. {
  155. var tcs = new TaskCompletionSource<int> ();
  156. var task = tcs.Task;
  157. bool result = task.Wait (50);
  158. Assert.IsFalse (result);
  159. }
  160. }
  161. }
  162. #endif