MutexTest.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. [Ignore("this hangs mono")]
  112. public void TestWaitAndSignal1()
  113. {
  114. Mutex Sem = new Mutex(false);
  115. ConcClassLoop class1 = new ConcClassLoop(1,Sem);
  116. Thread thread1 = new Thread(new ThreadStart(class1.Loop));
  117. thread1.Start();
  118. while(thread1.IsAlive);
  119. AssertEquals("#41 Mutex Worked InCorrecly:",100,class1.marker);
  120. }
  121. [Ignore("Test hangs mono")]
  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. thread1.Start();
  130. while(thread1.IsAlive);
  131. thread2.Start();
  132. while(thread2.IsAlive);
  133. AssertEquals("#51 The Mutex Has been Kept after end of the thread:",
  134. class2.id,class2.marker);
  135. }
  136. public void TestHandle()
  137. {
  138. Mutex Sem = new Mutex();
  139. try
  140. {
  141. IntPtr Handle = Sem.Handle;
  142. }
  143. catch (Exception e)
  144. {
  145. Fail("#61 Unexpected Exception accessing Sem.Handle:" + e.ToString());
  146. }
  147. }
  148. }
  149. }