MonitorTest.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //
  2. // MonitorTest.cs - NUnit test cases for System.Threading.Monitor
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Sebastien Pouliot ([email protected])
  7. //
  8. // Copyright (C) 2005, 2009 Novell, Inc (http://www.novell.com)
  9. //
  10. using NUnit.Framework;
  11. using System;
  12. using System.Threading;
  13. namespace MonoTests.System.Threading {
  14. [TestFixture]
  15. public class MonitorTest {
  16. TimeSpan Infinite = new TimeSpan (-10000); // -10000 ticks == -1 ms
  17. TimeSpan SmallNegative = new TimeSpan (-2); // between 0 and -1.0 (infinite) ms
  18. TimeSpan Negative = new TimeSpan (-20000); // really negative
  19. TimeSpan MaxValue = TimeSpan.FromMilliseconds ((long) Int32.MaxValue);
  20. TimeSpan TooLarge = TimeSpan.FromMilliseconds ((long) Int32.MaxValue + 1);
  21. [Test]
  22. [ExpectedException (typeof (SynchronizationLockException))]
  23. [Category ("NotWorking")] // test fails under MS FX 2.0 - maybe that worked on 1.x ?
  24. public void ExitNoEnter ()
  25. {
  26. object o = new object ();
  27. Monitor.Exit (o);
  28. }
  29. [Test]
  30. [ExpectedException (typeof (SynchronizationLockException))]
  31. [Category ("NotWorking")] // test fails under MS FX 2.0 - maybe that worked on 1.x ?
  32. public void OneEnterSeveralExits ()
  33. {
  34. object o = new object ();
  35. Monitor.Enter (o);
  36. Monitor.Exit (o);
  37. // fails here
  38. Monitor.Exit (o);
  39. Monitor.Exit (o);
  40. Monitor.Exit (o);
  41. }
  42. [Test]
  43. [ExpectedException (typeof (ArgumentNullException))]
  44. public void Enter_Null ()
  45. {
  46. Monitor.Enter (null);
  47. }
  48. [Test]
  49. [ExpectedException (typeof (ArgumentNullException))]
  50. public void Exit_Null ()
  51. {
  52. Monitor.Exit (null);
  53. }
  54. [Test]
  55. public void Enter_Exit ()
  56. {
  57. object o = new object ();
  58. Monitor.Enter (o);
  59. try {
  60. Assert.IsNotNull (o);
  61. }
  62. finally {
  63. Monitor.Exit (o);
  64. }
  65. }
  66. [Test]
  67. [ExpectedException (typeof (ArgumentNullException))]
  68. public void Pulse_Null ()
  69. {
  70. Monitor.Pulse (null);
  71. }
  72. [Test]
  73. [ExpectedException (typeof (SynchronizationLockException))]
  74. public void Pulse_Unlocked ()
  75. {
  76. object o = new object ();
  77. Monitor.Pulse (o);
  78. }
  79. [Test]
  80. public void Pulse ()
  81. {
  82. object o = new object ();
  83. lock (o) {
  84. Monitor.Pulse (o);
  85. }
  86. }
  87. [Test]
  88. [ExpectedException (typeof (ArgumentNullException))]
  89. public void PulseAll_Null ()
  90. {
  91. Monitor.PulseAll (null);
  92. }
  93. [Test]
  94. [ExpectedException (typeof (SynchronizationLockException))]
  95. public void PulseAll_Unlocked ()
  96. {
  97. object o = new object ();
  98. Monitor.PulseAll (o);
  99. }
  100. [Test]
  101. public void PulseAll ()
  102. {
  103. object o = new object ();
  104. lock (o) {
  105. Monitor.PulseAll (o);
  106. }
  107. }
  108. [Test]
  109. [ExpectedException (typeof (ArgumentNullException))]
  110. public void TryEnter_Null ()
  111. {
  112. Monitor.TryEnter (null);
  113. }
  114. [Test]
  115. public void TryEnter ()
  116. {
  117. object o = new object ();
  118. Assert.IsTrue (Monitor.TryEnter (o), "TryEnter");
  119. Assert.IsTrue (Monitor.TryEnter (o), "TryEnter-2");
  120. }
  121. [Test]
  122. [ExpectedException (typeof (ArgumentNullException))]
  123. public void TryEnter_Null_Int ()
  124. {
  125. Monitor.TryEnter (null, Timeout.Infinite);
  126. }
  127. [Test]
  128. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  129. public void TryEnter_Int_Negative ()
  130. {
  131. object o = new object ();
  132. Monitor.TryEnter (o, -2);
  133. }
  134. [Test]
  135. public void TryEnter_Int ()
  136. {
  137. object o = new object ();
  138. Assert.IsTrue (Monitor.TryEnter (o, 1), "TryEnter");
  139. Assert.IsTrue (Monitor.TryEnter (o, 2), "TryEnter-2");
  140. }
  141. [Test]
  142. [ExpectedException (typeof (ArgumentNullException))]
  143. public void TryEnter_Null_TimeSpan ()
  144. {
  145. Monitor.TryEnter (null, Timeout.Infinite);
  146. }
  147. [Test]
  148. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  149. public void TryEnter_TimeSpan_Negative ()
  150. {
  151. object o = new object ();
  152. Monitor.TryEnter (o, Negative);
  153. }
  154. [Test]
  155. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  156. public void TryEnter_TimeSpan_TooLarge ()
  157. {
  158. object o = new object ();
  159. Monitor.TryEnter (o, TooLarge);
  160. }
  161. [Test]
  162. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  163. public void TryEnter_Null_TimeSpan_TooLarge ()
  164. {
  165. // exception ordering test
  166. Monitor.TryEnter (null, TooLarge);
  167. }
  168. [Test]
  169. public void TryEnter_TimeSpan ()
  170. {
  171. object o = new object ();
  172. Assert.IsTrue (Monitor.TryEnter (o, Infinite), "TryEnter");
  173. Assert.IsTrue (Monitor.TryEnter (o, SmallNegative), "TryEnter-2");
  174. Assert.IsTrue (Monitor.TryEnter (o, MaxValue), "TryEnter-3");
  175. }
  176. [Test]
  177. [ExpectedException (typeof (ArgumentNullException))]
  178. public void Wait_Null ()
  179. {
  180. Monitor.Wait (null);
  181. }
  182. [Test]
  183. [ExpectedException (typeof (SynchronizationLockException))]
  184. public void Wait_Unlocked ()
  185. {
  186. object o = new object ();
  187. Assert.IsTrue (Monitor.Wait (o), "Wait");
  188. }
  189. // [Test] that would be Infinite
  190. public void Wait ()
  191. {
  192. object o = new object ();
  193. lock (o) {
  194. Assert.IsFalse (Monitor.Wait (o), "Wait");
  195. }
  196. }
  197. [Test]
  198. [ExpectedException (typeof (ArgumentNullException))]
  199. public void Wait_Null_Int ()
  200. {
  201. Monitor.Wait (null, Timeout.Infinite);
  202. }
  203. [Test]
  204. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  205. public void Wait_Int_Negative ()
  206. {
  207. object o = new object ();
  208. Monitor.Wait (o, -2);
  209. }
  210. [Test]
  211. [ExpectedException (typeof (SynchronizationLockException))]
  212. public void Wait_Int_Unlocked ()
  213. {
  214. object o = new object ();
  215. Assert.IsTrue (Monitor.Wait (o, 1), "Wait");
  216. }
  217. [Test]
  218. public void Wait_Int ()
  219. {
  220. object o = new object ();
  221. lock (o) {
  222. Assert.IsFalse (Monitor.Wait (o, 1), "Wait");
  223. }
  224. }
  225. [Test]
  226. [ExpectedException (typeof (ArgumentNullException))]
  227. public void Wait_Null_TimeSpan ()
  228. {
  229. Monitor.Wait (null, Timeout.Infinite);
  230. }
  231. [Test]
  232. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  233. public void Wait_TimeSpan_Negative ()
  234. {
  235. object o = new object ();
  236. Monitor.Wait (o, Negative);
  237. }
  238. [Test]
  239. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  240. public void Wait_TimeSpan_TooLarge ()
  241. {
  242. object o = new object ();
  243. Monitor.Wait (o, TooLarge);
  244. }
  245. [Test]
  246. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  247. public void Wait_Null_TimeSpan_TooLarge ()
  248. {
  249. // exception ordering test
  250. Monitor.Wait (null, TooLarge);
  251. }
  252. [Test]
  253. [ExpectedException (typeof (SynchronizationLockException))]
  254. public void Wait_TimeSpan_Unlocked ()
  255. {
  256. object o = new object ();
  257. Assert.IsTrue (Monitor.Wait (o, Infinite), "Wait");
  258. }
  259. [Test]
  260. public void Wait_TimeSpan ()
  261. {
  262. object o = new object ();
  263. lock (o) {
  264. Assert.IsFalse (Monitor.Wait (o, SmallNegative), "Wait");
  265. }
  266. }
  267. [Test]
  268. public void Enter_bool ()
  269. {
  270. object o = new object ();
  271. bool taken = false;
  272. Monitor.Enter (o, ref taken);
  273. Assert.IsTrue (taken, "Monitor.Enter (obj, ref taken)");
  274. }
  275. [Test]
  276. [ExpectedException (typeof (ArgumentException))]
  277. public void Enter_bool_argcheck ()
  278. {
  279. object o = new object ();
  280. bool taken = true;
  281. Monitor.Enter (o, ref taken);
  282. }
  283. [Test]
  284. [ExpectedException (typeof (ArgumentException))]
  285. public void Enter_bool_argcheck_fastpath ()
  286. {
  287. object o = new object ();
  288. bool taken = false;
  289. Monitor.Enter (o, ref taken);
  290. taken = true;
  291. Monitor.Enter (o, ref taken);
  292. }
  293. }
  294. }