SemaphoreTest.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // SemaphoreTest.cs - Unit tests for System.Threading.Semaphore
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  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.
  27. //
  28. #if NET_2_0
  29. using NUnit.Framework;
  30. using System;
  31. using System.Security.AccessControl;
  32. using System.Threading;
  33. namespace MonoTests.System.Threading {
  34. [TestFixture]
  35. public class SemaphoreTest {
  36. [Test]
  37. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  38. public void Constructor_IntInt_NegativeInitialCount ()
  39. {
  40. new Semaphore (-1, 1);
  41. }
  42. [Test]
  43. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  44. public void Constructor_IntInt_ZeroMaximumCount ()
  45. {
  46. new Semaphore (0, 0);
  47. }
  48. [Test]
  49. [ExpectedException (typeof (ArgumentException))]
  50. public void Constructor_IntInt_InitialBiggerThanMaximum ()
  51. {
  52. new Semaphore (2, 1);
  53. }
  54. [Test]
  55. [Category ("NotWorking")] // not implemented in Mono
  56. public void Constructor_IntInt ()
  57. {
  58. new Semaphore (1, 2);
  59. }
  60. [Test]
  61. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  62. public void Constructor_IntIntString_NegativeInitialCount ()
  63. {
  64. new Semaphore (-1, 1, "mono");
  65. }
  66. [Test]
  67. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  68. public void Constructor_IntIntString_ZeroMaximumCount ()
  69. {
  70. new Semaphore (0, 0, "mono");
  71. }
  72. [Test]
  73. [ExpectedException (typeof (ArgumentException))]
  74. public void Constructor_IntIntString_InitialBiggerThanMaximum ()
  75. {
  76. new Semaphore (2, 1, "mono");
  77. }
  78. [Test]
  79. [Category ("NotWorking")] // not implemented in Mono
  80. public void Constructor_IntIntString_NullName ()
  81. {
  82. new Semaphore (0, 1, null);
  83. }
  84. [Test]
  85. public void Constructor_IntIntStringBool_NegativeInitialCount ()
  86. {
  87. bool created = true;
  88. try {
  89. new Semaphore (-1, 1, "mono", out created);
  90. }
  91. catch (ArgumentOutOfRangeException) {
  92. Assert.IsTrue (created, "Created");
  93. }
  94. }
  95. [Test]
  96. public void Constructor_IntIntStringBool_ZeroMaximumCount ()
  97. {
  98. bool created = true;
  99. try {
  100. new Semaphore (0, 0, "mono", out created);
  101. }
  102. catch (ArgumentOutOfRangeException) {
  103. Assert.IsTrue (created, "Created");
  104. }
  105. }
  106. [Test]
  107. public void Constructor_IntIntStringBool_InitialBiggerThanMaximum ()
  108. {
  109. bool created = true;
  110. try {
  111. new Semaphore (2, 1, "mono", out created);
  112. }
  113. catch (ArgumentException) {
  114. Assert.IsTrue (created, "Created");
  115. }
  116. }
  117. [Test]
  118. [Category ("NotWorking")] // not implemented in Mono
  119. public void Constructor_IntIntStringBool_NullName ()
  120. {
  121. bool created = false;
  122. new Semaphore (0, 1, null, out created);
  123. Assert.IsTrue (created, "Created");
  124. }
  125. [Test]
  126. public void Constructor_IntIntStringBoolSecurity_NegativeInitialCount ()
  127. {
  128. bool created = true;
  129. try {
  130. new Semaphore (-1, 1, "mono", out created, null);
  131. }
  132. catch (ArgumentOutOfRangeException) {
  133. Assert.IsTrue (created, "Created");
  134. }
  135. }
  136. [Test]
  137. public void Constructor_IntIntStringBoolSecurity_ZeroMaximumCount ()
  138. {
  139. bool created = true;
  140. try {
  141. new Semaphore (0, 0, "mono", out created, null);
  142. }
  143. catch (ArgumentOutOfRangeException) {
  144. Assert.IsTrue (created, "Created");
  145. }
  146. }
  147. [Test]
  148. public void Constructor_IntIntStringBoolSecurity_InitialBiggerThanMaximum ()
  149. {
  150. bool created = true;
  151. try {
  152. new Semaphore (2, 1, "mono", out created, null);
  153. }
  154. catch (ArgumentException) {
  155. Assert.IsTrue (created, "Created");
  156. }
  157. }
  158. [Test]
  159. [Category ("NotWorking")] // not implemented in Mono
  160. public void Constructor_IntIntStringBoolSecurity_NullName ()
  161. {
  162. bool created = false;
  163. new Semaphore (0, 1, null, out created, null);
  164. Assert.IsTrue (created, "Created");
  165. }
  166. [Test]
  167. [Category ("NotWorking")] // not implemented in Mono
  168. public void Constructor_IntIntStringBoolSecurity ()
  169. {
  170. bool created = false;
  171. SemaphoreSecurity ss = new SemaphoreSecurity ();
  172. new Semaphore (0, 1, "secure", out created, ss);
  173. Assert.IsTrue (created, "Created");
  174. }
  175. [Test]
  176. [ExpectedException (typeof (ArgumentNullException))]
  177. public void OpenExisting_NullName ()
  178. {
  179. Semaphore.OpenExisting (null);
  180. }
  181. [Test]
  182. [ExpectedException (typeof (ArgumentException))]
  183. public void OpenExisting_EmptyName ()
  184. {
  185. Semaphore.OpenExisting (String.Empty);
  186. }
  187. [Test]
  188. [ExpectedException (typeof (ArgumentException))]
  189. public void OpenExisting_TooLongName ()
  190. {
  191. Semaphore.OpenExisting (new String (' ', 261));
  192. }
  193. [Test]
  194. [Category ("NotWorking")] // not implemented in Mono
  195. [ExpectedException (typeof (WaitHandleCannotBeOpenedException))]
  196. public void OpenExisting_Unexisting ()
  197. {
  198. Semaphore.OpenExisting (new String ('a', 260));
  199. }
  200. [Test]
  201. [Category ("NotWorking")] // not implemented in Mono
  202. public void OpenExisting_BadRights ()
  203. {
  204. Semaphore s = new Semaphore (0, 1, "bad-rights");
  205. SemaphoreRights rights = (SemaphoreRights) Int32.MinValue;
  206. Semaphore existing = Semaphore.OpenExisting ("bad-rights", rights);
  207. // rights bits aren't validated
  208. Assert.IsNotNull (existing, "OpenExisting");
  209. Assert.IsFalse (Object.ReferenceEquals (s, existing), "!ref");
  210. }
  211. [Test]
  212. [Category ("NotWorking")] // not implemented in Mono
  213. public void AccessControl_Unnamed ()
  214. {
  215. Semaphore s = new Semaphore (0, 1, null);
  216. SemaphoreSecurity ss = s.GetAccessControl ();
  217. Assert.IsNotNull (ss, "GetAccessControl");
  218. s.SetAccessControl (ss);
  219. }
  220. [Test]
  221. [ExpectedException (typeof (ArgumentNullException))]
  222. [Category ("NotWorking")] // not implemented in Mono
  223. public void SetAccessControl_Null ()
  224. {
  225. Semaphore s = new Semaphore (0, 1, null);
  226. s.SetAccessControl (null);
  227. }
  228. [Test]
  229. [Category ("NotWorking")] // not implemented in Mono
  230. public void Release ()
  231. {
  232. Semaphore s = new Semaphore (0, 1, null);
  233. s.Release ();
  234. }
  235. [Test]
  236. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  237. [Category ("NotWorking")] // not implemented in Mono
  238. public void Release_Zero ()
  239. {
  240. Semaphore s = new Semaphore (0, 1, null);
  241. s.Release (0);
  242. }
  243. }
  244. }
  245. #endif