1 // CountdownEventTests.cs
4 // Marek Safar <marek.safar@gmail.com>
6 // Copyright (c) 2008 Jérémie "Garuma" Laval
7 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 using System.Threading;
34 using NUnit.Framework;
36 using MonoTests.System.Threading.Tasks;
38 namespace MonoTests.System.Threading
40 [TestFixtureAttribute]
41 public class CountdownEventTests
44 public void Constructor_Invalid ()
47 new CountdownEvent (-2);
49 } catch (ArgumentOutOfRangeException) {
54 public void Constructor_Zero ()
56 var ce = new CountdownEvent (0);
57 Assert.IsTrue (ce.IsSet, "#1");
58 Assert.AreEqual (0, ce.InitialCount, "#2");
59 Assert.IsTrue (ce.Wait (0), "#3");
63 public void Constructor_Max ()
65 new CountdownEvent (int.MaxValue);
69 public void AddCount_Invalid ()
71 var ev = new CountdownEvent (1);
75 } catch (ArgumentOutOfRangeException) {
81 } catch (ArgumentOutOfRangeException) {
86 public void AddCount_HasBeenSet ()
88 var ev = new CountdownEvent (0);
92 } catch (InvalidOperationException) {
95 ev = new CountdownEvent (1);
96 Assert.IsTrue (ev.Signal (), "#2");
100 } catch (InvalidOperationException) {
105 public void AddCountSignalStressTestCase ()
107 var evt = new CountdownEvent (5);
110 ParallelTestHelper.ParallelStressTest (evt, delegate (CountdownEvent e) {
111 int num = Interlocked.Increment (ref count);
118 Assert.AreEqual (4, evt.CurrentCount, "#1");
119 Assert.IsFalse (evt.IsSet, "#2");
123 public void InitialTestCase()
125 var evt = new CountdownEvent (5);
127 Assert.AreEqual(5, evt.InitialCount, "#1");
130 Assert.AreEqual(5, evt.InitialCount, "#2");
134 public void CurrentCountTestCase()
136 var evt = new CountdownEvent (5);
138 Assert.AreEqual(5, evt.CurrentCount, "#1");
141 Assert.AreEqual(6, evt.CurrentCount, "#2");
144 Assert.AreEqual(8, evt.CurrentCount, "#3");
147 Assert.AreEqual(4, evt.CurrentCount, "#4");
150 Assert.AreEqual(5, evt.CurrentCount, "#5");
154 public void Dispose ()
156 var ce = new CountdownEvent (1);
158 Assert.AreEqual (1, ce.CurrentCount, "#0a");
159 Assert.AreEqual (1, ce.InitialCount, "#0b");
160 Assert.IsFalse (ce.IsSet, "#0c");
165 } catch (ObjectDisposedException) {
171 } catch (ObjectDisposedException) {
177 } catch (ObjectDisposedException) {
183 } catch (ObjectDisposedException) {
189 } catch (ObjectDisposedException) {
193 var v = ce.WaitHandle;
195 } catch (ObjectDisposedException) {
200 public void Dispose_Double ()
202 var ce = new CountdownEvent (1);
208 public void IsSetTestCase()
210 var evt = new CountdownEvent (5);
212 Assert.IsFalse(evt.IsSet, "#1");
215 Assert.IsTrue(evt.IsSet, "#2");
218 Assert.IsFalse(evt.IsSet, "#3");
222 public void Reset_Invalid ()
224 var ev = new CountdownEvent (1);
228 } catch (ArgumentOutOfRangeException) {
229 Assert.AreEqual (1, ev.CurrentCount, "#1a");
234 public void Reset_FullInitialized ()
236 var ev = new CountdownEvent (0);
237 Assert.IsTrue (ev.IsSet, "#1");
238 Assert.AreEqual (0, ev.CurrentCount, "#2");
241 Assert.IsFalse (ev.IsSet, "#3");
242 Assert.AreEqual (4, ev.CurrentCount, "#4");
243 Assert.IsFalse (ev.Wait (0), "#5");
247 public void Reset_Zero ()
249 var ev = new CountdownEvent (1);
250 Assert.IsFalse (ev.IsSet, "#1");
253 Assert.IsTrue (ev.IsSet, "#2");
254 Assert.IsTrue (ev.Wait (0), "#3");
255 Assert.AreEqual (0, ev.CurrentCount, "#4");
259 public void Signal_Invalid ()
261 var ev = new CountdownEvent (1);
265 } catch (ArgumentOutOfRangeException) {
266 Assert.AreEqual (1, ev.CurrentCount, "#1a");
272 } catch (ArgumentOutOfRangeException) {
273 Assert.AreEqual (1, ev.CurrentCount, "#2a");
278 public void Signal_Negative ()
280 var ev = new CountdownEvent (1);
284 } catch (InvalidOperationException) {
285 Assert.AreEqual (1, ev.CurrentCount, "#1a");
292 } catch (InvalidOperationException) {
293 Assert.AreEqual (0, ev.CurrentCount, "#2a");
298 public void Signal_Concurrent ()
300 for (int r = 0; r < 100; ++r) {
301 using (var ce = new CountdownEvent (500)) {
302 for (int i = 0; i < ce.InitialCount; ++i) {
303 ThreadPool.QueueUserWorkItem (delegate {
308 Assert.IsTrue (ce.Wait (1000), "#1");
314 public void TryAddCountTestCase()
316 var evt = new CountdownEvent (5);
318 Assert.IsTrue(evt.TryAddCount(2), "#1");
320 Assert.IsFalse(evt.TryAddCount(), "#2");
324 public void TryAddCount_Invalid ()
326 var ev = new CountdownEvent (1);
330 } catch (ArgumentOutOfRangeException) {
336 } catch (ArgumentOutOfRangeException) {
341 public void TryAddCount_HasBeenSet ()
343 var ev = new CountdownEvent (0);
344 Assert.IsFalse (ev.TryAddCount (1), "#1");
346 ev = new CountdownEvent (1);
348 Assert.IsFalse (ev.TryAddCount (1), "#2");
350 ev = new CountdownEvent (2);
352 Assert.IsFalse (ev.TryAddCount (66), "#3");
356 public void WaitTestCase()
358 var evt = new CountdownEvent (5);
363 ParallelTestHelper.ParallelStressTest(evt, delegate (CountdownEvent e) {
364 if (Interlocked.Increment(ref count) % 2 == 0) {
374 Assert.IsTrue(s, "#1");
375 Assert.IsTrue(evt.IsSet, "#2");
379 public void ResetTest ()
381 var evt = new CountdownEvent (5);
383 Assert.AreEqual (5, evt.CurrentCount);
385 Assert.AreEqual (4, evt.CurrentCount);
387 Assert.AreEqual (5, evt.CurrentCount);
388 Assert.AreEqual (5, evt.InitialCount);
391 Assert.AreEqual (3, evt.CurrentCount);
392 Assert.AreEqual (5, evt.InitialCount);
394 Assert.AreEqual (10, evt.CurrentCount);
395 Assert.AreEqual (10, evt.InitialCount);