TaskFactoryTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. //
  2. // TaskFactoryTest.cs
  3. //
  4. // Authors:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. // Marek Safar <[email protected]>
  7. //
  8. // Copyright (c) 2010 Jérémie "Garuma" Laval
  9. // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in
  19. // all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. // THE SOFTWARE.
  28. //
  29. //
  30. #if NET_4_0
  31. using System;
  32. using System.Threading;
  33. using System.Threading.Tasks;
  34. using System.Collections.Generic;
  35. using NUnit.Framework;
  36. #if !MOBILE
  37. using NUnit.Framework.SyntaxHelpers;
  38. #endif
  39. namespace MonoTests.System.Threading.Tasks
  40. {
  41. [TestFixture]
  42. public class TaskFactoryTests
  43. {
  44. class CompletedAsyncResult : IAsyncResult
  45. {
  46. public object AsyncState
  47. {
  48. get { throw new NotImplementedException (); }
  49. }
  50. public WaitHandle AsyncWaitHandle
  51. {
  52. get { throw new NotImplementedException (); }
  53. }
  54. public bool CompletedSynchronously
  55. {
  56. get { throw new NotImplementedException (); }
  57. }
  58. public bool IsCompleted
  59. {
  60. get { return true; }
  61. }
  62. }
  63. class TestAsyncResult : IAsyncResult
  64. {
  65. WaitHandle wh = new ManualResetEvent (true);
  66. public object AsyncState
  67. {
  68. get { throw new NotImplementedException (); }
  69. }
  70. public WaitHandle AsyncWaitHandle
  71. {
  72. get
  73. {
  74. return wh;
  75. }
  76. }
  77. public bool CompletedSynchronously
  78. {
  79. get { throw new NotImplementedException (); }
  80. }
  81. public bool IsCompleted
  82. {
  83. get { return false; }
  84. }
  85. }
  86. class TestScheduler : TaskScheduler
  87. {
  88. public bool ExecutedInline { get; set; }
  89. protected override void QueueTask (Task task)
  90. {
  91. throw new NotImplementedException ();
  92. }
  93. protected override bool TryDequeue (Task task)
  94. {
  95. throw new NotImplementedException ();
  96. }
  97. protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
  98. {
  99. if (taskWasPreviouslyQueued)
  100. throw new ArgumentException ("taskWasPreviouslyQueued");
  101. if (task.Status != TaskStatus.WaitingToRun)
  102. throw new ArgumentException ("task.Status");
  103. ExecutedInline = true;
  104. return TryExecuteTask (task);
  105. }
  106. protected override IEnumerable<Task> GetScheduledTasks ()
  107. {
  108. throw new NotImplementedException ();
  109. }
  110. }
  111. TaskFactory factory;
  112. [SetUp]
  113. public void Setup ()
  114. {
  115. this.factory = Task.Factory;
  116. }
  117. [Test]
  118. public void StartNewTest ()
  119. {
  120. bool result = false;
  121. factory.StartNew (() => result = true).Wait ();
  122. Assert.IsTrue (result);
  123. }
  124. [Test]
  125. public void NoDefaultScheduler ()
  126. {
  127. Assert.IsNull (factory.Scheduler, "#1");
  128. }
  129. [Test]
  130. public void ContinueWhenAll_Simple ()
  131. {
  132. var mre = new ManualResetEventSlim (false);
  133. Task[] tasks = new Task[3];
  134. tasks[0] = new Task (() => { Thread.Sleep (0); Assert.IsTrue (mre.Wait (5000)); });
  135. tasks[1] = new Task (() => { Assert.IsTrue (mre.Wait (5000)); });
  136. tasks[2] = new Task (() => { Assert.IsTrue (mre.Wait (5000)); });
  137. bool ran = false;
  138. Task cont = factory.ContinueWhenAll (tasks, ts => {
  139. Assert.AreEqual (tasks, ts, "#0");
  140. ran = true;
  141. });
  142. foreach (Task t in tasks)
  143. t.Start ();
  144. mre.Set ();
  145. Assert.IsTrue (cont.Wait (3000), "#1");
  146. Assert.IsTrue (ran, "#2");
  147. }
  148. [Test]
  149. public void ContinueWhenAll_WithMixedCompletionState ()
  150. {
  151. var mre = new ManualResetEventSlim ();
  152. var task = Task.Factory.StartNew (() => mre.Wait (1000));
  153. var contFailed = task.ContinueWith (t => {}, TaskContinuationOptions.OnlyOnFaulted);
  154. var contCanceled = task.ContinueWith (t => {}, TaskContinuationOptions.OnlyOnCanceled);
  155. var contSuccess = task.ContinueWith (t => {}, TaskContinuationOptions.OnlyOnRanToCompletion);
  156. bool ran = false;
  157. var cont = Task.Factory.ContinueWhenAll (new Task[] { contFailed, contCanceled, contSuccess }, _ => ran = true);
  158. mre.Set ();
  159. cont.Wait (3000);
  160. Assert.IsTrue (ran);
  161. Assert.AreEqual (TaskStatus.RanToCompletion, cont.Status);
  162. }
  163. [Test]
  164. public void ContinueWhenAll_InvalidArguments ()
  165. {
  166. try {
  167. factory.ContinueWhenAll (null, delegate { });
  168. Assert.Fail ("#1");
  169. } catch (ArgumentNullException) {
  170. }
  171. try {
  172. factory.ContinueWhenAll (new Task[0], delegate { });
  173. Assert.Fail ("#2");
  174. } catch (ArgumentException) {
  175. }
  176. try {
  177. factory.ContinueWhenAll (new Task[] { null }, delegate { });
  178. Assert.Fail ("#3");
  179. } catch (ArgumentException) {
  180. }
  181. var tasks = new Task [] {
  182. factory.StartNew (delegate {})
  183. };
  184. try {
  185. factory.ContinueWhenAll (tasks, null);
  186. Assert.Fail ("#4");
  187. } catch (ArgumentNullException) {
  188. }
  189. try {
  190. factory.ContinueWhenAll (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.None, null);
  191. Assert.Fail ("#5");
  192. } catch (ArgumentNullException) {
  193. }
  194. try {
  195. factory.ContinueWhenAll (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.OnlyOnCanceled, null);
  196. Assert.Fail ("#6");
  197. } catch (ArgumentNullException) {
  198. } catch (ArgumentOutOfRangeException) {
  199. }
  200. }
  201. [Test]
  202. public void ContinueWhenAll_WithExceptions ()
  203. {
  204. var t1 = Task.Factory.StartNew (() => { throw new ApplicationException ("Foo"); });
  205. var t2 = Task.Factory.StartNew (() => { throw new ApplicationException ("Bar"); });
  206. var cont = Task.Factory.ContinueWhenAll (new[] { t1, t2 }, delegate {});
  207. cont.Wait (200);
  208. Assert.IsTrue (t1.IsFaulted);
  209. Assert.IsTrue (t2.IsFaulted);
  210. Assert.AreEqual (TaskStatus.RanToCompletion, cont.Status);
  211. }
  212. [Test]
  213. public void ContinueWhenAny_Simple ()
  214. {
  215. var t1 = new ManualResetEvent (false);
  216. var t2 = new ManualResetEvent (false);
  217. var tasks = new Task[2] {
  218. Task.Factory.StartNew (() => { t1.WaitOne (5000); }),
  219. Task.Factory.StartNew (() => { t2.WaitOne (5000); })
  220. };
  221. bool ran = false;
  222. var ct = new CancellationToken ();
  223. Task cont = factory.ContinueWhenAny (tasks, t => {
  224. Assert.AreEqual (tasks[0], t, "#1");
  225. ran = true;
  226. }, ct);
  227. Assert.AreEqual (TaskStatus.WaitingForActivation, cont.Status, "#2");
  228. t1.Set ();
  229. Assert.IsTrue (cont.Wait (3000), "#10");
  230. Assert.IsTrue (ran, "#11");
  231. t2.Set ();
  232. }
  233. [Test]
  234. public void ContinueWhenAny_WithResult ()
  235. {
  236. var tcs = new TaskCompletionSource<int>();
  237. tcs.SetResult(1);
  238. Task[] tasks = new[] { tcs.Task };
  239. var res = Task.Factory.ContinueWhenAny (tasks, l => 4);
  240. Assert.AreEqual (4, res.Result);
  241. }
  242. [Test]
  243. public void ContinueWhenAny_InvalidArguments ()
  244. {
  245. try {
  246. factory.ContinueWhenAny (null, delegate { });
  247. Assert.Fail ("#1");
  248. } catch (ArgumentNullException) {
  249. }
  250. try {
  251. factory.ContinueWhenAny (new Task[0], delegate { });
  252. Assert.Fail ("#2");
  253. } catch (ArgumentException) {
  254. }
  255. try {
  256. factory.ContinueWhenAny (new Task[] { null }, delegate { });
  257. Assert.Fail ("#3");
  258. } catch (ArgumentException) {
  259. }
  260. var tasks = new Task [] {
  261. factory.StartNew (delegate {})
  262. };
  263. try {
  264. factory.ContinueWhenAny (tasks, null);
  265. Assert.Fail ("#4");
  266. } catch (ArgumentNullException) {
  267. }
  268. try {
  269. factory.ContinueWhenAny (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.None, null);
  270. Assert.Fail ("#5");
  271. } catch (ArgumentNullException) {
  272. }
  273. try {
  274. factory.ContinueWhenAny (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.OnlyOnCanceled, null);
  275. Assert.Fail ("#6");
  276. } catch (ArgumentNullException) {
  277. } catch (ArgumentOutOfRangeException) {
  278. }
  279. }
  280. [Test]
  281. public void FromAsyncBeginInvoke_WithResult ()
  282. {
  283. bool result = false;
  284. Func<int, int> func = (i) => {
  285. Assert.IsTrue (Thread.CurrentThread.IsThreadPoolThread);
  286. result = true; return i + 3;
  287. };
  288. var task = factory.FromAsync<int, int> (func.BeginInvoke, func.EndInvoke, 1, "state", TaskCreationOptions.AttachedToParent);
  289. Assert.IsTrue (task.Wait (5000), "#1");
  290. Assert.IsTrue (result, "#2");
  291. Assert.AreEqual (4, task.Result, "#3");
  292. Assert.AreEqual ("state", (string) task.AsyncState, "#4");
  293. Assert.AreEqual (TaskCreationOptions.AttachedToParent, task.CreationOptions, "#5");
  294. }
  295. [Test]
  296. public void FromAsyncBeginMethod_DirectResult ()
  297. {
  298. bool result = false;
  299. bool continuationTest = false;
  300. Func<int, int> func = (i) => { result = true; return i + 3; };
  301. Task<int> task = factory.FromAsync<int> (func.BeginInvoke (1, delegate { }, null), func.EndInvoke);
  302. var cont = task.ContinueWith (_ => continuationTest = true, TaskContinuationOptions.ExecuteSynchronously);
  303. task.Wait ();
  304. cont.Wait ();
  305. Assert.IsTrue (result);
  306. Assert.IsTrue (continuationTest);
  307. Assert.AreEqual (4, task.Result);
  308. }
  309. [Test]
  310. public void FromAsyncBeginMethod_Exception ()
  311. {
  312. bool result = false;
  313. bool continuationTest = false;
  314. Func<int, int> func = (i) => { result = true; throw new ApplicationException ("bleh"); };
  315. Task<int> task = factory.FromAsync<int, int> (func.BeginInvoke, func.EndInvoke, 1, null);
  316. var cont = task.ContinueWith (_ => continuationTest = true, TaskContinuationOptions.ExecuteSynchronously);
  317. try {
  318. task.Wait (2000);
  319. } catch { }
  320. Assert.IsTrue (cont.Wait (2000), "#1");
  321. Assert.IsTrue (result);
  322. Assert.IsTrue (continuationTest);
  323. Assert.IsNotNull (task.Exception);
  324. var agg = task.Exception;
  325. Assert.AreEqual (1, agg.InnerExceptions.Count);
  326. Assert.That (agg.InnerExceptions[0], Is.TypeOf (typeof (ApplicationException)));
  327. Assert.AreEqual (TaskStatus.Faulted, task.Status);
  328. try {
  329. var a = task.Result;
  330. Assert.Fail ();
  331. } catch (AggregateException) {
  332. }
  333. }
  334. [Test]
  335. public void FromAsync_ArgumentsCheck ()
  336. {
  337. var result = new CompletedAsyncResult ();
  338. try {
  339. factory.FromAsync (null, l => { });
  340. Assert.Fail ("#1");
  341. } catch (ArgumentNullException) {
  342. }
  343. try {
  344. factory.FromAsync (result, null);
  345. Assert.Fail ("#2");
  346. } catch (ArgumentNullException) {
  347. }
  348. try {
  349. factory.FromAsync (result, l => { }, TaskCreationOptions.LongRunning);
  350. Assert.Fail ("#3");
  351. } catch (ArgumentOutOfRangeException) {
  352. }
  353. try {
  354. factory.FromAsync (result, l => { }, TaskCreationOptions.PreferFairness);
  355. Assert.Fail ("#4");
  356. } catch (ArgumentOutOfRangeException) {
  357. }
  358. try {
  359. factory.FromAsync (result, l => { }, TaskCreationOptions.None, null);
  360. Assert.Fail ("#5");
  361. } catch (ArgumentNullException) {
  362. }
  363. try {
  364. factory.FromAsync (null, l => { }, null, TaskCreationOptions.None);
  365. Assert.Fail ("#6");
  366. } catch (ArgumentNullException) {
  367. }
  368. try {
  369. factory.FromAsync ((a, b) => null, l => { }, null, TaskCreationOptions.LongRunning);
  370. Assert.Fail ("#7");
  371. } catch (ArgumentOutOfRangeException) {
  372. }
  373. }
  374. [Test]
  375. public void FromAsync_Completed ()
  376. {
  377. var completed = new CompletedAsyncResult ();
  378. bool? valid = null;
  379. Action<IAsyncResult> end = l => {
  380. Assert.IsFalse (Thread.CurrentThread.IsThreadPoolThread, "#2");
  381. valid = l == completed;
  382. };
  383. Task task = factory.FromAsync (completed, end);
  384. Assert.IsTrue (valid == true, "#1");
  385. }
  386. [Test]
  387. public void FromAsync_CompletedWithException ()
  388. {
  389. var completed = new CompletedAsyncResult ();
  390. Action<IAsyncResult> end = l => {
  391. throw new ApplicationException ();
  392. };
  393. Task task = factory.FromAsync (completed, end);
  394. Assert.AreEqual (TaskStatus.Faulted, task.Status, "#1");
  395. }
  396. [Test]
  397. public void FromAsync_CompletedCanceled ()
  398. {
  399. var completed = new CompletedAsyncResult ();
  400. Action<IAsyncResult> end = l => {
  401. throw new OperationCanceledException ();
  402. };
  403. Task task = factory.FromAsync (completed, end);
  404. Assert.AreEqual (TaskStatus.Canceled, task.Status, "#1");
  405. Assert.IsNull (task.Exception, "#2");
  406. }
  407. [Test]
  408. public void FromAsync_SimpleAsyncResult ()
  409. {
  410. var result = new TestAsyncResult ();
  411. bool called = false;
  412. var task = factory.FromAsync (result, l => {
  413. called = true;
  414. });
  415. Assert.IsTrue (task.Wait (1000), "#1");
  416. Assert.IsTrue (called, "#2");
  417. }
  418. [Test]
  419. public void FromAsync_ResultException ()
  420. {
  421. var result = new TestAsyncResult ();
  422. var task = factory.FromAsync (result, l => {
  423. throw new ApplicationException ();
  424. });
  425. try {
  426. Assert.IsFalse (task.Wait (1000), "#1");
  427. } catch (AggregateException) {
  428. }
  429. Assert.AreEqual (TaskStatus.Faulted, task.Status, "#2");
  430. }
  431. [Test]
  432. public void FromAsync_ReturnInt ()
  433. {
  434. var result = new TestAsyncResult ();
  435. bool called = false;
  436. var task = factory.FromAsync<int> (result, l => {
  437. called = true;
  438. return 4;
  439. });
  440. Assert.IsTrue (task.Wait (1000), "#1");
  441. Assert.IsTrue (called, "#2");
  442. Assert.AreEqual (4, task.Result, "#3");
  443. }
  444. [Test]
  445. public void FromAsync_Scheduler_Explicit ()
  446. {
  447. var result = new TestAsyncResult ();
  448. bool called = false;
  449. var scheduler = new TestScheduler ();
  450. var task = factory.FromAsync (result, l => {
  451. called = true;
  452. }, TaskCreationOptions.None, scheduler);
  453. Assert.IsTrue (task.Wait (5000), "#1");
  454. Assert.IsTrue (called, "#2");
  455. Assert.IsTrue (scheduler.ExecutedInline, "#3");
  456. }
  457. [Test]
  458. public void FromAsync_Scheduler_Implicit ()
  459. {
  460. var result = new TestAsyncResult ();
  461. bool called = false;
  462. var scheduler = new TestScheduler ();
  463. factory = new TaskFactory (scheduler);
  464. Task task = factory.FromAsync (result, l => {
  465. Assert.IsTrue (Thread.CurrentThread.IsThreadPoolThread, "#6");
  466. called = true;
  467. }, TaskCreationOptions.AttachedToParent);
  468. Assert.AreEqual (TaskCreationOptions.AttachedToParent, task.CreationOptions, "#1");
  469. Assert.IsNull (task.AsyncState, "#2");
  470. Assert.IsTrue (task.Wait (5000), "#3");
  471. Assert.IsTrue (called, "#4");
  472. Assert.IsTrue (scheduler.ExecutedInline, "#5");
  473. }
  474. [Test]
  475. public void FromAsync_BeginCallback ()
  476. {
  477. bool called = false;
  478. bool called2 = false;
  479. var task = factory.FromAsync (
  480. (a, b, c) => {
  481. if (a != "h")
  482. Assert.Fail ("#10");
  483. if ((TaskCreationOptions) c != TaskCreationOptions.AttachedToParent)
  484. Assert.Fail ("#11");
  485. Assert.IsFalse (Thread.CurrentThread.IsThreadPoolThread, "#12");
  486. called2 = true;
  487. b.Invoke (null);
  488. return null;
  489. },
  490. l => {
  491. called = true;
  492. },
  493. "h", TaskCreationOptions.AttachedToParent);
  494. Assert.AreEqual (TaskCreationOptions.None, task.CreationOptions, "#1");
  495. Assert.AreEqual (TaskCreationOptions.AttachedToParent, (TaskCreationOptions) task.AsyncState, "#2");
  496. Assert.IsTrue (task.Wait (5000), "#3");
  497. Assert.IsTrue (called, "#4");
  498. Assert.IsTrue (called2, "#5");
  499. }
  500. [Test]
  501. public void StartNewCancelled ()
  502. {
  503. var ct = new CancellationToken (true);
  504. var task = factory.StartNew (() => Assert.Fail ("Should never be called"), ct);
  505. try {
  506. task.Start ();
  507. Assert.Fail ("#1");
  508. } catch (InvalidOperationException) {
  509. }
  510. try {
  511. task.Wait ();
  512. Assert.Fail ("#2");
  513. } catch (AggregateException e) {
  514. Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");
  515. }
  516. Assert.IsTrue (task.IsCanceled, "#4");
  517. }
  518. }
  519. }
  520. #endif