1 // TaskSchedulerTest.cs
3 // Copyright (c) 2008 Jérémie "Garuma" Laval
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:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
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
28 using System.Threading;
29 using System.Threading.Tasks;
30 using System.Collections.Generic;
32 using NUnit.Framework;
34 using NUnit.Framework.SyntaxHelpers;
37 namespace MonoTests.System.Threading.Tasks
40 public class TaskSchedulerTests
42 class LazyCatScheduler : TaskScheduler
44 public TaskStatus ExecuteInlineStatus
50 protected override void QueueTask (Task task)
52 throw new NotImplementedException ();
55 protected override bool TryDequeue (Task task)
57 throw new NotImplementedException ();
60 protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
62 ExecuteInlineStatus = task.Status;
66 protected override IEnumerable<Task> GetScheduledTasks ()
68 throw new NotImplementedException ();
72 class DefaultScheduler : TaskScheduler
74 protected override IEnumerable<Task> GetScheduledTasks ()
76 throw new NotImplementedException ();
79 protected override void QueueTask (Task task)
81 throw new NotImplementedException ();
84 protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
86 throw new NotImplementedException ();
89 public void TestDefaultMethod ()
91 Assert.IsFalse (TryDequeue (null), "#1");
95 class UserSynchronizationContext : SynchronizationContext
101 public void FromCurrentSynchronizationContextTest_Invalid()
103 var c = SynchronizationContext.Current;
105 SynchronizationContext.SetSynchronizationContext (null);
106 TaskScheduler.FromCurrentSynchronizationContext ();
108 } catch (InvalidOperationException) {
110 SynchronizationContext.SetSynchronizationContext (c);
115 public void FromUserSynchronizationContext ()
117 var c = SynchronizationContext.Current;
119 SynchronizationContext.SetSynchronizationContext (new UserSynchronizationContext ());
120 var ts = TaskScheduler.FromCurrentSynchronizationContext ();
121 Assert.AreEqual (1, ts.MaximumConcurrencyLevel, "#1");
123 SynchronizationContext.SetSynchronizationContext (c);
128 public void BasicRunSynchronouslyTest ()
131 var t = new Task (() => ran = true);
133 t.RunSynchronously ();
134 Assert.IsTrue (t.IsCompleted);
135 Assert.IsFalse (t.IsFaulted);
136 Assert.IsFalse (t.IsCanceled);
141 public void RunSynchronouslyButNoExecutionTest ()
143 TaskSchedulerException ex = null;
145 var ts = new LazyCatScheduler ();
146 Task t = new Task (() => {});
149 t.RunSynchronously (ts);
150 } catch (TaskSchedulerException e) {
154 Assert.IsNotNull (ex);
155 Assert.IsNotNull (ex.InnerException);
156 Assert.That (ex.InnerException, Is.TypeOf (typeof (InvalidOperationException)));
160 public void RunSynchronouslyTaskStatusTest ()
162 var ts = new LazyCatScheduler ();
163 var t = new Task (() => { });
166 t.RunSynchronously (ts);
168 Assert.AreEqual (TaskStatus.WaitingToRun, ts.ExecuteInlineStatus);
171 static int finalizerThreadId = -1;
173 class FinalizerCatcher
177 finalizerThreadId = Thread.CurrentThread.ManagedThreadId;
182 public void DefaultBehaviourTest ()
184 var s = new DefaultScheduler ();
185 s.TestDefaultMethod ();
188 // This test doesn't work if the GC uses multiple finalizer thread.
189 // For now it's fine since only one thread is used
191 // Depends on objects getting GCd plus installs an EH handler which catches
192 // exceptions thrown by other tasks
193 [Category ("NotWorking")]
194 public void UnobservedTaskExceptionOnFinalizerThreadTest ()
196 var foo = new FinalizerCatcher ();
199 GC.WaitForPendingFinalizers ();
200 // Same than following test, if GC didn't run don't execute the rest of this test
201 if (finalizerThreadId == -1)
204 int evtThreadId = -2;
205 TaskScheduler.UnobservedTaskException += delegate {
206 evtThreadId = Thread.CurrentThread.ManagedThreadId;
208 var evt = new ManualResetEventSlim ();
209 CreateAndForgetFaultedTask (evt);
213 GC.WaitForPendingFinalizers ();
214 Assert.AreEqual (finalizerThreadId, evtThreadId, "Should be ran on finalizer thread");
218 // Depends on objects getting GCd plus installs an EH handler which catches
219 // exceptions thrown by other tasks
220 [Category ("NotWorking")]
221 public void UnobservedTaskExceptionArgumentTest ()
224 bool senderIsRight = false;
225 UnobservedTaskExceptionEventArgs args = null;
227 TaskScheduler.UnobservedTaskException += (o, a) => {
228 senderIsRight = o.GetType ().ToString () == "System.Threading.Tasks.Task";
233 var evt = new ManualResetEventSlim ();
234 CreateAndForgetFaultedTask (evt);
238 GC.WaitForPendingFinalizers ();
240 // GC is too unreliable for some reason in that test, so backoff if finalizer wasn't ran
241 // it needs to be run for the above test to work though (♥)
245 Assert.IsNotNull (args.Exception);
246 Assert.IsNotNull (args.Exception.InnerException);
247 Assert.AreEqual ("foo", args.Exception.InnerException.Message);
248 Assert.IsFalse (args.Observed);
249 Assert.IsTrue (senderIsRight, "Sender is a task");
252 // We use this intermediary method to improve chances of GC kicking
253 static void CreateAndForgetFaultedTask (ManualResetEventSlim evt)
255 Task.Factory.StartNew (() => { evt.Set (); throw new Exception ("foo"); });