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
79 protected override void SetUp() {}
\r
81 protected override void TearDown() {}
\r
83 public void TestCtor1()
\r
87 Mutex Sem = new Mutex();
\r
91 Fail("#01 Error Creating The Simple Mutex:" + e.ToString());
\r
95 public void TestCtorDefaultValue()
\r
97 Mutex Sem = new Mutex();
\r
98 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
99 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
\r
101 while(thread1.IsAlive);
\r
102 AssertEquals("#02 The default value of The mutex wrong set:",class1.id,class1.marker);
\r
105 public void TestCtorCtor2()
\r
107 Mutex Sem = new Mutex(false);
\r
108 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
109 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
\r
111 while(thread1.IsAlive);
\r
112 AssertEquals("#03 The value of The mutex wrong set:",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 AssertEquals("#04 The default value of The mutex wrong set:",class1.id,class1.marker);
\r
125 public void TestWaitAndSignal1()
\r
127 Mutex Sem = new Mutex(false);
\r
128 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
129 Thread thread1 = new Thread(new ThreadStart(class1.Loop));
\r
131 while(thread1.IsAlive);
\r
132 AssertEquals("#41 Mutex Worked InCorrecly:",100,class1.marker);
\r
134 public void TestWaitAndFoget1()
\r
136 Mutex Sem = new Mutex(false);
\r
137 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
\r
138 ConcClassLoop class2 = new ConcClassLoop(2,Sem);
\r
139 Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));
\r
140 Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));
\r
142 while(thread1.IsAlive);
\r
144 while(thread2.IsAlive);
\r
145 AssertEquals("#51 The Mutex Has been Kept after end of the thread:",
\r
146 class2.id,class2.marker);
\r
149 public void TestHandle()
\r
151 Mutex Sem = new Mutex();
\r
154 IntPtr Handle = Sem.Handle;
\r
156 catch (Exception e)
\r
158 Fail("#61 Unexpected Exception accessing Sem.Handle:" + e.ToString());
\r