Merge branch 'atsushi'
[mono.git] / mcs / class / corlib / Test / System.Threading / MonitorTest.cs
1 //
2 // MonitorTest.cs - NUnit test cases for System.Threading.Monitor
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //      Sebastien Pouliot (sebastien@ximian.com)
7 //
8 // Copyright (C) 2005, 2009 Novell, Inc (http://www.novell.com)
9 //
10
11 using NUnit.Framework;
12 using System;
13 using System.Threading;
14
15 namespace MonoTests.System.Threading {
16
17         [TestFixture]
18         public class MonitorTest {
19
20                 TimeSpan Infinite = new TimeSpan (-10000);      // -10000 ticks == -1 ms
21                 TimeSpan SmallNegative = new TimeSpan (-2);     // between 0 and -1.0 (infinite) ms
22                 TimeSpan Negative = new TimeSpan (-20000);      // really negative
23                 TimeSpan MaxValue = TimeSpan.FromMilliseconds ((long) Int32.MaxValue);
24                 TimeSpan TooLarge = TimeSpan.FromMilliseconds ((long) Int32.MaxValue + 1);
25
26                 [Test]
27                 [ExpectedException (typeof (SynchronizationLockException))]
28                 [Category ("NotWorking")] // test fails under MS FX 2.0 - maybe that worked on 1.x ?
29                 public void ExitNoEnter ()
30                 {
31                         object o = new object ();
32                         Monitor.Exit (o);
33                 }
34
35                 [Test]
36                 [ExpectedException (typeof (SynchronizationLockException))]
37                 [Category ("NotWorking")] // test fails under MS FX 2.0 - maybe that worked on 1.x ?
38                 public void OneEnterSeveralExits ()
39                 {
40                         object o = new object ();
41                         Monitor.Enter (o);
42                         Monitor.Exit (o);
43                         // fails here
44                         Monitor.Exit (o);
45                         Monitor.Exit (o);
46                         Monitor.Exit (o);
47                 }
48
49                 [Test]
50                 [ExpectedException (typeof (ArgumentNullException))]
51                 public void Enter_Null ()
52                 {
53                         Monitor.Enter (null);
54                 }
55
56                 [Test]
57                 [ExpectedException (typeof (ArgumentNullException))]
58                 public void Exit_Null ()
59                 {
60                         Monitor.Exit (null);
61                 }
62
63                 [Test]
64                 public void Enter_Exit ()
65                 {
66                         object o = new object ();
67                         Monitor.Enter (o);
68                         try {
69                                 Assert.IsNotNull (o);
70                         }
71                         finally {
72                                 Monitor.Exit (o);
73                         }
74                 }
75
76                 [Test]
77                 [ExpectedException (typeof (ArgumentNullException))]
78                 public void Pulse_Null ()
79                 {
80                         Monitor.Pulse (null);
81                 }
82
83                 [Test]
84                 [ExpectedException (typeof (SynchronizationLockException))]
85                 public void Pulse_Unlocked ()
86                 {
87                         object o = new object ();
88                         Monitor.Pulse (o);
89                 }
90
91                 [Test]
92                 public void Pulse ()
93                 {
94                         object o = new object ();
95                         lock (o) {
96                                 Monitor.Pulse (o);
97                         }
98                 }
99
100                 [Test]
101                 [ExpectedException (typeof (ArgumentNullException))]
102                 public void PulseAll_Null ()
103                 {
104                         Monitor.PulseAll (null);
105                 }
106
107                 [Test]
108                 [ExpectedException (typeof (SynchronizationLockException))]
109                 public void PulseAll_Unlocked ()
110                 {
111                         object o = new object ();
112                         Monitor.PulseAll (o);
113                 }
114
115                 [Test]
116                 public void PulseAll ()
117                 {
118                         object o = new object ();
119                         lock (o) {
120                                 Monitor.PulseAll (o);
121                         }
122                 }
123
124                 [Test]
125                 [ExpectedException (typeof (ArgumentNullException))]
126                 public void TryEnter_Null ()
127                 {
128                         Monitor.TryEnter (null);
129                 }
130
131                 [Test]
132                 public void TryEnter ()
133                 {
134                         object o = new object ();
135                         Assert.IsTrue (Monitor.TryEnter (o), "TryEnter");
136                         Assert.IsTrue (Monitor.TryEnter (o), "TryEnter-2");
137                 }
138
139                 [Test]
140                 [ExpectedException (typeof (ArgumentNullException))]
141                 public void TryEnter_Null_Int ()
142                 {
143                         Monitor.TryEnter (null, Timeout.Infinite);
144                 }
145
146                 [Test]
147                 [ExpectedException (typeof (ArgumentException))]
148                 public void TryEnter_Int_Negative ()
149                 {
150                         object o = new object ();
151                         Monitor.TryEnter (o, -2);
152                 }
153
154                 [Test]
155                 public void TryEnter_Int ()
156                 {
157                         object o = new object ();
158                         Assert.IsTrue (Monitor.TryEnter (o, 1), "TryEnter");
159                         Assert.IsTrue (Monitor.TryEnter (o, 2), "TryEnter-2");
160                 }
161
162                 [Test]
163                 [ExpectedException (typeof (ArgumentNullException))]
164                 public void TryEnter_Null_TimeSpan ()
165                 {
166                         Monitor.TryEnter (null, Timeout.Infinite);
167                 }
168
169                 [Test]
170                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
171                 public void TryEnter_TimeSpan_Negative ()
172                 {
173                         object o = new object ();
174                         Monitor.TryEnter (o, Negative);
175                 }
176
177                 [Test]
178                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
179                 public void TryEnter_TimeSpan_TooLarge ()
180                 {
181                         object o = new object ();
182                         Monitor.TryEnter (o, TooLarge);
183                 }
184
185                 [Test]
186                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
187                 public void TryEnter_Null_TimeSpan_TooLarge ()
188                 {
189                         // exception ordering test
190                         Monitor.TryEnter (null, TooLarge);
191                 }
192
193                 [Test]
194                 public void TryEnter_TimeSpan ()
195                 {
196                         object o = new object ();
197                         Assert.IsTrue (Monitor.TryEnter (o, Infinite), "TryEnter");
198                         Assert.IsTrue (Monitor.TryEnter (o, SmallNegative), "TryEnter-2");
199                         Assert.IsTrue (Monitor.TryEnter (o, MaxValue), "TryEnter-3");
200                 }
201
202
203                 [Test]
204                 [ExpectedException (typeof (ArgumentNullException))]
205                 public void Wait_Null ()
206                 {
207                         Monitor.Wait (null);
208                 }
209
210                 [Test]
211                 [ExpectedException (typeof (SynchronizationLockException))]
212                 public void Wait_Unlocked ()
213                 {
214                         object o = new object ();
215                         Assert.IsTrue (Monitor.Wait (o), "Wait");
216                 }
217
218                 // [Test] that would be Infinite
219                 public void Wait ()
220                 {
221                         object o = new object ();
222                         lock (o) {
223                                 Assert.IsFalse (Monitor.Wait (o), "Wait");
224                         }
225                 }
226
227                 [Test]
228                 [ExpectedException (typeof (ArgumentNullException))]
229                 public void Wait_Null_Int ()
230                 {
231                         Monitor.Wait (null, Timeout.Infinite);
232                 }
233
234                 [Test]
235                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
236                 public void Wait_Int_Negative ()
237                 {
238                         object o = new object ();
239                         Monitor.Wait (o, -2);
240                 }
241
242                 [Test]
243                 [ExpectedException (typeof (SynchronizationLockException))]
244                 public void Wait_Int_Unlocked ()
245                 {
246                         object o = new object ();
247                         Assert.IsTrue (Monitor.Wait (o, 1), "Wait");
248                 }
249
250                 [Test]
251                 public void Wait_Int ()
252                 {
253                         object o = new object ();
254                         lock (o) {
255                                 Assert.IsFalse (Monitor.Wait (o, 1), "Wait");
256                         }
257                 }
258
259                 [Test]
260                 [ExpectedException (typeof (ArgumentNullException))]
261                 public void Wait_Null_TimeSpan ()
262                 {
263                         Monitor.Wait (null, Timeout.Infinite);
264                 }
265
266                 [Test]
267                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
268                 public void Wait_TimeSpan_Negative ()
269                 {
270                         object o = new object ();
271                         Monitor.Wait (o, Negative);
272                 }
273
274                 [Test]
275                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
276                 public void Wait_TimeSpan_TooLarge ()
277                 {
278                         object o = new object ();
279                         Monitor.Wait (o, TooLarge);
280                 }
281
282                 [Test]
283                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
284                 public void Wait_Null_TimeSpan_TooLarge ()
285                 {
286                         // exception ordering test
287                         Monitor.Wait (null, TooLarge);
288                 }
289
290                 [Test]
291                 [ExpectedException (typeof (SynchronizationLockException))]
292                 public void Wait_TimeSpan_Unlocked ()
293                 {
294                         object o = new object ();
295                         Assert.IsTrue (Monitor.Wait (o, Infinite), "Wait");
296                 }
297
298                 [Test]
299                 public void Wait_TimeSpan ()
300                 {
301                         object o = new object ();
302                         lock (o) {
303                                 Assert.IsFalse (Monitor.Wait (o, SmallNegative), "Wait");
304                         }
305                 }
306 #if NET_4_0
307                 [Test]
308                 public void Enter_bool ()
309                 {
310                         object o = new object ();
311                         bool taken = false;
312                         Monitor.Enter (o, ref taken);
313                         Assert.IsTrue (taken, "Monitor.Enter (obj, ref taken)");
314                 }
315
316                 [Test]
317                 [ExpectedException (typeof (ArgumentException))]
318                 public void Enter_bool_argcheck ()
319                 {
320                         object o = new object ();
321                         bool taken = true;
322                         Monitor.Enter (o, ref taken);
323                 }
324
325 #endif
326
327         }
328 }
329