1 // MutexTest.cs - NUnit Test Cases for System.Threading.Mutex
3 // Eduardo Garcia Cebollero <kiwnix@yahoo.es>
5 // (C) Eduardo Garcia Cebollero
9 using System
.Threading
;
11 using NUnit
.Framework
;
13 namespace MonoTests
.System
.Threading
16 public class MutexTest
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;
79 public void TestCtor1()
81 Mutex Sem
= new Mutex();
84 // These tests produce mutex release errors
87 public void TestCtorDefaultValue()
89 Mutex Sem = new Mutex();
90 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
91 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
93 while(thread1.IsAlive);
94 Assert.AreEqual(class1.id,class1.marker);
98 public void TestCtorCtor2()
100 Mutex Sem = new Mutex(false);
101 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
102 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
104 while(thread1.IsAlive);
105 Assert.AreEqual(class1.id,class1.marker);
109 public void TestCtorCtor3()
111 Mutex Sem = new Mutex(true);
112 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
113 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
115 while(thread1.IsAlive);
116 Assert.AreEqual(class1.id,class1.marker);
121 public void TestWaitAndSignal1()
123 Mutex Sem
= new Mutex (false);
124 ConcClassLoop class1
= new ConcClassLoop (1, Sem
);
125 Thread thread1
= new Thread (new ThreadStart (class1
.Loop
));
128 TestUtil
.WaitForNotAlive (thread1
, "");
129 Assert
.AreEqual (100, class1
.marker
);
131 #if MONO_FEATURE_THREAD_ABORT
134 thread1
.Interrupt ();
140 public void TestWaitAndFoget1()
142 Mutex Sem
= new Mutex(false);
143 ConcClassLoop class1
= new ConcClassLoop(1,Sem
);
144 ConcClassLoop class2
= new ConcClassLoop(2,Sem
);
145 Thread thread1
= new Thread(new ThreadStart(class1
.WaitAndForget
));
146 Thread thread2
= new Thread(new ThreadStart(class2
.WaitAndForget
));
150 TestUtil
.WaitForNotAlive (thread1
, "t1");
153 TestUtil
.WaitForNotAlive (thread2
, "t2");
155 Assert
.AreEqual (class2
.id
, class2
.marker
);
157 #if MONO_FEATURE_THREAD_ABORT
161 thread1
.Interrupt ();
162 thread2
.Interrupt ();
168 public void TestHandle()
170 Mutex Sem
= new Mutex();
171 IntPtr Handle
= Sem
.Handle
;
175 public void DoubleRelease ()
177 Mutex mutex
= new Mutex ();
179 mutex
.ReleaseMutex ();
182 mutex
.ReleaseMutex ();
184 } catch (ApplicationException ex
) {
185 Assert
.AreEqual (typeof (ApplicationException
), ex
.GetType (), "#2");