MutexTest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. thread1.Abort ();
  119. }
  120. }
  121. [Test]
  122. public void TestWaitAndFoget1()
  123. {
  124. Mutex Sem = new Mutex(false);
  125. ConcClassLoop class1 = new ConcClassLoop(1,Sem);
  126. ConcClassLoop class2 = new ConcClassLoop(2,Sem);
  127. Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));
  128. Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));
  129. try {
  130. thread1.Start();
  131. TestUtil.WaitForNotAlive (thread1, "t1");
  132. thread2.Start();
  133. TestUtil.WaitForNotAlive (thread2, "t2");
  134. Assert.AreEqual (class2.id, class2.marker);
  135. } finally {
  136. thread1.Abort ();
  137. thread2.Abort ();
  138. }
  139. }
  140. [Test]
  141. [Category("TargetJvmNotSupported")] // IntPtr native handles are not supported for TARGET_JVM.
  142. public void TestHandle()
  143. {
  144. Mutex Sem = new Mutex();
  145. IntPtr Handle = Sem.Handle;
  146. }
  147. [Test] // bug #79358
  148. public void DoubleRelease ()
  149. {
  150. Mutex mutex = new Mutex ();
  151. mutex.WaitOne ();
  152. mutex.ReleaseMutex ();
  153. try {
  154. mutex.ReleaseMutex ();
  155. Assert.Fail ("#1");
  156. } catch (ApplicationException ex) {
  157. Assert.AreEqual (typeof (ApplicationException), ex.GetType (), "#2");
  158. }
  159. }
  160. }
  161. }