5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Threading;
33 using NUnit.Framework;
35 namespace MonoTests.System.Threading {
38 public class WaitHandleTest {
40 TimeSpan Infinite = new TimeSpan (-10000); // -10000 ticks == -1 ms
41 TimeSpan SmallNegative = new TimeSpan (-2); // between 0 and -1.0 (infinite) ms
42 TimeSpan Negative = new TimeSpan (-20000); // really negative
44 WaitHandle [] TooLarge = new Mutex [65];
45 WaitHandle [] Empty = new Mutex [1];
46 WaitHandle [] Single = new Mutex [1] { new Mutex (true) };
50 [ExpectedException (typeof (ArgumentNullException))]
51 public void WaitAny_WaitHandle_Null ()
53 WaitHandle.WaitAny (null);
57 [ExpectedException (typeof (NotSupportedException))]
58 public void WaitAny_WaitHandle_TooLarge ()
60 WaitHandle.WaitAny (TooLarge);
64 [ExpectedException (typeof (ArgumentNullException))]
65 public void WaitAny_WaitHandle_Empty ()
67 WaitHandle.WaitAny (Empty);
71 public void WaitAny_WaitHandle ()
73 Assert.AreEqual (0, WaitHandle.WaitAny (Single), "WaitAny");
77 [ExpectedException (typeof (ArgumentNullException))]
78 public void WaitAny_WaitHandleNull_Int ()
80 WaitHandle.WaitAny (null, -1);
84 [ExpectedException (typeof (NotSupportedException))]
85 public void WaitAny_WaitHandle_TooLarge_Int ()
87 WaitHandle.WaitAny (TooLarge, -1);
91 [ExpectedException (typeof (ArgumentNullException))]
92 public void WaitAny_WaitHandle_Empty_Int ()
94 WaitHandle.WaitAny (Empty, -1);
98 public void WaitAny_WaitHandle_Int ()
101 Assert.AreEqual (0, WaitHandle.WaitAny (Single, -1), "WaitAny");
105 [ExpectedException (typeof (ArgumentOutOfRangeException))]
106 public void WaitAny_WaitHandle_Int_Negative ()
108 Assert.AreEqual (0, WaitHandle.WaitAny (Single, -2), "WaitAny");
112 [ExpectedException (typeof (ArgumentNullException))]
113 public void WaitAny_WaitHandleNull_TimeSpan ()
115 WaitHandle.WaitAny (null, Infinite);
119 [ExpectedException (typeof (NotSupportedException))]
120 public void WaitAny_WaitHandle_TooLarge_TimeSpan ()
122 WaitHandle.WaitAny (TooLarge, Infinite);
126 [ExpectedException (typeof (ArgumentNullException))]
127 public void WaitAny_WaitHandle_Empty_TimeSpan ()
129 WaitHandle.WaitAny (Empty, Infinite);
133 public void WaitAny_WaitHandle_TimeSpan ()
135 Assert.AreEqual (Timeout.Infinite, (int) Infinite.TotalMilliseconds, "Infinite");
136 Assert.AreEqual (0, WaitHandle.WaitAny (Single, Infinite), "WaitAny-Infinite");
137 Assert.AreEqual (0, WaitHandle.WaitAny (Single, SmallNegative), "WaitAny-SmallNegative");
141 [ExpectedException (typeof (ArgumentOutOfRangeException))]
142 public void WaitAny_WaitHandle_TimeSpan_Negative ()
144 Assert.AreEqual (0, WaitHandle.WaitAny (Single, Negative), "WaitAny");
148 [ExpectedException (typeof (ArgumentOutOfRangeException))]
149 public void WaitAny_WaitHandle_TimeSpan_MaxValue ()
151 Assert.AreEqual (0, WaitHandle.WaitAny (Single, TimeSpan.MaxValue), "WaitAny");
156 [ExpectedException (typeof (ArgumentNullException))]
157 public void WaitAll_WaitHandle_Null ()
159 WaitHandle.WaitAll (null);
163 [ExpectedException (typeof (NotSupportedException))]
164 public void WaitAll_WaitHandle_TooLarge ()
166 WaitHandle.WaitAll (TooLarge);
170 [ExpectedException (typeof (ArgumentNullException))]
171 public void WaitAll_WaitHandle_Empty ()
173 WaitHandle.WaitAll (Empty);
177 public void WaitAll_WaitHandle ()
179 Assert.IsTrue (WaitHandle.WaitAll (Single), "WaitAll");
183 [ExpectedException (typeof (ArgumentNullException))]
184 public void WaitAll_WaitHandleNull_Int ()
186 WaitHandle.WaitAll (null, -1);
190 [ExpectedException (typeof (NotSupportedException))]
191 public void WaitAll_WaitHandle_TooLarge_Int ()
193 WaitHandle.WaitAll (TooLarge, -1);
197 [ExpectedException (typeof (ArgumentNullException))]
198 public void WaitAll_WaitHandle_Empty_Int ()
200 WaitHandle.WaitAll (Empty, -1);
204 public void WaitAll_WaitHandle_Int ()
207 Assert.IsTrue (WaitHandle.WaitAll (Single, -1), "WaitAll");
211 [ExpectedException (typeof (ArgumentOutOfRangeException))]
212 public void WaitAll_WaitHandle_Int_Negative ()
214 Assert.IsTrue (WaitHandle.WaitAll (Single, -2), "WaitAll");
218 [ExpectedException (typeof (ArgumentNullException))]
219 public void WaitAll_WaitHandleNull_TimeSpan ()
221 WaitHandle.WaitAll (null, Infinite);
225 [ExpectedException (typeof (NotSupportedException))]
226 public void WaitAll_WaitHandle_TooLarge_TimeSpan ()
228 WaitHandle.WaitAll (TooLarge, Infinite);
232 [ExpectedException (typeof (ArgumentNullException))]
233 public void WaitAll_WaitHandle_Empty_TimeSpan ()
235 WaitHandle.WaitAll (Empty, Infinite);
239 public void WaitAll_WaitHandle_TimeSpan ()
241 Assert.AreEqual (Timeout.Infinite, (int) Infinite.TotalMilliseconds, "Infinite");
242 Assert.IsTrue (WaitHandle.WaitAll (Single, Infinite), "WaitAll-Infinite");
243 Assert.IsTrue (WaitHandle.WaitAll (Single, SmallNegative), "WaitAll-SmallNegative");
247 [ExpectedException (typeof (ArgumentOutOfRangeException))]
248 public void WaitAll_WaitHandle_TimeSpan_Negative ()
250 Assert.IsTrue (WaitHandle.WaitAll (Single, Negative), "WaitAll");
254 [ExpectedException (typeof (ArgumentOutOfRangeException))]
255 public void WaitAll_WaitHandle_TimeSpan_MaxValue ()
257 Assert.IsTrue (WaitHandle.WaitAll (Single, TimeSpan.MaxValue), "WaitAll");
262 public void WaitOne ()
264 Assert.IsTrue (Single [0].WaitOne (), "WaitOne");
268 public void WaitOne_Int ()
271 Assert.IsTrue (Single [0].WaitOne (-1), "WaitOne");
275 [ExpectedException (typeof (ArgumentOutOfRangeException))]
276 public void WaitOne_Int_Negative ()
278 Assert.IsTrue (Single [0].WaitOne (-2), "WaitOne");
282 public void WaitOne_TimeSpan ()
284 Assert.AreEqual (Timeout.Infinite, (int) Infinite.TotalMilliseconds, "Infinite");
285 Assert.IsTrue (Single [0].WaitOne (Infinite), "WaitOne-Infinite");
286 Assert.IsTrue (Single [0].WaitOne (SmallNegative), "WaitOne-SmallNegative");
290 [ExpectedException (typeof (ArgumentOutOfRangeException))]
291 public void WaitOne_TimeSpan_Negative ()
293 Assert.IsTrue (Single [0].WaitOne (Negative), "WaitOne");
297 [ExpectedException (typeof (ArgumentOutOfRangeException))]
298 public void WaitOne_TimeSpan_MaxValue ()
300 Assert.IsTrue (Single [0].WaitOne (TimeSpan.MaxValue), "WaitOne");
304 [ExpectedException (typeof (ArgumentNullException))]
305 public void WaitAll_Empty ()
307 WaitHandle.WaitAll (new WaitHandle [0]);
311 [ExpectedException (typeof (ArgumentException))]
312 public void WaitAny_Empty ()
314 WaitHandle.WaitAny (new WaitHandle [0]);
318 public void InterrupedWaitAny ()
320 using (var m1 = new Mutex (true)) {
321 using (var m2 = new Mutex (true)) {
322 using (var done = new ManualResetEvent (false)) {
323 var thread = new Thread (() =>
326 WaitHandle.WaitAny (new WaitHandle [] { m1, m2 });
327 } catch (ThreadInterruptedException) {
332 Thread.Sleep (100); // wait a bit so the thread can enter its wait
335 Assert.IsTrue (thread.Join (1000), "Join");
336 Assert.IsTrue (done.WaitOne (1000), "done");
346 public void InterrupedWaitAll ()
348 using (var m1 = new Mutex (true)) {
349 using (var m2 = new Mutex (true)) {
350 using (var done = new ManualResetEvent (false)) {
351 var thread = new Thread (() =>
354 WaitHandle.WaitAll (new WaitHandle [] { m1, m2 });
355 } catch (ThreadInterruptedException) {
360 Thread.Sleep (100); // wait a bit so the thread can enter its wait
363 Assert.IsTrue (thread.Join (1000), "Join");
364 Assert.IsTrue (done.WaitOne (1000), "done");
374 public void InterrupedWaitOne ()
376 using (var m1 = new Mutex (true)) {
377 using (var done = new ManualResetEvent (false)) {
378 var thread = new Thread (() =>
382 } catch (ThreadInterruptedException) {
387 Thread.Sleep (100); // wait a bit so the thread can enter its wait
390 Assert.IsTrue (thread.Join (1000), "Join");
391 Assert.IsTrue (done.WaitOne (1000), "done");