TaskFactoryTest.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #if NET_4_0
  2. // TaskFactoryTest.cs
  3. //
  4. // Copyright (c) 2010 Jérémie "Garuma" Laval
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. //
  25. using System;
  26. using System.Threading;
  27. using System.Threading.Tasks;
  28. using NUnit.Framework;
  29. namespace MonoTests.System.Threading.Tasks
  30. {
  31. [TestFixture]
  32. public class TaskFactoryTests
  33. {
  34. TaskFactory factory;
  35. [SetUp]
  36. public void Setup ()
  37. {
  38. this.factory = Task.Factory;
  39. }
  40. [Test]
  41. public void StartNewTest ()
  42. {
  43. bool result = false;
  44. factory.StartNew (() => result = true).Wait ();
  45. Assert.IsTrue (result);
  46. }
  47. [Test]
  48. public void ContinueWhenAllTest ()
  49. {
  50. bool r1 = false, r2 = false, r3 = false;
  51. Task[] tasks = new Task[3];
  52. tasks[0] = new Task (() => { Thread.Sleep (100); r1 = true; });
  53. tasks[1] = new Task (() => { Thread.Sleep (500); r2 = true; });
  54. tasks[2] = new Task (() => { Thread.Sleep (300); r3 = true; });
  55. bool result = false;
  56. Task cont = factory.ContinueWhenAll (tasks, (ts) => { if (r1 && r2 && r3) result = true; });
  57. foreach (Task t in tasks)
  58. t.Start ();
  59. cont.Wait ();
  60. Assert.IsTrue (r1, "#1");
  61. Assert.IsTrue (r2, "#2");
  62. Assert.IsTrue (r3, "#3");
  63. Assert.IsTrue (result, "#4");
  64. }
  65. [Test]
  66. public void ContinueWhenAnyTest ()
  67. {
  68. bool r = false, result = false, finished = false;
  69. Task[] tasks = new Task[3];
  70. tasks[0] = new Task (() => { Thread.Sleep (300); r = true; });
  71. tasks[1] = new Task (() => { SpinWait sw; while (!finished) sw.SpinOnce (); });
  72. tasks[2] = new Task (() => { SpinWait sw; while (!finished) sw.SpinOnce (); });
  73. Task cont = factory.ContinueWhenAny (tasks, (t) => { if (r) result = t == tasks[0]; finished = true; });
  74. foreach (Task t in tasks)
  75. t.Start ();
  76. cont.Wait ();
  77. Assert.IsTrue (r, "#1");
  78. Assert.IsTrue (result, "#2");
  79. Assert.IsTrue (finished, "#3");
  80. }
  81. }
  82. }
  83. #endif