MutexTest.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. public void TestCtorDefaultValue()
  82. {
  83. Mutex Sem = new Mutex();
  84. ConcClassLoop class1 = new ConcClassLoop(1,Sem);
  85. Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
  86. thread1.Start();
  87. while(thread1.IsAlive);
  88. AssertEquals("#02 The default value of The mutex wrong set:",class1.id,class1.marker);
  89. }
  90. public void TestCtorCtor2()
  91. {
  92. Mutex Sem = new Mutex(false);
  93. ConcClassLoop class1 = new ConcClassLoop(1,Sem);
  94. Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
  95. thread1.Start();
  96. while(thread1.IsAlive);
  97. AssertEquals("#03 The value of The mutex wrong set:",class1.id,class1.marker);
  98. }
  99. public void TestCtorCtor3()
  100. {
  101. Mutex Sem = new Mutex(true);
  102. ConcClassLoop class1 = new ConcClassLoop(1,Sem);
  103. Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
  104. thread1.Start();
  105. while(thread1.IsAlive);
  106. AssertEquals("#04 The default value of The mutex wrong set:",class1.id,class1.marker);
  107. }
  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. thread1.Start();
  114. while(thread1.IsAlive);
  115. AssertEquals("#41 Mutex Worked InCorrecly:",100,class1.marker);
  116. }
  117. public void TestWaitAndFoget1()
  118. {
  119. Mutex Sem = new Mutex(false);
  120. ConcClassLoop class1 = new ConcClassLoop(1,Sem);
  121. ConcClassLoop class2 = new ConcClassLoop(2,Sem);
  122. Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));
  123. Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));
  124. thread1.Start();
  125. while(thread1.IsAlive);
  126. thread2.Start();
  127. while(thread2.IsAlive);
  128. AssertEquals("#51 The Mutex Has been Kept after end of the thread:",
  129. class2.id,class2.marker);
  130. }
  131. public void TestHandle()
  132. {
  133. Mutex Sem = new Mutex();
  134. try
  135. {
  136. IntPtr Handle = Sem.Handle;
  137. }
  138. catch (Exception e)
  139. {
  140. Fail("#61 Unexpected Exception accessing Sem.Handle:" + e.ToString());
  141. }
  142. }
  143. }
  144. }