WaitHandleTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. using System;
  29. using System.Threading;
  30. using NUnit.Framework;
  31. namespace MonoTests.System.Threading {
  32. [TestFixture]
  33. public class WaitHandleTest {
  34. TimeSpan Infinite = new TimeSpan (-10000); // -10000 ticks == -1 ms
  35. TimeSpan SmallNegative = new TimeSpan (-2); // between 0 and -1.0 (infinite) ms
  36. TimeSpan Negative = new TimeSpan (-20000); // really negative
  37. WaitHandle [] TooLarge = new Mutex [65];
  38. WaitHandle [] Empty = new Mutex [1];
  39. WaitHandle [] Single = new Mutex [1] { new Mutex (true) };
  40. [Test]
  41. [ExpectedException (typeof (ArgumentNullException))]
  42. public void WaitAny_WaitHandle_Null ()
  43. {
  44. WaitHandle.WaitAny (null);
  45. }
  46. [Test]
  47. [ExpectedException (typeof (NotSupportedException))]
  48. public void WaitAny_WaitHandle_TooLarge ()
  49. {
  50. WaitHandle.WaitAny (TooLarge);
  51. }
  52. [Test]
  53. [ExpectedException (typeof (ArgumentNullException))]
  54. public void WaitAny_WaitHandle_Empty ()
  55. {
  56. WaitHandle.WaitAny (Empty);
  57. }
  58. [Test]
  59. public void WaitAny_WaitHandle ()
  60. {
  61. Assert.AreEqual (0, WaitHandle.WaitAny (Single), "WaitAny");
  62. }
  63. [Test]
  64. [ExpectedException (typeof (ArgumentNullException))]
  65. public void WaitAny_WaitHandleNull_Int ()
  66. {
  67. WaitHandle.WaitAny (null, -1);
  68. }
  69. [Test]
  70. [ExpectedException (typeof (NotSupportedException))]
  71. public void WaitAny_WaitHandle_TooLarge_Int ()
  72. {
  73. WaitHandle.WaitAny (TooLarge, -1);
  74. }
  75. [Test]
  76. [ExpectedException (typeof (ArgumentNullException))]
  77. public void WaitAny_WaitHandle_Empty_Int ()
  78. {
  79. WaitHandle.WaitAny (Empty, -1);
  80. }
  81. [Test]
  82. public void WaitAny_WaitHandle_Int ()
  83. {
  84. // -1 is infinite
  85. Assert.AreEqual (0, WaitHandle.WaitAny (Single, -1), "WaitAny");
  86. }
  87. [Test]
  88. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  89. public void WaitAny_WaitHandle_Int_Negative ()
  90. {
  91. Assert.AreEqual (0, WaitHandle.WaitAny (Single, -2), "WaitAny");
  92. }
  93. [Test]
  94. [ExpectedException (typeof (ArgumentNullException))]
  95. public void WaitAny_WaitHandleNull_TimeSpan ()
  96. {
  97. WaitHandle.WaitAny (null, Infinite);
  98. }
  99. [Test]
  100. [ExpectedException (typeof (NotSupportedException))]
  101. public void WaitAny_WaitHandle_TooLarge_TimeSpan ()
  102. {
  103. WaitHandle.WaitAny (TooLarge, Infinite);
  104. }
  105. [Test]
  106. [ExpectedException (typeof (ArgumentNullException))]
  107. public void WaitAny_WaitHandle_Empty_TimeSpan ()
  108. {
  109. WaitHandle.WaitAny (Empty, Infinite);
  110. }
  111. [Test]
  112. public void WaitAny_WaitHandle_TimeSpan ()
  113. {
  114. Assert.AreEqual (Timeout.Infinite, (int) Infinite.TotalMilliseconds, "Infinite");
  115. Assert.AreEqual (0, WaitHandle.WaitAny (Single, Infinite), "WaitAny-Infinite");
  116. Assert.AreEqual (0, WaitHandle.WaitAny (Single, SmallNegative), "WaitAny-SmallNegative");
  117. }
  118. [Test]
  119. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  120. public void WaitAny_WaitHandle_TimeSpan_Negative ()
  121. {
  122. Assert.AreEqual (0, WaitHandle.WaitAny (Single, Negative), "WaitAny");
  123. }
  124. [Test]
  125. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  126. public void WaitAny_WaitHandle_TimeSpan_MaxValue ()
  127. {
  128. Assert.AreEqual (0, WaitHandle.WaitAny (Single, TimeSpan.MaxValue), "WaitAny");
  129. }
  130. [Test]
  131. [ExpectedException (typeof (ArgumentNullException))]
  132. public void WaitAll_WaitHandle_Null ()
  133. {
  134. WaitHandle.WaitAll (null);
  135. }
  136. [Test]
  137. [ExpectedException (typeof (NotSupportedException))]
  138. public void WaitAll_WaitHandle_TooLarge ()
  139. {
  140. WaitHandle.WaitAll (TooLarge);
  141. }
  142. [Test]
  143. [ExpectedException (typeof (ArgumentNullException))]
  144. public void WaitAll_WaitHandle_Empty ()
  145. {
  146. WaitHandle.WaitAll (Empty);
  147. }
  148. [Test]
  149. public void WaitAll_WaitHandle ()
  150. {
  151. Assert.IsTrue (WaitHandle.WaitAll (Single), "WaitAll");
  152. }
  153. [Test]
  154. [ExpectedException (typeof (ArgumentNullException))]
  155. public void WaitAll_WaitHandleNull_Int ()
  156. {
  157. WaitHandle.WaitAll (null, -1);
  158. }
  159. [Test]
  160. [ExpectedException (typeof (NotSupportedException))]
  161. public void WaitAll_WaitHandle_TooLarge_Int ()
  162. {
  163. WaitHandle.WaitAll (TooLarge, -1);
  164. }
  165. [Test]
  166. [ExpectedException (typeof (ArgumentNullException))]
  167. public void WaitAll_WaitHandle_Empty_Int ()
  168. {
  169. WaitHandle.WaitAll (Empty, -1);
  170. }
  171. [Test]
  172. public void WaitAll_WaitHandle_Int ()
  173. {
  174. // -1 is infinite
  175. Assert.IsTrue (WaitHandle.WaitAll (Single, -1), "WaitAll");
  176. }
  177. [Test]
  178. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  179. public void WaitAll_WaitHandle_Int_Negative ()
  180. {
  181. Assert.IsTrue (WaitHandle.WaitAll (Single, -2), "WaitAll");
  182. }
  183. [Test]
  184. [ExpectedException (typeof (ArgumentNullException))]
  185. public void WaitAll_WaitHandleNull_TimeSpan ()
  186. {
  187. WaitHandle.WaitAll (null, Infinite);
  188. }
  189. [Test]
  190. [ExpectedException (typeof (NotSupportedException))]
  191. public void WaitAll_WaitHandle_TooLarge_TimeSpan ()
  192. {
  193. WaitHandle.WaitAll (TooLarge, Infinite);
  194. }
  195. [Test]
  196. [ExpectedException (typeof (ArgumentNullException))]
  197. public void WaitAll_WaitHandle_Empty_TimeSpan ()
  198. {
  199. WaitHandle.WaitAll (Empty, Infinite);
  200. }
  201. [Test]
  202. public void WaitAll_WaitHandle_TimeSpan ()
  203. {
  204. Assert.AreEqual (Timeout.Infinite, (int) Infinite.TotalMilliseconds, "Infinite");
  205. Assert.IsTrue (WaitHandle.WaitAll (Single, Infinite), "WaitAll-Infinite");
  206. Assert.IsTrue (WaitHandle.WaitAll (Single, SmallNegative), "WaitAll-SmallNegative");
  207. }
  208. [Test]
  209. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  210. public void WaitAll_WaitHandle_TimeSpan_Negative ()
  211. {
  212. Assert.IsTrue (WaitHandle.WaitAll (Single, Negative), "WaitAll");
  213. }
  214. [Test]
  215. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  216. public void WaitAll_WaitHandle_TimeSpan_MaxValue ()
  217. {
  218. Assert.IsTrue (WaitHandle.WaitAll (Single, TimeSpan.MaxValue), "WaitAll");
  219. }
  220. [Test]
  221. public void WaitOne ()
  222. {
  223. Assert.IsTrue (Single [0].WaitOne (), "WaitOne");
  224. }
  225. [Test]
  226. public void WaitOne_Int ()
  227. {
  228. // -1 is infinite
  229. Assert.IsTrue (Single [0].WaitOne (-1), "WaitOne");
  230. }
  231. [Test]
  232. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  233. public void WaitOne_Int_Negative ()
  234. {
  235. Assert.IsTrue (Single [0].WaitOne (-2), "WaitOne");
  236. }
  237. [Test]
  238. public void WaitOne_TimeSpan ()
  239. {
  240. Assert.AreEqual (Timeout.Infinite, (int) Infinite.TotalMilliseconds, "Infinite");
  241. Assert.IsTrue (Single [0].WaitOne (Infinite), "WaitOne-Infinite");
  242. Assert.IsTrue (Single [0].WaitOne (SmallNegative), "WaitOne-SmallNegative");
  243. }
  244. [Test]
  245. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  246. public void WaitOne_TimeSpan_Negative ()
  247. {
  248. Assert.IsTrue (Single [0].WaitOne (Negative), "WaitOne");
  249. }
  250. [Test]
  251. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  252. public void WaitOne_TimeSpan_MaxValue ()
  253. {
  254. Assert.IsTrue (Single [0].WaitOne (TimeSpan.MaxValue), "WaitOne");
  255. }
  256. [Test]
  257. [ExpectedException (typeof (ArgumentNullException))]
  258. public void WaitAll_Empty ()
  259. {
  260. WaitHandle.WaitAll (new WaitHandle [0]);
  261. }
  262. [Test]
  263. [ExpectedException (typeof (ArgumentException))]
  264. public void WaitAny_Empty ()
  265. {
  266. WaitHandle.WaitAny (new WaitHandle [0]);
  267. }
  268. [Test]
  269. public void InterrupedWaitAny ()
  270. {
  271. using (var m1 = new Mutex (true)) {
  272. using (var m2 = new Mutex (true)) {
  273. using (var done = new ManualResetEvent (false)) {
  274. var thread = new Thread (() =>
  275. {
  276. try {
  277. WaitHandle.WaitAny (new WaitHandle [] { m1, m2 });
  278. } catch (ThreadInterruptedException) {
  279. done.Set ();
  280. }
  281. });
  282. thread.Start ();
  283. Thread.Sleep (100); // wait a bit so the thread can enter its wait
  284. thread.Interrupt ();
  285. Assert.IsTrue (thread.Join (1000), "Join");
  286. Assert.IsTrue (done.WaitOne (1000), "done");
  287. m1.ReleaseMutex ();
  288. m2.ReleaseMutex ();
  289. }
  290. }
  291. }
  292. }
  293. [Test]
  294. public void InterrupedWaitAll ()
  295. {
  296. using (var m1 = new Mutex (true)) {
  297. using (var m2 = new Mutex (true)) {
  298. using (var done = new ManualResetEvent (false)) {
  299. var thread = new Thread (() =>
  300. {
  301. try {
  302. WaitHandle.WaitAll (new WaitHandle [] { m1, m2 });
  303. } catch (ThreadInterruptedException) {
  304. done.Set ();
  305. }
  306. });
  307. thread.Start ();
  308. Thread.Sleep (100); // wait a bit so the thread can enter its wait
  309. thread.Interrupt ();
  310. Assert.IsTrue (thread.Join (1000), "Join");
  311. Assert.IsTrue (done.WaitOne (1000), "done");
  312. m1.ReleaseMutex ();
  313. m2.ReleaseMutex ();
  314. }
  315. }
  316. }
  317. }
  318. [Test]
  319. public void InterrupedWaitOne ()
  320. {
  321. using (var m1 = new Mutex (true)) {
  322. using (var done = new ManualResetEvent (false)) {
  323. var thread = new Thread (() =>
  324. {
  325. try {
  326. m1.WaitOne ();
  327. } catch (ThreadInterruptedException) {
  328. done.Set ();
  329. }
  330. });
  331. thread.Start ();
  332. Thread.Sleep (100); // wait a bit so the thread can enter its wait
  333. thread.Interrupt ();
  334. Assert.IsTrue (thread.Join (1000), "Join");
  335. Assert.IsTrue (done.WaitOne (1000), "done");
  336. m1.ReleaseMutex ();
  337. }
  338. }
  339. }
  340. }
  341. }