2002-12-21 Nick Drochak <ndrochak@gol.com>
[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 \r
79                 protected override void SetUp() {}\r
80 \r
81                 protected override void TearDown() {}\r
82 \r
83                 public void TestCtor1()\r
84                 {\r
85                         try\r
86                         {\r
87                                 Mutex Sem = new Mutex();\r
88                         }\r
89                         catch (Exception e)\r
90                         {\r
91                                 Fail("#01 Error Creating The Simple Mutex:" + e.ToString());\r
92                         }\r
93                 }\r
94                 \r
95                 public void TestCtorDefaultValue()\r
96                 {\r
97                         Mutex Sem = new Mutex();\r
98                         ConcClassLoop class1 = new ConcClassLoop(1,Sem);\r
99                         Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));\r
100                         thread1.Start();\r
101                         while(thread1.IsAlive);\r
102                         AssertEquals("#02 The default value of The mutex wrong set:",class1.id,class1.marker);\r
103                 }\r
104 \r
105                 public void TestCtorCtor2()\r
106                 {\r
107                         Mutex Sem = new Mutex(false);\r
108                         ConcClassLoop class1 = new ConcClassLoop(1,Sem);\r
109                         Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));\r
110                         thread1.Start();\r
111                         while(thread1.IsAlive);\r
112                         AssertEquals("#03 The value of The mutex wrong set:",class1.id,class1.marker);\r
113                 }\r
114                 \r
115                 public void TestCtorCtor3()\r
116                 {\r
117                         Mutex Sem = new Mutex(true);\r
118                         ConcClassLoop class1 = new ConcClassLoop(1,Sem);\r
119                         Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));\r
120                         thread1.Start();\r
121                         while(thread1.IsAlive);\r
122                         AssertEquals("#04 The default value of The mutex wrong set:",class1.id,class1.marker);\r
123                 }\r
124 \r
125                 public void TestWaitAndSignal1()\r
126                 {\r
127                         Mutex Sem = new Mutex(false);\r
128                         ConcClassLoop class1 = new ConcClassLoop(1,Sem);\r
129                         Thread thread1 = new Thread(new ThreadStart(class1.Loop));\r
130                         thread1.Start();\r
131                         while(thread1.IsAlive);\r
132                         AssertEquals("#41 Mutex Worked InCorrecly:",100,class1.marker);\r
133                 }\r
134                 public void TestWaitAndFoget1()\r
135                 {\r
136                         Mutex Sem = new Mutex(false);\r
137                         ConcClassLoop class1 = new ConcClassLoop(1,Sem);\r
138                         ConcClassLoop class2 = new ConcClassLoop(2,Sem);\r
139                         Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));\r
140                         Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));\r
141                         thread1.Start();\r
142                         while(thread1.IsAlive);\r
143                         thread2.Start();\r
144                         while(thread2.IsAlive);\r
145                         AssertEquals("#51 The Mutex Has been Kept after end of the thread:",\r
146                                 class2.id,class2.marker);\r
147                 }\r
148 \r
149                 public void TestHandle()\r
150                 {\r
151                         Mutex Sem = new Mutex();\r
152                         try\r
153                         {\r
154                                 IntPtr Handle = Sem.Handle;\r
155                         }\r
156                         catch (Exception e)\r
157                         {\r
158                                 Fail("#61 Unexpected Exception accessing Sem.Handle:" + e.ToString());\r
159                         }\r
160                 }\r
161 \r
162         }\r
163 }