MutexTest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. public void TestWaitAndSignal1()
  112. {
  113. Mutex Sem = new Mutex(false);
  114. ConcClassLoop class1 = new ConcClassLoop(1,Sem);
  115. Thread thread1 = new Thread(new ThreadStart(class1.Loop));
  116. try {
  117. thread1.Start();
  118. TestUtil.WaitForNotAlive (thread1, "");
  119. AssertEquals("#41 Mutex Worked InCorrecly:",100,class1.marker);
  120. }
  121. finally {
  122. thread1.Abort ();
  123. }
  124. }
  125. public void TestWaitAndFoget1()
  126. {
  127. Mutex Sem = new Mutex(false);
  128. ConcClassLoop class1 = new ConcClassLoop(1,Sem);
  129. ConcClassLoop class2 = new ConcClassLoop(2,Sem);
  130. Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));
  131. Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));
  132. try {
  133. thread1.Start();
  134. TestUtil.WaitForNotAlive (thread1, "t1");
  135. thread2.Start();
  136. TestUtil.WaitForNotAlive (thread2, "t2");
  137. AssertEquals("#51 The Mutex Has been Kept after end of the thread:", class2.id,class2.marker);
  138. }
  139. finally {
  140. thread1.Abort ();
  141. thread2.Abort ();
  142. }
  143. }
  144. public void TestHandle()
  145. {
  146. Mutex Sem = new Mutex();
  147. try
  148. {
  149. IntPtr Handle = Sem.Handle;
  150. }
  151. catch (Exception e)
  152. {
  153. Fail("#61 Unexpected Exception accessing Sem.Handle:" + e.ToString());
  154. }
  155. }
  156. }
  157. }