1 // MutexTest.cs - NUnit Test Cases for System.Threading.Mutex
\r
3 // Eduardo Garcia Cebollero <kiwnix@yahoo.es>
\r
5 // (C) Eduardo Garcia Cebollero
\r
9 using System.Threading;
\r
11 using NUnit.Framework;
\r
13 namespace MonoTests.System.Threading
\r
16 public class MutexTest
\r
18 //Auxiliary Classes (Future Threads)
\r
19 private class ConcClass
\r
23 public ConcClass(int id,Mutex mut)
\r
32 public void Signal()
\r
37 private class ConcClassLoop: ConcClass
\r
41 public ConcClassLoop(int id,Mutex mut) :
\r
47 public void WithoutWait()
\r
49 this.marker = this.id;
\r
56 while (this.marker<100)
\r
64 public void WaitAndForget()
\r
69 public void WaitAndWait()
\r
72 this.marker = this.id;
\r
74 this.marker = this.id+1;
\r
79 public void TestCtor1()
\r
81 Mutex Sem = new Mutex();
\r
84 // These tests produce mutex release errors
\r
87 public void TestCtorDefaultValue()
\r
89 Mutex Sem = new Mutex();
\r
90 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
91 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
\r
93 while(thread1.IsAlive);
\r
94 Assert.AreEqual(class1.id,class1.marker);
\r
98 public void TestCtorCtor2()
\r
100 Mutex Sem = new Mutex(false);
\r
101 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
102 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
\r
104 while(thread1.IsAlive);
\r
105 Assert.AreEqual(class1.id,class1.marker);
\r
109 public void TestCtorCtor3()
\r
111 Mutex Sem = new Mutex(true);
\r
112 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
113 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
\r
115 while(thread1.IsAlive);
\r
116 Assert.AreEqual(class1.id,class1.marker);
\r
121 public void TestWaitAndSignal1()
\r
123 Mutex Sem = new Mutex (false);
\r
124 ConcClassLoop class1 = new ConcClassLoop (1, Sem);
\r
125 Thread thread1 = new Thread (new ThreadStart (class1.Loop));
\r
128 TestUtil.WaitForNotAlive (thread1, "");
\r
129 Assert.AreEqual (100, class1.marker);
\r
131 #if MONO_FEATURE_THREAD_ABORT
\r
134 thread1.Interrupt ();
\r
140 public void TestWaitAndFoget1()
\r
142 Mutex Sem = new Mutex(false);
\r
143 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
144 ConcClassLoop class2 = new ConcClassLoop(2,Sem);
\r
145 Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));
\r
146 Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));
\r
150 TestUtil.WaitForNotAlive (thread1, "t1");
\r
153 TestUtil.WaitForNotAlive (thread2, "t2");
\r
155 Assert.AreEqual (class2.id, class2.marker);
\r
157 #if MONO_FEATURE_THREAD_ABORT
\r
161 thread1.Interrupt ();
\r
162 thread2.Interrupt ();
\r
168 public void TestHandle()
\r
170 Mutex Sem = new Mutex();
\r
171 IntPtr Handle = Sem.Handle;
\r
174 [Test] // bug #79358
\r
175 public void DoubleRelease ()
\r
177 Mutex mutex = new Mutex ();
\r
179 mutex.ReleaseMutex ();
\r
182 mutex.ReleaseMutex ();
\r
183 Assert.Fail ("#1");
\r
184 } catch (ApplicationException ex) {
\r
185 Assert.AreEqual (typeof (ApplicationException), ex.GetType (), "#2");
\r