TaskSchedulerTest.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. namespace MonoTests.System.Threading.Tasks
  31. {
  32. [TestFixture]
  33. public class TaskSchedulerTests
  34. {
  35. class LazyCatScheduler : TaskScheduler
  36. {
  37. public TaskStatus ExecuteInlineStatus
  38. {
  39. get;
  40. set;
  41. }
  42. protected override void QueueTask (Task task)
  43. {
  44. throw new NotImplementedException ();
  45. }
  46. protected override bool TryDequeue (Task task)
  47. {
  48. throw new NotImplementedException ();
  49. }
  50. protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
  51. {
  52. ExecuteInlineStatus = task.Status;
  53. return true;
  54. }
  55. protected override IEnumerable<Task> GetScheduledTasks ()
  56. {
  57. throw new NotImplementedException ();
  58. }
  59. }
  60. class DefaultScheduler : TaskScheduler
  61. {
  62. protected override IEnumerable<Task> GetScheduledTasks ()
  63. {
  64. throw new NotImplementedException ();
  65. }
  66. protected override void QueueTask (Task task)
  67. {
  68. throw new NotImplementedException ();
  69. }
  70. protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
  71. {
  72. throw new NotImplementedException ();
  73. }
  74. public void TestDefaultMethod ()
  75. {
  76. Assert.IsFalse (TryDequeue (null), "#1");
  77. }
  78. }
  79. [Test]
  80. public void FromCurrentSynchronizationContextTest_Invalid()
  81. {
  82. var c = SynchronizationContext.Current;
  83. try {
  84. SynchronizationContext.SetSynchronizationContext (null);
  85. TaskScheduler.FromCurrentSynchronizationContext ();
  86. Assert.Fail ("#1");
  87. } catch (InvalidOperationException) {
  88. } finally {
  89. SynchronizationContext.SetSynchronizationContext (c);
  90. }
  91. }
  92. [Test]
  93. public void BasicRunSynchronouslyTest ()
  94. {
  95. bool ran = false;
  96. var t = new Task (() => ran = true);
  97. t.RunSynchronously ();
  98. Assert.IsTrue (t.IsCompleted);
  99. Assert.IsFalse (t.IsFaulted);
  100. Assert.IsFalse (t.IsCanceled);
  101. Assert.IsTrue (ran);
  102. }
  103. [Test]
  104. public void RunSynchronouslyButNoExecutionTest ()
  105. {
  106. TaskSchedulerException ex = null;
  107. var ts = new LazyCatScheduler ();
  108. Task t = new Task (() => {});
  109. try {
  110. t.RunSynchronously (ts);
  111. } catch (TaskSchedulerException e) {
  112. ex = e;
  113. }
  114. Assert.IsNotNull (ex);
  115. Assert.IsNotNull (ex.InnerException);
  116. Assert.IsInstanceOfType (typeof (InvalidOperationException), ex.InnerException);
  117. }
  118. [Test]
  119. public void RunSynchronouslyTaskStatusTest ()
  120. {
  121. var ts = new LazyCatScheduler ();
  122. var t = new Task (() => { });
  123. try {
  124. t.RunSynchronously (ts);
  125. } catch {}
  126. Assert.AreEqual (TaskStatus.WaitingToRun, ts.ExecuteInlineStatus);
  127. }
  128. static int finalizerThreadId = -1;
  129. class FinalizerCatcher
  130. {
  131. ~FinalizerCatcher ()
  132. {
  133. finalizerThreadId = Thread.CurrentThread.ManagedThreadId;
  134. }
  135. }
  136. [Test]
  137. public void DefaultBehaviourTest ()
  138. {
  139. var s = new DefaultScheduler ();
  140. s.TestDefaultMethod ();
  141. }
  142. // This test doesn't work if the GC uses multiple finalizer thread.
  143. // For now it's fine since only one thread is used
  144. [Test]
  145. // Depends on objects getting GCd plus installs an EH handler which catches
  146. // exceptions thrown by other tasks
  147. [Category ("NotWorking")]
  148. public void UnobservedTaskExceptionOnFinalizerThreadTest ()
  149. {
  150. var foo = new FinalizerCatcher ();
  151. foo = null;
  152. GC.Collect ();
  153. GC.WaitForPendingFinalizers ();
  154. // Same than following test, if GC didn't run don't execute the rest of this test
  155. if (finalizerThreadId == -1)
  156. return;
  157. int evtThreadId = -2;
  158. TaskScheduler.UnobservedTaskException += delegate {
  159. evtThreadId = Thread.CurrentThread.ManagedThreadId;
  160. };
  161. var evt = new ManualResetEventSlim ();
  162. CreateAndForgetFaultedTask (evt);
  163. evt.Wait (500);
  164. Thread.Sleep (100);
  165. GC.Collect ();
  166. GC.WaitForPendingFinalizers ();
  167. Assert.AreEqual (finalizerThreadId, evtThreadId, "Should be ran on finalizer thread");
  168. }
  169. [Test]
  170. // Depends on objects getting GCd plus installs an EH handler which catches
  171. // exceptions thrown by other tasks
  172. [Category ("NotWorking")]
  173. public void UnobservedTaskExceptionArgumentTest ()
  174. {
  175. bool ran = false;
  176. bool senderIsRight = false;
  177. UnobservedTaskExceptionEventArgs args = null;
  178. TaskScheduler.UnobservedTaskException += (o, a) => {
  179. senderIsRight = o.GetType ().ToString () == "System.Threading.Tasks.Task";
  180. args = a;
  181. ran = true;
  182. };
  183. var evt = new ManualResetEventSlim ();
  184. CreateAndForgetFaultedTask (evt);
  185. evt.Wait (500);
  186. Thread.Sleep (100);
  187. GC.Collect ();
  188. GC.WaitForPendingFinalizers ();
  189. // GC is too unreliable for some reason in that test, so backoff if finalizer wasn't ran
  190. // it needs to be run for the above test to work though (♥)
  191. if (!ran)
  192. return;
  193. Assert.IsNotNull (args.Exception);
  194. Assert.IsNotNull (args.Exception.InnerException);
  195. Assert.AreEqual ("foo", args.Exception.InnerException.Message);
  196. Assert.IsFalse (args.Observed);
  197. Assert.IsTrue (senderIsRight, "Sender is a task");
  198. }
  199. // We use this intermediary method to improve chances of GC kicking
  200. static void CreateAndForgetFaultedTask (ManualResetEventSlim evt)
  201. {
  202. Task.Factory.StartNew (() => { evt.Set (); throw new Exception ("foo"); });
  203. }
  204. }
  205. }
  206. #endif