WaitHandleTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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.Collections.Generic;
  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. [Test]
  258. [ExpectedException (typeof (ArgumentNullException))]
  259. public void WaitAll_Empty ()
  260. {
  261. WaitHandle.WaitAll (new WaitHandle [0]);
  262. }
  263. [Test]
  264. [ExpectedException (typeof (ArgumentException))]
  265. public void WaitAny_Empty ()
  266. {
  267. WaitHandle.WaitAny (new WaitHandle [0]);
  268. }
  269. [Test]
  270. public void InterrupedWaitAny ()
  271. {
  272. using (var m1 = new Mutex (true)) {
  273. using (var m2 = new Mutex (true)) {
  274. using (var done = new ManualResetEvent (false)) {
  275. var thread = new Thread (() =>
  276. {
  277. try {
  278. WaitHandle.WaitAny (new WaitHandle [] { m1, m2 });
  279. } catch (ThreadInterruptedException) {
  280. done.Set ();
  281. }
  282. });
  283. thread.Start ();
  284. Thread.Sleep (100); // wait a bit so the thread can enter its wait
  285. thread.Interrupt ();
  286. Assert.IsTrue (thread.Join (1000), "Join");
  287. Assert.IsTrue (done.WaitOne (1000), "done");
  288. m1.ReleaseMutex ();
  289. m2.ReleaseMutex ();
  290. }
  291. }
  292. }
  293. }
  294. [Test]
  295. public void InterrupedWaitAll ()
  296. {
  297. using (var m1 = new Mutex (true)) {
  298. using (var m2 = new Mutex (true)) {
  299. using (var done = new ManualResetEvent (false)) {
  300. var thread = new Thread (() =>
  301. {
  302. try {
  303. WaitHandle.WaitAll (new WaitHandle [] { m1, m2 });
  304. } catch (ThreadInterruptedException) {
  305. done.Set ();
  306. }
  307. });
  308. thread.Start ();
  309. Thread.Sleep (100); // wait a bit so the thread can enter its wait
  310. thread.Interrupt ();
  311. Assert.IsTrue (thread.Join (1000), "Join");
  312. Assert.IsTrue (done.WaitOne (1000), "done");
  313. m1.ReleaseMutex ();
  314. m2.ReleaseMutex ();
  315. }
  316. }
  317. }
  318. }
  319. [Test]
  320. public void InterrupedWaitOne ()
  321. {
  322. using (var m1 = new Mutex (true)) {
  323. using (var done = new ManualResetEvent (false)) {
  324. var thread = new Thread (() =>
  325. {
  326. try {
  327. m1.WaitOne ();
  328. } catch (ThreadInterruptedException) {
  329. done.Set ();
  330. }
  331. });
  332. thread.Start ();
  333. Thread.Sleep (100); // wait a bit so the thread can enter its wait
  334. thread.Interrupt ();
  335. Assert.IsTrue (thread.Join (1000), "Join");
  336. Assert.IsTrue (done.WaitOne (1000), "done");
  337. m1.ReleaseMutex ();
  338. }
  339. }
  340. }
  341. [Test]
  342. public void WaitOneWithAbandonedMutex ()
  343. {
  344. using (var m = new Mutex (false)) {
  345. var thread1 = new Thread (() => {
  346. m.WaitOne ();
  347. });
  348. thread1.Start ();
  349. thread1.Join (1000);
  350. try {
  351. m.WaitOne ();
  352. Assert.Fail ("Expected AbandonedMutexException");
  353. } catch (AbandonedMutexException) {
  354. }
  355. // Current thread should own the Mutex now
  356. var signalled = false;
  357. var thread2 = new Thread (() => {
  358. signalled = m.WaitOne (100);
  359. });
  360. thread2.Start ();
  361. thread2.Join (1000);
  362. Assert.IsFalse (signalled);
  363. // Since this thread owns the Mutex releasing it shouldn't fail
  364. m.ReleaseMutex ();
  365. // The Mutex should now be unowned
  366. try {
  367. m.ReleaseMutex ();
  368. Assert.Fail ("Expected ApplicationException");
  369. } catch (ApplicationException) {
  370. }
  371. }
  372. }
  373. [Test]
  374. public void WaitOneWithAbandonedMutexAndMultipleThreads ()
  375. {
  376. using (var m = new Mutex (true)) {
  377. var nonAbandoned = 0;
  378. var abandoned = 0;
  379. var n = 0;
  380. var threads = new List<Thread> ();
  381. for (int i = 0; i < 50; i++) {
  382. var thread = new Thread (() => {
  383. try {
  384. m.WaitOne ();
  385. nonAbandoned++;
  386. } catch (AbandonedMutexException) {
  387. abandoned++;
  388. }
  389. if (((n++) % 5) != 0)
  390. m.ReleaseMutex ();
  391. });
  392. thread.Start ();
  393. threads.Add (thread);
  394. }
  395. m.ReleaseMutex ();
  396. foreach (var thread in threads) {
  397. if (!thread.Join (1000)) {
  398. Assert.Fail ("Timed out");
  399. }
  400. }
  401. Assert.AreEqual (40, nonAbandoned);
  402. Assert.AreEqual (10, abandoned);
  403. }
  404. }
  405. [Test]
  406. public void WaitAnyWithSecondMutexAbandoned ()
  407. {
  408. using (var m1 = new Mutex (false)) {
  409. using (var m2 = new Mutex (false)) {
  410. var mainProceed = false;
  411. var thread2Proceed = false;
  412. var thread1 = new Thread (() => {
  413. m2.WaitOne ();
  414. });
  415. var thread2 = new Thread (() => {
  416. m1.WaitOne ();
  417. mainProceed = true;
  418. while (!thread2Proceed) {
  419. Thread.Sleep (10);
  420. }
  421. m1.ReleaseMutex ();
  422. });
  423. thread1.Start ();
  424. thread1.Join (1000);
  425. thread2.Start ();
  426. while (!mainProceed) {
  427. Thread.Sleep (10);
  428. }
  429. try {
  430. WaitHandle.WaitAny (new WaitHandle [] { m1, m2 });
  431. Assert.Fail ("Expected AbandonedMutexException");
  432. } catch (AbandonedMutexException e) {
  433. Assert.AreEqual (1, e.MutexIndex);
  434. Assert.AreEqual (m2, e.Mutex);
  435. } finally {
  436. thread2Proceed = true;
  437. thread2.Join (1000);
  438. }
  439. // Current thread should own the second Mutex now
  440. var signalled = -1;
  441. var thread3 = new Thread (() => {
  442. signalled = WaitHandle.WaitAny (new WaitHandle [] { m1, m2 }, 0);
  443. });
  444. thread3.Start ();
  445. thread3.Join (1000);
  446. Assert.AreEqual (0, signalled);
  447. // Since this thread owns the second Mutex releasing it shouldn't fail
  448. m2.ReleaseMutex ();
  449. // Second Mutex should now be unowned
  450. try {
  451. m2.ReleaseMutex ();
  452. Assert.Fail ("Expected ApplicationException");
  453. } catch (ApplicationException) {
  454. }
  455. // .NET allows the first Mutex which is now abandoned to be released multiple times by this thread
  456. m1.ReleaseMutex ();
  457. m1.ReleaseMutex ();
  458. }
  459. }
  460. }
  461. [Test]
  462. [ExpectedException (typeof (AbandonedMutexException))]
  463. public void WaitAllWithOneAbandonedMutex ()
  464. {
  465. using (var m1 = new Mutex (false)) {
  466. using (var m2 = new Mutex (false)) {
  467. var thread = new Thread (() => {
  468. m1.WaitOne ();
  469. });
  470. thread.Start ();
  471. thread.Join (1000);
  472. WaitHandle.WaitAll (new WaitHandle [] { m1, m2 });
  473. }
  474. }
  475. }
  476. #if MONO_FEATURE_THREAD_SUSPEND_RESUME
  477. [Test]
  478. public void WaitOneWithTimeoutAndSpuriousWake ()
  479. {
  480. /* This is to test that WaitEvent.WaitOne is not going to wait largely
  481. * more than its timeout. In this test, it shouldn't wait more than
  482. * 1500 milliseconds, with its timeout being 1000ms */
  483. using (ManualResetEvent mre = new ManualResetEvent (false))
  484. using (ManualResetEvent ready = new ManualResetEvent (false)) {
  485. var thread = new Thread (() => {
  486. ready.Set ();
  487. mre.WaitOne (1000);
  488. });
  489. thread.Start ();
  490. ready.WaitOne ();
  491. Thread.Sleep (10); // wait a bit so we enter mre.WaitOne
  492. DateTime end = DateTime.Now.AddMilliseconds (500);
  493. while (DateTime.Now < end) {
  494. thread.Suspend ();
  495. thread.Resume ();
  496. }
  497. Assert.IsTrue (thread.Join (1000), "#1");
  498. }
  499. }
  500. [Test]
  501. public void WaitAnyWithTimeoutAndSpuriousWake ()
  502. {
  503. /* This is to test that WaitEvent.WaitAny is not going to wait largely
  504. * more than its timeout. In this test, it shouldn't wait more than
  505. * 1500 milliseconds, with its timeout being 1000ms */
  506. using (ManualResetEvent mre1 = new ManualResetEvent (false))
  507. using (ManualResetEvent mre2 = new ManualResetEvent (false))
  508. using (ManualResetEvent ready = new ManualResetEvent (false)) {
  509. var thread = new Thread (() => {
  510. ready.Set ();
  511. WaitHandle.WaitAny (new [] { mre1, mre2 }, 1000);
  512. });
  513. thread.Start ();
  514. ready.WaitOne ();
  515. Thread.Sleep (10); // wait a bit so we enter WaitHandle.WaitAny ({mre1, mre2})
  516. DateTime end = DateTime.Now.AddMilliseconds (500);
  517. while (DateTime.Now < end) {
  518. thread.Suspend ();
  519. thread.Resume ();
  520. }
  521. Assert.IsTrue (thread.Join (1000), "#1");
  522. }
  523. }
  524. [Test]
  525. public void WaitAllWithTimeoutAndSpuriousWake ()
  526. {
  527. /* This is to test that WaitEvent.WaitAll is not going to wait largely
  528. * more than its timeout. In this test, it shouldn't wait more than
  529. * 1500 milliseconds, with its timeout being 1000ms */
  530. using (ManualResetEvent mre1 = new ManualResetEvent (false))
  531. using (ManualResetEvent mre2 = new ManualResetEvent (false))
  532. using (ManualResetEvent ready = new ManualResetEvent (false)) {
  533. var thread = new Thread (() => {
  534. ready.Set ();
  535. WaitHandle.WaitAll (new [] { mre1, mre2 }, 1000);
  536. });
  537. thread.Start ();
  538. ready.WaitOne ();
  539. Thread.Sleep (10); // wait a bit so we enter WaitHandle.WaitAll ({mre1, mre2})
  540. DateTime end = DateTime.Now.AddMilliseconds (500);
  541. while (DateTime.Now < end) {
  542. thread.Suspend ();
  543. thread.Resume ();
  544. }
  545. Assert.IsTrue (thread.Join (1000), "#1");
  546. }
  547. }
  548. #endif // MONO_FEATURE_THREAD_SUSPEND_RESUME
  549. }
  550. }