TaskTest.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.AreNotEqual (-1, index, "#3");
  64. Assert.AreEqual (1, flag, "#1");
  65. Assert.AreEqual (1, finished, "#2");
  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), ex, "#2");
  97. Assert.IsNull (t.Exception, "#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. Assert.IsTrue (t.Wait (2000), "First wait, (status, {0})", t.Status);
  110. Assert.IsTrue (cont.Wait(2000), "Cont wait, (result, {0}) (parent status, {2}) (status, {1})", result, cont.Status, t.Status);
  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; },
  140. TaskContinuationOptions.OnlyOnCanceled | TaskContinuationOptions.ExecuteSynchronously);
  141. t.Start();
  142. cont.Wait();
  143. Assert.IsFalse (taskResult, "#-1");
  144. Assert.AreEqual (TaskStatus.Canceled, t.Status, "#0");
  145. Assert.IsTrue (t.IsCanceled, "#0bis");
  146. Assert.IsNull(cont.Exception, "#1");
  147. Assert.IsNotNull(cont, "#2");
  148. Assert.IsTrue(result, "#3");
  149. });
  150. }
  151. [Test]
  152. public void ContinueWithOnFailedTestCase()
  153. {
  154. ParallelTestHelper.Repeat (delegate {
  155. bool result = false;
  156. Task t = Task.Factory.StartNew(delegate { throw new Exception("foo"); });
  157. Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnFaulted);
  158. cont.Wait();
  159. Assert.IsNotNull (t.Exception, "#1");
  160. Assert.IsNotNull (cont, "#2");
  161. Assert.IsTrue (result, "#3");
  162. });
  163. }
  164. [Test]
  165. public void ContinueWithChildren ()
  166. {
  167. ParallelTestHelper.Repeat (delegate {
  168. bool result = false;
  169. var t = Task.Factory.StartNew (() => Task.Factory.StartNew (() => Thread.Sleep (100), TaskCreationOptions.AttachedToParent));
  170. t.ContinueWith (_ => result = true);
  171. while (!t.IsCompleted)
  172. Thread.Sleep (200);
  173. Assert.IsTrue (result);
  174. }, 2);
  175. }
  176. [TestAttribute]
  177. public void MultipleTaskTestCase()
  178. {
  179. ParallelTestHelper.Repeat (delegate {
  180. bool r1 = false, r2 = false, r3 = false;
  181. Task t1 = Task.Factory.StartNew(delegate {
  182. r1 = true;
  183. });
  184. Task t2 = Task.Factory.StartNew(delegate {
  185. r2 = true;
  186. });
  187. Task t3 = Task.Factory.StartNew(delegate {
  188. r3 = true;
  189. });
  190. t1.Wait();
  191. t2.Wait();
  192. t3.Wait();
  193. Assert.IsTrue(r1, "#1");
  194. Assert.IsTrue(r2, "#2");
  195. Assert.IsTrue(r3, "#3");
  196. });
  197. }
  198. [Test]
  199. public void WaitChildTestCase()
  200. {
  201. ParallelTestHelper.Repeat (delegate {
  202. bool r1 = false, r2 = false, r3 = false;
  203. Task t = Task.Factory.StartNew(delegate {
  204. Task.Factory.StartNew(delegate {
  205. Thread.Sleep(50);
  206. r1 = true;
  207. }, TaskCreationOptions.AttachedToParent);
  208. Task.Factory.StartNew(delegate {
  209. Thread.Sleep(300);
  210. r2 = true;
  211. }, TaskCreationOptions.AttachedToParent);
  212. Task.Factory.StartNew(delegate {
  213. Thread.Sleep(150);
  214. r3 = true;
  215. }, TaskCreationOptions.AttachedToParent);
  216. });
  217. t.Wait();
  218. Assert.IsTrue(r2, "#1");
  219. Assert.IsTrue(r3, "#2");
  220. Assert.IsTrue(r1, "#3");
  221. Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#4");
  222. }, 10);
  223. }
  224. [Test]
  225. public void ExecuteSynchronouslyTest ()
  226. {
  227. var val = 0;
  228. Task t = new Task (() => { Thread.Sleep (100); val = 1; });
  229. t.RunSynchronously ();
  230. Assert.AreEqual (1, val);
  231. }
  232. }
  233. }
  234. #endif