Merge branch 'BigIntegerParse'
[mono.git] / mcs / class / corlib / Test / System.Threading.Tasks / TaskSchedulerTest.cs
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
25 #if NET_4_0
26
27 using System;
28 using System.Threading;
29 using System.Threading.Tasks;
30 using System.Collections.Generic;
31
32 using NUnit.Framework;
33 #if !MOBILE
34 using NUnit.Framework.SyntaxHelpers;
35 #endif
36
37 namespace MonoTests.System.Threading.Tasks
38 {
39         [TestFixture]
40         public class TaskSchedulerTests
41         {
42                 class LazyCatScheduler : TaskScheduler
43                 {
44                         public TaskStatus ExecuteInlineStatus
45                         {
46                                 get;
47                                 set;
48                         }
49
50                         protected override void QueueTask (Task task)
51                         {
52                                 throw new NotImplementedException ();
53                         }
54
55                         protected override bool TryDequeue (Task task)
56                         {
57                                 throw new NotImplementedException ();
58                         }
59
60                         protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
61                         {
62                                 ExecuteInlineStatus = task.Status;
63                                 return true;
64                         }
65
66                         protected override IEnumerable<Task> GetScheduledTasks ()
67                         {
68                                 throw new NotImplementedException ();
69                         }
70                 }
71
72                 class DefaultScheduler : TaskScheduler
73                 {
74                         protected override IEnumerable<Task> GetScheduledTasks ()
75                         {
76                                 throw new NotImplementedException ();
77                         }
78
79                         protected override void QueueTask (Task task)
80                         {
81                                 throw new NotImplementedException ();
82                         }
83
84                         protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
85                         {
86                                 throw new NotImplementedException ();
87                         }
88
89                         public void TestDefaultMethod ()
90                         {
91                                 Assert.IsFalse (TryDequeue (null), "#1");
92                         }
93                 }
94
95                 class UserSynchronizationContext : SynchronizationContext
96                 {
97                         
98                 }
99
100                 [Test]
101                 public void FromCurrentSynchronizationContextTest_Invalid()
102                 {
103                         var c = SynchronizationContext.Current;
104                         try {
105                                 SynchronizationContext.SetSynchronizationContext (null);
106                                 TaskScheduler.FromCurrentSynchronizationContext ();
107                                 Assert.Fail ("#1");
108                         } catch (InvalidOperationException) {
109                         } finally {
110                                 SynchronizationContext.SetSynchronizationContext (c);
111                         }
112                 }
113
114                 [Test]
115                 public void FromUserSynchronizationContext ()
116                 {
117                         var c = SynchronizationContext.Current;
118                         try {
119                                 SynchronizationContext.SetSynchronizationContext (new UserSynchronizationContext ());
120                                 var ts = TaskScheduler.FromCurrentSynchronizationContext ();
121                                 Assert.AreEqual (1, ts.MaximumConcurrencyLevel, "#1");
122                         } finally {
123                                 SynchronizationContext.SetSynchronizationContext (c);
124                         }
125                 }
126
127                 [Test]
128                 public void BasicRunSynchronouslyTest ()
129                 {
130                         bool ran = false;
131                         var t = new Task (() => ran = true);
132
133                         t.RunSynchronously ();
134                         Assert.IsTrue (t.IsCompleted);
135                         Assert.IsFalse (t.IsFaulted);
136                         Assert.IsFalse (t.IsCanceled);
137                         Assert.IsTrue (ran);
138                 }
139
140                 [Test]
141                 public void RunSynchronouslyButNoExecutionTest ()
142                 {
143                         TaskSchedulerException ex = null;
144
145                         var ts = new LazyCatScheduler ();
146                         Task t = new Task (() => {});
147
148                         try {
149                                 t.RunSynchronously (ts);
150                         } catch (TaskSchedulerException e) {
151                                 ex = e;
152                         }
153
154                         Assert.IsNotNull (ex);
155                         Assert.IsNotNull (ex.InnerException);
156                         Assert.That (ex.InnerException, Is.TypeOf (typeof (InvalidOperationException)));
157                 }
158
159                 [Test]
160                 public void RunSynchronouslyTaskStatusTest ()
161                 {
162                         var ts = new LazyCatScheduler ();
163                         var t = new Task (() => { });
164
165                         try {
166                                 t.RunSynchronously (ts);
167                         } catch {}
168                         Assert.AreEqual (TaskStatus.WaitingToRun, ts.ExecuteInlineStatus);
169                 }
170
171                 static int finalizerThreadId = -1;
172         
173                 class FinalizerCatcher
174                 {
175                         ~FinalizerCatcher ()
176                         {
177                                 finalizerThreadId = Thread.CurrentThread.ManagedThreadId;
178                         }
179                 }
180
181                 [Test]
182                 public void DefaultBehaviourTest ()
183                 {
184                         var s = new DefaultScheduler ();
185                         s.TestDefaultMethod ();
186                 }
187
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
190                 [Test]
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 ()
195                 {
196                         var foo = new FinalizerCatcher ();
197                         foo = null;
198                         GC.Collect ();
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)
202                                 return;
203
204                         int evtThreadId = -2;
205                         TaskScheduler.UnobservedTaskException += delegate {
206                                 evtThreadId = Thread.CurrentThread.ManagedThreadId;
207                         };
208                         var evt = new ManualResetEventSlim ();
209                         CreateAndForgetFaultedTask (evt);
210                         evt.Wait (500);
211                         Thread.Sleep (100);
212                         GC.Collect ();
213                         GC.WaitForPendingFinalizers ();
214                         Assert.AreEqual (finalizerThreadId, evtThreadId, "Should be ran on finalizer thread");
215                 }
216
217                 [Test]
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 ()
222                 {
223                         bool ran = false;
224                         bool senderIsRight = false;
225                         UnobservedTaskExceptionEventArgs args = null;
226
227                         TaskScheduler.UnobservedTaskException += (o, a) => {
228                                 senderIsRight = o.GetType ().ToString () == "System.Threading.Tasks.Task";
229                                 args = a;
230                                 ran = true;
231                         };
232
233                         var evt = new ManualResetEventSlim ();
234                         CreateAndForgetFaultedTask (evt);
235                         evt.Wait (500);
236                         Thread.Sleep (100);
237                         GC.Collect ();
238                         GC.WaitForPendingFinalizers ();
239
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 (♥)
242                         if (!ran)
243                                 return;
244
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");
250                 }
251
252                 // We use this intermediary method to improve chances of GC kicking
253                 static void CreateAndForgetFaultedTask (ManualResetEventSlim evt)
254                 {
255                         Task.Factory.StartNew (() => { evt.Set (); throw new Exception ("foo"); });
256                 }
257         }
258 }
259 #endif