(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / Test / System.Threading / MutexTest.cs
blobbf7d2fb58c1393fd3b628c6f88b12adab9f78c79
1 // MutexTest.cs - NUnit Test Cases for System.Threading.Mutex
2 //
3 // Eduardo Garcia Cebollero <kiwnix@yahoo.es>
4 //
5 // (C) Eduardo Garcia Cebollero
6 //
8 using NUnit.Framework;
9 using System;
10 using System.Threading;
12 namespace MonoTests.System.Threading
15 public class MutexTest : TestCase
18 //Auxiliary Classes (Future Threads)
19 private class ConcClass
21 public int id;
22 public Mutex mut;
23 public ConcClass(int id,Mutex mut)
25 this.id = id;
26 this.mut = mut;
28 public void wait()
30 mut.WaitOne();
32 public void signal()
34 mut.ReleaseMutex();
37 private class ConcClassLoop: ConcClass
39 public int marker;
41 public ConcClassLoop(int id,Mutex mut) :
42 base(id,mut)
44 this.marker = 0;
47 public void WithoutWait()
49 this.marker = this.id;
50 this.signal();
54 public void Loop()
56 while (this.marker<100)
58 this.wait();
59 this.marker++;
60 this.signal();
64 public void WaitAndForget()
66 this.wait();
67 this.marker = id;
69 public void WaitAndWait()
71 mut.WaitOne();
72 this.marker = this.id;
73 mut.WaitOne();
74 this.marker = this.id+1;
78 protected override void SetUp() {}
80 protected override void TearDown() {}
82 public void TestCtor1()
84 try
86 Mutex Sem = new Mutex();
88 catch (Exception e)
90 Fail("#01 Error Creating The Simple Mutex:" + e.ToString());
94 // These tests produce mutex release errors
96 public void TestCtorDefaultValue()
98 Mutex Sem = new Mutex();
99 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
100 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
101 thread1.Start();
102 while(thread1.IsAlive);
103 AssertEquals("#02 The default value of The mutex wrong set:",class1.id,class1.marker);
106 public void TestCtorCtor2()
108 Mutex Sem = new Mutex(false);
109 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
110 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
111 thread1.Start();
112 while(thread1.IsAlive);
113 AssertEquals("#03 The value of The mutex wrong set:",class1.id,class1.marker);
116 public void TestCtorCtor3()
118 Mutex Sem = new Mutex(true);
119 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
120 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
121 thread1.Start();
122 while(thread1.IsAlive);
123 AssertEquals("#04 The default value of The mutex wrong set:",class1.id,class1.marker);
127 public void TestWaitAndSignal1()
129 Mutex Sem = new Mutex(false);
130 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
131 Thread thread1 = new Thread(new ThreadStart(class1.Loop));
132 try {
133 thread1.Start();
134 TestUtil.WaitForNotAlive (thread1, "");
135 AssertEquals("#41 Mutex Worked InCorrecly:",100,class1.marker);
137 finally {
138 thread1.Abort ();
142 public void TestWaitAndFoget1()
144 Mutex Sem = new Mutex(false);
145 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
146 ConcClassLoop class2 = new ConcClassLoop(2,Sem);
147 Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));
148 Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));
150 try {
151 thread1.Start();
152 TestUtil.WaitForNotAlive (thread1, "t1");
154 thread2.Start();
155 TestUtil.WaitForNotAlive (thread2, "t2");
157 AssertEquals("#51 The Mutex Has been Kept after end of the thread:", class2.id,class2.marker);
159 finally {
160 thread1.Abort ();
161 thread2.Abort ();
165 public void TestHandle()
167 Mutex Sem = new Mutex();
170 IntPtr Handle = Sem.Handle;
172 catch (Exception e)
174 Fail("#61 Unexpected Exception accessing Sem.Handle:" + e.ToString());