[corlib] Fix tests to compile when Thread.Suspend/Resume is not supported.
[mono.git] / mcs / class / corlib / Test / System.Threading / WaitHandleTest.cs
index 47289d77d01136ca36a77cfd432b9f44b6b920b8..2fc0a6dd01d6963dfb8ae777ca494622d2dcee12 100644 (file)
@@ -26,7 +26,6 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
 
 using System;
 using System.Threading;
@@ -315,8 +314,178 @@ namespace MonoTests.System.Threading {
                        WaitHandle.WaitAny (new WaitHandle [0]);
                }
 
+               [Test]
+               public void InterrupedWaitAny ()
+               {
+                       using (var m1 = new Mutex (true)) {
+                               using (var m2 = new Mutex (true)) {
+                                       using (var done = new ManualResetEvent (false)) {
+                                               var thread = new Thread (() =>
+                                               {
+                                                       try {
+                                                               WaitHandle.WaitAny (new WaitHandle [] { m1, m2 });
+                                                       } catch (ThreadInterruptedException) {
+                                                               done.Set ();
+                                                       }
+                                               });
+                                               thread.Start ();
+                                               Thread.Sleep (100); // wait a bit so the thread can enter its wait
+                                               thread.Interrupt ();
+
+                                               Assert.IsTrue (thread.Join (1000), "Join");
+                                               Assert.IsTrue (done.WaitOne (1000), "done");
+
+                                               m1.ReleaseMutex ();
+                                               m2.ReleaseMutex ();
+                                       }
+                               }
+                       }
+               }
+               
+               [Test]
+               public void InterrupedWaitAll ()
+               {
+                       using (var m1 = new Mutex (true)) {
+                               using (var m2 = new Mutex (true)) {
+                                       using (var done = new ManualResetEvent (false)) {
+                                               var thread = new Thread (() =>
+                                                                        {
+                                                       try {
+                                                               WaitHandle.WaitAll (new WaitHandle [] { m1, m2 });
+                                                       } catch (ThreadInterruptedException) {
+                                                               done.Set ();
+                                                       }
+                                               });
+                                               thread.Start ();
+                                               Thread.Sleep (100); // wait a bit so the thread can enter its wait
+                                               thread.Interrupt ();
+
+                                               Assert.IsTrue (thread.Join (1000), "Join");
+                                               Assert.IsTrue (done.WaitOne (1000), "done");
+
+                                               m1.ReleaseMutex ();
+                                               m2.ReleaseMutex ();
+                                       }
+                               }
+                       }
+               }
+               
+               [Test]
+               public void InterrupedWaitOne ()
+               {
+                       using (var m1 = new Mutex (true)) {
+                               using (var done = new ManualResetEvent (false)) {
+                                       var thread = new Thread (() =>
+                                                                {
+                                               try {
+                                                       m1.WaitOne ();
+                                               } catch (ThreadInterruptedException) {
+                                                       done.Set ();
+                                               }
+                                       });
+                                       thread.Start ();
+                                       Thread.Sleep (100); // wait a bit so the thread can enter its wait
+                                       thread.Interrupt ();
+
+                                       Assert.IsTrue (thread.Join (1000), "Join");
+                                       Assert.IsTrue (done.WaitOne (1000), "done");
+
+                                       m1.ReleaseMutex ();
+                               }
+                       }
+               }
+
+#if MONO_FEATURE_THREAD_SUSPEND_RESUME
+               [Test]
+               public void WaitOneWithTimeoutAndSpuriousWake ()
+               {
+                       /* This is to test that WaitEvent.WaitOne is not going to wait largely
+                        * more than its timeout. In this test, it shouldn't wait more than
+                        * 1500 milliseconds, with its timeout being 1000ms */
+
+                       using (ManualResetEvent mre = new ManualResetEvent (false))
+                       using (ManualResetEvent ready = new ManualResetEvent (false)) {
+                               var thread = new Thread (() => {
+                                       ready.Set ();
+                                       mre.WaitOne (1000);
+                               });
+
+                               thread.Start ();
+                               ready.WaitOne ();
+
+                               Thread.Sleep (10); // wait a bit so we enter mre.WaitOne
+
+                               DateTime end = DateTime.Now.AddMilliseconds (500);
+                               while (DateTime.Now < end) {
+                                       thread.Suspend ();
+                                       thread.Resume ();
+                               }
+
+                               Assert.IsTrue (thread.Join (1000), "#1");
+                       }
+               }
+
+               [Test]
+               public void WaitAnyWithTimeoutAndSpuriousWake ()
+               {
+                       /* This is to test that WaitEvent.WaitAny is not going to wait largely
+                        * more than its timeout. In this test, it shouldn't wait more than
+                        * 1500 milliseconds, with its timeout being 1000ms */
+
+                       using (ManualResetEvent mre1 = new ManualResetEvent (false))
+                       using (ManualResetEvent mre2 = new ManualResetEvent (false))
+                       using (ManualResetEvent ready = new ManualResetEvent (false)) {
+                               var thread = new Thread (() => {
+                                       ready.Set ();
+                                       WaitHandle.WaitAny (new [] { mre1, mre2 }, 1000);
+                               });
+
+                               thread.Start ();
+                               ready.WaitOne ();
+
+                               Thread.Sleep (10); // wait a bit so we enter WaitHandle.WaitAny ({mre1, mre2})
+
+                               DateTime end = DateTime.Now.AddMilliseconds (500);
+                               while (DateTime.Now < end) {
+                                       thread.Suspend ();
+                                       thread.Resume ();
+                               }
+
+                               Assert.IsTrue (thread.Join (1000), "#1");
+                       }
+               }
+
+               [Test]
+               public void WaitAllWithTimeoutAndSpuriousWake ()
+               {
+                       /* This is to test that WaitEvent.WaitAll is not going to wait largely
+                        * more than its timeout. In this test, it shouldn't wait more than
+                        * 1500 milliseconds, with its timeout being 1000ms */
+
+                       using (ManualResetEvent mre1 = new ManualResetEvent (false))
+                       using (ManualResetEvent mre2 = new ManualResetEvent (false))
+                       using (ManualResetEvent ready = new ManualResetEvent (false)) {
+                               var thread = new Thread (() => {
+                                       ready.Set ();
+                                       WaitHandle.WaitAll (new [] { mre1, mre2 }, 1000);
+                               });
+
+                               thread.Start ();
+                               ready.WaitOne ();
+
+                               Thread.Sleep (10); // wait a bit so we enter WaitHandle.WaitAll ({mre1, mre2})
+
+                               DateTime end = DateTime.Now.AddMilliseconds (500);
+                               while (DateTime.Now < end) {
+                                       thread.Suspend ();
+                                       thread.Resume ();
+                               }
+
+                               Assert.IsTrue (thread.Join (1000), "#1");
+                       }
+               }
+#endif // MONO_FEATURE_THREAD_SUSPEND_RESUME
        }
 }
 
-#endif