2 // ManualResetEventSlimTests.cs
4 // Copyright (c) 2008 Jérémie "Garuma" Laval
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 using System.Threading;
29 using NUnit.Framework;
31 using MonoTests.System.Threading.Tasks;
33 namespace MonoTests.System.Threading
37 public class ManualResetEventSlimTests
39 ManualResetEventSlim mre;
44 mre = new ManualResetEventSlim();
48 public void Constructor_Defaults ()
50 Assert.IsFalse (mre.IsSet, "#1");
51 Assert.AreEqual (10, mre.SpinCount, "#2");
55 public void Constructor_Invalid ()
58 new ManualResetEventSlim (true, -1);
60 } catch (ArgumentException) {
64 new ManualResetEventSlim (true, 2048);
66 } catch (ArgumentException) {
71 public void IsSetTestCase()
73 Assert.IsFalse(mre.IsSet, "#1");
75 Assert.IsTrue(mre.IsSet, "#2");
77 Assert.IsFalse(mre.IsSet, "#3");
81 public void WaitTest()
86 ParallelTestHelper.ParallelStressTest(mre, delegate (ManualResetEventSlim m) {
87 if (Interlocked.Increment(ref count) % 2 == 0) {
89 for (int i = 0; i < 10; i++) {
101 Assert.IsTrue(s, "#1");
102 Assert.IsTrue(mre.IsSet, "#2");
106 public void Wait_SetConcurrent ()
108 for (int i = 0; i < 10000; ++i) {
109 var mre = new ManualResetEventSlim ();
112 ThreadPool.QueueUserWorkItem (delegate {
116 ThreadPool.QueueUserWorkItem (delegate {
117 b &= mre.Wait (1000);
120 Assert.IsTrue (mre.Wait (1000), i.ToString ());
121 Assert.IsTrue (b, i.ToString ());
126 public void Wait_DisposeWithCancel ()
128 var token = new CancellationTokenSource ();
129 ThreadPool.QueueUserWorkItem (delegate {
136 mre.Wait (10000, token.Token);
138 } catch (OperationCanceledException e) {
143 public void Wait_Expired ()
145 Assert.IsFalse (mre.Wait (10));
148 [Test, ExpectedException (typeof (ObjectDisposedException))]
149 public void WaitAfterDisposeTest ()
156 public void SetAfterDisposeTest ()
158 ParallelTestHelper.Repeat (delegate {
159 Exception disp = null, setting = null;
161 CountdownEvent evt = new CountdownEvent (2);
162 CountdownEvent evtFinish = new CountdownEvent (2);
164 ThreadPool.QueueUserWorkItem (delegate {
169 } catch (Exception e) {
174 ThreadPool.QueueUserWorkItem (delegate {
179 } catch (Exception e) {
185 bool bb = evtFinish.Wait (1000);
187 Assert.AreEqual (true, evtFinish.IsSet);
189 Assert.IsTrue (bb, "#0");
190 Assert.IsNull (disp, "#1");
191 Assert.IsNull (setting, "#2");
194 evtFinish.Dispose ();
199 public void WaitHandle_Initialized ()
201 var mre = new ManualResetEventSlim (true);
202 Assert.IsTrue (mre.WaitHandle.WaitOne (0), "#1");
204 Assert.IsFalse (mre.WaitHandle.WaitOne (0), "#2");
205 Assert.AreEqual (mre.WaitHandle, mre.WaitHandle, "#3");
209 public void WaitHandle_NotInitialized ()
211 var mre = new ManualResetEventSlim (false);
212 Assert.IsFalse (mre.WaitHandle.WaitOne (0), "#1");
214 Assert.IsTrue (mre.WaitHandle.WaitOne (0), "#2");
218 public void Dispose ()
220 var mre = new ManualResetEventSlim (false);
222 Assert.IsFalse (mre.IsSet, "#0a");
227 } catch (ObjectDisposedException) {
235 } catch (ObjectDisposedException) {
239 var v = mre.WaitHandle;
241 } catch (ObjectDisposedException) {
246 public void Dispose_Double ()
248 var mre = new ManualResetEventSlim ();