TaskTest.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  1. //
  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. #if NET_4_0
  26. using System;
  27. using System.Threading;
  28. using System.Threading.Tasks;
  29. using NUnit.Framework;
  30. namespace MonoTests.System.Threading.Tasks
  31. {
  32. [TestFixture]
  33. public class TaskTests
  34. {
  35. Task[] tasks;
  36. const int max = 6;
  37. [SetUp]
  38. public void Setup()
  39. {
  40. tasks = new Task[max];
  41. }
  42. void InitWithDelegate(Action action)
  43. {
  44. for (int i = 0; i < max; i++) {
  45. tasks[i] = Task.Factory.StartNew(action);
  46. }
  47. }
  48. [Test]
  49. public void WaitAnyTest()
  50. {
  51. ParallelTestHelper.Repeat (delegate {
  52. int flag = 0;
  53. int finished = 0;
  54. InitWithDelegate(delegate {
  55. int times = Interlocked.Exchange (ref flag, 1);
  56. if (times == 1) {
  57. SpinWait sw = new SpinWait ();
  58. while (finished == 0) sw.SpinOnce ();
  59. } else {
  60. Interlocked.Increment (ref finished);
  61. }
  62. });
  63. int index = Task.WaitAny(tasks, 1000);
  64. Assert.AreNotEqual (-1, index, "#3");
  65. Assert.AreEqual (1, flag, "#1");
  66. Assert.AreEqual (1, finished, "#2");
  67. });
  68. }
  69. [Test]
  70. public void WaitAny_Empty ()
  71. {
  72. Assert.AreEqual (-1, Task.WaitAny (new Task[0]));
  73. }
  74. [Test]
  75. public void WaitAny_Zero ()
  76. {
  77. Assert.AreEqual (-1, Task.WaitAny (new[] { new Task (delegate { })}, 0), "#1");
  78. Assert.AreEqual (-1, Task.WaitAny (new[] { new Task (delegate { }) }, 20), "#1");
  79. }
  80. [Test]
  81. public void WaitAny_Cancelled ()
  82. {
  83. var cancelation = new CancellationTokenSource ();
  84. var tasks = new Task[] {
  85. new Task (delegate { }),
  86. new Task (delegate { }, cancelation.Token)
  87. };
  88. cancelation.Cancel ();
  89. Assert.AreEqual (1, Task.WaitAny (tasks, 1000), "#1");
  90. Assert.IsTrue (tasks[1].IsCompleted, "#2");
  91. Assert.IsTrue (tasks[1].IsCanceled, "#3");
  92. }
  93. [Test]
  94. public void WaitAny_CancelledWithoutExecution ()
  95. {
  96. var cancelation = new CancellationTokenSource ();
  97. var tasks = new Task[] {
  98. new Task (delegate { }),
  99. new Task (delegate { })
  100. };
  101. int res = 0;
  102. var mre = new ManualResetEventSlim (false);
  103. ThreadPool.QueueUserWorkItem (delegate {
  104. res = Task.WaitAny (tasks, 20);
  105. mre.Set ();
  106. });
  107. cancelation.Cancel ();
  108. Assert.IsTrue (mre.Wait (1000), "#1");
  109. Assert.AreEqual (-1, res);
  110. }
  111. [Test]
  112. public void WaitAny_OneException ()
  113. {
  114. var mre = new ManualResetEventSlim (false);
  115. var tasks = new Task[] {
  116. Task.Factory.StartNew (delegate { mre.Wait (1000); }),
  117. Task.Factory.StartNew (delegate { throw new ApplicationException (); })
  118. };
  119. Assert.AreEqual (1, Task.WaitAny (tasks, 1000), "#1");
  120. Assert.IsFalse (tasks[0].IsCompleted, "#2");
  121. Assert.IsTrue (tasks[1].IsFaulted, "#3");
  122. mre.Set ();
  123. }
  124. [Test]
  125. public void WaitAny_SingleCanceled ()
  126. {
  127. var src = new CancellationTokenSource ();
  128. var t = Task.Factory.StartNew (() => { Thread.Sleep (200); src.Cancel (); src.Token.ThrowIfCancellationRequested (); }, src.Token);
  129. Assert.AreEqual (0, Task.WaitAny (new [] { t }));
  130. }
  131. public void WaitAny_ManyExceptions ()
  132. {
  133. CountdownEvent cde = new CountdownEvent (3);
  134. var tasks = new [] {
  135. Task.Factory.StartNew (delegate { try { throw new ApplicationException (); } finally { cde.Signal (); } }),
  136. Task.Factory.StartNew (delegate { try { throw new ApplicationException (); } finally { cde.Signal (); } }),
  137. Task.Factory.StartNew (delegate { try { throw new ApplicationException (); } finally { cde.Signal (); } })
  138. };
  139. Assert.IsTrue (cde.Wait (1000), "#1");
  140. try {
  141. Assert.IsTrue (Task.WaitAll (tasks, 1000), "#2");
  142. } catch (AggregateException e) {
  143. Assert.AreEqual (3, e.InnerExceptions.Count, "#3");
  144. }
  145. }
  146. [Test]
  147. public void WaitAny_ManyCanceled ()
  148. {
  149. var cancellation = new CancellationToken (true);
  150. var tasks = new[] {
  151. Task.Factory.StartNew (delegate { }, cancellation),
  152. Task.Factory.StartNew (delegate { }, cancellation),
  153. Task.Factory.StartNew (delegate { }, cancellation)
  154. };
  155. try {
  156. Assert.IsTrue (Task.WaitAll (tasks, 1000), "#1");
  157. } catch (AggregateException e) {
  158. Assert.AreEqual (3, e.InnerExceptions.Count, "#2");
  159. }
  160. }
  161. [Test]
  162. public void WaitAllTest()
  163. {
  164. ParallelTestHelper.Repeat (delegate {
  165. int achieved = 0;
  166. InitWithDelegate(delegate { Interlocked.Increment(ref achieved); });
  167. Task.WaitAll(tasks);
  168. Assert.AreEqual(max, achieved, "#1");
  169. });
  170. }
  171. [Test]
  172. public void WaitAll_ManyTasks ()
  173. {
  174. for (int r = 0; r < 2000; ++r) {
  175. var tasks = new Task[60];
  176. for (int i = 0; i < tasks.Length; i++) {
  177. tasks[i] = Task.Factory.StartNew (delegate { Thread.Sleep (0); });
  178. }
  179. Assert.IsTrue (Task.WaitAll (tasks, 2000));
  180. }
  181. }
  182. [Test]
  183. public void WaitAll_Zero ()
  184. {
  185. Assert.IsFalse (Task.WaitAll (new Task[1] { new Task (delegate { }) }, 0), "#0");
  186. Assert.IsFalse (Task.WaitAll (new Task[1] { new Task (delegate { }) }, 10), "#1");
  187. }
  188. [Test]
  189. public void WaitAll_WithExceptions ()
  190. {
  191. InitWithDelegate (delegate { throw new ApplicationException (); });
  192. try {
  193. Task.WaitAll (tasks);
  194. Assert.Fail ("#1");
  195. } catch (AggregateException e) {
  196. Assert.AreEqual (6, e.InnerExceptions.Count, "#2");
  197. }
  198. Assert.IsNotNull (tasks[0].Exception, "#3");
  199. }
  200. [Test]
  201. public void WaitAll_TimeoutWithExceptionsAfter ()
  202. {
  203. CountdownEvent cde = new CountdownEvent (2);
  204. var mre = new ManualResetEvent (false);
  205. var tasks = new[] {
  206. Task.Factory.StartNew (delegate { mre.WaitOne (); }),
  207. Task.Factory.StartNew (delegate { try { throw new ApplicationException (); } finally { cde.Signal (); } }),
  208. Task.Factory.StartNew (delegate { try { throw new ApplicationException (); } finally { cde.Signal (); } })
  209. };
  210. Assert.IsTrue (cde.Wait (100), "#1");
  211. Assert.IsFalse (Task.WaitAll (tasks, 100), "#2");
  212. mre.Set ();
  213. try {
  214. Assert.IsTrue (Task.WaitAll (tasks, 1000), "#3");
  215. Assert.Fail ("#4");
  216. } catch (AggregateException e) {
  217. Assert.AreEqual (2, e.InnerExceptions.Count, "#5");
  218. }
  219. }
  220. [Test]
  221. public void WaitAll_TimeoutWithExceptionsBefore ()
  222. {
  223. CountdownEvent cde = new CountdownEvent (2);
  224. var mre = new ManualResetEvent (false);
  225. var tasks = new[] {
  226. Task.Factory.StartNew (delegate { try { throw new ApplicationException (); } finally { cde.Signal (); } }),
  227. Task.Factory.StartNew (delegate { try { throw new ApplicationException (); } finally { cde.Signal (); } }),
  228. Task.Factory.StartNew (delegate { mre.WaitOne (); })
  229. };
  230. Assert.IsTrue (cde.Wait (100), "#1");
  231. Assert.IsFalse (Task.WaitAll (tasks, 100), "#2");
  232. mre.Set ();
  233. try {
  234. Assert.IsTrue (Task.WaitAll (tasks, 1000), "#3");
  235. Assert.Fail ("#4");
  236. } catch (AggregateException e) {
  237. Assert.AreEqual (2, e.InnerExceptions.Count, "#5");
  238. }
  239. }
  240. [Test]
  241. public void WaitAll_Cancelled ()
  242. {
  243. var cancelation = new CancellationTokenSource ();
  244. var tasks = new Task[] {
  245. new Task (delegate { cancelation.Cancel (); }),
  246. new Task (delegate { }, cancelation.Token)
  247. };
  248. tasks[0].Start ();
  249. try {
  250. Task.WaitAll (tasks);
  251. Assert.Fail ("#1");
  252. } catch (AggregateException e) {
  253. var inner = (TaskCanceledException) e.InnerException;
  254. Assert.AreEqual (tasks[1], inner.Task, "#2");
  255. }
  256. Assert.IsTrue (tasks[0].IsCompleted, "#3");
  257. Assert.IsTrue (tasks[1].IsCanceled, "#4");
  258. }
  259. [Test]
  260. public void WaitAllExceptionThenCancelled ()
  261. {
  262. var cancelation = new CancellationTokenSource ();
  263. var tasks = new Task[] {
  264. new Task (delegate { cancelation.Cancel (); throw new ApplicationException (); }),
  265. new Task (delegate { }, cancelation.Token)
  266. };
  267. tasks[0].Start ();
  268. try {
  269. Task.WaitAll (tasks);
  270. Assert.Fail ("#1");
  271. } catch (AggregateException e) {
  272. Assert.IsInstanceOfType (typeof (ApplicationException), e.InnerException, "#2");
  273. var inner = (TaskCanceledException) e.InnerExceptions[1];
  274. Assert.AreEqual (tasks[1], inner.Task, "#3");
  275. }
  276. Assert.IsTrue (tasks[0].IsCompleted, "#4");
  277. Assert.IsTrue (tasks[1].IsCanceled, "#5");
  278. }
  279. [Test]
  280. public void WaitAll_StartedUnderWait ()
  281. {
  282. var task1 = new Task (delegate { });
  283. ThreadPool.QueueUserWorkItem (delegate {
  284. // Sleep little to let task to start and hit internal wait
  285. Thread.Sleep (20);
  286. task1.Start ();
  287. });
  288. Assert.IsTrue (Task.WaitAll (new [] { task1 }, 1000), "#1");
  289. }
  290. [Test]
  291. public void CancelBeforeStart ()
  292. {
  293. var src = new CancellationTokenSource ();
  294. Task t = new Task (delegate { }, src.Token);
  295. src.Cancel ();
  296. Assert.AreEqual (TaskStatus.Canceled, t.Status, "#1");
  297. try {
  298. t.Start ();
  299. Assert.Fail ("#2");
  300. } catch (InvalidOperationException) {
  301. }
  302. }
  303. [Test]
  304. public void Wait_CancelledTask ()
  305. {
  306. var src = new CancellationTokenSource ();
  307. Task t = new Task (delegate { }, src.Token);
  308. src.Cancel ();
  309. try {
  310. t.Wait (1000);
  311. Assert.Fail ("#1");
  312. } catch (AggregateException e) {
  313. var details = (TaskCanceledException) e.InnerException;
  314. Assert.AreEqual (t, details.Task, "#1e");
  315. }
  316. try {
  317. t.Wait ();
  318. Assert.Fail ("#2");
  319. } catch (AggregateException e) {
  320. var details = (TaskCanceledException) e.InnerException;
  321. Assert.AreEqual (t, details.Task, "#2e");
  322. Assert.IsNull (details.Task.Exception, "#2e2");
  323. }
  324. }
  325. [Test, ExpectedException (typeof (InvalidOperationException))]
  326. public void CreationWhileInitiallyCanceled ()
  327. {
  328. var token = new CancellationToken (true);
  329. var task = new Task (() => { }, token);
  330. Assert.AreEqual (TaskStatus.Canceled, task.Status);
  331. task.Start ();
  332. }
  333. [Test]
  334. public void ContinueWithInvalidArguments ()
  335. {
  336. var task = new Task (() => { });
  337. try {
  338. task.ContinueWith (null);
  339. Assert.Fail ("#1");
  340. } catch (ArgumentException) {
  341. }
  342. try {
  343. task.ContinueWith (delegate { }, null);
  344. Assert.Fail ("#2");
  345. } catch (ArgumentException) {
  346. }
  347. try {
  348. task.ContinueWith (delegate { }, TaskContinuationOptions.OnlyOnCanceled | TaskContinuationOptions.NotOnCanceled);
  349. Assert.Fail ("#3");
  350. } catch (ArgumentException) {
  351. }
  352. try {
  353. task.ContinueWith (delegate { }, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.NotOnRanToCompletion);
  354. Assert.Fail ("#4");
  355. } catch (ArgumentException) {
  356. }
  357. }
  358. [Test]
  359. public void ContinueWithOnAnyTestCase()
  360. {
  361. ParallelTestHelper.Repeat (delegate {
  362. bool result = false;
  363. Task t = Task.Factory.StartNew(delegate { });
  364. Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.None);
  365. Assert.IsTrue (t.Wait (2000), "First wait, (status, {0})", t.Status);
  366. Assert.IsTrue (cont.Wait(2000), "Cont wait, (result, {0}) (parent status, {2}) (status, {1})", result, cont.Status, t.Status);
  367. Assert.IsNull(cont.Exception, "#1");
  368. Assert.IsNotNull(cont, "#2");
  369. Assert.IsTrue(result, "#3");
  370. });
  371. }
  372. [Test]
  373. public void ContinueWithOnCompletedSuccessfullyTestCase()
  374. {
  375. ParallelTestHelper.Repeat (delegate {
  376. bool result = false;
  377. Task t = Task.Factory.StartNew(delegate { });
  378. Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnRanToCompletion);
  379. Assert.IsTrue (t.Wait(1000), "#4");
  380. Assert.IsTrue (cont.Wait(1000), "#5");
  381. Assert.IsNull(cont.Exception, "#1");
  382. Assert.IsNotNull(cont, "#2");
  383. Assert.IsTrue(result, "#3");
  384. });
  385. }
  386. [Test]
  387. public void ContinueWithOnAbortedTestCase()
  388. {
  389. bool result = false;
  390. bool taskResult = false;
  391. CancellationTokenSource src = new CancellationTokenSource ();
  392. Task t = new Task (delegate { taskResult = true; }, src.Token);
  393. Task cont = t.ContinueWith (delegate { result = true; },
  394. TaskContinuationOptions.OnlyOnCanceled | TaskContinuationOptions.ExecuteSynchronously);
  395. src.Cancel ();
  396. Assert.AreEqual (TaskStatus.Canceled, t.Status, "#1a");
  397. Assert.IsTrue (cont.IsCompleted, "#1b");
  398. Assert.IsTrue (result, "#1c");
  399. try {
  400. t.Start ();
  401. Assert.Fail ("#2");
  402. } catch (InvalidOperationException) {
  403. }
  404. Assert.IsTrue (cont.Wait (1000), "#3");
  405. Assert.IsFalse (taskResult, "#4");
  406. Assert.IsNull (cont.Exception, "#5");
  407. Assert.AreEqual (TaskStatus.RanToCompletion, cont.Status, "#6");
  408. }
  409. [Test]
  410. public void ContinueWithOnFailedTestCase()
  411. {
  412. ParallelTestHelper.Repeat (delegate {
  413. bool result = false;
  414. Task t = Task.Factory.StartNew(delegate { throw new Exception("foo"); });
  415. Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnFaulted);
  416. Assert.IsTrue (cont.Wait(1000), "#0");
  417. Assert.IsNotNull (t.Exception, "#1");
  418. Assert.IsNotNull (cont, "#2");
  419. Assert.IsTrue (result, "#3");
  420. });
  421. }
  422. [Test]
  423. public void ContinueWithWithStart ()
  424. {
  425. Task t = new Task<int> (() => 1);
  426. t = t.ContinueWith (l => { });
  427. try {
  428. t.Start ();
  429. Assert.Fail ();
  430. } catch (InvalidOperationException) {
  431. }
  432. }
  433. [Test]
  434. public void ContinueWithChildren ()
  435. {
  436. ParallelTestHelper.Repeat (delegate {
  437. bool result = false;
  438. var t = Task.Factory.StartNew (() => Task.Factory.StartNew (() => {}, TaskCreationOptions.AttachedToParent));
  439. var mre = new ManualResetEvent (false);
  440. t.ContinueWith (l => {
  441. result = true;
  442. mre.Set ();
  443. });
  444. Assert.IsTrue (mre.WaitOne (1000), "#1");
  445. Assert.IsTrue (result, "#2");
  446. }, 2);
  447. }
  448. [Test]
  449. public void MultipleTasks()
  450. {
  451. ParallelTestHelper.Repeat (delegate {
  452. bool r1 = false, r2 = false, r3 = false;
  453. Task t1 = Task.Factory.StartNew(delegate {
  454. r1 = true;
  455. });
  456. Task t2 = Task.Factory.StartNew(delegate {
  457. r2 = true;
  458. });
  459. Task t3 = Task.Factory.StartNew(delegate {
  460. r3 = true;
  461. });
  462. t1.Wait(2000);
  463. t2.Wait(2000);
  464. t3.Wait(2000);
  465. Assert.IsTrue(r1, "#1");
  466. Assert.IsTrue(r2, "#2");
  467. Assert.IsTrue(r3, "#3");
  468. }, 100);
  469. }
  470. [Test]
  471. public void WaitChildTestCase()
  472. {
  473. ParallelTestHelper.Repeat (delegate {
  474. bool r1 = false, r2 = false, r3 = false;
  475. var mre = new ManualResetEvent (false);
  476. Task t = Task.Factory.StartNew(delegate {
  477. Task.Factory.StartNew(delegate {
  478. r1 = true;
  479. mre.Set ();
  480. }, TaskCreationOptions.AttachedToParent);
  481. Task.Factory.StartNew(delegate {
  482. Assert.IsTrue (mre.WaitOne (1000), "#0");
  483. r2 = true;
  484. }, TaskCreationOptions.AttachedToParent);
  485. Task.Factory.StartNew(delegate {
  486. Assert.IsTrue (mre.WaitOne (1000), "#0");
  487. r3 = true;
  488. }, TaskCreationOptions.AttachedToParent);
  489. });
  490. Assert.IsTrue (t.Wait(2000), "#0");
  491. Assert.IsTrue(r2, "#1");
  492. Assert.IsTrue(r3, "#2");
  493. Assert.IsTrue(r1, "#3");
  494. Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#4");
  495. }, 10);
  496. }
  497. [Test]
  498. public void WaitChildWithContinuationAttachedTest ()
  499. {
  500. bool result = false;
  501. var task = new Task(() =>
  502. {
  503. Task.Factory.StartNew(() => {
  504. Thread.Sleep (200);
  505. }, TaskCreationOptions.AttachedToParent).ContinueWith(t => {
  506. Thread.Sleep (200);
  507. result = true;
  508. }, TaskContinuationOptions.AttachedToParent);
  509. });
  510. task.Start();
  511. task.Wait();
  512. Assert.IsTrue (result);
  513. }
  514. [Test]
  515. public void WaitChildWithContinuationNotAttachedTest ()
  516. {
  517. var task = new Task(() =>
  518. {
  519. Task.Factory.StartNew(() => {
  520. Thread.Sleep (200);
  521. }, TaskCreationOptions.AttachedToParent).ContinueWith(t => {
  522. Thread.Sleep (3000);
  523. });
  524. });
  525. task.Start();
  526. Assert.IsTrue (task.Wait(400));
  527. }
  528. [Test]
  529. public void WaitChildWithNesting ()
  530. {
  531. var result = false;
  532. var t = Task.Factory.StartNew (() => {
  533. Task.Factory.StartNew (() => {
  534. Task.Factory.StartNew (() => {
  535. Thread.Sleep (500);
  536. result = true;
  537. }, TaskCreationOptions.AttachedToParent);
  538. }, TaskCreationOptions.AttachedToParent);
  539. });
  540. t.Wait ();
  541. Assert.IsTrue (result);
  542. }
  543. [Test]
  544. public void DoubleWaitTest ()
  545. {
  546. ParallelTestHelper.Repeat (delegate {
  547. Console.WriteLine ("run");
  548. var evt = new ManualResetEventSlim ();
  549. var t = Task.Factory.StartNew (() => evt.Wait (2000));
  550. var cntd = new CountdownEvent (2);
  551. bool r1 = false, r2 = false;
  552. ThreadPool.QueueUserWorkItem (delegate { cntd.Signal (); r1 = t.Wait (1000); Console.WriteLine ("out 1 {0}", r1); cntd.Signal (); });
  553. ThreadPool.QueueUserWorkItem (delegate { cntd.Signal (); r2 = t.Wait (1000); Console.WriteLine ("out 2 {0}", r2); cntd.Signal (); });
  554. cntd.Wait (2000);
  555. cntd.Reset ();
  556. evt.Set ();
  557. cntd.Wait (2000);
  558. Assert.IsTrue (r1);
  559. Assert.IsTrue (r2);
  560. }, 5);
  561. }
  562. [Test]
  563. public void DoubleTimeoutedWaitTest ()
  564. {
  565. var evt = new ManualResetEventSlim ();
  566. var t = new Task (delegate { });
  567. var cntd = new CountdownEvent (2);
  568. bool r1 = false, r2 = false;
  569. ThreadPool.QueueUserWorkItem (delegate { r1 = !t.Wait (100); cntd.Signal (); });
  570. ThreadPool.QueueUserWorkItem (delegate { r2 = !t.Wait (100); cntd.Signal (); });
  571. cntd.Wait (2000);
  572. Assert.IsTrue (r1);
  573. Assert.IsTrue (r2);
  574. }
  575. [Test]
  576. public void ExecuteSynchronouslyTest ()
  577. {
  578. var val = 0;
  579. Task t = new Task (() => { Thread.Sleep (100); val = 1; });
  580. t.RunSynchronously ();
  581. Assert.AreEqual (1, val);
  582. }
  583. [Test]
  584. public void RunSynchronouslyArgumentChecks ()
  585. {
  586. Task t = new Task (() => { });
  587. try {
  588. t.RunSynchronously (null);
  589. Assert.Fail ("#1");
  590. } catch (ArgumentNullException) {
  591. }
  592. }
  593. [Test]
  594. public void RunSynchronouslyWithAttachedChildren ()
  595. {
  596. var result = false;
  597. var t = new Task (() => {
  598. Task.Factory.StartNew (() => { Thread.Sleep (500); result = true; }, TaskCreationOptions.AttachedToParent);
  599. });
  600. t.RunSynchronously ();
  601. Assert.IsTrue (result);
  602. }
  603. [Test]
  604. public void UnobservedExceptionOnFinalizerThreadTest ()
  605. {
  606. bool wasCalled = false;
  607. TaskScheduler.UnobservedTaskException += (o, args) => {
  608. wasCalled = true;
  609. args.SetObserved ();
  610. };
  611. var inner = new ApplicationException ();
  612. Task.Factory.StartNew (() => { throw inner; });
  613. Thread.Sleep (1000);
  614. GC.Collect ();
  615. Thread.Sleep (1000);
  616. GC.WaitForPendingFinalizers ();
  617. Assert.IsTrue (wasCalled);
  618. }
  619. [Test, ExpectedException (typeof (InvalidOperationException))]
  620. public void StartFinishedTaskTest ()
  621. {
  622. var t = Task.Factory.StartNew (delegate () { });
  623. t.Wait ();
  624. t.Start ();
  625. }
  626. [Test]
  627. public void Start_NullArgument ()
  628. {
  629. var t = Task.Factory.StartNew (delegate () { });
  630. try {
  631. t.Start (null);
  632. Assert.Fail ();
  633. } catch (ArgumentNullException) {
  634. }
  635. }
  636. [Test, ExpectedException (typeof (InvalidOperationException))]
  637. public void DisposeUnstartedTest ()
  638. {
  639. var t = new Task (() => { });
  640. t.Dispose ();
  641. }
  642. [Test]
  643. public void ThrowingUnrelatedCanceledExceptionTest ()
  644. {
  645. Task t = new Task (() => {
  646. throw new TaskCanceledException ();
  647. });
  648. t.RunSynchronously ();
  649. Assert.IsTrue (t.IsFaulted);
  650. Assert.IsFalse (t.IsCanceled);
  651. }
  652. #if NET_4_5
  653. [Test]
  654. public void Delay_Invalid ()
  655. {
  656. try {
  657. Task.Delay (-100);
  658. } catch (ArgumentOutOfRangeException) {
  659. }
  660. }
  661. [Test]
  662. public void Delay_Start ()
  663. {
  664. var t = Task.Delay (5000);
  665. try {
  666. t.Start ();
  667. } catch (InvalidOperationException) {
  668. }
  669. }
  670. [Test]
  671. public void Delay_Simple ()
  672. {
  673. var t = Task.Delay (100);
  674. Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#1");
  675. Assert.IsTrue (t.Wait (110), "#2");
  676. }
  677. [Test]
  678. public void Delay_Cancelled ()
  679. {
  680. var cancelation = new CancellationTokenSource ();
  681. var t = Task.Delay (1000, cancelation.Token);
  682. Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#1");
  683. cancelation.Cancel ();
  684. Assert.AreEqual (TaskStatus.Canceled, t.Status, "#2");
  685. }
  686. [Test]
  687. public void WaitAny_WithNull ()
  688. {
  689. var tasks = new [] {
  690. Task.FromResult (2),
  691. null
  692. };
  693. try {
  694. Task.WaitAny (tasks);
  695. Assert.Fail ();
  696. } catch (ArgumentException) {
  697. }
  698. }
  699. [Test]
  700. public void WhenAll_WithNull ()
  701. {
  702. var tasks = new[] {
  703. Task.FromResult (2),
  704. null
  705. };
  706. try {
  707. Task.WhenAll (tasks);
  708. Assert.Fail ("#1");
  709. } catch (ArgumentException) {
  710. }
  711. tasks = null;
  712. try {
  713. Task.WhenAll (tasks);
  714. Assert.Fail ("#2");
  715. } catch (ArgumentException) {
  716. }
  717. }
  718. [Test]
  719. public void WhenAll_Start ()
  720. {
  721. Task[] tasks = new[] {
  722. Task.FromResult (2),
  723. };
  724. var t = Task.WhenAll (tasks);
  725. Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#1");
  726. try {
  727. t.Start ();
  728. Assert.Fail ("#2");
  729. } catch (InvalidOperationException) {
  730. }
  731. tasks = new [] {
  732. new Task (delegate { }),
  733. };
  734. t = Task.WhenAll (tasks);
  735. Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#11");
  736. try {
  737. t.Start ();
  738. Assert.Fail ("#12");
  739. } catch (InvalidOperationException) {
  740. }
  741. }
  742. [Test]
  743. public void WhenAll_Cancelled ()
  744. {
  745. var cancelation = new CancellationTokenSource ();
  746. var tasks = new Task[] {
  747. new Task (delegate { }),
  748. new Task (delegate { }, cancelation.Token)
  749. };
  750. cancelation.Cancel ();
  751. var t = Task.WhenAll (tasks);
  752. Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#1");
  753. tasks[0].Start ();
  754. try {
  755. Assert.IsTrue (t.Wait (1000), "#2");
  756. Assert.Fail ("#2a");
  757. } catch (AggregateException e) {
  758. Assert.IsInstanceOfType (typeof (TaskCanceledException), e.InnerException, "#3");
  759. }
  760. }
  761. [Test]
  762. public void WhenAll_Faulted ()
  763. {
  764. var tcs = new TaskCompletionSource<object> ();
  765. tcs.SetException (new ApplicationException ());
  766. var tcs2 = new TaskCompletionSource<object> ();
  767. tcs2.SetException (new InvalidTimeZoneException ());
  768. var cancelation = new CancellationTokenSource ();
  769. var tasks = new Task[] {
  770. new Task (delegate { }),
  771. new Task (delegate { }, cancelation.Token),
  772. tcs.Task,
  773. tcs2.Task
  774. };
  775. cancelation.Cancel ();
  776. var t = Task.WhenAll (tasks);
  777. Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#1");
  778. tasks[0].Start ();
  779. try {
  780. Assert.IsTrue (t.Wait (1000), "#2");
  781. Assert.Fail ("#2a");
  782. } catch (AggregateException e) {
  783. Assert.IsInstanceOfType (typeof (ApplicationException), e.InnerException, "#3");
  784. Assert.IsInstanceOfType (typeof (InvalidTimeZoneException), e.InnerExceptions[1], "#4");
  785. }
  786. }
  787. [Test]
  788. public void WhenAll ()
  789. {
  790. var t1 = new Task (delegate { });
  791. var t2 = new Task (delegate { t1.Start (); });
  792. var tasks = new Task[] {
  793. t1,
  794. t2,
  795. };
  796. var t = Task.WhenAll (tasks);
  797. Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#1");
  798. t2.Start ();
  799. Assert.IsTrue (t.Wait (1000), "#2");
  800. }
  801. [Test]
  802. public void WhenAllResult_WithNull ()
  803. {
  804. var tasks = new[] {
  805. Task.FromResult (2),
  806. null
  807. };
  808. try {
  809. Task.WhenAll<int> (tasks);
  810. Assert.Fail ("#1");
  811. } catch (ArgumentException) {
  812. }
  813. tasks = null;
  814. try {
  815. Task.WhenAll<int> (tasks);
  816. Assert.Fail ("#2");
  817. } catch (ArgumentException) {
  818. }
  819. }
  820. [Test]
  821. public void WhenAllResult_Cancelled ()
  822. {
  823. var cancelation = new CancellationTokenSource ();
  824. var tasks = new [] {
  825. new Task<int> (delegate { return 9; }),
  826. new Task<int> (delegate { return 1; }, cancelation.Token)
  827. };
  828. cancelation.Cancel ();
  829. var t = Task.WhenAll (tasks);
  830. Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#1");
  831. tasks[0].Start ();
  832. try {
  833. Assert.IsTrue (t.Wait (1000), "#2");
  834. Assert.Fail ("#2a");
  835. } catch (AggregateException e) {
  836. Assert.IsInstanceOfType (typeof (TaskCanceledException), e.InnerException, "#3");
  837. }
  838. try {
  839. var r = t.Result;
  840. Assert.Fail ("#4");
  841. } catch (AggregateException) {
  842. }
  843. }
  844. [Test]
  845. public void WhenAllResult ()
  846. {
  847. var t1 = new Task<string> (delegate { return "a"; });
  848. var t2 = new Task<string> (delegate { t1.Start (); return "b"; });
  849. var tasks = new [] {
  850. t1,
  851. t2,
  852. };
  853. var t = Task.WhenAll<string> (tasks);
  854. Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#1");
  855. t2.Start ();
  856. Assert.IsTrue (t.Wait (1000), "#2");
  857. Assert.AreEqual (2, t.Result.Length, "#3");
  858. Assert.AreEqual ("a", t.Result[0], "#3a");
  859. Assert.AreEqual ("b", t.Result[1], "#3b");
  860. }
  861. [Test]
  862. public void WhenAllResult_Completed ()
  863. {
  864. var tasks = new[] {
  865. Task.FromResult (1),
  866. Task.FromResult (2)
  867. };
  868. var t = Task.WhenAll<int> (tasks);
  869. Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#1");
  870. Assert.AreEqual (2, t.Result.Length, "#2");
  871. Assert.AreEqual (1, t.Result[0], "#2a");
  872. Assert.AreEqual (2, t.Result[1], "#2b");
  873. }
  874. [Test]
  875. public void WhenAny_WithNull ()
  876. {
  877. var tasks = new Task[] {
  878. Task.FromResult (2),
  879. null
  880. };
  881. try {
  882. Task.WhenAny (tasks);
  883. Assert.Fail ("#1");
  884. } catch (ArgumentException) {
  885. }
  886. tasks = null;
  887. try {
  888. Task.WhenAny (tasks);
  889. Assert.Fail ("#2");
  890. } catch (ArgumentException) {
  891. }
  892. try {
  893. Task.WhenAny (new Task[0]);
  894. Assert.Fail ("#3");
  895. } catch (ArgumentException) {
  896. }
  897. }
  898. [Test]
  899. public void WhenAny_Start ()
  900. {
  901. Task[] tasks = new[] {
  902. Task.FromResult (2),
  903. };
  904. var t = Task.WhenAny (tasks);
  905. Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#1");
  906. try {
  907. t.Start ();
  908. Assert.Fail ("#2");
  909. } catch (InvalidOperationException) {
  910. }
  911. tasks = new[] {
  912. new Task (delegate { }),
  913. };
  914. t = Task.WhenAny (tasks);
  915. Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#11");
  916. try {
  917. t.Start ();
  918. Assert.Fail ("#12");
  919. } catch (InvalidOperationException) {
  920. }
  921. }
  922. [Test]
  923. public void WhenAny_Cancelled ()
  924. {
  925. var cancelation = new CancellationTokenSource ();
  926. var tasks = new Task[] {
  927. new Task (delegate { }),
  928. new Task (delegate { }, cancelation.Token)
  929. };
  930. cancelation.Cancel ();
  931. var t = Task.WhenAny (tasks);
  932. Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#1");
  933. tasks[0].Start ();
  934. Assert.IsTrue (t.Wait (1000), "#2");
  935. Assert.AreEqual (TaskStatus.Canceled, t.Result.Status, "#3");
  936. }
  937. [Test]
  938. public void WhenAny_Faulted ()
  939. {
  940. var tcs = new TaskCompletionSource<object> ();
  941. tcs.SetException (new ApplicationException ());
  942. var tcs2 = new TaskCompletionSource<object> ();
  943. tcs2.SetException (new InvalidTimeZoneException ());
  944. var cancelation = new CancellationTokenSource ();
  945. var tasks = new Task[] {
  946. new Task (delegate { }),
  947. tcs.Task,
  948. new Task (delegate { }, cancelation.Token),
  949. tcs2.Task
  950. };
  951. cancelation.Cancel ();
  952. var t = Task.WhenAny (tasks);
  953. Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#1");
  954. tasks[0].Start ();
  955. Assert.IsTrue (t.Wait (1000), "#2");
  956. Assert.IsNull (t.Exception, "#3");
  957. Assert.IsInstanceOfType (typeof (ApplicationException), t.Result.Exception.InnerException, "#4");
  958. }
  959. [Test]
  960. public void WhenAny ()
  961. {
  962. var t1 = new Task (delegate { });
  963. var t2 = new Task (delegate { t1.Start (); });
  964. var tasks = new Task[] {
  965. t1,
  966. t2,
  967. };
  968. var t = Task.WhenAny (tasks);
  969. Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#1");
  970. t2.Start ();
  971. Assert.IsTrue (t.Wait (1000), "#2");
  972. Assert.IsNotNull (t.Result, "#3");
  973. }
  974. [Test]
  975. public void WhenAnyResult_WithNull ()
  976. {
  977. var tasks = new [] {
  978. Task.FromResult (2),
  979. null
  980. };
  981. try {
  982. Task.WhenAny<int> (tasks);
  983. Assert.Fail ("#1");
  984. } catch (ArgumentException) {
  985. }
  986. tasks = null;
  987. try {
  988. Task.WhenAny<int> (tasks);
  989. Assert.Fail ("#2");
  990. } catch (ArgumentException) {
  991. }
  992. try {
  993. Task.WhenAny<short> (new Task<short>[0]);
  994. Assert.Fail ("#3");
  995. } catch (ArgumentException) {
  996. }
  997. }
  998. [Test]
  999. public void WhenAnyResult_Start ()
  1000. {
  1001. var tasks = new[] {
  1002. Task.FromResult (2),
  1003. };
  1004. var t = Task.WhenAny<int> (tasks);
  1005. Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#1");
  1006. try {
  1007. t.Start ();
  1008. Assert.Fail ("#2");
  1009. } catch (InvalidOperationException) {
  1010. }
  1011. tasks = new[] {
  1012. new Task<int> (delegate { return 55; }),
  1013. };
  1014. t = Task.WhenAny<int> (tasks);
  1015. Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#11");
  1016. try {
  1017. t.Start ();
  1018. Assert.Fail ("#12");
  1019. } catch (InvalidOperationException) {
  1020. }
  1021. }
  1022. [Test]
  1023. public void WhenAnyResult_Cancelled ()
  1024. {
  1025. var cancelation = new CancellationTokenSource ();
  1026. var tasks = new [] {
  1027. new Task<double> (delegate { return 1.1; }),
  1028. new Task<double> (delegate { return -4.4; }, cancelation.Token)
  1029. };
  1030. cancelation.Cancel ();
  1031. var t = Task.WhenAny<double> (tasks);
  1032. Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#1");
  1033. tasks[0].Start ();
  1034. Assert.IsTrue (t.Wait (1000), "#2");
  1035. Assert.AreEqual (TaskStatus.Canceled, t.Result.Status, "#3");
  1036. }
  1037. [Test]
  1038. public void WhenAnyResult_Faulted ()
  1039. {
  1040. var tcs = new TaskCompletionSource<object> ();
  1041. tcs.SetException (new ApplicationException ());
  1042. var tcs2 = new TaskCompletionSource<object> ();
  1043. tcs2.SetException (new InvalidTimeZoneException ());
  1044. var cancelation = new CancellationTokenSource ();
  1045. var tasks = new Task<object>[] {
  1046. new Task<object> (delegate { return null; }),
  1047. tcs.Task,
  1048. new Task<object> (delegate { return ""; }, cancelation.Token),
  1049. tcs2.Task
  1050. };
  1051. cancelation.Cancel ();
  1052. var t = Task.WhenAny<object> (tasks);
  1053. Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#1");
  1054. tasks[0].Start ();
  1055. Assert.IsTrue (t.Wait (1000), "#2");
  1056. Assert.IsNull (t.Exception, "#3");
  1057. Assert.IsInstanceOfType (typeof (ApplicationException), t.Result.Exception.InnerException, "#4");
  1058. }
  1059. [Test]
  1060. public void WhenAnyResult ()
  1061. {
  1062. var t1 = new Task<byte> (delegate { return 3; });
  1063. var t2 = new Task<byte> (delegate { t1.Start (); return 2; });
  1064. var tasks = new [] {
  1065. t1,
  1066. t2,
  1067. };
  1068. var t = Task.WhenAny<byte> (tasks);
  1069. Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#1");
  1070. t2.Start ();
  1071. Assert.IsTrue (t.Wait (1000), "#2");
  1072. Assert.IsTrue (t.Result.Result > 1, "#3");
  1073. }
  1074. [Test]
  1075. public void ContinueWith_StateValue ()
  1076. {
  1077. var t = Task.Factory.StartNew (l => {
  1078. Assert.AreEqual (1, l, "a-1");
  1079. }, 1);
  1080. var c = t.ContinueWith ((a, b) => {
  1081. Assert.AreEqual (t, a, "c-1");
  1082. Assert.AreEqual (2, b, "c-2");
  1083. }, 2);
  1084. var d = t.ContinueWith ((a, b) => {
  1085. Assert.AreEqual (t, a, "d-1");
  1086. Assert.AreEqual (3, b, "d-2");
  1087. return 77;
  1088. }, 3);
  1089. Assert.IsTrue (d.Wait (1000), "#1");
  1090. Assert.AreEqual (1, t.AsyncState, "#2");
  1091. Assert.AreEqual (2, c.AsyncState, "#3");
  1092. Assert.AreEqual (3, d.AsyncState, "#4");
  1093. }
  1094. [Test]
  1095. public void ContinueWith_StateValueGeneric ()
  1096. {
  1097. var t = Task<int>.Factory.StartNew (l => {
  1098. Assert.AreEqual (1, l, "a-1");
  1099. return 80;
  1100. }, 1);
  1101. var c = t.ContinueWith ((a, b) => {
  1102. Assert.AreEqual (t, a, "c-1");
  1103. Assert.AreEqual (2, b, "c-2");
  1104. return "c";
  1105. }, 2);
  1106. var d = t.ContinueWith ((a, b) => {
  1107. Assert.AreEqual (t, a, "d-1");
  1108. Assert.AreEqual (3, b, "d-2");
  1109. return 'd';
  1110. }, 3);
  1111. Assert.IsTrue (d.Wait (1000), "#1");
  1112. Assert.AreEqual (1, t.AsyncState, "#2");
  1113. Assert.AreEqual (80, t.Result, "#2r");
  1114. Assert.AreEqual (2, c.AsyncState, "#3");
  1115. Assert.AreEqual ("c", c.Result, "#3r");
  1116. Assert.AreEqual (3, d.AsyncState, "#4");
  1117. Assert.AreEqual ('d', d.Result, "#3r");
  1118. }
  1119. [Test]
  1120. public void FromResult ()
  1121. {
  1122. var t = Task.FromResult<object> (null);
  1123. Assert.IsTrue (t.IsCompleted, "#1");
  1124. Assert.AreEqual (null, t.Result, "#2");
  1125. t.Dispose ();
  1126. t.Dispose ();
  1127. }
  1128. [Test]
  1129. public void Run_ArgumentCheck ()
  1130. {
  1131. try {
  1132. Task.Run (null as Action);
  1133. Assert.Fail ("#1");
  1134. } catch (ArgumentNullException) {
  1135. }
  1136. }
  1137. [Test]
  1138. public void Run ()
  1139. {
  1140. var t = Task.Run (delegate { });
  1141. Assert.AreEqual (TaskCreationOptions.DenyChildAttach, t.CreationOptions, "#1");
  1142. t.Wait ();
  1143. }
  1144. [Test]
  1145. public void Run_Cancel ()
  1146. {
  1147. var t = Task.Run (() => 1, new CancellationToken (true));
  1148. try {
  1149. var r = t.Result;
  1150. Assert.Fail ("#1");
  1151. } catch (AggregateException) {
  1152. }
  1153. Assert.IsTrue (t.IsCanceled, "#2");
  1154. }
  1155. [Test]
  1156. public void Run_ExistingTask ()
  1157. {
  1158. var t = new Task<int> (() => 5);
  1159. var t2 = Task.Run (() => { t.Start (); return t; });
  1160. Assert.IsTrue (t2.Wait (1000), "#1");
  1161. Assert.AreEqual (5, t2.Result, "#2");
  1162. }
  1163. #endif
  1164. }
  1165. }
  1166. #endif