TaskCompletionSourceTests.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.None);
  43. }
  44. [Test]
  45. public void CreationCheckTest ()
  46. {
  47. Assert.IsNotNull (completionSource.Task, "#1");
  48. Assert.AreEqual (TaskCreationOptions.None, completionSource.Task.CreationOptions, "#2");
  49. }
  50. [Test]
  51. public void CtorInvalidOptions ()
  52. {
  53. try {
  54. new TaskCompletionSource<long> (TaskCreationOptions.LongRunning);
  55. Assert.Fail ("#1");
  56. } catch (ArgumentOutOfRangeException) {
  57. }
  58. try {
  59. new TaskCompletionSource<long> (TaskCreationOptions.PreferFairness);
  60. Assert.Fail ("#2");
  61. } catch (ArgumentOutOfRangeException) {
  62. }
  63. }
  64. [Test]
  65. public void SetResultTest ()
  66. {
  67. Assert.IsNotNull (completionSource.Task, "#1");
  68. Assert.IsTrue (completionSource.TrySetResult (42), "#2");
  69. Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#3");
  70. Assert.AreEqual (42, completionSource.Task.Result, "#4");
  71. Assert.IsFalse (completionSource.TrySetResult (43), "#5");
  72. Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#6");
  73. Assert.AreEqual (42, completionSource.Task.Result, "#7");
  74. Assert.IsFalse (completionSource.TrySetCanceled (), "#8");
  75. Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#9");
  76. }
  77. [Test]
  78. public void SetCanceledTest ()
  79. {
  80. Assert.IsNotNull (completionSource.Task, "#1");
  81. Assert.IsTrue (completionSource.TrySetCanceled (), "#2");
  82. Assert.AreEqual (TaskStatus.Canceled, completionSource.Task.Status, "#3");
  83. Assert.IsFalse (completionSource.TrySetResult (42), "#4");
  84. Assert.AreEqual (TaskStatus.Canceled, completionSource.Task.Status, "#5");
  85. try {
  86. Console.WriteLine (completionSource.Task.Result);
  87. Assert.Fail ("#6");
  88. } catch (AggregateException e) {
  89. var details = (TaskCanceledException) e.InnerException;
  90. Assert.AreEqual (completionSource.Task, details.Task, "#6e");
  91. Assert.IsNull (details.Task.Exception, "#6e2");
  92. }
  93. }
  94. [Test]
  95. public void SetExceptionTest ()
  96. {
  97. Exception e = new Exception ("foo");
  98. Assert.IsNotNull (completionSource.Task, "#1");
  99. Assert.IsTrue (completionSource.TrySetException (e), "#2");
  100. Assert.AreEqual (TaskStatus.Faulted, completionSource.Task.Status, "#3");
  101. Assert.IsInstanceOfType (typeof (AggregateException), completionSource.Task.Exception, "#4.1");
  102. AggregateException aggr = (AggregateException)completionSource.Task.Exception;
  103. Assert.AreEqual (1, aggr.InnerExceptions.Count, "#4.2");
  104. Assert.AreEqual (e, aggr.InnerExceptions[0], "#4.3");
  105. Assert.IsFalse (completionSource.TrySetResult (42), "#5");
  106. Assert.AreEqual (TaskStatus.Faulted, completionSource.Task.Status, "#6");
  107. Assert.IsFalse (completionSource.TrySetCanceled (), "#8");
  108. Assert.AreEqual (TaskStatus.Faulted, completionSource.Task.Status, "#9");
  109. }
  110. [Test]
  111. public void SetExceptionInvalid ()
  112. {
  113. try {
  114. completionSource.TrySetException (new ApplicationException[0]);
  115. Assert.Fail ("#1");
  116. } catch (ArgumentException) {
  117. }
  118. try {
  119. completionSource.TrySetException (new [] { new ApplicationException (), null });
  120. Assert.Fail ("#2");
  121. } catch (ArgumentException) {
  122. }
  123. Assert.AreEqual (TaskStatus.WaitingForActivation, completionSource.Task.Status, "r1");
  124. }
  125. [Test, ExpectedException (typeof (InvalidOperationException))]
  126. public void SetResultExceptionTest ()
  127. {
  128. Assert.IsNotNull (completionSource.Task, "#1");
  129. Assert.IsTrue (completionSource.TrySetResult (42), "#2");
  130. Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#3");
  131. Assert.AreEqual (42, completionSource.Task.Result, "#4");
  132. completionSource.SetResult (43);
  133. }
  134. [Test]
  135. public void ContinuationTest ()
  136. {
  137. bool result = false;
  138. var t = completionSource.Task.ContinueWith ((p) => { if (p.Result == 2) result = true; });
  139. Assert.AreEqual (TaskStatus.WaitingForActivation, completionSource.Task.Status, "#A");
  140. completionSource.SetResult (2);
  141. t.Wait ();
  142. Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#1");
  143. Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#2");
  144. Assert.IsTrue (result);
  145. }
  146. [Test]
  147. public void FaultedFutureTest ()
  148. {
  149. var thrown = new ApplicationException ();
  150. var source = new TaskCompletionSource<int> ();
  151. source.TrySetException (thrown);
  152. var f = source.Task;
  153. AggregateException ex = null;
  154. try {
  155. f.Wait ();
  156. } catch (AggregateException e) {
  157. ex = e;
  158. }
  159. Assert.IsNotNull (ex);
  160. Assert.AreEqual (thrown, ex.InnerException);
  161. Assert.AreEqual (thrown, f.Exception.InnerException);
  162. Assert.AreEqual (TaskStatus.Faulted, f.Status);
  163. ex = null;
  164. try {
  165. var result = f.Result;
  166. } catch (AggregateException e) {
  167. ex = e;
  168. }
  169. Assert.IsNotNull (ex);
  170. Assert.AreEqual (TaskStatus.Faulted, f.Status);
  171. Assert.AreEqual (thrown, f.Exception.InnerException);
  172. Assert.AreEqual (thrown, ex.InnerException);
  173. }
  174. [Test]
  175. public void SetExceptionAndUnobservedEvent ()
  176. {
  177. bool wasCalled = false;
  178. bool notFromMainThread = false;
  179. int mainThreadId = Thread.CurrentThread.ManagedThreadId;
  180. TaskScheduler.UnobservedTaskException += (o, args) => {
  181. wasCalled = true;
  182. notFromMainThread = Thread.CurrentThread.ManagedThreadId != mainThreadId;
  183. args.SetObserved ();
  184. };
  185. var inner = new ApplicationException ();
  186. CreateFaultedTaskCompletionSource (inner);
  187. Thread.Sleep (1000);
  188. GC.Collect ();
  189. GC.WaitForPendingFinalizers ();
  190. Assert.IsTrue (wasCalled);
  191. Assert.IsTrue (notFromMainThread);
  192. }
  193. void CreateFaultedTaskCompletionSource (Exception inner)
  194. {
  195. var tcs = new TaskCompletionSource<int> ();
  196. tcs.SetException (inner);
  197. tcs = null;
  198. }
  199. [Test]
  200. public void WaitingTest ()
  201. {
  202. var tcs = new TaskCompletionSource<int> ();
  203. var task = tcs.Task;
  204. bool result = task.Wait (50);
  205. Assert.IsFalse (result);
  206. }
  207. }
  208. }
  209. #endif