TaskScheduler.FromCurrentSynchronizationContext cannot be constructed from null context
[mono.git] / mcs / class / corlib / Test / System.Threading.Tasks / TaskSchedulerTest.cs
index 4efa13e34809757e52b6a3c36da7b7f1f0aec128..7c697a528282c92e54940bec39ec0741b4c0a2ca 100644 (file)
@@ -36,22 +36,10 @@ namespace MonoTests.System.Threading.Tasks
        [TestFixture]
        public class TaskSchedulerTests
        {
-               [Test]
-               public void BasicRunSynchronouslyTest ()
-               {
-                       bool ran = false;
-                       var t = new Task (() => ran = true);
-
-                       t.RunSynchronously ();
-                       Assert.IsTrue (t.IsCompleted);
-                       Assert.IsFalse (t.IsFaulted);
-                       Assert.IsFalse (t.IsCanceled);
-                       Assert.IsTrue (ran);
-               }
-
                class LazyCatScheduler : TaskScheduler
                {
-                       public TaskStatus ExecuteInlineStatus {
+                       public TaskStatus ExecuteInlineStatus
+                       {
                                get;
                                set;
                        }
@@ -78,6 +66,56 @@ namespace MonoTests.System.Threading.Tasks
                        }
                }
 
+               class DefaultScheduler : TaskScheduler
+               {
+                       protected override IEnumerable<Task> GetScheduledTasks ()
+                       {
+                               throw new NotImplementedException ();
+                       }
+
+                       protected override void QueueTask (Task task)
+                       {
+                               throw new NotImplementedException ();
+                       }
+
+                       protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
+                       {
+                               throw new NotImplementedException ();
+                       }
+
+                       public void TestDefaultMethod ()
+                       {
+                               Assert.IsFalse (TryDequeue (null), "#1");
+                       }
+               }
+
+               [Test]
+               public void FromCurrentSynchronizationContextTest_Invalid()
+               {
+                       var c = SynchronizationContext.Current;
+                       try {
+                               SynchronizationContext.SetSynchronizationContext (null);
+                               TaskScheduler.FromCurrentSynchronizationContext ();
+                               Assert.Fail ("#1");
+                       } catch (InvalidOperationException) {
+                       } finally {
+                               SynchronizationContext.SetSynchronizationContext (c);
+                       }
+               }
+
+               [Test]
+               public void BasicRunSynchronouslyTest ()
+               {
+                       bool ran = false;
+                       var t = new Task (() => ran = true);
+
+                       t.RunSynchronously ();
+                       Assert.IsTrue (t.IsCompleted);
+                       Assert.IsFalse (t.IsFaulted);
+                       Assert.IsFalse (t.IsCanceled);
+                       Assert.IsTrue (ran);
+               }
+
                [Test]
                public void RunSynchronouslyButNoExecutionTest ()
                {
@@ -119,6 +157,13 @@ namespace MonoTests.System.Threading.Tasks
                        }
                }
 
+               [Test]
+               public void DefaultBehaviourTest ()
+               {
+                       var s = new DefaultScheduler ();
+                       s.TestDefaultMethod ();
+               }
+
                // This test doesn't work if the GC uses multiple finalizer thread.
                // For now it's fine since only one thread is used
                [Test]