64aeed5d944734f8a614ea89719eaa225c4f9221
[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                                 thread1.Abort ();\r
132                         }\r
133                 }\r
134 \r
135                 [Test]\r
136                 public void TestWaitAndFoget1()\r
137                 {\r
138                         Mutex Sem = new Mutex(false);\r
139                         ConcClassLoop class1 = new ConcClassLoop(1,Sem);\r
140                         ConcClassLoop class2 = new ConcClassLoop(2,Sem);\r
141                         Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));\r
142                         Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));\r
143                         \r
144                         try {\r
145                                 thread1.Start();\r
146                                 TestUtil.WaitForNotAlive (thread1, "t1");\r
147         \r
148                                 thread2.Start();\r
149                                 TestUtil.WaitForNotAlive (thread2, "t2");\r
150                         \r
151                                 Assert.AreEqual (class2.id, class2.marker);\r
152                         } finally {\r
153                                 thread1.Abort ();\r
154                                 thread2.Abort ();\r
155                         }\r
156                 }\r
157 \r
158                 [Test]\r
159                 [Category("TargetJvmNotSupported")] // IntPtr native handles are not supported for TARGET_JVM.\r
160                 public void TestHandle()\r
161                 {\r
162                         Mutex Sem = new Mutex();\r
163                         IntPtr Handle = Sem.Handle;\r
164                 }\r
165 \r
166                 [Test] // bug #79358\r
167                 public void DoubleRelease ()\r
168                 {\r
169                         Mutex mutex = new Mutex ();\r
170                         mutex.WaitOne ();\r
171                         mutex.ReleaseMutex ();\r
172 \r
173                         try {\r
174                                 mutex.ReleaseMutex ();\r
175                                 Assert.Fail ("#1");\r
176                         } catch (ApplicationException ex) {\r
177                                 Assert.AreEqual (typeof (ApplicationException), ex.GetType (), "#2");\r
178                         }\r
179                 }\r
180         }\r
181 }\r