2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / Test / System.Threading / MonitorTest.cs
blob34cbfdfaa857959ae1955ec69cc9a95b8b4f43d6
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 //
11 using NUnit.Framework;
12 using System;
13 using System.Threading;
15 namespace MonoTests.System.Threading {
17 [TestFixture]
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);
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 ()
31 object o = new object ();
32 Monitor.Exit (o);
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 ()
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);
49 [Test]
50 [ExpectedException (typeof (ArgumentNullException))]
51 public void Enter_Null ()
53 Monitor.Enter (null);
56 [Test]
57 [ExpectedException (typeof (ArgumentNullException))]
58 public void Exit_Null ()
60 Monitor.Exit (null);
63 [Test]
64 public void Enter_Exit ()
66 object o = new object ();
67 Monitor.Enter (o);
68 try {
69 Assert.IsNotNull (o);
71 finally {
72 Monitor.Exit (o);
76 [Test]
77 [ExpectedException (typeof (ArgumentNullException))]
78 public void Pulse_Null ()
80 Monitor.Pulse (null);
83 [Test]
84 [ExpectedException (typeof (SynchronizationLockException))]
85 public void Pulse_Unlocked ()
87 object o = new object ();
88 Monitor.Pulse (o);
91 [Test]
92 public void Pulse ()
94 object o = new object ();
95 lock (o) {
96 Monitor.Pulse (o);
100 [Test]
101 [ExpectedException (typeof (ArgumentNullException))]
102 public void PulseAll_Null ()
104 Monitor.PulseAll (null);
107 [Test]
108 [ExpectedException (typeof (SynchronizationLockException))]
109 public void PulseAll_Unlocked ()
111 object o = new object ();
112 Monitor.PulseAll (o);
115 [Test]
116 public void PulseAll ()
118 object o = new object ();
119 lock (o) {
120 Monitor.PulseAll (o);
124 [Test]
125 [ExpectedException (typeof (ArgumentNullException))]
126 public void TryEnter_Null ()
128 Monitor.TryEnter (null);
131 [Test]
132 public void TryEnter ()
134 object o = new object ();
135 Assert.IsTrue (Monitor.TryEnter (o), "TryEnter");
136 Assert.IsTrue (Monitor.TryEnter (o), "TryEnter-2");
139 [Test]
140 [ExpectedException (typeof (ArgumentNullException))]
141 public void TryEnter_Null_Int ()
143 Monitor.TryEnter (null, Timeout.Infinite);
146 [Test]
147 [ExpectedException (typeof (ArgumentException))]
148 public void TryEnter_Int_Negative ()
150 object o = new object ();
151 Monitor.TryEnter (o, -2);
154 [Test]
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");
162 [Test]
163 [ExpectedException (typeof (ArgumentNullException))]
164 public void TryEnter_Null_TimeSpan ()
166 Monitor.TryEnter (null, Timeout.Infinite);
169 [Test]
170 [ExpectedException (typeof (ArgumentOutOfRangeException))]
171 public void TryEnter_TimeSpan_Negative ()
173 object o = new object ();
174 Monitor.TryEnter (o, Negative);
177 [Test]
178 [ExpectedException (typeof (ArgumentOutOfRangeException))]
179 public void TryEnter_TimeSpan_TooLarge ()
181 object o = new object ();
182 Monitor.TryEnter (o, TooLarge);
185 [Test]
186 [ExpectedException (typeof (ArgumentOutOfRangeException))]
187 public void TryEnter_Null_TimeSpan_TooLarge ()
189 // exception ordering test
190 Monitor.TryEnter (null, TooLarge);
193 [Test]
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");
203 [Test]
204 [ExpectedException (typeof (ArgumentNullException))]
205 public void Wait_Null ()
207 Monitor.Wait (null);
210 [Test]
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
219 public void Wait ()
221 object o = new object ();
222 lock (o) {
223 Assert.IsFalse (Monitor.Wait (o), "Wait");
227 [Test]
228 [ExpectedException (typeof (ArgumentNullException))]
229 public void Wait_Null_Int ()
231 Monitor.Wait (null, Timeout.Infinite);
234 [Test]
235 [ExpectedException (typeof (ArgumentOutOfRangeException))]
236 public void Wait_Int_Negative ()
238 object o = new object ();
239 Monitor.Wait (o, -2);
242 [Test]
243 [ExpectedException (typeof (SynchronizationLockException))]
244 public void Wait_Int_Unlocked ()
246 object o = new object ();
247 Assert.IsTrue (Monitor.Wait (o, 1), "Wait");
250 [Test]
251 public void Wait_Int ()
253 object o = new object ();
254 lock (o) {
255 Assert.IsFalse (Monitor.Wait (o, 1), "Wait");
259 [Test]
260 [ExpectedException (typeof (ArgumentNullException))]
261 public void Wait_Null_TimeSpan ()
263 Monitor.Wait (null, Timeout.Infinite);
266 [Test]
267 [ExpectedException (typeof (ArgumentOutOfRangeException))]
268 public void Wait_TimeSpan_Negative ()
270 object o = new object ();
271 Monitor.Wait (o, Negative);
274 [Test]
275 [ExpectedException (typeof (ArgumentOutOfRangeException))]
276 public void Wait_TimeSpan_TooLarge ()
278 object o = new object ();
279 Monitor.Wait (o, TooLarge);
282 [Test]
283 [ExpectedException (typeof (ArgumentOutOfRangeException))]
284 public void Wait_Null_TimeSpan_TooLarge ()
286 // exception ordering test
287 Monitor.Wait (null, TooLarge);
290 [Test]
291 [ExpectedException (typeof (SynchronizationLockException))]
292 public void Wait_TimeSpan_Unlocked ()
294 object o = new object ();
295 Assert.IsTrue (Monitor.Wait (o, Infinite), "Wait");
298 [Test]
299 public void Wait_TimeSpan ()
301 object o = new object ();
302 lock (o) {
303 Assert.IsFalse (Monitor.Wait (o, SmallNegative), "Wait");