fix freeze
[mono.git] / mcs / class / corlib / Test / System.Runtime.Remoting / SynchronizationAttributeTest.cs
1 //\r
2 // MonoTests.System.Runtime.Remoting.SynchronizationAttributeTest.cs\r
3 //\r
4 // Author: Lluis Sanchez Gual (lluis@ximian.com)\r
5 //\r
6 // 2003 (C) Copyright, Novell, Inc.\r
7 //\r
8 \r
9 using System;\r
10 using System.Threading;\r
11 using System.Runtime.Remoting.Contexts;\r
12 using NUnit.Framework;\r
13 \r
14 namespace MonoTests.System.Runtime.Remoting\r
15 {\r
16         enum SynchRes { SameSync, NewSync, NoSync }\r
17 \r
18         class SincroBase: ContextBoundObject\r
19         {\r
20                 public int idx = 0;\r
21 \r
22                 public bool CheckConcurrency ()\r
23                 {\r
24                         int t = idx;\r
25                         for (int n=0; n<40; n++)\r
26                         {\r
27                                 idx++;\r
28                                 Thread.Sleep (25);\r
29                         }\r
30                         return (t+40 != idx);\r
31                 }\r
32                 \r
33                 public bool CheckUnlockedConcurrency ()\r
34                 {\r
35                         Lock (false);\r
36                         return CheckConcurrency ();\r
37                 }\r
38                 \r
39                 public SynchRes CheckContext (Context ctx)\r
40                 {\r
41                         object otherp = ctx.GetProperty ("Synchronization");\r
42                         object thisp = Thread.CurrentContext.GetProperty ("Synchronization");\r
43 \r
44                         if (thisp == null) return SynchRes.NoSync;\r
45                         if (thisp == otherp) return SynchRes.SameSync;\r
46                         return SynchRes.NewSync;\r
47                 }\r
48 \r
49                 public SynchRes CheckContextTransition (Type type)\r
50                 {\r
51                         SincroBase bob = (SincroBase)Activator.CreateInstance (type);\r
52                         return bob.CheckContext (Thread.CurrentContext);\r
53                 }\r
54 \r
55                 public bool CheckCalloutConcurrency (SincroBase bob)\r
56                 {\r
57                         bool res = bob.CheckConcurrency ();\r
58                         return res;\r
59                 }\r
60 \r
61                 public void CheckLock1 ()\r
62                 {\r
63                         Thread.Sleep (2000);\r
64                         Lock (false);\r
65                         Thread.Sleep (6000);\r
66                 }\r
67 \r
68                 public void CheckLock2 ()\r
69                 {\r
70                         Thread.Sleep (1000);\r
71                         Lock (true);\r
72                         Thread.Sleep (2000);\r
73                 }\r
74 \r
75                 public void Lock (bool b)\r
76                 {\r
77                         SynchronizationAttribute thisp = (SynchronizationAttribute) Thread.CurrentContext.GetProperty ("Synchronization");\r
78                         thisp.Locked = b;\r
79                 }\r
80 \r
81                 public bool GetLocked ()\r
82                 {\r
83                         SynchronizationAttribute thisp = (SynchronizationAttribute) Thread.CurrentContext.GetProperty ("Synchronization");\r
84                         return thisp.Locked;\r
85                 }\r
86                 \r
87                 public bool CheckMonitorWait (bool exitContext)\r
88                 {\r
89                         lock (this)\r
90                         {\r
91                                 return Monitor.Wait (this, 1000, exitContext);\r
92                         }\r
93                 }\r
94                 \r
95                 public void CheckMonitorPulse ()\r
96                 {\r
97                         lock (this)\r
98                         {\r
99                                 Monitor.Pulse (this);\r
100                         }\r
101                 }\r
102         }\r
103 \r
104         [Synchronization (SynchronizationAttribute.SUPPORTED)]\r
105         class SincroSupported: SincroBase\r
106         {\r
107         }\r
108 \r
109         [Synchronization (SynchronizationAttribute.REQUIRED)]\r
110         class SincroRequired: SincroBase\r
111         {\r
112         }\r
113 \r
114         [Synchronization (SynchronizationAttribute.REQUIRES_NEW)]\r
115         class SincroRequiresNew: SincroBase\r
116         {\r
117                 public bool TestCallback ()\r
118                 {\r
119                         SincroNotSupported bob = new SincroNotSupported ();\r
120                         return bob.CallBack (this);\r
121                 }\r
122         }\r
123 \r
124         [Synchronization (SynchronizationAttribute.NOT_SUPPORTED)]\r
125         class SincroNotSupported: SincroBase\r
126         {\r
127                 public bool CallBack (SincroRequiresNew bob)\r
128                 {\r
129                         return bob.CheckConcurrency ();\r
130                 }\r
131         }\r
132 \r
133         [Synchronization (SynchronizationAttribute.REQUIRES_NEW, true)]\r
134         class SincroRequiresNewReentrant: SincroBase\r
135         {\r
136         }\r
137 \r
138         [TestFixture]
139         // http://bugzilla.ximian.com/show_bug.cgi?id=72576
140         [Category ("NotWorking")]
141         public class SynchronizationAttributeTest: Assertion\r
142         {\r
143                 SincroRequiresNew sincob = new SincroRequiresNew ();\r
144                 SincroNotSupported notsup = new SincroNotSupported ();\r
145                 SincroRequiresNewReentrant reentrant = new SincroRequiresNewReentrant ();\r
146                 SincroRequiresNew notreentrant = new SincroRequiresNew ();\r
147                 bool otResult;\r
148 \r
149                 [Test]\r
150                 public void TestSynchronization ()\r
151                 {\r
152                         Thread tr = new Thread (new ThreadStart (FirstSyncThread));\r
153                         tr.Start ();\r
154                         Thread.Sleep (200);\r
155                         SecondSyncThread ();\r
156                         \r
157                         tr.Join ();\r
158                         Assert ("Concurrency detected in FirstSyncThread", !otResult);\r
159                 }\r
160 \r
161                 void FirstSyncThread ()\r
162                 {\r
163                         otResult = sincob.CheckConcurrency ();\r
164                 }\r
165 \r
166                 void SecondSyncThread ()\r
167                 {\r
168                         bool concurrent = sincob.CheckConcurrency ();\r
169                         Assert ("Concurrency detected", !concurrent);\r
170                 }\r
171 \r
172                 [Test]\r
173                 public void TestSupported ()\r
174                 {\r
175                         SincroRequiresNew ob = new SincroRequiresNew ();\r
176                         SynchRes res = ob.CheckContextTransition (typeof(SincroSupported));\r
177                         Assert ("Synchronizaton context expected", res == SynchRes.SameSync);\r
178 \r
179                         SincroSupported ob2 = new SincroSupported ();\r
180                         res = ob2.CheckContext (Thread.CurrentContext);\r
181                         Assert ("Synchronizaton context not expected", res == SynchRes.NoSync);\r
182                 }\r
183 \r
184                 [Test]\r
185                 public void TestRequired ()\r
186                 {\r
187                         SincroRequiresNew ob = new SincroRequiresNew ();\r
188                         SynchRes res = ob.CheckContextTransition (typeof(SincroRequired));\r
189                         Assert ("Synchronizaton context expected 1", res == SynchRes.SameSync);\r
190 \r
191                         SincroRequired ob2 = new SincroRequired ();\r
192                         res = ob2.CheckContext (Thread.CurrentContext);\r
193                         Assert ("Synchronizaton context expected 2", res == SynchRes.NewSync);\r
194                 }\r
195 \r
196                 [Test]\r
197                 public void TestRequiresNew ()\r
198                 {\r
199                         SincroRequiresNew ob = new SincroRequiresNew ();\r
200                         SynchRes res = ob.CheckContextTransition (typeof(SincroRequiresNew));\r
201                         Assert ("New synchronizaton context expected", res == SynchRes.NewSync);\r
202 \r
203                         SincroRequiresNew ob2 = new SincroRequiresNew ();\r
204                         res = ob2.CheckContext (Thread.CurrentContext);\r
205                         Assert ("Synchronizaton context not expected", res == SynchRes.NewSync);\r
206                 }\r
207 \r
208                 [Test]\r
209                 public void TestNotSupported ()\r
210                 {\r
211                         SincroRequiresNew ob = new SincroRequiresNew ();\r
212                         SynchRes res = ob.CheckContextTransition (typeof(SincroNotSupported));\r
213                         Assert ("Synchronizaton context not expected 1", res == SynchRes.NoSync);\r
214 \r
215                         SincroNotSupported ob2 = new SincroNotSupported ();\r
216                         res = ob2.CheckContext (Thread.CurrentContext);\r
217                         Assert ("Synchronizaton context not expected 2", res == SynchRes.NoSync);\r
218                 }\r
219 \r
220                 [Test]\r
221                 public void TestLocked1 ()\r
222                 {\r
223                         sincob.Lock (false);\r
224                         Thread tr = new Thread (new ThreadStart (FirstSyncThread));\r
225                         tr.Start ();\r
226                         Thread.Sleep (200);\r
227                         SecondSyncThread ();\r
228                         \r
229                         tr.Join ();\r
230                         Assert ("Concurrency detected in FirstSyncThread", !otResult);\r
231                 }\r
232 \r
233                 [Test]\r
234                 public void TestLocked2 ()\r
235                 {\r
236                         Thread tr = new Thread (new ThreadStart (FirstNotSyncThread));\r
237                         tr.Start ();\r
238                         Thread.Sleep (200);\r
239                         SecondNotSyncThread ();\r
240                         \r
241                         tr.Join ();\r
242                         Assert ("Concurrency not detected in FirstReentryThread", otResult);\r
243                 }\r
244 \r
245                 void FirstNotSyncThread ()\r
246                 {\r
247                         otResult = sincob.CheckUnlockedConcurrency ();\r
248                 }\r
249 \r
250                 void SecondNotSyncThread ()\r
251                 {\r
252                         bool concurrent = sincob.CheckConcurrency ();\r
253                         Assert ("Concurrency not detected", concurrent);\r
254                 }\r
255 \r
256                 [Test]\r
257                 public void TestLocked3 ()\r
258                 {\r
259                         Thread tr = new Thread (new ThreadStart (Lock1Thread));\r
260                         tr.Start ();\r
261                         Thread.Sleep (200);\r
262                         Lock2Thread ();\r
263                 }\r
264 \r
265                 void Lock1Thread ()\r
266                 {\r
267                         sincob.CheckLock1 ();\r
268                 }\r
269 \r
270                 void Lock2Thread ()\r
271                 {\r
272                         sincob.CheckLock2 ();\r
273                 }\r
274 \r
275                 [Test]\r
276                 public void TestReentry ()\r
277                 {\r
278                         Thread tr = new Thread (new ThreadStart (FirstReentryThread));\r
279                         tr.Start ();\r
280                         Thread.Sleep (200);\r
281                         SecondReentryThread ();\r
282                         \r
283                         tr.Join ();\r
284                         Assert ("Concurrency not detected in FirstReentryThread", otResult);\r
285                 }\r
286 \r
287                 void FirstReentryThread ()\r
288                 {\r
289                         otResult = reentrant.CheckCalloutConcurrency (notsup);\r
290                 }\r
291 \r
292                 void SecondReentryThread ()\r
293                 {\r
294                         bool concurrent = reentrant.CheckCalloutConcurrency (notsup);\r
295                         Assert ("Concurrency not detected", concurrent);\r
296                 }\r
297 \r
298                 [Test]\r
299                 public void TestNoReentry ()\r
300                 {\r
301                         Thread tr = new Thread (new ThreadStart (FirstNoReentryThread));\r
302                         tr.Start ();\r
303                         Thread.Sleep (200);\r
304                         SecondNoReentryThread ();\r
305                         \r
306                         tr.Join ();\r
307                         Assert ("Concurrency detected in FirstNoReentryThread", !otResult);\r
308                 }\r
309 \r
310                 void FirstNoReentryThread ()\r
311                 {\r
312                         otResult = notreentrant.CheckCalloutConcurrency (notsup);\r
313                 }\r
314 \r
315                 void SecondNoReentryThread ()\r
316                 {\r
317                         bool concurrent = notreentrant.CheckCalloutConcurrency (notsup);\r
318                         Assert ("Concurrency detected", !concurrent);\r
319                 }\r
320 \r
321                 [Test]\r
322                 public void TestCallback ()\r
323                 {\r
324                         Thread tr = new Thread (new ThreadStart (CallbackThread));\r
325                         tr.Start ();\r
326                         Thread.Sleep (200);\r
327                         bool concurrent = notreentrant.CheckConcurrency ();\r
328                         Assert ("Concurrency detected", !concurrent);\r
329                         notreentrant.CheckContext (Thread.CurrentContext);\r
330                         \r
331                         tr.Join ();\r
332                         Assert ("Concurrency detected in CallbackThread", !otResult);\r
333                 }\r
334 \r
335                 void CallbackThread ()\r
336                 {\r
337                         otResult = notreentrant.TestCallback ();\r
338                 }\r
339                 \r
340                 [Test]
341                 [Category("NotDotNet")]\r
342                 public void TestMonitorWait ()\r
343                 {\r
344                         Thread tr = new Thread (new ThreadStart (DoMonitorPulse));\r
345                         tr.Start ();\r
346                         \r
347                         bool r = sincob.CheckMonitorWait (true);\r
348                         Assert ("Wait timeout", r);\r
349                         \r
350                         r = tr.Join (1000);\r
351                         Assert ("Join timeout", r);\r
352                         \r
353                         tr = new Thread (new ThreadStart (DoMonitorPulse));\r
354                         tr.Start ();\r
355                         \r
356                         r = sincob.CheckMonitorWait (false);\r
357                         Assert ("Expected wait timeout", !r);\r
358                         \r
359                         r = tr.Join (1000);\r
360                         Assert ("Join timeout 2", r);\r
361                 }\r
362 \r
363                 void DoMonitorPulse ()\r
364                 {\r
365                         Thread.Sleep (100);\r
366                         sincob.CheckMonitorPulse ();\r
367                 }\r
368         }\r
369 }\r