MutexTest.cs 3.9 KB

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