Remove Thread.[Abort|Suspend|Resume] from TvOS/WatchOS.
[mono.git] / mcs / class / corlib / Test / System.Threading / MutexTest.cs
1 // MutexTest.cs - NUnit Test Cases for System.Threading.Mutex\r
2 //\r
3 // Eduardo Garcia Cebollero <kiwnix@yahoo.es>\r
4 //\r
5 // (C) Eduardo Garcia Cebollero\r
6 // \r
7 \r
8 using System;\r
9 using System.Threading;\r
10 \r
11 using NUnit.Framework;\r
12 \r
13 namespace MonoTests.System.Threading\r
14 {\r
15         [TestFixture]\r
16         public class MutexTest\r
17         {\r
18                 //Auxiliary Classes (Future Threads)\r
19                 private class ConcClass\r
20                 {\r
21                         public int id;\r
22                         public Mutex mut;\r
23                         public ConcClass(int id,Mutex mut)\r
24                         {\r
25                                 this.id = id;\r
26                                 this.mut = mut;\r
27                         }\r
28                         public void Wait()\r
29                         {\r
30                                 mut.WaitOne();\r
31                         }\r
32                         public void Signal()\r
33                         {\r
34                                 mut.ReleaseMutex();\r
35                         }\r
36                 }\r
37                 private class ConcClassLoop: ConcClass\r
38                 {\r
39                         public int marker;\r
40 \r
41                         public ConcClassLoop(int id,Mutex mut) : \r
42                                 base(id,mut) \r
43                                 {\r
44                                         this.marker = 0;\r
45                                 }\r
46                         \r
47                         public void WithoutWait()\r
48                         {\r
49                                 this.marker = this.id;\r
50                                 this.Signal();\r
51                         }\r
52 \r
53 \r
54                         public void Loop()\r
55                         {\r
56                                 while (this.marker<100)\r
57                                 {\r
58                                         this.Wait();\r
59                                         this.marker++;\r
60                                         this.Signal();\r
61                                 }\r
62                         }\r
63 \r
64                         public void WaitAndForget()\r
65                         {\r
66                                 this.Wait();\r
67                                 this.marker = id;\r
68                         }\r
69                         public void WaitAndWait()\r
70                         {\r
71                                 mut.WaitOne();\r
72                                 this.marker = this.id;\r
73                                 mut.WaitOne();\r
74                                 this.marker = this.id+1;\r
75                         }\r
76                 }\r
77 \r
78                 [Test]\r
79                 public void TestCtor1()\r
80                 {\r
81                         Mutex Sem = new Mutex();\r
82                 }\r
83 \r
84 // These tests produce mutex release errors\r
85 /**\r
86                 [Test]\r
87                 public void TestCtorDefaultValue()\r
88                 {\r
89                         Mutex Sem = new Mutex();\r
90                         ConcClassLoop class1 = new ConcClassLoop(1,Sem);\r
91                         Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));\r
92                         thread1.Start();\r
93                         while(thread1.IsAlive);\r
94                         Assert.AreEqual(class1.id,class1.marker);\r
95                 }\r
96 \r
97                 [Test]\r
98                 public void TestCtorCtor2()\r
99                 {\r
100                         Mutex Sem = new Mutex(false);\r
101                         ConcClassLoop class1 = new ConcClassLoop(1,Sem);\r
102                         Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));\r
103                         thread1.Start();\r
104                         while(thread1.IsAlive);\r
105                         Assert.AreEqual(class1.id,class1.marker);\r
106                 }\r
107         \r
108                 [Test]\r
109                 public void TestCtorCtor3()\r
110                 {\r
111                         Mutex Sem = new Mutex(true);\r
112                         ConcClassLoop class1 = new ConcClassLoop(1,Sem);\r
113                         Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));\r
114                         thread1.Start();\r
115                         while(thread1.IsAlive);\r
116                         Assert.AreEqual(class1.id,class1.marker);\r
117                 }\r
118 */\r
119 \r
120                 [Test]\r
121                 public void TestWaitAndSignal1()\r
122                 {\r
123                         Mutex Sem = new Mutex (false);\r
124                         ConcClassLoop class1 = new ConcClassLoop (1, Sem);\r
125                         Thread thread1 = new Thread (new ThreadStart (class1.Loop));\r
126                         try {\r
127                                 thread1.Start ();\r
128                                 TestUtil.WaitForNotAlive (thread1, "");\r
129                                 Assert.AreEqual (100, class1.marker);\r
130                         } finally {\r
131 #if MONO_FEATURE_THREAD_ABORT\r
132                                 thread1.Abort ();\r
133 #else\r
134                                 thread1.Interrupt ();\r
135 #endif\r
136                         }\r
137                 }\r
138 \r
139                 [Test]\r
140                 public void TestWaitAndFoget1()\r
141                 {\r
142                         Mutex Sem = new Mutex(false);\r
143                         ConcClassLoop class1 = new ConcClassLoop(1,Sem);\r
144                         ConcClassLoop class2 = new ConcClassLoop(2,Sem);\r
145                         Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));\r
146                         Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));\r
147                         \r
148                         try {\r
149                                 thread1.Start();\r
150                                 TestUtil.WaitForNotAlive (thread1, "t1");\r
151         \r
152                                 thread2.Start();\r
153                                 TestUtil.WaitForNotAlive (thread2, "t2");\r
154                         \r
155                                 Assert.AreEqual (class2.id, class2.marker);\r
156                         } finally {\r
157 #if MONO_FEATURE_THREAD_ABORT\r
158                                 thread1.Abort ();\r
159                                 thread2.Abort ();\r
160 #else\r
161                                 thread1.Interrupt ();\r
162                                 thread2.Interrupt ();\r
163 #endif\r
164                         }\r
165                 }\r
166 \r
167                 [Test]\r
168                 public void TestHandle()\r
169                 {\r
170                         Mutex Sem = new Mutex();\r
171                         IntPtr Handle = Sem.Handle;\r
172                 }\r
173 \r
174                 [Test] // bug #79358\r
175                 public void DoubleRelease ()\r
176                 {\r
177                         Mutex mutex = new Mutex ();\r
178                         mutex.WaitOne ();\r
179                         mutex.ReleaseMutex ();\r
180 \r
181                         try {\r
182                                 mutex.ReleaseMutex ();\r
183                                 Assert.Fail ("#1");\r
184                         } catch (ApplicationException ex) {\r
185                                 Assert.AreEqual (typeof (ApplicationException), ex.GetType (), "#2");\r
186                         }\r
187                 }\r
188         }\r
189 }\r