TaskFactoryTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 ContinueWhenAny_Simple ()
  199. {
  200. var t1 = new ManualResetEvent (false);
  201. var t2 = new ManualResetEvent (false);
  202. var tasks = new Task[2] {
  203. Task.Factory.StartNew (() => { t1.WaitOne (3000); }),
  204. Task.Factory.StartNew (() => { t2.WaitOne (3000); })
  205. };
  206. bool ran = false;
  207. var ct = new CancellationToken ();
  208. Task cont = factory.ContinueWhenAny (tasks, t => {
  209. Assert.AreEqual (tasks[0], t, "#1");
  210. ran = true;
  211. }, ct);
  212. Assert.AreEqual (TaskStatus.WaitingForActivation, cont.Status, "#2");
  213. t1.Set ();
  214. Assert.IsTrue (cont.Wait (2000), "#10");
  215. Assert.IsTrue (ran, "#11");
  216. t2.Set ();
  217. }
  218. [Test]
  219. public void ContinueWhenAny_InvalidArguments ()
  220. {
  221. try {
  222. factory.ContinueWhenAny (null, delegate { });
  223. Assert.Fail ("#1");
  224. } catch (ArgumentNullException) {
  225. }
  226. try {
  227. factory.ContinueWhenAny (new Task[0], delegate { });
  228. Assert.Fail ("#2");
  229. } catch (ArgumentException) {
  230. }
  231. try {
  232. factory.ContinueWhenAny (new Task[] { null }, delegate { });
  233. Assert.Fail ("#3");
  234. } catch (ArgumentException) {
  235. }
  236. var tasks = new Task [] {
  237. factory.StartNew (delegate {})
  238. };
  239. try {
  240. factory.ContinueWhenAny (tasks, null);
  241. Assert.Fail ("#4");
  242. } catch (ArgumentException) {
  243. }
  244. try {
  245. factory.ContinueWhenAny (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.None, null);
  246. Assert.Fail ("#5");
  247. } catch (ArgumentException) {
  248. }
  249. try {
  250. factory.ContinueWhenAny (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.OnlyOnCanceled, null);
  251. Assert.Fail ("#6");
  252. } catch (ArgumentException) {
  253. }
  254. }
  255. [Test]
  256. public void FromAsyncBeginInvoke_WithResult ()
  257. {
  258. bool result = false;
  259. Func<int, int> func = (i) => {
  260. Assert.IsTrue (Thread.CurrentThread.IsThreadPoolThread);
  261. result = true; return i + 3;
  262. };
  263. var task = factory.FromAsync<int, int> (func.BeginInvoke, func.EndInvoke, 1, "state", TaskCreationOptions.AttachedToParent);
  264. Assert.IsTrue (task.Wait (5000), "#1");
  265. Assert.IsTrue (result, "#2");
  266. Assert.AreEqual (4, task.Result, "#3");
  267. Assert.AreEqual ("state", (string) task.AsyncState, "#4");
  268. Assert.AreEqual (TaskCreationOptions.AttachedToParent, task.CreationOptions, "#5");
  269. }
  270. [Test]
  271. public void FromAsyncBeginMethod_DirectResult ()
  272. {
  273. bool result = false;
  274. bool continuationTest = false;
  275. Func<int, int> func = (i) => { result = true; return i + 3; };
  276. Task<int> task = factory.FromAsync<int> (func.BeginInvoke (1, delegate { }, null), func.EndInvoke);
  277. var cont = task.ContinueWith (_ => continuationTest = true, TaskContinuationOptions.ExecuteSynchronously);
  278. task.Wait ();
  279. cont.Wait ();
  280. Assert.IsTrue (result);
  281. Assert.IsTrue (continuationTest);
  282. Assert.AreEqual (4, task.Result);
  283. }
  284. [Test]
  285. public void FromAsyncBeginMethod_Exception ()
  286. {
  287. bool result = false;
  288. bool continuationTest = false;
  289. Func<int, int> func = (i) => { result = true; throw new ApplicationException ("bleh"); };
  290. Task<int> task = factory.FromAsync<int, int> (func.BeginInvoke, func.EndInvoke, 1, null);
  291. var cont = task.ContinueWith (_ => continuationTest = true, TaskContinuationOptions.ExecuteSynchronously);
  292. try {
  293. task.Wait ();
  294. } catch { }
  295. cont.Wait ();
  296. Assert.IsTrue (result);
  297. Assert.IsTrue (continuationTest);
  298. Assert.IsNotNull (task.Exception);
  299. var agg = task.Exception;
  300. Assert.AreEqual (1, agg.InnerExceptions.Count);
  301. Assert.IsInstanceOfType (typeof (ApplicationException), agg.InnerExceptions[0]);
  302. Assert.AreEqual (TaskStatus.Faulted, task.Status);
  303. try {
  304. var a = task.Result;
  305. Assert.Fail ();
  306. } catch (AggregateException) {
  307. }
  308. }
  309. [Test]
  310. public void FromAsync_ArgumentsCheck ()
  311. {
  312. var result = new CompletedAsyncResult ();
  313. try {
  314. factory.FromAsync (null, l => { });
  315. Assert.Fail ("#1");
  316. } catch (ArgumentNullException) {
  317. }
  318. try {
  319. factory.FromAsync (result, null);
  320. Assert.Fail ("#2");
  321. } catch (ArgumentNullException) {
  322. }
  323. try {
  324. factory.FromAsync (result, l => { }, TaskCreationOptions.LongRunning);
  325. Assert.Fail ("#3");
  326. } catch (ArgumentOutOfRangeException) {
  327. }
  328. try {
  329. factory.FromAsync (result, l => { }, TaskCreationOptions.PreferFairness);
  330. Assert.Fail ("#4");
  331. } catch (ArgumentOutOfRangeException) {
  332. }
  333. try {
  334. factory.FromAsync (result, l => { }, TaskCreationOptions.None, null);
  335. Assert.Fail ("#5");
  336. } catch (ArgumentNullException) {
  337. }
  338. try {
  339. factory.FromAsync (null, l => { }, null, TaskCreationOptions.None);
  340. Assert.Fail ("#6");
  341. } catch (ArgumentNullException) {
  342. }
  343. try {
  344. factory.FromAsync ((a, b) => null, l => { }, null, TaskCreationOptions.LongRunning);
  345. Assert.Fail ("#7");
  346. } catch (ArgumentOutOfRangeException) {
  347. }
  348. }
  349. [Test]
  350. public void FromAsync_Completed ()
  351. {
  352. var completed = new CompletedAsyncResult ();
  353. bool? valid = null;
  354. Action<IAsyncResult> end = l => {
  355. Assert.IsFalse (Thread.CurrentThread.IsThreadPoolThread, "#2");
  356. valid = l == completed;
  357. };
  358. Task task = factory.FromAsync (completed, end);
  359. Assert.IsTrue (valid == true, "#1");
  360. }
  361. [Test]
  362. public void FromAsync_CompletedWithException ()
  363. {
  364. var completed = new CompletedAsyncResult ();
  365. Action<IAsyncResult> end = l => {
  366. throw new ApplicationException ();
  367. };
  368. Task task = factory.FromAsync (completed, end);
  369. Assert.AreEqual (TaskStatus.Faulted, task.Status, "#1");
  370. }
  371. [Test]
  372. public void FromAsync_CompletedCanceled ()
  373. {
  374. var completed = new CompletedAsyncResult ();
  375. Action<IAsyncResult> end = l => {
  376. throw new OperationCanceledException ();
  377. };
  378. Task task = factory.FromAsync (completed, end);
  379. Assert.AreEqual (TaskStatus.Canceled, task.Status, "#1");
  380. Assert.IsNull (task.Exception, "#2");
  381. }
  382. [Test]
  383. public void FromAsync_SimpleAsyncResult ()
  384. {
  385. var result = new TestAsyncResult ();
  386. bool called = false;
  387. var task = factory.FromAsync (result, l => {
  388. called = true;
  389. });
  390. Assert.IsTrue (task.Wait (1000), "#1");
  391. Assert.IsTrue (called, "#2");
  392. }
  393. [Test]
  394. public void FromAsync_ResultException ()
  395. {
  396. var result = new TestAsyncResult ();
  397. var task = factory.FromAsync (result, l => {
  398. throw new ApplicationException ();
  399. });
  400. try {
  401. Assert.IsFalse (task.Wait (1000), "#1");
  402. } catch (AggregateException) {
  403. }
  404. Assert.AreEqual (TaskStatus.Faulted, task.Status, "#2");
  405. }
  406. [Test]
  407. public void FromAsync_ReturnInt ()
  408. {
  409. var result = new TestAsyncResult ();
  410. bool called = false;
  411. var task = factory.FromAsync<int> (result, l => {
  412. called = true;
  413. return 4;
  414. });
  415. Assert.IsTrue (task.Wait (1000), "#1");
  416. Assert.IsTrue (called, "#2");
  417. Assert.AreEqual (4, task.Result, "#3");
  418. }
  419. [Test]
  420. public void FromAsync_Scheduler_Explicit ()
  421. {
  422. var result = new TestAsyncResult ();
  423. bool called = false;
  424. var scheduler = new TestScheduler ();
  425. var task = factory.FromAsync (result, l => {
  426. called = true;
  427. }, TaskCreationOptions.None, scheduler);
  428. Assert.IsTrue (task.Wait (5000), "#1");
  429. Assert.IsTrue (called, "#2");
  430. Assert.IsTrue (scheduler.ExecutedInline, "#3");
  431. }
  432. [Test]
  433. public void FromAsync_Scheduler_Implicit ()
  434. {
  435. var result = new TestAsyncResult ();
  436. bool called = false;
  437. var scheduler = new TestScheduler ();
  438. factory = new TaskFactory (scheduler);
  439. Task task = factory.FromAsync (result, l => {
  440. Assert.IsTrue (Thread.CurrentThread.IsThreadPoolThread, "#6");
  441. called = true;
  442. }, TaskCreationOptions.AttachedToParent);
  443. Assert.AreEqual (TaskCreationOptions.AttachedToParent, task.CreationOptions, "#1");
  444. Assert.IsNull (task.AsyncState, "#2");
  445. Assert.IsTrue (task.Wait (5000), "#3");
  446. Assert.IsTrue (called, "#4");
  447. Assert.IsTrue (scheduler.ExecutedInline, "#5");
  448. }
  449. [Test]
  450. public void FromAsync_BeginCallback ()
  451. {
  452. bool called = false;
  453. bool called2 = false;
  454. var task = factory.FromAsync (
  455. (a, b, c) => {
  456. if (a != "h")
  457. Assert.Fail ("#10");
  458. if ((TaskCreationOptions) c != TaskCreationOptions.AttachedToParent)
  459. Assert.Fail ("#11");
  460. Assert.IsFalse (Thread.CurrentThread.IsThreadPoolThread, "#12");
  461. called2 = true;
  462. b.Invoke (null);
  463. return null;
  464. },
  465. l => {
  466. called = true;
  467. },
  468. "h", TaskCreationOptions.AttachedToParent);
  469. Assert.AreEqual (TaskCreationOptions.None, task.CreationOptions, "#1");
  470. Assert.AreEqual (TaskCreationOptions.AttachedToParent, (TaskCreationOptions) task.AsyncState, "#2");
  471. Assert.IsTrue (task.Wait (5000), "#3");
  472. Assert.IsTrue (called, "#4");
  473. Assert.IsTrue (called2, "#5");
  474. }
  475. [Test]
  476. public void StartNewCancelled ()
  477. {
  478. var cts = new CancellationTokenSource ();
  479. cts.Cancel ();
  480. var task = factory.StartNew (() => Assert.Fail ("Should never be called"), cts.Token);
  481. try {
  482. task.Start ();
  483. } catch (InvalidOperationException) {
  484. }
  485. Assert.IsTrue (task.IsCanceled, "#2");
  486. }
  487. }
  488. }
  489. #endif