ManualResetEventSlimTests.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // ManualResetEventSlimTests.cs
  2. //
  3. // Authors:
  4. // Marek Safar ([email protected])
  5. // Jeremie Laval ([email protected])
  6. //
  7. // Copyright (c) 2008 Jérémie "Garuma" Laval
  8. // Copyright (c) 2012 Xamarin, Inc (http://www.xamarin.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to deal
  12. // in the Software without restriction, including without limitation the rights
  13. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. // THE SOFTWARE.
  27. //
  28. //
  29. using System;
  30. using System.Threading;
  31. using NUnit.Framework;
  32. using MonoTests.System.Threading.Tasks;
  33. namespace MonoTests.System.Threading
  34. {
  35. [TestFixture]
  36. public class ManualResetEventSlimTests
  37. {
  38. ManualResetEventSlim mre;
  39. [SetUp]
  40. public void Setup()
  41. {
  42. mre = new ManualResetEventSlim();
  43. }
  44. [Test]
  45. public void Constructor_Defaults ()
  46. {
  47. Assert.IsFalse (mre.IsSet, "#1");
  48. Assert.AreEqual (Environment.ProcessorCount == 1 ? 1 : 10, mre.SpinCount, "#2");
  49. }
  50. [Test]
  51. public void Constructor_Invalid ()
  52. {
  53. try {
  54. new ManualResetEventSlim (true, -1);
  55. Assert.Fail ("#1");
  56. } catch (ArgumentException) {
  57. }
  58. try {
  59. new ManualResetEventSlim (true, 2048);
  60. Assert.Fail ("#2");
  61. } catch (ArgumentException) {
  62. }
  63. }
  64. [Test]
  65. public void IsSetTestCase()
  66. {
  67. Assert.IsFalse(mre.IsSet, "#1");
  68. mre.Set();
  69. Assert.IsTrue(mre.IsSet, "#2");
  70. mre.Reset();
  71. Assert.IsFalse(mre.IsSet, "#3");
  72. }
  73. [Test]
  74. public void WaitTest()
  75. {
  76. int count = 0;
  77. bool s = false;
  78. ParallelTestHelper.ParallelStressTest(mre, delegate (ManualResetEventSlim m) {
  79. if (Interlocked.Increment(ref count) % 2 == 0) {
  80. Thread.Sleep(50);
  81. for (int i = 0; i < 10; i++) {
  82. if (i % 2 == 0)
  83. m.Reset();
  84. else
  85. m.Set();
  86. }
  87. } else {
  88. m.Wait();
  89. s = true;
  90. }
  91. }, 2);
  92. Assert.IsTrue(s, "#1");
  93. Assert.IsTrue(mre.IsSet, "#2");
  94. }
  95. [Test]
  96. public void Wait_SetConcurrent ()
  97. {
  98. for (int i = 0; i < 10000; ++i) {
  99. var mre = new ManualResetEventSlim ();
  100. bool b = true;
  101. ThreadPool.QueueUserWorkItem (delegate {
  102. mre.Set ();
  103. });
  104. ThreadPool.QueueUserWorkItem (delegate {
  105. b &= mre.Wait (1000);
  106. });
  107. Assert.IsTrue (mre.Wait (1000), i.ToString ());
  108. Assert.IsTrue (b, i.ToString ());
  109. }
  110. }
  111. [Test]
  112. public void Wait_DisposeWithCancel ()
  113. {
  114. var token = new CancellationTokenSource ();
  115. ThreadPool.QueueUserWorkItem (delegate {
  116. Thread.Sleep (10);
  117. mre.Dispose ();
  118. token.Cancel ();
  119. });
  120. try {
  121. mre.Wait (10000, token.Token);
  122. Assert.Fail ("#0");
  123. } catch (OperationCanceledException e) {
  124. }
  125. }
  126. [Test]
  127. public void Wait_Expired ()
  128. {
  129. Assert.IsFalse (mre.Wait (10));
  130. }
  131. [Test, ExpectedException (typeof (ObjectDisposedException))]
  132. public void WaitAfterDisposeTest ()
  133. {
  134. mre.Dispose ();
  135. mre.Wait ();
  136. }
  137. [Test]
  138. public void SetAfterDisposeTest ()
  139. {
  140. ParallelTestHelper.Repeat (delegate {
  141. Exception disp = null, setting = null;
  142. CountdownEvent evt = new CountdownEvent (2);
  143. CountdownEvent evtFinish = new CountdownEvent (2);
  144. ThreadPool.QueueUserWorkItem (delegate {
  145. try {
  146. evt.Signal ();
  147. evt.Wait (1000);
  148. mre.Dispose ();
  149. } catch (Exception e) {
  150. disp = e;
  151. }
  152. evtFinish.Signal ();
  153. });
  154. ThreadPool.QueueUserWorkItem (delegate {
  155. try {
  156. evt.Signal ();
  157. evt.Wait (1000);
  158. mre.Set ();
  159. } catch (Exception e) {
  160. setting = e;
  161. }
  162. evtFinish.Signal ();
  163. });
  164. bool bb = evtFinish.Wait (1000);
  165. if (!bb)
  166. Assert.AreEqual (true, evtFinish.IsSet);
  167. Assert.IsTrue (bb, "#0");
  168. Assert.IsNull (disp, "#1");
  169. Assert.IsNull (setting, "#2");
  170. evt.Dispose ();
  171. evtFinish.Dispose ();
  172. });
  173. }
  174. [Test]
  175. public void WaitHandle_Initialized ()
  176. {
  177. var mre = new ManualResetEventSlim (true);
  178. Assert.IsTrue (mre.WaitHandle.WaitOne (0), "#1");
  179. mre.Reset ();
  180. Assert.IsFalse (mre.WaitHandle.WaitOne (0), "#2");
  181. Assert.AreEqual (mre.WaitHandle, mre.WaitHandle, "#3");
  182. }
  183. [Test]
  184. public void WaitHandle_NotInitialized ()
  185. {
  186. var mre = new ManualResetEventSlim (false);
  187. Assert.IsFalse (mre.WaitHandle.WaitOne (0), "#1");
  188. mre.Set ();
  189. Assert.IsTrue (mre.WaitHandle.WaitOne (0), "#2");
  190. }
  191. [Test]
  192. public void WaitHandleConsistencyTest ()
  193. {
  194. var mre = new ManualResetEventSlim ();
  195. mre.WaitHandle.WaitOne (0);
  196. for (int i = 0; i < 10000; i++) {
  197. int count = 2;
  198. SpinWait wait = new SpinWait ();
  199. ThreadPool.QueueUserWorkItem (_ => { while (count > 1) wait.SpinOnce (); mre.Set (); Interlocked.Decrement (ref count); });
  200. ThreadPool.QueueUserWorkItem (_ => { mre.Reset (); Interlocked.Decrement (ref count); });
  201. while (count > 0)
  202. wait.SpinOnce ();
  203. Assert.AreEqual (mre.IsSet, mre.WaitHandle.WaitOne (0));
  204. }
  205. }
  206. [Test]
  207. public void WaitWithCancellationTokenAndNotImmediateSetTest ()
  208. {
  209. var mres = new ManualResetEventSlim ();
  210. var cts = new CancellationTokenSource ();
  211. ThreadPool.QueueUserWorkItem(x => { Thread.Sleep (1000); mres.Set (); });
  212. Assert.IsTrue (mres.Wait (TimeSpan.FromSeconds (10), cts.Token), "Wait returned false despite event was set.");
  213. }
  214. [Test]
  215. public void WaitWithCancellationTokenAndCancel ()
  216. {
  217. var mres = new ManualResetEventSlim ();
  218. var cts = new CancellationTokenSource ();
  219. ThreadPool.QueueUserWorkItem(x => { Thread.Sleep (1000); cts.Cancel (); });
  220. try {
  221. mres.Wait (TimeSpan.FromSeconds (10), cts.Token);
  222. Assert.Fail ("Wait did not throw an exception despite cancellation token was cancelled.");
  223. } catch (OperationCanceledException) {
  224. }
  225. }
  226. [Test]
  227. public void WaitWithCancellationTokenAndTimeout ()
  228. {
  229. var mres = new ManualResetEventSlim ();
  230. var cts = new CancellationTokenSource ();
  231. Assert.IsFalse (mres.Wait (TimeSpan.FromSeconds (1), cts.Token), "Wait returned true despite timeout.");
  232. }
  233. [Test]
  234. public void Dispose ()
  235. {
  236. var mre = new ManualResetEventSlim (false);
  237. mre.Dispose ();
  238. Assert.IsFalse (mre.IsSet, "#0a");
  239. try {
  240. mre.Reset ();
  241. Assert.Fail ("#1");
  242. } catch (ObjectDisposedException) {
  243. }
  244. mre.Set ();
  245. try {
  246. mre.Wait (0);
  247. Assert.Fail ("#3");
  248. } catch (ObjectDisposedException) {
  249. }
  250. try {
  251. var v = mre.WaitHandle;
  252. Assert.Fail ("#4");
  253. } catch (ObjectDisposedException) {
  254. }
  255. }
  256. [Test]
  257. public void Dispose_Double ()
  258. {
  259. var mre = new ManualResetEventSlim ();
  260. mre.Dispose ();
  261. mre.Dispose ();
  262. }
  263. }
  264. }