remove unreliable stuff
[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>
4 //\r
5 // (C) Eduardo Garcia Cebollero\r
6 // \r
7 \r
8 using NUnit.Framework;\r
9 using System;\r
10 using System.Threading;\r
11 \r
12 namespace MonoTests.System.Threading\r
13 {\r
14 \r
15         public class MutexTest : TestCase \r
16         {\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                 protected override void SetUp() {}\r
79 \r
80                 protected override void TearDown() {}\r
81 \r
82                 public void TestCtor1()\r
83                 {\r
84                         try\r
85                         {\r
86                                 Mutex Sem = new Mutex();\r
87                         }\r
88                         catch (Exception e)\r
89                         {\r
90                                 Fail("#01 Error Creating The Simple Mutex:" + e.ToString());\r
91                         }\r
92                 }\r
93 \r
94 // These tests produce mutex release errors\r
95 /*\r
96                 public void TestCtorDefaultValue()\r
97                 {\r
98                         Mutex Sem = new Mutex();\r
99                         ConcClassLoop class1 = new ConcClassLoop(1,Sem);\r
100                         Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));\r
101                         thread1.Start();\r
102                         while(thread1.IsAlive);\r
103                         AssertEquals("#02 The default value of The mutex wrong set:",class1.id,class1.marker);\r
104                 }\r
105 \r
106                 public void TestCtorCtor2()\r
107                 {\r
108                         Mutex Sem = new Mutex(false);\r
109                         ConcClassLoop class1 = new ConcClassLoop(1,Sem);\r
110                         Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));\r
111                         thread1.Start();\r
112                         while(thread1.IsAlive);\r
113                         AssertEquals("#03 The value of The mutex wrong set:",class1.id,class1.marker);\r
114                 }\r
115                 \r
116                 public void TestCtorCtor3()\r
117                 {\r
118                         Mutex Sem = new Mutex(true);\r
119                         ConcClassLoop class1 = new ConcClassLoop(1,Sem);\r
120                         Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));\r
121                         thread1.Start();\r
122                         while(thread1.IsAlive);\r
123                         AssertEquals("#04 The default value of The mutex wrong set:",class1.id,class1.marker);\r
124                 }\r
125 \r
126 */
127                 
128                 // Hangs #72534
129                 [Category("NotWorking")]\r
130                 public void TestWaitAndSignal1()\r
131                 {\r
132                         Mutex Sem = new Mutex(false);\r
133                         ConcClassLoop class1 = new ConcClassLoop(1,Sem);\r
134                         Thread thread1 = new Thread(new ThreadStart(class1.Loop));\r
135                         try {\r
136                                 thread1.Start();\r
137                                 TestUtil.WaitForNotAlive (thread1, "");\r
138                                 AssertEquals("#41 Mutex Worked InCorrecly:",100,class1.marker);\r
139                         }\r
140                         finally {\r
141                                 thread1.Abort ();\r
142                         }\r
143                 }\r
144
145                 // Hangs
146                 [Category("NotWorking")]
147                 public void TestWaitAndFoget1()\r
148                 {\r
149                         Mutex Sem = new Mutex(false);\r
150                         ConcClassLoop class1 = new ConcClassLoop(1,Sem);\r
151                         ConcClassLoop class2 = new ConcClassLoop(2,Sem);\r
152                         Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));\r
153                         Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));\r
154                         \r
155                         try {\r
156                                 thread1.Start();\r
157                                 TestUtil.WaitForNotAlive (thread1, "t1");\r
158         \r
159                                 thread2.Start();\r
160                                 TestUtil.WaitForNotAlive (thread2, "t2");\r
161                         \r
162                                 AssertEquals("#51 The Mutex Has been Kept after end of the thread:", class2.id,class2.marker);\r
163                         }\r
164                         finally {\r
165                                 thread1.Abort ();\r
166                                 thread2.Abort ();\r
167                         }\r
168                 }\r
169 \r
170                 public void TestHandle()\r
171                 {\r
172                         Mutex Sem = new Mutex();\r
173                         try\r
174                         {\r
175                                 IntPtr Handle = Sem.Handle;\r
176                         }\r
177                         catch (Exception e)\r
178                         {\r
179                                 Fail("#61 Unexpected Exception accessing Sem.Handle:" + e.ToString());\r
180                         }\r
181                 }\r
182         }\r
183 }