fixes for tests that fail
[mono-project.git] / mcs / class / corlib / Test / System.Threading / MutexTest.cs
blob2b4d786ca2c8d7504a2b2eb89271f7d6f7517b57
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);
128 // Hangs
129 [Category("NotWorking")]
130 public void TestWaitAndSignal1()
132 Mutex Sem = new Mutex(false);
133 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
134 Thread thread1 = new Thread(new ThreadStart(class1.Loop));
135 try {
136 thread1.Start();
137 TestUtil.WaitForNotAlive (thread1, "");
138 AssertEquals("#41 Mutex Worked InCorrecly:",100,class1.marker);
140 finally {
141 thread1.Abort ();
145 // Hangs
146 [Category("NotWorking")]
147 public void TestWaitAndFoget1()
149 Mutex Sem = new Mutex(false);
150 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
151 ConcClassLoop class2 = new ConcClassLoop(2,Sem);
152 Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));
153 Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));
155 try {
156 thread1.Start();
157 TestUtil.WaitForNotAlive (thread1, "t1");
159 thread2.Start();
160 TestUtil.WaitForNotAlive (thread2, "t2");
162 AssertEquals("#51 The Mutex Has been Kept after end of the thread:", class2.id,class2.marker);
164 finally {
165 thread1.Abort ();
166 thread2.Abort ();
170 public void TestHandle()
172 Mutex Sem = new Mutex();
175 IntPtr Handle = Sem.Handle;
177 catch (Exception e)
179 Fail("#61 Unexpected Exception accessing Sem.Handle:" + e.ToString());