TaskSchedulerTest.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // TaskSchedulerTest.cs
  2. //
  3. // Copyright (c) 2008 Jérémie "Garuma" Laval
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. //
  24. #if NET_4_0
  25. using System;
  26. using System.Threading;
  27. using System.Threading.Tasks;
  28. using System.Collections.Generic;
  29. using NUnit.Framework;
  30. #if !MOBILE
  31. using NUnit.Framework.SyntaxHelpers;
  32. #endif
  33. namespace MonoTests.System.Threading.Tasks
  34. {
  35. [TestFixture]
  36. public class TaskSchedulerTests
  37. {
  38. class LazyCatScheduler : TaskScheduler
  39. {
  40. public TaskStatus ExecuteInlineStatus
  41. {
  42. get;
  43. set;
  44. }
  45. protected override void QueueTask (Task task)
  46. {
  47. throw new NotImplementedException ();
  48. }
  49. protected override bool TryDequeue (Task task)
  50. {
  51. throw new NotImplementedException ();
  52. }
  53. protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
  54. {
  55. ExecuteInlineStatus = task.Status;
  56. return true;
  57. }
  58. protected override IEnumerable<Task> GetScheduledTasks ()
  59. {
  60. throw new NotImplementedException ();
  61. }
  62. }
  63. class DefaultScheduler : TaskScheduler
  64. {
  65. protected override IEnumerable<Task> GetScheduledTasks ()
  66. {
  67. throw new NotImplementedException ();
  68. }
  69. protected override void QueueTask (Task task)
  70. {
  71. throw new NotImplementedException ();
  72. }
  73. protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
  74. {
  75. throw new NotImplementedException ();
  76. }
  77. public void TestDefaultMethod ()
  78. {
  79. Assert.IsFalse (TryDequeue (null), "#1");
  80. }
  81. }
  82. [Test]
  83. public void FromCurrentSynchronizationContextTest_Invalid()
  84. {
  85. var c = SynchronizationContext.Current;
  86. try {
  87. SynchronizationContext.SetSynchronizationContext (null);
  88. TaskScheduler.FromCurrentSynchronizationContext ();
  89. Assert.Fail ("#1");
  90. } catch (InvalidOperationException) {
  91. } finally {
  92. SynchronizationContext.SetSynchronizationContext (c);
  93. }
  94. }
  95. [Test]
  96. public void BasicRunSynchronouslyTest ()
  97. {
  98. bool ran = false;
  99. var t = new Task (() => ran = true);
  100. t.RunSynchronously ();
  101. Assert.IsTrue (t.IsCompleted);
  102. Assert.IsFalse (t.IsFaulted);
  103. Assert.IsFalse (t.IsCanceled);
  104. Assert.IsTrue (ran);
  105. }
  106. [Test]
  107. public void RunSynchronouslyButNoExecutionTest ()
  108. {
  109. TaskSchedulerException ex = null;
  110. var ts = new LazyCatScheduler ();
  111. Task t = new Task (() => {});
  112. try {
  113. t.RunSynchronously (ts);
  114. } catch (TaskSchedulerException e) {
  115. ex = e;
  116. }
  117. Assert.IsNotNull (ex);
  118. Assert.IsNotNull (ex.InnerException);
  119. Assert.That (ex.InnerException, Is.TypeOf (typeof (InvalidOperationException)));
  120. }
  121. [Test]
  122. public void RunSynchronouslyTaskStatusTest ()
  123. {
  124. var ts = new LazyCatScheduler ();
  125. var t = new Task (() => { });
  126. try {
  127. t.RunSynchronously (ts);
  128. } catch {}
  129. Assert.AreEqual (TaskStatus.WaitingToRun, ts.ExecuteInlineStatus);
  130. }
  131. static int finalizerThreadId = -1;
  132. class FinalizerCatcher
  133. {
  134. ~FinalizerCatcher ()
  135. {
  136. finalizerThreadId = Thread.CurrentThread.ManagedThreadId;
  137. }
  138. }
  139. [Test]
  140. public void DefaultBehaviourTest ()
  141. {
  142. var s = new DefaultScheduler ();
  143. s.TestDefaultMethod ();
  144. }
  145. // This test doesn't work if the GC uses multiple finalizer thread.
  146. // For now it's fine since only one thread is used
  147. [Test]
  148. // Depends on objects getting GCd plus installs an EH handler which catches
  149. // exceptions thrown by other tasks
  150. [Category ("NotWorking")]
  151. public void UnobservedTaskExceptionOnFinalizerThreadTest ()
  152. {
  153. var foo = new FinalizerCatcher ();
  154. foo = null;
  155. GC.Collect ();
  156. GC.WaitForPendingFinalizers ();
  157. // Same than following test, if GC didn't run don't execute the rest of this test
  158. if (finalizerThreadId == -1)
  159. return;
  160. int evtThreadId = -2;
  161. TaskScheduler.UnobservedTaskException += delegate {
  162. evtThreadId = Thread.CurrentThread.ManagedThreadId;
  163. };
  164. var evt = new ManualResetEventSlim ();
  165. CreateAndForgetFaultedTask (evt);
  166. evt.Wait (500);
  167. Thread.Sleep (100);
  168. GC.Collect ();
  169. GC.WaitForPendingFinalizers ();
  170. Assert.AreEqual (finalizerThreadId, evtThreadId, "Should be ran on finalizer thread");
  171. }
  172. [Test]
  173. // Depends on objects getting GCd plus installs an EH handler which catches
  174. // exceptions thrown by other tasks
  175. [Category ("NotWorking")]
  176. public void UnobservedTaskExceptionArgumentTest ()
  177. {
  178. bool ran = false;
  179. bool senderIsRight = false;
  180. UnobservedTaskExceptionEventArgs args = null;
  181. TaskScheduler.UnobservedTaskException += (o, a) => {
  182. senderIsRight = o.GetType ().ToString () == "System.Threading.Tasks.Task";
  183. args = a;
  184. ran = true;
  185. };
  186. var evt = new ManualResetEventSlim ();
  187. CreateAndForgetFaultedTask (evt);
  188. evt.Wait (500);
  189. Thread.Sleep (100);
  190. GC.Collect ();
  191. GC.WaitForPendingFinalizers ();
  192. // GC is too unreliable for some reason in that test, so backoff if finalizer wasn't ran
  193. // it needs to be run for the above test to work though (♥)
  194. if (!ran)
  195. return;
  196. Assert.IsNotNull (args.Exception);
  197. Assert.IsNotNull (args.Exception.InnerException);
  198. Assert.AreEqual ("foo", args.Exception.InnerException.Message);
  199. Assert.IsFalse (args.Observed);
  200. Assert.IsTrue (senderIsRight, "Sender is a task");
  201. }
  202. // We use this intermediary method to improve chances of GC kicking
  203. static void CreateAndForgetFaultedTask (ManualResetEventSlim evt)
  204. {
  205. Task.Factory.StartNew (() => { evt.Set (); throw new Exception ("foo"); });
  206. }
  207. }
  208. }
  209. #endif