WaitHandleTest.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //
  2. // WaitHandleTest.cs
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2009 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 System;
  30. using System.Threading;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.Threading {
  33. [TestFixture]
  34. public class WaitHandleTest {
  35. TimeSpan Infinite = new TimeSpan (-10000); // -10000 ticks == -1 ms
  36. TimeSpan SmallNegative = new TimeSpan (-2); // between 0 and -1.0 (infinite) ms
  37. TimeSpan Negative = new TimeSpan (-20000); // really negative
  38. WaitHandle [] TooLarge = new Mutex [65];
  39. WaitHandle [] Empty = new Mutex [1];
  40. WaitHandle [] Single = new Mutex [1] { new Mutex (true) };
  41. [Test]
  42. [ExpectedException (typeof (ArgumentNullException))]
  43. public void WaitAny_WaitHandle_Null ()
  44. {
  45. WaitHandle.WaitAny (null);
  46. }
  47. [Test]
  48. [ExpectedException (typeof (NotSupportedException))]
  49. public void WaitAny_WaitHandle_TooLarge ()
  50. {
  51. WaitHandle.WaitAny (TooLarge);
  52. }
  53. [Test]
  54. [ExpectedException (typeof (ArgumentNullException))]
  55. public void WaitAny_WaitHandle_Empty ()
  56. {
  57. WaitHandle.WaitAny (Empty);
  58. }
  59. [Test]
  60. public void WaitAny_WaitHandle ()
  61. {
  62. Assert.AreEqual (0, WaitHandle.WaitAny (Single), "WaitAny");
  63. }
  64. [Test]
  65. [ExpectedException (typeof (ArgumentNullException))]
  66. public void WaitAny_WaitHandleNull_Int ()
  67. {
  68. WaitHandle.WaitAny (null, -1);
  69. }
  70. [Test]
  71. [ExpectedException (typeof (NotSupportedException))]
  72. public void WaitAny_WaitHandle_TooLarge_Int ()
  73. {
  74. WaitHandle.WaitAny (TooLarge, -1);
  75. }
  76. [Test]
  77. [ExpectedException (typeof (ArgumentNullException))]
  78. public void WaitAny_WaitHandle_Empty_Int ()
  79. {
  80. WaitHandle.WaitAny (Empty, -1);
  81. }
  82. [Test]
  83. public void WaitAny_WaitHandle_Int ()
  84. {
  85. // -1 is infinite
  86. Assert.AreEqual (0, WaitHandle.WaitAny (Single, -1), "WaitAny");
  87. }
  88. [Test]
  89. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  90. public void WaitAny_WaitHandle_Int_Negative ()
  91. {
  92. Assert.AreEqual (0, WaitHandle.WaitAny (Single, -2), "WaitAny");
  93. }
  94. [Test]
  95. [ExpectedException (typeof (ArgumentNullException))]
  96. public void WaitAny_WaitHandleNull_TimeSpan ()
  97. {
  98. WaitHandle.WaitAny (null, Infinite);
  99. }
  100. [Test]
  101. [ExpectedException (typeof (NotSupportedException))]
  102. public void WaitAny_WaitHandle_TooLarge_TimeSpan ()
  103. {
  104. WaitHandle.WaitAny (TooLarge, Infinite);
  105. }
  106. [Test]
  107. [ExpectedException (typeof (ArgumentNullException))]
  108. public void WaitAny_WaitHandle_Empty_TimeSpan ()
  109. {
  110. WaitHandle.WaitAny (Empty, Infinite);
  111. }
  112. [Test]
  113. public void WaitAny_WaitHandle_TimeSpan ()
  114. {
  115. Assert.AreEqual (Timeout.Infinite, (int) Infinite.TotalMilliseconds, "Infinite");
  116. Assert.AreEqual (0, WaitHandle.WaitAny (Single, Infinite), "WaitAny-Infinite");
  117. Assert.AreEqual (0, WaitHandle.WaitAny (Single, SmallNegative), "WaitAny-SmallNegative");
  118. }
  119. [Test]
  120. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  121. public void WaitAny_WaitHandle_TimeSpan_Negative ()
  122. {
  123. Assert.AreEqual (0, WaitHandle.WaitAny (Single, Negative), "WaitAny");
  124. }
  125. [Test]
  126. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  127. public void WaitAny_WaitHandle_TimeSpan_MaxValue ()
  128. {
  129. Assert.AreEqual (0, WaitHandle.WaitAny (Single, TimeSpan.MaxValue), "WaitAny");
  130. }
  131. [Test]
  132. [ExpectedException (typeof (ArgumentNullException))]
  133. public void WaitAll_WaitHandle_Null ()
  134. {
  135. WaitHandle.WaitAll (null);
  136. }
  137. [Test]
  138. [ExpectedException (typeof (NotSupportedException))]
  139. public void WaitAll_WaitHandle_TooLarge ()
  140. {
  141. WaitHandle.WaitAll (TooLarge);
  142. }
  143. [Test]
  144. [ExpectedException (typeof (ArgumentNullException))]
  145. public void WaitAll_WaitHandle_Empty ()
  146. {
  147. WaitHandle.WaitAll (Empty);
  148. }
  149. [Test]
  150. public void WaitAll_WaitHandle ()
  151. {
  152. Assert.IsTrue (WaitHandle.WaitAll (Single), "WaitAll");
  153. }
  154. [Test]
  155. [ExpectedException (typeof (ArgumentNullException))]
  156. public void WaitAll_WaitHandleNull_Int ()
  157. {
  158. WaitHandle.WaitAll (null, -1);
  159. }
  160. [Test]
  161. [ExpectedException (typeof (NotSupportedException))]
  162. public void WaitAll_WaitHandle_TooLarge_Int ()
  163. {
  164. WaitHandle.WaitAll (TooLarge, -1);
  165. }
  166. [Test]
  167. [ExpectedException (typeof (ArgumentNullException))]
  168. public void WaitAll_WaitHandle_Empty_Int ()
  169. {
  170. WaitHandle.WaitAll (Empty, -1);
  171. }
  172. [Test]
  173. public void WaitAll_WaitHandle_Int ()
  174. {
  175. // -1 is infinite
  176. Assert.IsTrue (WaitHandle.WaitAll (Single, -1), "WaitAll");
  177. }
  178. [Test]
  179. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  180. public void WaitAll_WaitHandle_Int_Negative ()
  181. {
  182. Assert.IsTrue (WaitHandle.WaitAll (Single, -2), "WaitAll");
  183. }
  184. [Test]
  185. [ExpectedException (typeof (ArgumentNullException))]
  186. public void WaitAll_WaitHandleNull_TimeSpan ()
  187. {
  188. WaitHandle.WaitAll (null, Infinite);
  189. }
  190. [Test]
  191. [ExpectedException (typeof (NotSupportedException))]
  192. public void WaitAll_WaitHandle_TooLarge_TimeSpan ()
  193. {
  194. WaitHandle.WaitAll (TooLarge, Infinite);
  195. }
  196. [Test]
  197. [ExpectedException (typeof (ArgumentNullException))]
  198. public void WaitAll_WaitHandle_Empty_TimeSpan ()
  199. {
  200. WaitHandle.WaitAll (Empty, Infinite);
  201. }
  202. [Test]
  203. public void WaitAll_WaitHandle_TimeSpan ()
  204. {
  205. Assert.AreEqual (Timeout.Infinite, (int) Infinite.TotalMilliseconds, "Infinite");
  206. Assert.IsTrue (WaitHandle.WaitAll (Single, Infinite), "WaitAll-Infinite");
  207. Assert.IsTrue (WaitHandle.WaitAll (Single, SmallNegative), "WaitAll-SmallNegative");
  208. }
  209. [Test]
  210. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  211. public void WaitAll_WaitHandle_TimeSpan_Negative ()
  212. {
  213. Assert.IsTrue (WaitHandle.WaitAll (Single, Negative), "WaitAll");
  214. }
  215. [Test]
  216. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  217. public void WaitAll_WaitHandle_TimeSpan_MaxValue ()
  218. {
  219. Assert.IsTrue (WaitHandle.WaitAll (Single, TimeSpan.MaxValue), "WaitAll");
  220. }
  221. [Test]
  222. public void WaitOne ()
  223. {
  224. Assert.IsTrue (Single [0].WaitOne (), "WaitOne");
  225. }
  226. [Test]
  227. public void WaitOne_Int ()
  228. {
  229. // -1 is infinite
  230. Assert.IsTrue (Single [0].WaitOne (-1), "WaitOne");
  231. }
  232. [Test]
  233. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  234. public void WaitOne_Int_Negative ()
  235. {
  236. Assert.IsTrue (Single [0].WaitOne (-2), "WaitOne");
  237. }
  238. [Test]
  239. public void WaitOne_TimeSpan ()
  240. {
  241. Assert.AreEqual (Timeout.Infinite, (int) Infinite.TotalMilliseconds, "Infinite");
  242. Assert.IsTrue (Single [0].WaitOne (Infinite), "WaitOne-Infinite");
  243. Assert.IsTrue (Single [0].WaitOne (SmallNegative), "WaitOne-SmallNegative");
  244. }
  245. [Test]
  246. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  247. public void WaitOne_TimeSpan_Negative ()
  248. {
  249. Assert.IsTrue (Single [0].WaitOne (Negative), "WaitOne");
  250. }
  251. [Test]
  252. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  253. public void WaitOne_TimeSpan_MaxValue ()
  254. {
  255. Assert.IsTrue (Single [0].WaitOne (TimeSpan.MaxValue), "WaitOne");
  256. }
  257. }
  258. }
  259. #endif