TaskSchedulerTest.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. class UserSynchronizationContext : SynchronizationContext
  83. {
  84. }
  85. [Test]
  86. public void FromCurrentSynchronizationContextTest_Invalid()
  87. {
  88. var c = SynchronizationContext.Current;
  89. try {
  90. SynchronizationContext.SetSynchronizationContext (null);
  91. TaskScheduler.FromCurrentSynchronizationContext ();
  92. Assert.Fail ("#1");
  93. } catch (InvalidOperationException) {
  94. } finally {
  95. SynchronizationContext.SetSynchronizationContext (c);
  96. }
  97. }
  98. [Test]
  99. public void FromUserSynchronizationContext ()
  100. {
  101. var c = SynchronizationContext.Current;
  102. try {
  103. SynchronizationContext.SetSynchronizationContext (new UserSynchronizationContext ());
  104. var ts = TaskScheduler.FromCurrentSynchronizationContext ();
  105. Assert.AreEqual (1, ts.MaximumConcurrencyLevel, "#1");
  106. } finally {
  107. SynchronizationContext.SetSynchronizationContext (c);
  108. }
  109. }
  110. [Test]
  111. public void BasicRunSynchronouslyTest ()
  112. {
  113. bool ran = false;
  114. var t = new Task (() => ran = true);
  115. t.RunSynchronously ();
  116. Assert.IsTrue (t.IsCompleted);
  117. Assert.IsFalse (t.IsFaulted);
  118. Assert.IsFalse (t.IsCanceled);
  119. Assert.IsTrue (ran);
  120. }
  121. [Test]
  122. public void RunSynchronouslyButNoExecutionTest ()
  123. {
  124. TaskSchedulerException ex = null;
  125. var ts = new LazyCatScheduler ();
  126. Task t = new Task (() => {});
  127. try {
  128. t.RunSynchronously (ts);
  129. } catch (TaskSchedulerException e) {
  130. ex = e;
  131. }
  132. Assert.IsNotNull (ex);
  133. Assert.IsNotNull (ex.InnerException);
  134. Assert.That (ex.InnerException, Is.TypeOf (typeof (InvalidOperationException)));
  135. }
  136. [Test]
  137. public void RunSynchronouslyTaskStatusTest ()
  138. {
  139. var ts = new LazyCatScheduler ();
  140. var t = new Task (() => { });
  141. try {
  142. t.RunSynchronously (ts);
  143. } catch {}
  144. Assert.AreEqual (TaskStatus.WaitingToRun, ts.ExecuteInlineStatus);
  145. }
  146. static int finalizerThreadId = -1;
  147. class FinalizerCatcher
  148. {
  149. ~FinalizerCatcher ()
  150. {
  151. finalizerThreadId = Thread.CurrentThread.ManagedThreadId;
  152. }
  153. }
  154. [Test]
  155. public void DefaultBehaviourTest ()
  156. {
  157. var s = new DefaultScheduler ();
  158. s.TestDefaultMethod ();
  159. }
  160. // This test doesn't work if the GC uses multiple finalizer thread.
  161. // For now it's fine since only one thread is used
  162. [Test]
  163. // Depends on objects getting GCd plus installs an EH handler which catches
  164. // exceptions thrown by other tasks
  165. [Category ("NotWorking")]
  166. public void UnobservedTaskExceptionOnFinalizerThreadTest ()
  167. {
  168. var foo = new FinalizerCatcher ();
  169. foo = null;
  170. GC.Collect ();
  171. GC.WaitForPendingFinalizers ();
  172. // Same than following test, if GC didn't run don't execute the rest of this test
  173. if (finalizerThreadId == -1)
  174. return;
  175. int evtThreadId = -2;
  176. TaskScheduler.UnobservedTaskException += delegate {
  177. evtThreadId = Thread.CurrentThread.ManagedThreadId;
  178. };
  179. var evt = new ManualResetEventSlim ();
  180. CreateAndForgetFaultedTask (evt);
  181. evt.Wait (500);
  182. Thread.Sleep (100);
  183. GC.Collect ();
  184. GC.WaitForPendingFinalizers ();
  185. Assert.AreEqual (finalizerThreadId, evtThreadId, "Should be ran on finalizer thread");
  186. }
  187. [Test]
  188. // Depends on objects getting GCd plus installs an EH handler which catches
  189. // exceptions thrown by other tasks
  190. [Category ("NotWorking")]
  191. public void UnobservedTaskExceptionArgumentTest ()
  192. {
  193. bool ran = false;
  194. bool senderIsRight = false;
  195. UnobservedTaskExceptionEventArgs args = null;
  196. TaskScheduler.UnobservedTaskException += (o, a) => {
  197. senderIsRight = o.GetType ().ToString () == "System.Threading.Tasks.Task";
  198. args = a;
  199. ran = true;
  200. };
  201. var evt = new ManualResetEventSlim ();
  202. CreateAndForgetFaultedTask (evt);
  203. evt.Wait (500);
  204. Thread.Sleep (100);
  205. GC.Collect ();
  206. GC.WaitForPendingFinalizers ();
  207. // GC is too unreliable for some reason in that test, so backoff if finalizer wasn't ran
  208. // it needs to be run for the above test to work though (♥)
  209. if (!ran)
  210. return;
  211. Assert.IsNotNull (args.Exception);
  212. Assert.IsNotNull (args.Exception.InnerException);
  213. Assert.AreEqual ("foo", args.Exception.InnerException.Message);
  214. Assert.IsFalse (args.Observed);
  215. Assert.IsTrue (senderIsRight, "Sender is a task");
  216. }
  217. // We use this intermediary method to improve chances of GC kicking
  218. static void CreateAndForgetFaultedTask (ManualResetEventSlim evt)
  219. {
  220. Task.Factory.StartNew (() => { evt.Set (); throw new Exception ("foo"); });
  221. }
  222. }
  223. }
  224. #endif