TaskFactoryTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 || MOBILE
  31. using System;
  32. using System.Threading;
  33. using System.Threading.Tasks;
  34. using System.Collections.Generic;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Threading.Tasks
  37. {
  38. [TestFixture]
  39. public class TaskFactoryTests
  40. {
  41. class CompletedAsyncResult : IAsyncResult
  42. {
  43. public object AsyncState
  44. {
  45. get { throw new NotImplementedException (); }
  46. }
  47. public WaitHandle AsyncWaitHandle
  48. {
  49. get { throw new NotImplementedException (); }
  50. }
  51. public bool CompletedSynchronously
  52. {
  53. get { throw new NotImplementedException (); }
  54. }
  55. public bool IsCompleted
  56. {
  57. get { return true; }
  58. }
  59. }
  60. class TestAsyncResult : IAsyncResult
  61. {
  62. WaitHandle wh = new ManualResetEvent (true);
  63. public object AsyncState
  64. {
  65. get { throw new NotImplementedException (); }
  66. }
  67. public WaitHandle AsyncWaitHandle
  68. {
  69. get
  70. {
  71. return wh;
  72. }
  73. }
  74. public bool CompletedSynchronously
  75. {
  76. get { throw new NotImplementedException (); }
  77. }
  78. public bool IsCompleted
  79. {
  80. get { return false; }
  81. }
  82. }
  83. class TestScheduler : TaskScheduler
  84. {
  85. public bool ExecutedInline { get; set; }
  86. protected override void QueueTask (Task task)
  87. {
  88. throw new NotImplementedException ();
  89. }
  90. protected override bool TryDequeue (Task task)
  91. {
  92. throw new NotImplementedException ();
  93. }
  94. protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
  95. {
  96. if (taskWasPreviouslyQueued)
  97. throw new ArgumentException ("taskWasPreviouslyQueued");
  98. if (task.Status != TaskStatus.WaitingToRun)
  99. throw new ArgumentException ("task.Status");
  100. ExecutedInline = true;
  101. return TryExecuteTask (task);
  102. }
  103. protected override IEnumerable<Task> GetScheduledTasks ()
  104. {
  105. throw new NotImplementedException ();
  106. }
  107. }
  108. TaskFactory factory;
  109. [SetUp]
  110. public void Setup ()
  111. {
  112. this.factory = Task.Factory;
  113. }
  114. [Test]
  115. public void StartNewTest ()
  116. {
  117. bool result = false;
  118. factory.StartNew (() => result = true).Wait ();
  119. Assert.IsTrue (result);
  120. }
  121. [Test]
  122. public void NoDefaultScheduler ()
  123. {
  124. Assert.IsNull (factory.Scheduler, "#1");
  125. }
  126. [Test]
  127. public void ContinueWhenAll_Simple ()
  128. {
  129. var mre = new ManualResetEventSlim (false);
  130. Task[] tasks = new Task[3];
  131. tasks[0] = new Task (() => { Thread.Sleep (0); Assert.IsTrue (mre.Wait (3000)); });
  132. tasks[1] = new Task (() => { Assert.IsTrue (mre.Wait (3000)); });
  133. tasks[2] = new Task (() => { Assert.IsTrue (mre.Wait (3000)); });
  134. bool ran = false;
  135. Task cont = factory.ContinueWhenAll (tasks, ts => {
  136. Assert.AreEqual (tasks, ts, "#0");
  137. ran = true;
  138. });
  139. foreach (Task t in tasks)
  140. t.Start ();
  141. mre.Set ();
  142. Assert.IsTrue (cont.Wait (1000), "#1");
  143. Assert.IsTrue (ran, "#2");
  144. }
  145. [Test]
  146. public void ContinueWhenAll_WithMixedCompletionState ()
  147. {
  148. var mre = new ManualResetEventSlim ();
  149. var task = Task.Factory.StartNew (() => mre.Wait (200));
  150. var contFailed = task.ContinueWith (t => {}, TaskContinuationOptions.OnlyOnFaulted);
  151. var contCanceled = task.ContinueWith (t => {}, TaskContinuationOptions.OnlyOnCanceled);
  152. var contSuccess = task.ContinueWith (t => {}, TaskContinuationOptions.OnlyOnRanToCompletion);
  153. bool ran = false;
  154. var cont = Task.Factory.ContinueWhenAll (new Task[] { contFailed, contCanceled, contSuccess }, _ => ran = true);
  155. mre.Set ();
  156. cont.Wait (200);
  157. Assert.IsTrue (ran);
  158. Assert.AreEqual (TaskStatus.RanToCompletion, cont.Status);
  159. }
  160. [Test]
  161. public void ContinueWhenAll_InvalidArguments ()
  162. {
  163. try {
  164. factory.ContinueWhenAll (null, delegate { });
  165. Assert.Fail ("#1");
  166. } catch (ArgumentNullException) {
  167. }
  168. try {
  169. factory.ContinueWhenAll (new Task[0], delegate { });
  170. Assert.Fail ("#2");
  171. } catch (ArgumentException) {
  172. }
  173. try {
  174. factory.ContinueWhenAll (new Task[] { null }, delegate { });
  175. Assert.Fail ("#3");
  176. } catch (ArgumentException) {
  177. }
  178. var tasks = new Task [] {
  179. factory.StartNew (delegate {})
  180. };
  181. try {
  182. factory.ContinueWhenAll (tasks, null);
  183. Assert.Fail ("#4");
  184. } catch (ArgumentException) {
  185. }
  186. try {
  187. factory.ContinueWhenAll (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.None, null);
  188. Assert.Fail ("#5");
  189. } catch (ArgumentException) {
  190. }
  191. try {
  192. factory.ContinueWhenAll (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.OnlyOnCanceled, null);
  193. Assert.Fail ("#6");
  194. } catch (ArgumentException) {
  195. }
  196. }
  197. [Test]
  198. public void ContinueWhenAll_WithExceptions ()
  199. {
  200. var t1 = Task.Factory.StartNew (() => { throw new ApplicationException ("Foo"); });
  201. var t2 = Task.Factory.StartNew (() => { throw new ApplicationException ("Bar"); });
  202. var cont = Task.Factory.ContinueWhenAll (new[] { t1, t2 }, delegate {});
  203. cont.Wait (200);
  204. Assert.IsTrue (t1.IsFaulted);
  205. Assert.IsTrue (t2.IsFaulted);
  206. Assert.AreEqual (TaskStatus.RanToCompletion, cont.Status);
  207. }
  208. [Test]
  209. public void ContinueWhenAny_Simple ()
  210. {
  211. var t1 = new ManualResetEvent (false);
  212. var t2 = new ManualResetEvent (false);
  213. var tasks = new Task[2] {
  214. Task.Factory.StartNew (() => { t1.WaitOne (3000); }),
  215. Task.Factory.StartNew (() => { t2.WaitOne (3000); })
  216. };
  217. bool ran = false;
  218. var ct = new CancellationToken ();
  219. Task cont = factory.ContinueWhenAny (tasks, t => {
  220. Assert.AreEqual (tasks[0], t, "#1");
  221. ran = true;
  222. }, ct);
  223. Assert.AreEqual (TaskStatus.WaitingForActivation, cont.Status, "#2");
  224. t1.Set ();
  225. Assert.IsTrue (cont.Wait (2000), "#10");
  226. Assert.IsTrue (ran, "#11");
  227. t2.Set ();
  228. }
  229. [Test]
  230. public void ContinueWhenAny_InvalidArguments ()
  231. {
  232. try {
  233. factory.ContinueWhenAny (null, delegate { });
  234. Assert.Fail ("#1");
  235. } catch (ArgumentNullException) {
  236. }
  237. try {
  238. factory.ContinueWhenAny (new Task[0], delegate { });
  239. Assert.Fail ("#2");
  240. } catch (ArgumentException) {
  241. }
  242. try {
  243. factory.ContinueWhenAny (new Task[] { null }, delegate { });
  244. Assert.Fail ("#3");
  245. } catch (ArgumentException) {
  246. }
  247. var tasks = new Task [] {
  248. factory.StartNew (delegate {})
  249. };
  250. try {
  251. factory.ContinueWhenAny (tasks, null);
  252. Assert.Fail ("#4");
  253. } catch (ArgumentException) {
  254. }
  255. try {
  256. factory.ContinueWhenAny (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.None, null);
  257. Assert.Fail ("#5");
  258. } catch (ArgumentException) {
  259. }
  260. try {
  261. factory.ContinueWhenAny (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.OnlyOnCanceled, null);
  262. Assert.Fail ("#6");
  263. } catch (ArgumentException) {
  264. }
  265. }
  266. [Test]
  267. public void FromAsyncBeginInvoke_WithResult ()
  268. {
  269. bool result = false;
  270. Func<int, int> func = (i) => {
  271. Assert.IsTrue (Thread.CurrentThread.IsThreadPoolThread);
  272. result = true; return i + 3;
  273. };
  274. var task = factory.FromAsync<int, int> (func.BeginInvoke, func.EndInvoke, 1, "state", TaskCreationOptions.AttachedToParent);
  275. Assert.IsTrue (task.Wait (5000), "#1");
  276. Assert.IsTrue (result, "#2");
  277. Assert.AreEqual (4, task.Result, "#3");
  278. Assert.AreEqual ("state", (string) task.AsyncState, "#4");
  279. Assert.AreEqual (TaskCreationOptions.AttachedToParent, task.CreationOptions, "#5");
  280. }
  281. [Test]
  282. public void FromAsyncBeginMethod_DirectResult ()
  283. {
  284. bool result = false;
  285. bool continuationTest = false;
  286. Func<int, int> func = (i) => { result = true; return i + 3; };
  287. Task<int> task = factory.FromAsync<int> (func.BeginInvoke (1, delegate { }, null), func.EndInvoke);
  288. var cont = task.ContinueWith (_ => continuationTest = true, TaskContinuationOptions.ExecuteSynchronously);
  289. task.Wait ();
  290. cont.Wait ();
  291. Assert.IsTrue (result);
  292. Assert.IsTrue (continuationTest);
  293. Assert.AreEqual (4, task.Result);
  294. }
  295. [Test]
  296. public void FromAsyncBeginMethod_Exception ()
  297. {
  298. bool result = false;
  299. bool continuationTest = false;
  300. Func<int, int> func = (i) => { result = true; throw new ApplicationException ("bleh"); };
  301. Task<int> task = factory.FromAsync<int, int> (func.BeginInvoke, func.EndInvoke, 1, null);
  302. var cont = task.ContinueWith (_ => continuationTest = true, TaskContinuationOptions.ExecuteSynchronously);
  303. try {
  304. task.Wait ();
  305. } catch { }
  306. cont.Wait ();
  307. Assert.IsTrue (result);
  308. Assert.IsTrue (continuationTest);
  309. Assert.IsNotNull (task.Exception);
  310. var agg = task.Exception;
  311. Assert.AreEqual (1, agg.InnerExceptions.Count);
  312. Assert.IsInstanceOfType (typeof (ApplicationException), agg.InnerExceptions[0]);
  313. Assert.AreEqual (TaskStatus.Faulted, task.Status);
  314. try {
  315. var a = task.Result;
  316. Assert.Fail ();
  317. } catch (AggregateException) {
  318. }
  319. }
  320. [Test]
  321. public void FromAsync_ArgumentsCheck ()
  322. {
  323. var result = new CompletedAsyncResult ();
  324. try {
  325. factory.FromAsync (null, l => { });
  326. Assert.Fail ("#1");
  327. } catch (ArgumentNullException) {
  328. }
  329. try {
  330. factory.FromAsync (result, null);
  331. Assert.Fail ("#2");
  332. } catch (ArgumentNullException) {
  333. }
  334. try {
  335. factory.FromAsync (result, l => { }, TaskCreationOptions.LongRunning);
  336. Assert.Fail ("#3");
  337. } catch (ArgumentOutOfRangeException) {
  338. }
  339. try {
  340. factory.FromAsync (result, l => { }, TaskCreationOptions.PreferFairness);
  341. Assert.Fail ("#4");
  342. } catch (ArgumentOutOfRangeException) {
  343. }
  344. try {
  345. factory.FromAsync (result, l => { }, TaskCreationOptions.None, null);
  346. Assert.Fail ("#5");
  347. } catch (ArgumentNullException) {
  348. }
  349. try {
  350. factory.FromAsync (null, l => { }, null, TaskCreationOptions.None);
  351. Assert.Fail ("#6");
  352. } catch (ArgumentNullException) {
  353. }
  354. try {
  355. factory.FromAsync ((a, b) => null, l => { }, null, TaskCreationOptions.LongRunning);
  356. Assert.Fail ("#7");
  357. } catch (ArgumentOutOfRangeException) {
  358. }
  359. }
  360. [Test]
  361. public void FromAsync_Completed ()
  362. {
  363. var completed = new CompletedAsyncResult ();
  364. bool? valid = null;
  365. Action<IAsyncResult> end = l => {
  366. Assert.IsFalse (Thread.CurrentThread.IsThreadPoolThread, "#2");
  367. valid = l == completed;
  368. };
  369. Task task = factory.FromAsync (completed, end);
  370. Assert.IsTrue (valid == true, "#1");
  371. }
  372. [Test]
  373. public void FromAsync_CompletedWithException ()
  374. {
  375. var completed = new CompletedAsyncResult ();
  376. Action<IAsyncResult> end = l => {
  377. throw new ApplicationException ();
  378. };
  379. Task task = factory.FromAsync (completed, end);
  380. Assert.AreEqual (TaskStatus.Faulted, task.Status, "#1");
  381. }
  382. [Test]
  383. public void FromAsync_CompletedCanceled ()
  384. {
  385. var completed = new CompletedAsyncResult ();
  386. Action<IAsyncResult> end = l => {
  387. throw new OperationCanceledException ();
  388. };
  389. Task task = factory.FromAsync (completed, end);
  390. Assert.AreEqual (TaskStatus.Canceled, task.Status, "#1");
  391. Assert.IsNull (task.Exception, "#2");
  392. }
  393. [Test]
  394. public void FromAsync_SimpleAsyncResult ()
  395. {
  396. var result = new TestAsyncResult ();
  397. bool called = false;
  398. var task = factory.FromAsync (result, l => {
  399. called = true;
  400. });
  401. Assert.IsTrue (task.Wait (1000), "#1");
  402. Assert.IsTrue (called, "#2");
  403. }
  404. [Test]
  405. public void FromAsync_ResultException ()
  406. {
  407. var result = new TestAsyncResult ();
  408. var task = factory.FromAsync (result, l => {
  409. throw new ApplicationException ();
  410. });
  411. try {
  412. Assert.IsFalse (task.Wait (1000), "#1");
  413. } catch (AggregateException) {
  414. }
  415. Assert.AreEqual (TaskStatus.Faulted, task.Status, "#2");
  416. }
  417. [Test]
  418. public void FromAsync_ReturnInt ()
  419. {
  420. var result = new TestAsyncResult ();
  421. bool called = false;
  422. var task = factory.FromAsync<int> (result, l => {
  423. called = true;
  424. return 4;
  425. });
  426. Assert.IsTrue (task.Wait (1000), "#1");
  427. Assert.IsTrue (called, "#2");
  428. Assert.AreEqual (4, task.Result, "#3");
  429. }
  430. [Test]
  431. public void FromAsync_Scheduler_Explicit ()
  432. {
  433. var result = new TestAsyncResult ();
  434. bool called = false;
  435. var scheduler = new TestScheduler ();
  436. var task = factory.FromAsync (result, l => {
  437. called = true;
  438. }, TaskCreationOptions.None, scheduler);
  439. Assert.IsTrue (task.Wait (5000), "#1");
  440. Assert.IsTrue (called, "#2");
  441. Assert.IsTrue (scheduler.ExecutedInline, "#3");
  442. }
  443. [Test]
  444. public void FromAsync_Scheduler_Implicit ()
  445. {
  446. var result = new TestAsyncResult ();
  447. bool called = false;
  448. var scheduler = new TestScheduler ();
  449. factory = new TaskFactory (scheduler);
  450. Task task = factory.FromAsync (result, l => {
  451. Assert.IsTrue (Thread.CurrentThread.IsThreadPoolThread, "#6");
  452. called = true;
  453. }, TaskCreationOptions.AttachedToParent);
  454. Assert.AreEqual (TaskCreationOptions.AttachedToParent, task.CreationOptions, "#1");
  455. Assert.IsNull (task.AsyncState, "#2");
  456. Assert.IsTrue (task.Wait (5000), "#3");
  457. Assert.IsTrue (called, "#4");
  458. Assert.IsTrue (scheduler.ExecutedInline, "#5");
  459. }
  460. [Test]
  461. public void FromAsync_BeginCallback ()
  462. {
  463. bool called = false;
  464. bool called2 = false;
  465. var task = factory.FromAsync (
  466. (a, b, c) => {
  467. if (a != "h")
  468. Assert.Fail ("#10");
  469. if ((TaskCreationOptions) c != TaskCreationOptions.AttachedToParent)
  470. Assert.Fail ("#11");
  471. Assert.IsFalse (Thread.CurrentThread.IsThreadPoolThread, "#12");
  472. called2 = true;
  473. b.Invoke (null);
  474. return null;
  475. },
  476. l => {
  477. called = true;
  478. },
  479. "h", TaskCreationOptions.AttachedToParent);
  480. Assert.AreEqual (TaskCreationOptions.None, task.CreationOptions, "#1");
  481. Assert.AreEqual (TaskCreationOptions.AttachedToParent, (TaskCreationOptions) task.AsyncState, "#2");
  482. Assert.IsTrue (task.Wait (5000), "#3");
  483. Assert.IsTrue (called, "#4");
  484. Assert.IsTrue (called2, "#5");
  485. }
  486. [Test]
  487. public void StartNewCancelled ()
  488. {
  489. var cts = new CancellationTokenSource ();
  490. cts.Cancel ();
  491. var task = factory.StartNew (() => Assert.Fail ("Should never be called"), cts.Token);
  492. try {
  493. task.Start ();
  494. } catch (InvalidOperationException) {
  495. }
  496. Assert.IsTrue (task.IsCanceled, "#2");
  497. }
  498. }
  499. }
  500. #endif