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 bool abandoned_exception;
\r
24 public ConcClass(int id,Mutex mut)
\r
33 public void Signal()
\r
38 private class ConcClassLoop: ConcClass
\r
42 public ConcClassLoop(int id,Mutex mut) :
\r
48 public void WithoutWait()
\r
50 this.marker = this.id;
\r
57 while (this.marker<100)
\r
65 public void WaitAndForget()
\r
69 } catch (AbandonedMutexException) {
\r
70 this.abandoned_exception = true;
\r
75 public void WaitAndWait()
\r
78 this.marker = this.id;
\r
80 this.marker = this.id+1;
\r
85 public void TestCtor1()
\r
87 Mutex Sem = new Mutex();
\r
90 // These tests produce mutex release errors
\r
93 public void TestCtorDefaultValue()
\r
95 Mutex Sem = new Mutex();
\r
96 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
97 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
\r
99 while(thread1.IsAlive);
\r
100 Assert.AreEqual(class1.id,class1.marker);
\r
104 public void TestCtorCtor2()
\r
106 Mutex Sem = new Mutex(false);
\r
107 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
108 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
\r
110 while(thread1.IsAlive);
\r
111 Assert.AreEqual(class1.id,class1.marker);
\r
115 public void TestCtorCtor3()
\r
117 Mutex Sem = new Mutex(true);
\r
118 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
119 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
\r
121 while(thread1.IsAlive);
\r
122 Assert.AreEqual(class1.id,class1.marker);
\r
127 public void TestWaitAndSignal1()
\r
129 Mutex Sem = new Mutex (false);
\r
130 ConcClassLoop class1 = new ConcClassLoop (1, Sem);
\r
131 Thread thread1 = new Thread (new ThreadStart (class1.Loop));
\r
134 TestUtil.WaitForNotAlive (thread1, "");
\r
135 Assert.AreEqual (100, class1.marker);
\r
137 #if MONO_FEATURE_THREAD_ABORT
\r
140 thread1.Interrupt ();
\r
146 public void TestWaitAndForget1()
\r
148 Mutex Sem = new Mutex(false);
\r
149 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
150 ConcClassLoop class2 = new ConcClassLoop(2,Sem);
\r
151 Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));
\r
152 Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));
\r
156 TestUtil.WaitForNotAlive (thread1, "t1");
\r
157 Assert.IsFalse (class1.abandoned_exception, "e1");
\r
160 TestUtil.WaitForNotAlive (thread2, "t2");
\r
161 Assert.IsTrue (class2.abandoned_exception, "e2");
\r
163 Assert.AreEqual (class2.id, class2.marker);
\r
165 #if MONO_FEATURE_THREAD_ABORT
\r
169 thread1.Interrupt ();
\r
170 thread2.Interrupt ();
\r
176 public void TestHandle()
\r
178 Mutex Sem = new Mutex();
\r
179 IntPtr Handle = Sem.Handle;
\r
182 [Test] // bug #79358
\r
183 public void DoubleRelease ()
\r
185 Mutex mutex = new Mutex ();
\r
187 mutex.ReleaseMutex ();
\r
190 mutex.ReleaseMutex ();
\r
191 Assert.Fail ("#1");
\r
192 } catch (ApplicationException ex) {
\r
193 Assert.AreEqual (typeof (ApplicationException), ex.GetType (), "#2");
\r