MutexTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 NUnit.Framework;
  8. using System;
  9. using System.Threading;
  10. namespace MonoTests.System.Threading
  11. {
  12. public class MutexTest : TestCase
  13. {
  14. //Auxiliary Classes (Future Threads)
  15. private class ConcClass
  16. {
  17. public int id;
  18. public Mutex mut;
  19. public ConcClass(int id,Mutex mut)
  20. {
  21. this.id = id;
  22. this.mut = mut;
  23. }
  24. public void wait()
  25. {
  26. mut.WaitOne();
  27. }
  28. public void signal()
  29. {
  30. mut.ReleaseMutex();
  31. }
  32. }
  33. private class ConcClassLoop: ConcClass
  34. {
  35. public int marker;
  36. public ConcClassLoop(int id,Mutex mut) :
  37. base(id,mut)
  38. {
  39. this.marker = 0;
  40. }
  41. public void WithoutWait()
  42. {
  43. this.marker = this.id;
  44. this.signal();
  45. }
  46. public void Loop()
  47. {
  48. while (this.marker<100)
  49. {
  50. this.wait();
  51. this.marker++;
  52. this.signal();
  53. }
  54. }
  55. public void WaitAndForget()
  56. {
  57. this.wait();
  58. this.marker = id;
  59. }
  60. public void WaitAndWait()
  61. {
  62. mut.WaitOne();
  63. this.marker = this.id;
  64. mut.WaitOne();
  65. this.marker = this.id+1;
  66. }
  67. }
  68. protected override void SetUp() {}
  69. protected override void TearDown() {}
  70. public void TestCtor1()
  71. {
  72. try
  73. {
  74. Mutex Sem = new Mutex();
  75. }
  76. catch (Exception e)
  77. {
  78. Fail("#01 Error Creating The Simple Mutex:" + e.ToString());
  79. }
  80. }
  81. // These tests produce mutex release errors
  82. /*
  83. public void TestCtorDefaultValue()
  84. {
  85. Mutex Sem = new Mutex();
  86. ConcClassLoop class1 = new ConcClassLoop(1,Sem);
  87. Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
  88. thread1.Start();
  89. while(thread1.IsAlive);
  90. AssertEquals("#02 The default value of The mutex wrong set:",class1.id,class1.marker);
  91. }
  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. AssertEquals("#03 The value of The mutex wrong set:",class1.id,class1.marker);
  100. }
  101. public void TestCtorCtor3()
  102. {
  103. Mutex Sem = new Mutex(true);
  104. ConcClassLoop class1 = new ConcClassLoop(1,Sem);
  105. Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
  106. thread1.Start();
  107. while(thread1.IsAlive);
  108. AssertEquals("#04 The default value of The mutex wrong set:",class1.id,class1.marker);
  109. }
  110. */
  111. // Hangs #72534
  112. [Category("NotWorking")]
  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. AssertEquals("#41 Mutex Worked InCorrecly:",100,class1.marker);
  122. }
  123. finally {
  124. thread1.Abort ();
  125. }
  126. }
  127. // Hangs
  128. [Category("NotWorking")]
  129. [Ignore ("It hangs and breaks the domain which runs nunit-console itself")]
  130. public void TestWaitAndFoget1()
  131. {
  132. Mutex Sem = new Mutex(false);
  133. ConcClassLoop class1 = new ConcClassLoop(1,Sem);
  134. ConcClassLoop class2 = new ConcClassLoop(2,Sem);
  135. Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));
  136. Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));
  137. try {
  138. thread1.Start();
  139. TestUtil.WaitForNotAlive (thread1, "t1");
  140. thread2.Start();
  141. TestUtil.WaitForNotAlive (thread2, "t2");
  142. AssertEquals("#51 The Mutex Has been Kept after end of the thread:", class2.id,class2.marker);
  143. }
  144. finally {
  145. thread1.Abort ();
  146. thread2.Abort ();
  147. }
  148. }
  149. public void TestHandle()
  150. {
  151. Mutex Sem = new Mutex();
  152. try
  153. {
  154. IntPtr Handle = Sem.Handle;
  155. }
  156. catch (Exception e)
  157. {
  158. Fail("#61 Unexpected Exception accessing Sem.Handle:" + e.ToString());
  159. }
  160. }
  161. }
  162. }