1 // MutexTest.cs - NUnit Test Cases for System.Threading.Mutex
\r
3 // Eduardo Garcia Cebollero <kiwnix@yahoo.es>
5 // (C) Eduardo Garcia Cebollero
\r
8 using NUnit.Framework;
\r
10 using System.Threading;
\r
12 namespace MonoTests.System.Threading
\r
15 public class MutexTest : TestCase
\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
78 protected override void SetUp() {}
\r
80 protected override void TearDown() {}
\r
82 public void TestCtor1()
\r
86 Mutex Sem = new Mutex();
\r
90 Fail("#01 Error Creating The Simple Mutex:" + e.ToString());
\r
94 // These tests produce mutex release errors
\r
96 public void TestCtorDefaultValue()
\r
98 Mutex Sem = new Mutex();
\r
99 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
100 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
\r
102 while(thread1.IsAlive);
\r
103 AssertEquals("#02 The default value of The mutex wrong set:",class1.id,class1.marker);
\r
106 public void TestCtorCtor2()
\r
108 Mutex Sem = new Mutex(false);
\r
109 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
110 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
\r
112 while(thread1.IsAlive);
\r
113 AssertEquals("#03 The value of The mutex wrong set:",class1.id,class1.marker);
\r
116 public void TestCtorCtor3()
\r
118 Mutex Sem = new Mutex(true);
\r
119 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
120 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
\r
122 while(thread1.IsAlive);
\r
123 AssertEquals("#04 The default value of The mutex wrong set:",class1.id,class1.marker);
\r
129 [Category("NotWorking")]
\r
130 public void TestWaitAndSignal1()
\r
132 Mutex Sem = new Mutex(false);
\r
133 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
134 Thread thread1 = new Thread(new ThreadStart(class1.Loop));
\r
137 TestUtil.WaitForNotAlive (thread1, "");
\r
138 AssertEquals("#41 Mutex Worked InCorrecly:",100,class1.marker);
\r
146 [Category("NotWorking")]
147 public void TestWaitAndFoget1()
\r
149 Mutex Sem = new Mutex(false);
\r
150 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
151 ConcClassLoop class2 = new ConcClassLoop(2,Sem);
\r
152 Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));
\r
153 Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));
\r
157 TestUtil.WaitForNotAlive (thread1, "t1");
\r
160 TestUtil.WaitForNotAlive (thread2, "t2");
\r
162 AssertEquals("#51 The Mutex Has been Kept after end of the thread:", class2.id,class2.marker);
\r
170 public void TestHandle()
\r
172 Mutex Sem = new Mutex();
\r
175 IntPtr Handle = Sem.Handle;
\r
177 catch (Exception e)
\r
179 Fail("#61 Unexpected Exception accessing Sem.Handle:" + e.ToString());
\r