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);
129 [Category("NotWorking")]
130 public void TestWaitAndSignal1()
132 Mutex Sem
= new Mutex(false);
133 ConcClassLoop class1
= new ConcClassLoop(1,Sem
);
134 Thread thread1
= new Thread(new ThreadStart(class1
.Loop
));
137 TestUtil
.WaitForNotAlive (thread1
, "");
138 AssertEquals("#41 Mutex Worked InCorrecly:",100,class1
.marker
);
146 [Category("NotWorking")]
147 public void TestWaitAndFoget1()
149 Mutex Sem
= new Mutex(false);
150 ConcClassLoop class1
= new ConcClassLoop(1,Sem
);
151 ConcClassLoop class2
= new ConcClassLoop(2,Sem
);
152 Thread thread1
= new Thread(new ThreadStart(class1
.WaitAndForget
));
153 Thread thread2
= new Thread(new ThreadStart(class2
.WaitAndForget
));
157 TestUtil
.WaitForNotAlive (thread1
, "t1");
160 TestUtil
.WaitForNotAlive (thread2
, "t2");
162 AssertEquals("#51 The Mutex Has been Kept after end of the thread:", class2
.id
,class2
.marker
);
170 public void TestHandle()
172 Mutex Sem
= new Mutex();
175 IntPtr Handle
= Sem
.Handle
;
179 Fail("#61 Unexpected Exception accessing Sem.Handle:" + e
.ToString());