TaskCompletionSourceTests.cs 7.5 KB

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