2 // SemaphoreTest.cs - Unit tests for System.Threading.Semaphore
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using NUnit.Framework;
33 using System.Security.AccessControl;
34 using System.Threading;
36 namespace MonoTests.System.Threading {
39 public class SemaphoreTest {
42 [ExpectedException (typeof (ArgumentOutOfRangeException))]
43 public void Constructor_IntInt_NegativeInitialCount ()
45 new Semaphore (-1, 1);
49 [ExpectedException (typeof (ArgumentOutOfRangeException))]
50 public void Constructor_IntInt_ZeroMaximumCount ()
56 [ExpectedException (typeof (ArgumentException))]
57 public void Constructor_IntInt_InitialBiggerThanMaximum ()
63 public void Constructor_IntInt ()
69 [ExpectedException (typeof (ArgumentOutOfRangeException))]
70 public void Constructor_IntIntString_NegativeInitialCount ()
72 new Semaphore (-1, 1, "mono");
76 [ExpectedException (typeof (ArgumentOutOfRangeException))]
77 public void Constructor_IntIntString_ZeroMaximumCount ()
79 new Semaphore (0, 0, "mono");
83 [ExpectedException (typeof (ArgumentException))]
84 public void Constructor_IntIntString_InitialBiggerThanMaximum ()
86 new Semaphore (2, 1, "mono");
90 public void Constructor_IntIntString_NullName ()
92 new Semaphore (0, 1, null);
96 public void Constructor_IntIntStringBool_NegativeInitialCount ()
100 new Semaphore (-1, 1, "mono", out created);
102 catch (ArgumentOutOfRangeException) {
103 Assert.IsTrue (created, "Created");
108 public void Constructor_IntIntStringBool_ZeroMaximumCount ()
112 new Semaphore (0, 0, "mono", out created);
114 catch (ArgumentOutOfRangeException) {
115 Assert.IsTrue (created, "Created");
120 public void Constructor_IntIntStringBool_InitialBiggerThanMaximum ()
124 new Semaphore (2, 1, "mono", out created);
126 catch (ArgumentException) {
127 Assert.IsTrue (created, "Created");
132 public void Constructor_IntIntStringBool_NullName ()
134 bool created = false;
135 new Semaphore (0, 1, null, out created);
136 Assert.IsTrue (created, "Created");
140 public void Constructor_IntIntStringBoolSecurity_NegativeInitialCount ()
144 new Semaphore (-1, 1, "mono", out created, null);
146 catch (ArgumentOutOfRangeException) {
147 Assert.IsTrue (created, "Created");
152 public void Constructor_IntIntStringBoolSecurity_ZeroMaximumCount ()
156 new Semaphore (0, 0, "mono", out created, null);
158 catch (ArgumentOutOfRangeException) {
159 Assert.IsTrue (created, "Created");
164 public void Constructor_IntIntStringBoolSecurity_InitialBiggerThanMaximum ()
168 new Semaphore (2, 1, "mono", out created, null);
170 catch (ArgumentException) {
171 Assert.IsTrue (created, "Created");
176 public void Constructor_IntIntStringBoolSecurity_NullName ()
178 bool created = false;
179 new Semaphore (0, 1, null, out created, null);
180 Assert.IsTrue (created, "Created");
184 public void Constructor_CreatedWithSameName ()
186 var s1 = new Semaphore(1, 5, "name");
188 var s2 = new Semaphore(1, 5, "name", out created);
189 Assert.IsFalse (created);
193 [Category ("MobileNotWorking")]
194 public void Constructor_IntIntStringBoolSecurity ()
196 bool created = false;
197 SemaphoreSecurity ss = new SemaphoreSecurity ();
198 new Semaphore (0, 1, "secure", out created, ss);
199 Assert.IsTrue (created, "Created");
203 [Category ("MobileNotWorking")]
204 [ExpectedException (typeof (ArgumentNullException))]
205 public void OpenExisting_NullName ()
207 Semaphore.OpenExisting (null);
211 [Category ("MobileNotWorking")]
212 [ExpectedException (typeof (ArgumentException))]
213 public void OpenExisting_EmptyName ()
215 Semaphore.OpenExisting (String.Empty);
219 [Category ("MobileNotWorking")]
220 [ExpectedException (typeof (ArgumentException))]
221 public void OpenExisting_TooLongName ()
223 Semaphore.OpenExisting (new String (' ', 261));
227 [Category ("MobileNotWorking")]
228 [ExpectedException (typeof (WaitHandleCannotBeOpenedException))]
229 public void OpenExisting_Unexisting ()
231 Semaphore.OpenExisting (new String ('a', 260));
235 [Category ("NotWorking")] // not implemented in Mono
236 public void OpenExisting_BadRights ()
238 Semaphore s = new Semaphore (0, 1, "bad-rights");
239 SemaphoreRights rights = (SemaphoreRights) Int32.MinValue;
240 Semaphore existing = Semaphore.OpenExisting ("bad-rights", rights);
241 // rights bits aren't validated
242 Assert.IsNotNull (existing, "OpenExisting");
243 Assert.IsFalse (Object.ReferenceEquals (s, existing), "!ref");
247 [Category ("NotWorking")] // not implemented in Mono
248 public void AccessControl_Unnamed ()
250 Semaphore s = new Semaphore (0, 1, null);
251 SemaphoreSecurity ss = s.GetAccessControl ();
252 Assert.IsNotNull (ss, "GetAccessControl");
253 s.SetAccessControl (ss);
257 [ExpectedException (typeof (ArgumentNullException))]
258 [Category ("NotWorking")] // not implemented in Mono
259 public void SetAccessControl_Null ()
261 Semaphore s = new Semaphore (0, 1, null);
262 s.SetAccessControl (null);
266 public void Release ()
268 Semaphore s = new Semaphore (0, 1, null);
273 [ExpectedException (typeof (ArgumentOutOfRangeException))]
274 public void Release_Zero ()
276 Semaphore s = new Semaphore (0, 1, null);