1 // MutexTest.cs - NUnit Test Cases for System.Threading.Mutex
3 // Eduardo Garcia Cebollero <kiwnix@yahoo.es>
5 // (C) Eduardo Garcia Cebollero
10 using System
.Threading
;
12 namespace MonoTests
.System
.Threading
15 public class MutexTest
: TestCase
18 //Auxiliary Classes (Future Threads)
19 private class ConcClass
23 public ConcClass(int id
,Mutex mut
)
37 private class ConcClassLoop
: ConcClass
41 public ConcClassLoop(int id
,Mutex mut
) :
47 public void WithoutWait()
49 this.marker
= this.id
;
56 while (this.marker
<100)
64 public void WaitAndForget()
69 public void WaitAndWait()
72 this.marker
= this.id
;
74 this.marker
= this.id
+1;
78 protected override void SetUp() {}
80 protected override void TearDown() {}
82 public void TestCtor1()
86 Mutex Sem
= new Mutex();
90 Fail("#01 Error Creating The Simple Mutex:" + e
.ToString());
94 // These tests produce mutex release errors
96 public void TestCtorDefaultValue()
98 Mutex Sem = new Mutex();
99 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
100 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
102 while(thread1.IsAlive);
103 AssertEquals("#02 The default value of The mutex wrong set:",class1.id,class1.marker);
106 public void TestCtorCtor2()
108 Mutex Sem = new Mutex(false);
109 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
110 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
112 while(thread1.IsAlive);
113 AssertEquals("#03 The value of The mutex wrong set:",class1.id,class1.marker);
116 public void TestCtorCtor3()
118 Mutex Sem = new Mutex(true);
119 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
120 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
122 while(thread1.IsAlive);
123 AssertEquals("#04 The default value of The mutex wrong set:",class1.id,class1.marker);
127 public void TestWaitAndSignal1()
129 Mutex Sem
= new Mutex(false);
130 ConcClassLoop class1
= new ConcClassLoop(1,Sem
);
131 Thread thread1
= new Thread(new ThreadStart(class1
.Loop
));
134 TestUtil
.WaitForNotAlive (thread1
, "");
135 AssertEquals("#41 Mutex Worked InCorrecly:",100,class1
.marker
);
142 public void TestWaitAndFoget1()
144 Mutex Sem
= new Mutex(false);
145 ConcClassLoop class1
= new ConcClassLoop(1,Sem
);
146 ConcClassLoop class2
= new ConcClassLoop(2,Sem
);
147 Thread thread1
= new Thread(new ThreadStart(class1
.WaitAndForget
));
148 Thread thread2
= new Thread(new ThreadStart(class2
.WaitAndForget
));
152 TestUtil
.WaitForNotAlive (thread1
, "t1");
155 TestUtil
.WaitForNotAlive (thread2
, "t2");
157 AssertEquals("#51 The Mutex Has been Kept after end of the thread:", class2
.id
,class2
.marker
);
165 public void TestHandle()
167 Mutex Sem
= new Mutex();
170 IntPtr Handle
= Sem
.Handle
;
174 Fail("#61 Unexpected Exception accessing Sem.Handle:" + e
.ToString());