MutexTest.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. AssertEquals("#02 The default value of The mutex wrong set:",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. AssertEquals("#03 The value of The mutex wrong set:",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. AssertEquals("#04 The default value of The mutex wrong set:",class1.id,class1.marker);
  105. }
  106. */
  107. // Hangs #72534
  108. [Test]
  109. [Category("NotWorking")]
  110. public void TestWaitAndSignal1()
  111. {
  112. Mutex Sem = new Mutex (false);
  113. ConcClassLoop class1 = new ConcClassLoop (1, Sem);
  114. Thread thread1 = new Thread (new ThreadStart (class1.Loop));
  115. try {
  116. thread1.Start ();
  117. TestUtil.WaitForNotAlive (thread1, "");
  118. Assert.AreEqual (100, class1.marker);
  119. } finally {
  120. thread1.Abort ();
  121. }
  122. }
  123. // Hangs
  124. [Test]
  125. [Category("NotWorking")]
  126. [Ignore ("It hangs and breaks the domain which runs nunit-console itself")]
  127. public void TestWaitAndFoget1()
  128. {
  129. Mutex Sem = new Mutex(false);
  130. ConcClassLoop class1 = new ConcClassLoop(1,Sem);
  131. ConcClassLoop class2 = new ConcClassLoop(2,Sem);
  132. Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));
  133. Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));
  134. try {
  135. thread1.Start();
  136. TestUtil.WaitForNotAlive (thread1, "t1");
  137. thread2.Start();
  138. TestUtil.WaitForNotAlive (thread2, "t2");
  139. Assert.AreEqual (class2.id, class2.marker);
  140. } finally {
  141. thread1.Abort ();
  142. thread2.Abort ();
  143. }
  144. }
  145. [Test]
  146. [Category("TargetJvmNotSupported")] // IntPtr native handles are not supported for TARGET_JVM.
  147. public void TestHandle()
  148. {
  149. Mutex Sem = new Mutex();
  150. IntPtr Handle = Sem.Handle;
  151. }
  152. [Test] // bug #79358
  153. public void DoubleRelease ()
  154. {
  155. Mutex mutex = new Mutex ();
  156. mutex.WaitOne ();
  157. mutex.ReleaseMutex ();
  158. try {
  159. mutex.ReleaseMutex ();
  160. Assert.Fail ("#1");
  161. } catch (ApplicationException ex) {
  162. Assert.AreEqual (typeof (ApplicationException), ex.GetType (), "#2");
  163. }
  164. }
  165. }
  166. }