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.
31 using NUnit.Framework;
34 using System.Security.AccessControl;
35 using System.Threading;
37 namespace MonoTests.System.Threading {
40 public class SemaphoreTest {
43 [ExpectedException (typeof (ArgumentOutOfRangeException))]
44 public void Constructor_IntInt_NegativeInitialCount ()
46 new Semaphore (-1, 1);
50 [ExpectedException (typeof (ArgumentOutOfRangeException))]
51 public void Constructor_IntInt_ZeroMaximumCount ()
57 [ExpectedException (typeof (ArgumentException))]
58 public void Constructor_IntInt_InitialBiggerThanMaximum ()
64 public void Constructor_IntInt ()
70 [ExpectedException (typeof (ArgumentOutOfRangeException))]
71 public void Constructor_IntIntString_NegativeInitialCount ()
73 new Semaphore (-1, 1, "mono");
77 [ExpectedException (typeof (ArgumentOutOfRangeException))]
78 public void Constructor_IntIntString_ZeroMaximumCount ()
80 new Semaphore (0, 0, "mono");
84 [ExpectedException (typeof (ArgumentException))]
85 public void Constructor_IntIntString_InitialBiggerThanMaximum ()
87 new Semaphore (2, 1, "mono");
91 public void Constructor_IntIntString_NullName ()
93 new Semaphore (0, 1, null);
97 public void Constructor_IntIntStringBool_NegativeInitialCount ()
101 new Semaphore (-1, 1, "mono", out created);
103 catch (ArgumentOutOfRangeException) {
104 Assert.IsTrue (created, "Created");
109 public void Constructor_IntIntStringBool_ZeroMaximumCount ()
113 new Semaphore (0, 0, "mono", out created);
115 catch (ArgumentOutOfRangeException) {
116 Assert.IsTrue (created, "Created");
121 public void Constructor_IntIntStringBool_InitialBiggerThanMaximum ()
125 new Semaphore (2, 1, "mono", out created);
127 catch (ArgumentException) {
128 Assert.IsTrue (created, "Created");
133 public void Constructor_IntIntStringBool_NullName ()
135 bool created = false;
136 new Semaphore (0, 1, null, out created);
137 Assert.IsTrue (created, "Created");
141 public void Constructor_IntIntStringBoolSecurity_NegativeInitialCount ()
145 new Semaphore (-1, 1, "mono", out created, null);
147 catch (ArgumentOutOfRangeException) {
148 Assert.IsTrue (created, "Created");
153 public void Constructor_IntIntStringBoolSecurity_ZeroMaximumCount ()
157 new Semaphore (0, 0, "mono", out created, null);
159 catch (ArgumentOutOfRangeException) {
160 Assert.IsTrue (created, "Created");
165 public void Constructor_IntIntStringBoolSecurity_InitialBiggerThanMaximum ()
169 new Semaphore (2, 1, "mono", out created, null);
171 catch (ArgumentException) {
172 Assert.IsTrue (created, "Created");
177 public void Constructor_IntIntStringBoolSecurity_NullName ()
179 bool created = false;
180 new Semaphore (0, 1, null, out created, null);
181 Assert.IsTrue (created, "Created");
185 public void Constructor_IntIntStringBoolSecurity ()
187 bool created = false;
188 SemaphoreSecurity ss = new SemaphoreSecurity ();
189 new Semaphore (0, 1, "secure", out created, ss);
190 Assert.IsTrue (created, "Created");
194 [ExpectedException (typeof (ArgumentNullException))]
195 public void OpenExisting_NullName ()
197 Semaphore.OpenExisting (null);
201 [ExpectedException (typeof (ArgumentException))]
202 public void OpenExisting_EmptyName ()
204 Semaphore.OpenExisting (String.Empty);
208 [ExpectedException (typeof (ArgumentException))]
209 public void OpenExisting_TooLongName ()
211 Semaphore.OpenExisting (new String (' ', 261));
215 [ExpectedException (typeof (WaitHandleCannotBeOpenedException))]
216 public void OpenExisting_Unexisting ()
218 Semaphore.OpenExisting (new String ('a', 260));
222 [Category ("NotWorking")] // not implemented in Mono
223 public void OpenExisting_BadRights ()
225 Semaphore s = new Semaphore (0, 1, "bad-rights");
226 SemaphoreRights rights = (SemaphoreRights) Int32.MinValue;
227 Semaphore existing = Semaphore.OpenExisting ("bad-rights", rights);
228 // rights bits aren't validated
229 Assert.IsNotNull (existing, "OpenExisting");
230 Assert.IsFalse (Object.ReferenceEquals (s, existing), "!ref");
234 [Category ("NotWorking")] // not implemented in Mono
235 public void AccessControl_Unnamed ()
237 Semaphore s = new Semaphore (0, 1, null);
238 SemaphoreSecurity ss = s.GetAccessControl ();
239 Assert.IsNotNull (ss, "GetAccessControl");
240 s.SetAccessControl (ss);
244 [ExpectedException (typeof (ArgumentNullException))]
245 [Category ("NotWorking")] // not implemented in Mono
246 public void SetAccessControl_Null ()
248 Semaphore s = new Semaphore (0, 1, null);
249 s.SetAccessControl (null);
253 public void Release ()
255 Semaphore s = new Semaphore (0, 1, null);
260 [ExpectedException (typeof (ArgumentOutOfRangeException))]
261 public void Release_Zero ()
263 Semaphore s = new Semaphore (0, 1, null);