[corlib] Use ManualResetEvent.WaitOne instead of Thread.Sleep to test Thread.Interrupt
authorLudovic Henry <ludovic.henry@xamarin.com>
Tue, 30 Jun 2015 20:53:24 +0000 (17:53 -0300)
committerLudovic Henry <ludovic.henry@xamarin.com>
Tue, 14 Jul 2015 22:04:15 +0000 (19:04 -0300)
This is because Thread.Sleep does not use the thread interrupt mechanism, while WaitHandle.{WaitOne, WaitAll and WaitAny} do

mcs/class/corlib/Test/System.Threading/ThreadTest.cs

index 9bfc1c131eaa41121183797895077b5a4f308e8f..53921d2e13f30f80360c47ddd904bd3b26d20f8b 100644 (file)
@@ -800,15 +800,17 @@ namespace MonoTests.System.Threading
                [Test]
                public void Test_Interrupt ()
                {
+                       ManualResetEvent mre = new ManualResetEvent (false);
                        bool interruptedExceptionThrown = false;
+
                        ThreadPool.QueueUserWorkItem (Test_Interrupt_Worker, Thread.CurrentThread);
 
                        try {
                                try {
-                                       Thread.Sleep (3000);
+                                       mre.WaitOne (3000);
                                } finally {
                                        try {
-                                               Thread.Sleep (0);
+                                               mre.WaitOne (0);
                                        } catch (ThreadInterruptedException) {
                                                Assert.Fail ("ThreadInterruptedException thrown twice");
                                        }
@@ -838,11 +840,12 @@ namespace MonoTests.System.Threading
                [Category ("NotDotNet")] // it crashes nunit.
                public void Test_InterruptCurrentThread ()
                {
+                       ManualResetEvent mre = new ManualResetEvent (false);
                        bool interruptedExceptionThrown = false;
 
                        Thread.CurrentThread.Interrupt ();
                        try {
-                               Thread.Sleep (0);
+                               mre.WaitOne (0);
                                Assert.Fail ();
                        } catch (ThreadInterruptedException) {
                        }