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