MutexTest.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // MutexTest.cs - NUnit Test Cases for System.Threading.Mutex
  2. //
  3. // Eduardo Garcia Cebollero <[email protected]>
  4. //
  5. // (C) Eduardo Garcia Cebollero
  6. //
  7. using System;
  8. using System.Threading;
  9. using NUnit.Framework;
  10. namespace MonoTests.System.Threading
  11. {
  12. [TestFixture]
  13. public class MutexTest
  14. {
  15. //Auxiliary Classes (Future Threads)
  16. private class ConcClass
  17. {
  18. public int id;
  19. public Mutex mut;
  20. public bool abandoned_exception;
  21. public ConcClass(int id,Mutex mut)
  22. {
  23. this.id = id;
  24. this.mut = mut;
  25. }
  26. public void Wait()
  27. {
  28. mut.WaitOne();
  29. }
  30. public void Signal()
  31. {
  32. mut.ReleaseMutex();
  33. }
  34. }
  35. private class ConcClassLoop: ConcClass
  36. {
  37. public int marker;
  38. public ConcClassLoop(int id,Mutex mut) :
  39. base(id,mut)
  40. {
  41. this.marker = 0;
  42. }
  43. public void WithoutWait()
  44. {
  45. this.marker = this.id;
  46. this.Signal();
  47. }
  48. public void Loop()
  49. {
  50. while (this.marker<100)
  51. {
  52. this.Wait();
  53. this.marker++;
  54. this.Signal();
  55. }
  56. }
  57. public void WaitAndForget()
  58. {
  59. try {
  60. this.Wait();
  61. } catch (AbandonedMutexException) {
  62. this.abandoned_exception = true;
  63. }
  64. this.marker = id;
  65. }
  66. public void WaitAndWait()
  67. {
  68. mut.WaitOne();
  69. this.marker = this.id;
  70. mut.WaitOne();
  71. this.marker = this.id+1;
  72. }
  73. }
  74. [Test]
  75. public void TestCtor1()
  76. {
  77. Mutex Sem = new Mutex();
  78. }
  79. // These tests produce mutex release errors
  80. /**
  81. [Test]
  82. public void TestCtorDefaultValue()
  83. {
  84. Mutex Sem = new Mutex();
  85. ConcClassLoop class1 = new ConcClassLoop(1,Sem);
  86. Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
  87. thread1.Start();
  88. while(thread1.IsAlive);
  89. Assert.AreEqual(class1.id,class1.marker);
  90. }
  91. [Test]
  92. public void TestCtorCtor2()
  93. {
  94. Mutex Sem = new Mutex(false);
  95. ConcClassLoop class1 = new ConcClassLoop(1,Sem);
  96. Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
  97. thread1.Start();
  98. while(thread1.IsAlive);
  99. Assert.AreEqual(class1.id,class1.marker);
  100. }
  101. [Test]
  102. public void TestCtorCtor3()
  103. {
  104. Mutex Sem = new Mutex(true);
  105. ConcClassLoop class1 = new ConcClassLoop(1,Sem);
  106. Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
  107. thread1.Start();
  108. while(thread1.IsAlive);
  109. Assert.AreEqual(class1.id,class1.marker);
  110. }
  111. */
  112. [Test]
  113. public void TestWaitAndSignal1()
  114. {
  115. Mutex Sem = new Mutex (false);
  116. ConcClassLoop class1 = new ConcClassLoop (1, Sem);
  117. Thread thread1 = new Thread (new ThreadStart (class1.Loop));
  118. try {
  119. thread1.Start ();
  120. TestUtil.WaitForNotAlive (thread1, "");
  121. Assert.AreEqual (100, class1.marker);
  122. } finally {
  123. #if MONO_FEATURE_THREAD_ABORT
  124. thread1.Abort ();
  125. #else
  126. thread1.Interrupt ();
  127. #endif
  128. }
  129. }
  130. [Test]
  131. public void TestWaitAndForget1()
  132. {
  133. Mutex Sem = new Mutex(false);
  134. ConcClassLoop class1 = new ConcClassLoop(1,Sem);
  135. ConcClassLoop class2 = new ConcClassLoop(2,Sem);
  136. Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));
  137. Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));
  138. try {
  139. thread1.Start();
  140. TestUtil.WaitForNotAlive (thread1, "t1");
  141. Assert.IsFalse (class1.abandoned_exception, "e1");
  142. thread2.Start();
  143. TestUtil.WaitForNotAlive (thread2, "t2");
  144. Assert.IsTrue (class2.abandoned_exception, "e2");
  145. Assert.AreEqual (class2.id, class2.marker);
  146. } finally {
  147. #if MONO_FEATURE_THREAD_ABORT
  148. thread1.Abort ();
  149. thread2.Abort ();
  150. #else
  151. thread1.Interrupt ();
  152. thread2.Interrupt ();
  153. #endif
  154. }
  155. }
  156. [Test]
  157. public void TestHandle()
  158. {
  159. Mutex Sem = new Mutex();
  160. IntPtr Handle = Sem.Handle;
  161. }
  162. [Test] // bug #79358
  163. public void DoubleRelease ()
  164. {
  165. Mutex mutex = new Mutex ();
  166. mutex.WaitOne ();
  167. mutex.ReleaseMutex ();
  168. try {
  169. mutex.ReleaseMutex ();
  170. Assert.Fail ("#1");
  171. } catch (ApplicationException ex) {
  172. Assert.AreEqual (typeof (ApplicationException), ex.GetType (), "#2");
  173. }
  174. }
  175. }
  176. }