TaskTest.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #if NET_4_0
  2. // TaskTest.cs
  3. //
  4. // Copyright (c) 2008 Jérémie "Garuma" Laval
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. //
  25. using System;
  26. using System.Threading;
  27. using System.Threading.Tasks;
  28. using NUnit.Framework;
  29. namespace MonoTests.System.Threading.Tasks
  30. {
  31. [TestFixture]
  32. public class TaskTests
  33. {
  34. Task[] tasks;
  35. static readonly int max = 3 * Environment.ProcessorCount;
  36. [SetUp]
  37. public void Setup()
  38. {
  39. tasks = new Task[max];
  40. }
  41. void InitWithDelegate(Action action)
  42. {
  43. for (int i = 0; i < max; i++) {
  44. tasks[i] = Task.Factory.StartNew(action);
  45. }
  46. }
  47. [TestAttribute]
  48. public void WaitAnyTest()
  49. {
  50. ParallelTestHelper.Repeat (delegate {
  51. int flag = 0;
  52. int finished = 0;
  53. InitWithDelegate(delegate {
  54. int times = Interlocked.Exchange (ref flag, 1);
  55. if (times == 1) {
  56. SpinWait sw = new SpinWait ();
  57. while (finished == 0) sw.SpinOnce ();
  58. } else {
  59. Interlocked.Increment (ref finished);
  60. }
  61. });
  62. int index = Task.WaitAny(tasks);
  63. Assert.IsTrue (flag == 1, "#1");
  64. Assert.AreEqual (1, finished, "#2");
  65. Assert.AreNotEqual (-1, index, "#3");
  66. Task.WaitAll (tasks);
  67. });
  68. }
  69. [TestAttribute]
  70. public void WaitAllTest()
  71. {
  72. ParallelTestHelper.Repeat (delegate {
  73. int achieved = 0;
  74. InitWithDelegate(delegate { Interlocked.Increment(ref achieved); });
  75. Task.WaitAll(tasks);
  76. Assert.AreEqual(max, achieved, "#1");
  77. });
  78. }
  79. [Test]
  80. public void CancelTestCase()
  81. {
  82. bool result = false;
  83. CancellationTokenSource src = new CancellationTokenSource ();
  84. Task t = new Task (delegate {
  85. result = true;
  86. }, src.Token);
  87. src.Cancel ();
  88. t.Start ();
  89. Exception ex = null;
  90. try {
  91. t.Wait ();
  92. } catch (Exception e) {
  93. ex = e;
  94. }
  95. Assert.IsNotNull (ex, "#1");
  96. Assert.IsInstanceOfType (typeof(AggregateException), t.Exception, "#2");
  97. Assert.AreEqual (t.Exception, ex, "#3");
  98. AggregateException aggr = (AggregateException)ex;
  99. Assert.AreEqual (1, aggr.InnerExceptions.Count, "#4");
  100. Assert.IsInstanceOfType (typeof (OperationCanceledException), aggr.InnerExceptions[0], "#5");
  101. }
  102. [Test]
  103. public void ContinueWithOnAnyTestCase()
  104. {
  105. ParallelTestHelper.Repeat (delegate {
  106. bool result = false;
  107. Task t = Task.Factory.StartNew(delegate { });
  108. Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.None);
  109. t.Wait();
  110. cont.Wait();
  111. Assert.IsNull(cont.Exception, "#1");
  112. Assert.IsNotNull(cont, "#2");
  113. Assert.IsTrue(result, "#3");
  114. });
  115. }
  116. [Test]
  117. public void ContinueWithOnCompletedSuccessfullyTestCase()
  118. {
  119. ParallelTestHelper.Repeat (delegate {
  120. bool result = false;
  121. Task t = Task.Factory.StartNew(delegate { });
  122. Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnRanToCompletion);
  123. t.Wait();
  124. cont.Wait();
  125. Assert.IsNull(cont.Exception, "#1");
  126. Assert.IsNotNull(cont, "#2");
  127. Assert.IsTrue(result, "#3");
  128. });
  129. }
  130. [Test]
  131. public void ContinueWithOnAbortedTestCase()
  132. {
  133. ParallelTestHelper.Repeat (delegate {
  134. bool result = false;
  135. bool taskResult = false;
  136. CancellationTokenSource src = new CancellationTokenSource ();
  137. Task t = new Task(delegate { taskResult = true; }, src.Token);
  138. src.Cancel ();
  139. Task cont = t.ContinueWith (delegate { result = true; }, TaskContinuationOptions.OnlyOnCanceled);
  140. t.Start();
  141. cont.Wait();
  142. Assert.IsFalse (taskResult, "#-1");
  143. Assert.AreEqual (TaskStatus.Canceled, t.Status, "#0");
  144. Assert.IsTrue (t.IsCanceled, "#0bis");
  145. Assert.IsNull(cont.Exception, "#1");
  146. Assert.IsNotNull(cont, "#2");
  147. Assert.IsTrue(result, "#3");
  148. });
  149. }
  150. [Test]
  151. public void ContinueWithOnFailedTestCase()
  152. {
  153. ParallelTestHelper.Repeat (delegate {
  154. bool result = false;
  155. Task t = Task.Factory.StartNew(delegate { throw new Exception("foo"); });
  156. Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnFaulted);
  157. cont.Wait();
  158. Assert.IsNotNull (t.Exception, "#1");
  159. Assert.IsNotNull (cont, "#2");
  160. Assert.IsTrue (result, "#3");
  161. });
  162. }
  163. [TestAttribute]
  164. public void MultipleTaskTestCase()
  165. {
  166. ParallelTestHelper.Repeat (delegate {
  167. bool r1 = false, r2 = false, r3 = false;
  168. Task t1 = Task.Factory.StartNew(delegate {
  169. r1 = true;
  170. });
  171. Task t2 = Task.Factory.StartNew(delegate {
  172. r2 = true;
  173. });
  174. Task t3 = Task.Factory.StartNew(delegate {
  175. r3 = true;
  176. });
  177. t1.Wait();
  178. t2.Wait();
  179. t3.Wait();
  180. Assert.IsTrue(r1, "#1");
  181. Assert.IsTrue(r2, "#2");
  182. Assert.IsTrue(r3, "#3");
  183. });
  184. }
  185. [Test]
  186. public void WaitChildTestCase()
  187. {
  188. ParallelTestHelper.Repeat (delegate {
  189. bool r1 = false, r2 = false, r3 = false;
  190. Task t = Task.Factory.StartNew(delegate {
  191. Task.Factory.StartNew(delegate {
  192. Thread.Sleep(50);
  193. r1 = true;
  194. }, TaskCreationOptions.AttachedToParent);
  195. Task.Factory.StartNew(delegate {
  196. Thread.Sleep(300);
  197. r2 = true;
  198. }, TaskCreationOptions.AttachedToParent);
  199. Task.Factory.StartNew(delegate {
  200. Thread.Sleep(150);
  201. r3 = true;
  202. }, TaskCreationOptions.AttachedToParent);
  203. });
  204. t.Wait();
  205. Assert.IsTrue(r2, "#1");
  206. Assert.IsTrue(r3, "#2");
  207. Assert.IsTrue(r1, "#3");
  208. Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#4");
  209. }, 10);
  210. }
  211. }
  212. }
  213. #endif