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 AssertEquals("#02 The default value of The mutex wrong set:",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 AssertEquals("#03 The value of The mutex wrong set:",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 AssertEquals("#04 The default value of The mutex wrong set:",class1.id,class1.marker);
\r
122 [Category("NotWorking")]
\r
123 public void TestWaitAndSignal1()
\r
125 Mutex Sem = new Mutex (false);
\r
126 ConcClassLoop class1 = new ConcClassLoop (1, Sem);
\r
127 Thread thread1 = new Thread (new ThreadStart (class1.Loop));
\r
130 TestUtil.WaitForNotAlive (thread1, "");
\r
131 Assert.AreEqual (100, class1.marker);
\r
139 [Category("NotWorking")]
\r
140 [Ignore ("It hangs and breaks the domain which runs nunit-console itself")]
\r
141 public void TestWaitAndFoget1()
\r
143 Mutex Sem = new Mutex(false);
\r
144 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
145 ConcClassLoop class2 = new ConcClassLoop(2,Sem);
\r
146 Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));
\r
147 Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));
\r
151 TestUtil.WaitForNotAlive (thread1, "t1");
\r
154 TestUtil.WaitForNotAlive (thread2, "t2");
\r
156 Assert.AreEqual (class2.id, class2.marker);
\r
164 [Category("TargetJvmNotSupported")] // IntPtr native handles are not supported for TARGET_JVM.
\r
165 public void TestHandle()
\r
167 Mutex Sem = new Mutex();
\r
168 IntPtr Handle = Sem.Handle;
\r
171 [Test] // bug #79358
\r
172 [Category ("NotWorking")]
\r
173 public void DoubleRelease ()
\r
175 Mutex mutex = new Mutex ();
\r
177 mutex.ReleaseMutex ();
\r
180 mutex.ReleaseMutex ();
\r
181 Assert.Fail ("#1");
\r
182 } catch (ApplicationException ex) {
\r
183 Assert.AreEqual (typeof (ApplicationException), ex.GetType (), "#2");
\r