Remove Thread.[Abort|Suspend|Resume] from TvOS/WatchOS.
[mono-project.git] / mcs / class / corlib / Test / System.Threading / MutexTest.cs
blob5149ece6900f034409ae09a0ef9558d292894388
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 System;
9 using System.Threading;
11 using NUnit.Framework;
13 namespace MonoTests.System.Threading
15 [TestFixture]
16 public class MutexTest
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 [Test]
79 public void TestCtor1()
81 Mutex Sem = new Mutex();
84 // These tests produce mutex release errors
85 /**
86 [Test]
87 public void TestCtorDefaultValue()
89 Mutex Sem = new Mutex();
90 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
91 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
92 thread1.Start();
93 while(thread1.IsAlive);
94 Assert.AreEqual(class1.id,class1.marker);
97 [Test]
98 public void TestCtorCtor2()
100 Mutex Sem = new Mutex(false);
101 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
102 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
103 thread1.Start();
104 while(thread1.IsAlive);
105 Assert.AreEqual(class1.id,class1.marker);
108 [Test]
109 public void TestCtorCtor3()
111 Mutex Sem = new Mutex(true);
112 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
113 Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
114 thread1.Start();
115 while(thread1.IsAlive);
116 Assert.AreEqual(class1.id,class1.marker);
120 [Test]
121 public void TestWaitAndSignal1()
123 Mutex Sem = new Mutex (false);
124 ConcClassLoop class1 = new ConcClassLoop (1, Sem);
125 Thread thread1 = new Thread (new ThreadStart (class1.Loop));
126 try {
127 thread1.Start ();
128 TestUtil.WaitForNotAlive (thread1, "");
129 Assert.AreEqual (100, class1.marker);
130 } finally {
131 #if MONO_FEATURE_THREAD_ABORT
132 thread1.Abort ();
133 #else
134 thread1.Interrupt ();
135 #endif
139 [Test]
140 public void TestWaitAndFoget1()
142 Mutex Sem = new Mutex(false);
143 ConcClassLoop class1 = new ConcClassLoop(1,Sem);
144 ConcClassLoop class2 = new ConcClassLoop(2,Sem);
145 Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));
146 Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));
148 try {
149 thread1.Start();
150 TestUtil.WaitForNotAlive (thread1, "t1");
152 thread2.Start();
153 TestUtil.WaitForNotAlive (thread2, "t2");
155 Assert.AreEqual (class2.id, class2.marker);
156 } finally {
157 #if MONO_FEATURE_THREAD_ABORT
158 thread1.Abort ();
159 thread2.Abort ();
160 #else
161 thread1.Interrupt ();
162 thread2.Interrupt ();
163 #endif
167 [Test]
168 public void TestHandle()
170 Mutex Sem = new Mutex();
171 IntPtr Handle = Sem.Handle;
174 [Test] // bug #79358
175 public void DoubleRelease ()
177 Mutex mutex = new Mutex ();
178 mutex.WaitOne ();
179 mutex.ReleaseMutex ();
181 try {
182 mutex.ReleaseMutex ();
183 Assert.Fail ("#1");
184 } catch (ApplicationException ex) {
185 Assert.AreEqual (typeof (ApplicationException), ex.GetType (), "#2");