Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / class / corlib / Test / System.Threading / WaitHandleTest.cs
1 //
2 // WaitHandleTest.cs
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 #if NET_2_0
30
31 using System;
32 using System.Threading;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Threading {
37
38         [TestFixture]
39         public class WaitHandleTest {
40
41                 TimeSpan Infinite = new TimeSpan (-10000);      // -10000 ticks == -1 ms
42                 TimeSpan SmallNegative = new TimeSpan (-2);     // between 0 and -1.0 (infinite) ms
43                 TimeSpan Negative = new TimeSpan (-20000);      // really negative
44
45                 WaitHandle [] TooLarge = new Mutex [65];
46                 WaitHandle [] Empty = new Mutex [1];
47                 WaitHandle [] Single = new Mutex [1] { new Mutex (true) };
48
49
50                 [Test]
51                 [ExpectedException (typeof (ArgumentNullException))]
52                 public void WaitAny_WaitHandle_Null ()
53                 {
54                         WaitHandle.WaitAny (null);
55                 }
56
57                 [Test]
58                 [ExpectedException (typeof (NotSupportedException))]
59                 public void WaitAny_WaitHandle_TooLarge ()
60                 {
61                         WaitHandle.WaitAny (TooLarge);
62                 }
63
64                 [Test]
65                 [ExpectedException (typeof (ArgumentNullException))]
66                 public void WaitAny_WaitHandle_Empty ()
67                 {
68                         WaitHandle.WaitAny (Empty);
69                 }
70
71                 [Test]
72                 public void WaitAny_WaitHandle ()
73                 {
74                         Assert.AreEqual (0, WaitHandle.WaitAny (Single), "WaitAny");
75                 }
76
77                 [Test]
78                 [ExpectedException (typeof (ArgumentNullException))]
79                 public void WaitAny_WaitHandleNull_Int ()
80                 {
81                         WaitHandle.WaitAny (null, -1);
82                 }
83
84                 [Test]
85                 [ExpectedException (typeof (NotSupportedException))]
86                 public void WaitAny_WaitHandle_TooLarge_Int ()
87                 {
88                         WaitHandle.WaitAny (TooLarge, -1);
89                 }
90
91                 [Test]
92                 [ExpectedException (typeof (ArgumentNullException))]
93                 public void WaitAny_WaitHandle_Empty_Int ()
94                 {
95                         WaitHandle.WaitAny (Empty, -1);
96                 }
97
98                 [Test]
99                 public void WaitAny_WaitHandle_Int ()
100                 {
101                         // -1 is infinite
102                         Assert.AreEqual (0, WaitHandle.WaitAny (Single, -1), "WaitAny");
103                 }
104
105                 [Test]
106                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
107                 public void WaitAny_WaitHandle_Int_Negative ()
108                 {
109                         Assert.AreEqual (0, WaitHandle.WaitAny (Single, -2), "WaitAny");
110                 }
111
112                 [Test]
113                 [ExpectedException (typeof (ArgumentNullException))]
114                 public void WaitAny_WaitHandleNull_TimeSpan ()
115                 {
116                         WaitHandle.WaitAny (null, Infinite);
117                 }
118
119                 [Test]
120                 [ExpectedException (typeof (NotSupportedException))]
121                 public void WaitAny_WaitHandle_TooLarge_TimeSpan ()
122                 {
123                         WaitHandle.WaitAny (TooLarge, Infinite);
124                 }
125
126                 [Test]
127                 [ExpectedException (typeof (ArgumentNullException))]
128                 public void WaitAny_WaitHandle_Empty_TimeSpan ()
129                 {
130                         WaitHandle.WaitAny (Empty, Infinite);
131                 }
132
133                 [Test]
134                 public void WaitAny_WaitHandle_TimeSpan ()
135                 {
136                         Assert.AreEqual (Timeout.Infinite, (int) Infinite.TotalMilliseconds, "Infinite");
137                         Assert.AreEqual (0, WaitHandle.WaitAny (Single, Infinite), "WaitAny-Infinite");
138                         Assert.AreEqual (0, WaitHandle.WaitAny (Single, SmallNegative), "WaitAny-SmallNegative");
139                 }
140
141                 [Test]
142                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
143                 public void WaitAny_WaitHandle_TimeSpan_Negative ()
144                 {
145                         Assert.AreEqual (0, WaitHandle.WaitAny (Single, Negative), "WaitAny");
146                 }
147
148                 [Test]
149                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
150                 public void WaitAny_WaitHandle_TimeSpan_MaxValue ()
151                 {
152                         Assert.AreEqual (0, WaitHandle.WaitAny (Single, TimeSpan.MaxValue), "WaitAny");
153                 }
154
155
156                 [Test]
157                 [ExpectedException (typeof (ArgumentNullException))]
158                 public void WaitAll_WaitHandle_Null ()
159                 {
160                         WaitHandle.WaitAll (null);
161                 }
162
163                 [Test]
164                 [ExpectedException (typeof (NotSupportedException))]
165                 public void WaitAll_WaitHandle_TooLarge ()
166                 {
167                         WaitHandle.WaitAll (TooLarge);
168                 }
169
170                 [Test]
171                 [ExpectedException (typeof (ArgumentNullException))]
172                 public void WaitAll_WaitHandle_Empty ()
173                 {
174                         WaitHandle.WaitAll (Empty);
175                 }
176
177                 [Test]
178                 public void WaitAll_WaitHandle ()
179                 {
180                         Assert.IsTrue (WaitHandle.WaitAll (Single), "WaitAll");
181                 }
182
183                 [Test]
184                 [ExpectedException (typeof (ArgumentNullException))]
185                 public void WaitAll_WaitHandleNull_Int ()
186                 {
187                         WaitHandle.WaitAll (null, -1);
188                 }
189
190                 [Test]
191                 [ExpectedException (typeof (NotSupportedException))]
192                 public void WaitAll_WaitHandle_TooLarge_Int ()
193                 {
194                         WaitHandle.WaitAll (TooLarge, -1);
195                 }
196
197                 [Test]
198                 [ExpectedException (typeof (ArgumentNullException))]
199                 public void WaitAll_WaitHandle_Empty_Int ()
200                 {
201                         WaitHandle.WaitAll (Empty, -1);
202                 }
203
204                 [Test]
205                 public void WaitAll_WaitHandle_Int ()
206                 {
207                         // -1 is infinite
208                         Assert.IsTrue (WaitHandle.WaitAll (Single, -1), "WaitAll");
209                 }
210
211                 [Test]
212                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
213                 public void WaitAll_WaitHandle_Int_Negative ()
214                 {
215                         Assert.IsTrue (WaitHandle.WaitAll (Single, -2), "WaitAll");
216                 }
217
218                 [Test]
219                 [ExpectedException (typeof (ArgumentNullException))]
220                 public void WaitAll_WaitHandleNull_TimeSpan ()
221                 {
222                         WaitHandle.WaitAll (null, Infinite);
223                 }
224
225                 [Test]
226                 [ExpectedException (typeof (NotSupportedException))]
227                 public void WaitAll_WaitHandle_TooLarge_TimeSpan ()
228                 {
229                         WaitHandle.WaitAll (TooLarge, Infinite);
230                 }
231
232                 [Test]
233                 [ExpectedException (typeof (ArgumentNullException))]
234                 public void WaitAll_WaitHandle_Empty_TimeSpan ()
235                 {
236                         WaitHandle.WaitAll (Empty, Infinite);
237                 }
238
239                 [Test]
240                 public void WaitAll_WaitHandle_TimeSpan ()
241                 {
242                         Assert.AreEqual (Timeout.Infinite, (int) Infinite.TotalMilliseconds, "Infinite");
243                         Assert.IsTrue (WaitHandle.WaitAll (Single, Infinite), "WaitAll-Infinite");
244                         Assert.IsTrue (WaitHandle.WaitAll (Single, SmallNegative), "WaitAll-SmallNegative");
245                 }
246
247                 [Test]
248                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
249                 public void WaitAll_WaitHandle_TimeSpan_Negative ()
250                 {
251                         Assert.IsTrue (WaitHandle.WaitAll (Single, Negative), "WaitAll");
252                 }
253
254                 [Test]
255                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
256                 public void WaitAll_WaitHandle_TimeSpan_MaxValue ()
257                 {
258                         Assert.IsTrue (WaitHandle.WaitAll (Single, TimeSpan.MaxValue), "WaitAll");
259                 }
260
261
262                 [Test]
263                 public void WaitOne ()
264                 {
265                         Assert.IsTrue (Single [0].WaitOne (), "WaitOne");
266                 }
267
268                 [Test]
269                 public void WaitOne_Int ()
270                 {
271                         // -1 is infinite
272                         Assert.IsTrue (Single [0].WaitOne (-1), "WaitOne");
273                 }
274
275                 [Test]
276                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
277                 public void WaitOne_Int_Negative ()
278                 {
279                         Assert.IsTrue (Single [0].WaitOne (-2), "WaitOne");
280                 }
281
282                 [Test]
283                 public void WaitOne_TimeSpan ()
284                 {
285                         Assert.AreEqual (Timeout.Infinite, (int) Infinite.TotalMilliseconds, "Infinite");
286                         Assert.IsTrue (Single [0].WaitOne (Infinite), "WaitOne-Infinite");
287                         Assert.IsTrue (Single [0].WaitOne (SmallNegative), "WaitOne-SmallNegative");
288                 }
289
290                 [Test]
291                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
292                 public void WaitOne_TimeSpan_Negative ()
293                 {
294                         Assert.IsTrue (Single [0].WaitOne (Negative), "WaitOne");
295                 }
296
297                 [Test]
298                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
299                 public void WaitOne_TimeSpan_MaxValue ()
300                 {
301                         Assert.IsTrue (Single [0].WaitOne (TimeSpan.MaxValue), "WaitOne");
302                 }
303
304                 [Test]
305                 [ExpectedException (typeof (ArgumentNullException))]
306                 public void WaitAll_Empty ()
307                 {
308                         WaitHandle.WaitAll (new WaitHandle [0]);
309                 }
310
311                 [Test]
312                 [ExpectedException (typeof (ArgumentException))]
313                 public void WaitAny_Empty ()
314                 {
315                         WaitHandle.WaitAny (new WaitHandle [0]);
316                 }
317
318                 [Test]
319                 public void InterrupedWaitAny ()
320                 {
321                         using (var m1 = new Mutex (true)) {
322                                 using (var m2 = new Mutex (true)) {
323                                         using (var done = new ManualResetEvent (false)) {
324                                                 var thread = new Thread (() =>
325                                                 {
326                                                         try {
327                                                                 WaitHandle.WaitAny (new WaitHandle [] { m1, m2 });
328                                                         } catch (ThreadInterruptedException) {
329                                                                 done.Set ();
330                                                         }
331                                                 });
332                                                 thread.Start ();
333                                                 Thread.Sleep (100); // wait a bit so the thread can enter its wait
334                                                 thread.Interrupt ();
335
336                                                 Assert.IsTrue (thread.Join (1000), "Join");
337                                                 Assert.IsTrue (done.WaitOne (1000), "done");
338
339                                                 m1.ReleaseMutex ();
340                                                 m2.ReleaseMutex ();
341                                         }
342                                 }
343                         }
344                 }
345                 
346                 [Test]
347                 public void InterrupedWaitAll ()
348                 {
349                         using (var m1 = new Mutex (true)) {
350                                 using (var m2 = new Mutex (true)) {
351                                         using (var done = new ManualResetEvent (false)) {
352                                                 var thread = new Thread (() =>
353                                                                          {
354                                                         try {
355                                                                 WaitHandle.WaitAll (new WaitHandle [] { m1, m2 });
356                                                         } catch (ThreadInterruptedException) {
357                                                                 done.Set ();
358                                                         }
359                                                 });
360                                                 thread.Start ();
361                                                 Thread.Sleep (100); // wait a bit so the thread can enter its wait
362                                                 thread.Interrupt ();
363
364                                                 Assert.IsTrue (thread.Join (1000), "Join");
365                                                 Assert.IsTrue (done.WaitOne (1000), "done");
366
367                                                 m1.ReleaseMutex ();
368                                                 m2.ReleaseMutex ();
369                                         }
370                                 }
371                         }
372                 }
373                 
374                 [Test]
375                 public void InterrupedWaitOne ()
376                 {
377                         using (var m1 = new Mutex (true)) {
378                                 using (var done = new ManualResetEvent (false)) {
379                                         var thread = new Thread (() =>
380                                                                  {
381                                                 try {
382                                                         m1.WaitOne ();
383                                                 } catch (ThreadInterruptedException) {
384                                                         done.Set ();
385                                                 }
386                                         });
387                                         thread.Start ();
388                                         Thread.Sleep (100); // wait a bit so the thread can enter its wait
389                                         thread.Interrupt ();
390
391                                         Assert.IsTrue (thread.Join (1000), "Join");
392                                         Assert.IsTrue (done.WaitOne (1000), "done");
393
394                                         m1.ReleaseMutex ();
395                                 }
396                         }
397                 }
398
399         }
400 }
401
402 #endif
403