Mark tests as not working under TARGET_JVM
[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                         AssertEquals("#02 The default value of The mutex wrong set:",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                         AssertEquals("#03 The value of The mutex wrong set:",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                         AssertEquals("#04 The default value of The mutex wrong set:",class1.id,class1.marker);\r
117                 }\r
118 */\r
119 \r
120                 // Hangs #72534\r
121                 [Test]\r
122                 [Category("NotWorking")]\r
123                 public void TestWaitAndSignal1()\r
124                 {\r
125                         Mutex Sem = new Mutex (false);\r
126                         ConcClassLoop class1 = new ConcClassLoop (1, Sem);\r
127                         Thread thread1 = new Thread (new ThreadStart (class1.Loop));\r
128                         try {\r
129                                 thread1.Start ();\r
130                                 TestUtil.WaitForNotAlive (thread1, "");\r
131                                 Assert.AreEqual (100, class1.marker);\r
132                         } finally {\r
133                                 thread1.Abort ();\r
134                         }\r
135                 }\r
136 \r
137                 // Hangs\r
138                 [Test]\r
139                 [Category("NotWorking")]\r
140                 [Ignore ("It hangs and breaks the domain which runs nunit-console itself")]\r
141                 public void TestWaitAndFoget1()\r
142                 {\r
143                         Mutex Sem = new Mutex(false);\r
144                         ConcClassLoop class1 = new ConcClassLoop(1,Sem);\r
145                         ConcClassLoop class2 = new ConcClassLoop(2,Sem);\r
146                         Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));\r
147                         Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));\r
148                         \r
149                         try {\r
150                                 thread1.Start();\r
151                                 TestUtil.WaitForNotAlive (thread1, "t1");\r
152         \r
153                                 thread2.Start();\r
154                                 TestUtil.WaitForNotAlive (thread2, "t2");\r
155                         \r
156                                 Assert.AreEqual (class2.id, class2.marker);\r
157                         } finally {\r
158                                 thread1.Abort ();\r
159                                 thread2.Abort ();\r
160                         }\r
161                 }\r
162 \r
163                 [Test]\r
164                 [Category("TargetJvmNotSupported")] // IntPtr native handles are not supported for TARGET_JVM.\r
165                 public void TestHandle()\r
166                 {\r
167                         Mutex Sem = new Mutex();\r
168                         IntPtr Handle = Sem.Handle;\r
169                 }\r
170 \r
171                 [Test] // bug #79358\r
172                 [Category ("NotWorking")]\r
173                 public void DoubleRelease ()\r
174                 {\r
175                         Mutex mutex = new Mutex ();\r
176                         mutex.WaitOne ();\r
177                         mutex.ReleaseMutex ();\r
178 \r
179                         try {\r
180                                 mutex.ReleaseMutex ();\r
181                                 Assert.Fail ("#1");\r
182                         } catch (ApplicationException ex) {\r
183                                 Assert.AreEqual (typeof (ApplicationException), ex.GetType (), "#2");\r
184                         }\r
185                 }\r
186         }\r
187 }\r