[corlib] Fix tests to compile when Thread.Suspend/Resume is not supported.
[mono.git] / mcs / class / corlib / Test / System.Threading / WaitHandleTest.cs
index 5f8056e67ce9d0bed34ae4ee8bab8def7e131b31..2fc0a6dd01d6963dfb8ae777ca494622d2dcee12 100644 (file)
@@ -395,6 +395,96 @@ namespace MonoTests.System.Threading {
                        }
                }
 
+#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
        }
 }