2 // MonitorTest.cs - NUnit test cases for System.Threading.Monitor
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Sebastien Pouliot (sebastien@ximian.com)
8 // Copyright (C) 2005, 2009 Novell, Inc (http://www.novell.com)
11 using NUnit.Framework;
13 using System.Threading;
15 namespace MonoTests.System.Threading {
18 public class MonitorTest {
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);
27 [ExpectedException (typeof (SynchronizationLockException))]
28 [Category ("NotWorking")] // test fails under MS FX 2.0 - maybe that worked on 1.x ?
29 public void ExitNoEnter ()
31 object o = new object ();
36 [ExpectedException (typeof (SynchronizationLockException))]
37 [Category ("NotWorking")] // test fails under MS FX 2.0 - maybe that worked on 1.x ?
38 public void OneEnterSeveralExits ()
40 object o = new object ();
50 [ExpectedException (typeof (ArgumentNullException))]
51 public void Enter_Null ()
57 [ExpectedException (typeof (ArgumentNullException))]
58 public void Exit_Null ()
64 public void Enter_Exit ()
66 object o = new object ();
77 [ExpectedException (typeof (ArgumentNullException))]
78 public void Pulse_Null ()
84 [ExpectedException (typeof (SynchronizationLockException))]
85 public void Pulse_Unlocked ()
87 object o = new object ();
94 object o = new object ();
101 [ExpectedException (typeof (ArgumentNullException))]
102 public void PulseAll_Null ()
104 Monitor.PulseAll (null);
108 [ExpectedException (typeof (SynchronizationLockException))]
109 public void PulseAll_Unlocked ()
111 object o = new object ();
112 Monitor.PulseAll (o);
116 public void PulseAll ()
118 object o = new object ();
120 Monitor.PulseAll (o);
125 [ExpectedException (typeof (ArgumentNullException))]
126 public void TryEnter_Null ()
128 Monitor.TryEnter (null);
132 public void TryEnter ()
134 object o = new object ();
135 Assert.IsTrue (Monitor.TryEnter (o), "TryEnter");
136 Assert.IsTrue (Monitor.TryEnter (o), "TryEnter-2");
140 [ExpectedException (typeof (ArgumentNullException))]
141 public void TryEnter_Null_Int ()
143 Monitor.TryEnter (null, Timeout.Infinite);
147 [ExpectedException (typeof (ArgumentException))]
148 public void TryEnter_Int_Negative ()
150 object o = new object ();
151 Monitor.TryEnter (o, -2);
155 public void TryEnter_Int ()
157 object o = new object ();
158 Assert.IsTrue (Monitor.TryEnter (o, 1), "TryEnter");
159 Assert.IsTrue (Monitor.TryEnter (o, 2), "TryEnter-2");
163 [ExpectedException (typeof (ArgumentNullException))]
164 public void TryEnter_Null_TimeSpan ()
166 Monitor.TryEnter (null, Timeout.Infinite);
170 [ExpectedException (typeof (ArgumentOutOfRangeException))]
171 public void TryEnter_TimeSpan_Negative ()
173 object o = new object ();
174 Monitor.TryEnter (o, Negative);
178 [ExpectedException (typeof (ArgumentOutOfRangeException))]
179 public void TryEnter_TimeSpan_TooLarge ()
181 object o = new object ();
182 Monitor.TryEnter (o, TooLarge);
186 [ExpectedException (typeof (ArgumentOutOfRangeException))]
187 public void TryEnter_Null_TimeSpan_TooLarge ()
189 // exception ordering test
190 Monitor.TryEnter (null, TooLarge);
194 public void TryEnter_TimeSpan ()
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");
204 [ExpectedException (typeof (ArgumentNullException))]
205 public void Wait_Null ()
211 [ExpectedException (typeof (SynchronizationLockException))]
212 public void Wait_Unlocked ()
214 object o = new object ();
215 Assert.IsTrue (Monitor.Wait (o), "Wait");
218 // [Test] that would be Infinite
221 object o = new object ();
223 Assert.IsFalse (Monitor.Wait (o), "Wait");
228 [ExpectedException (typeof (ArgumentNullException))]
229 public void Wait_Null_Int ()
231 Monitor.Wait (null, Timeout.Infinite);
235 [ExpectedException (typeof (ArgumentOutOfRangeException))]
236 public void Wait_Int_Negative ()
238 object o = new object ();
239 Monitor.Wait (o, -2);
243 [ExpectedException (typeof (SynchronizationLockException))]
244 public void Wait_Int_Unlocked ()
246 object o = new object ();
247 Assert.IsTrue (Monitor.Wait (o, 1), "Wait");
251 public void Wait_Int ()
253 object o = new object ();
255 Assert.IsFalse (Monitor.Wait (o, 1), "Wait");
260 [ExpectedException (typeof (ArgumentNullException))]
261 public void Wait_Null_TimeSpan ()
263 Monitor.Wait (null, Timeout.Infinite);
267 [ExpectedException (typeof (ArgumentOutOfRangeException))]
268 public void Wait_TimeSpan_Negative ()
270 object o = new object ();
271 Monitor.Wait (o, Negative);
275 [ExpectedException (typeof (ArgumentOutOfRangeException))]
276 public void Wait_TimeSpan_TooLarge ()
278 object o = new object ();
279 Monitor.Wait (o, TooLarge);
283 [ExpectedException (typeof (ArgumentOutOfRangeException))]
284 public void Wait_Null_TimeSpan_TooLarge ()
286 // exception ordering test
287 Monitor.Wait (null, TooLarge);
291 [ExpectedException (typeof (SynchronizationLockException))]
292 public void Wait_TimeSpan_Unlocked ()
294 object o = new object ();
295 Assert.IsTrue (Monitor.Wait (o, Infinite), "Wait");
299 public void Wait_TimeSpan ()
301 object o = new object ();
303 Assert.IsFalse (Monitor.Wait (o, SmallNegative), "Wait");
308 public void Enter_bool ()
310 object o = new object ();
312 Monitor.Enter (o, ref taken);
313 Assert.IsTrue (taken, "Monitor.Enter (obj, ref taken)");
317 [ExpectedException (typeof (ArgumentException))]
318 public void Enter_bool_argcheck ()
320 object o = new object ();
322 Monitor.Enter (o, ref taken);