2 // MonoTests.System.Runtime.Remoting.SynchronizationAttributeTest.cs
\r
4 // Author: Lluis Sanchez Gual (lluis@ximian.com)
\r
6 // 2003 (C) Copyright, Novell, Inc.
\r
10 using System.Threading;
\r
11 using System.Runtime.Remoting.Contexts;
\r
12 using NUnit.Framework;
\r
14 namespace MonoTests.System.Runtime.Remoting
\r
16 enum SynchRes { SameSync, NewSync, NoSync }
\r
18 class SincroBase: ContextBoundObject
\r
22 public bool CheckConcurrency ()
\r
25 for (int n=0; n<40; n++)
\r
30 return (t+40 != idx);
\r
33 public bool CheckUnlockedConcurrency ()
\r
36 return CheckConcurrency ();
\r
39 public SynchRes CheckContext (Context ctx)
\r
41 object otherp = ctx.GetProperty ("Synchronization");
\r
42 object thisp = Thread.CurrentContext.GetProperty ("Synchronization");
\r
44 if (thisp == null) return SynchRes.NoSync;
\r
45 if (thisp == otherp) return SynchRes.SameSync;
\r
46 return SynchRes.NewSync;
\r
49 public SynchRes CheckContextTransition (Type type)
\r
51 SincroBase bob = (SincroBase)Activator.CreateInstance (type);
\r
52 return bob.CheckContext (Thread.CurrentContext);
\r
55 public bool CheckCalloutConcurrency (SincroBase bob)
\r
57 bool res = bob.CheckConcurrency ();
\r
61 public void CheckLock1 ()
\r
63 Thread.Sleep (2000);
\r
65 Thread.Sleep (6000);
\r
68 public void CheckLock2 ()
\r
70 Thread.Sleep (1000);
\r
72 Thread.Sleep (2000);
\r
75 public void Lock (bool b)
\r
77 SynchronizationAttribute thisp = (SynchronizationAttribute) Thread.CurrentContext.GetProperty ("Synchronization");
\r
81 public bool GetLocked ()
\r
83 SynchronizationAttribute thisp = (SynchronizationAttribute) Thread.CurrentContext.GetProperty ("Synchronization");
\r
84 return thisp.Locked;
\r
87 public bool CheckMonitorWait (bool exitContext)
\r
91 return Monitor.Wait (this, 1000, exitContext);
\r
95 public void CheckMonitorPulse ()
\r
99 Monitor.Pulse (this);
\r
104 [Synchronization (SynchronizationAttribute.SUPPORTED)]
\r
105 class SincroSupported: SincroBase
\r
109 [Synchronization (SynchronizationAttribute.REQUIRED)]
\r
110 class SincroRequired: SincroBase
\r
114 [Synchronization (SynchronizationAttribute.REQUIRES_NEW)]
\r
115 class SincroRequiresNew: SincroBase
\r
117 public bool TestCallback ()
\r
119 SincroNotSupported bob = new SincroNotSupported ();
\r
120 return bob.CallBack (this);
\r
124 [Synchronization (SynchronizationAttribute.NOT_SUPPORTED)]
\r
125 class SincroNotSupported: SincroBase
\r
127 public bool CallBack (SincroRequiresNew bob)
\r
129 return bob.CheckConcurrency ();
\r
133 [Synchronization (SynchronizationAttribute.REQUIRES_NEW, true)]
\r
134 class SincroRequiresNewReentrant: SincroBase
\r
139 public class SynchronizationAttributeTest: Assertion
\r
141 SincroRequiresNew sincob = new SincroRequiresNew ();
\r
142 SincroNotSupported notsup = new SincroNotSupported ();
\r
143 SincroRequiresNewReentrant reentrant = new SincroRequiresNewReentrant ();
\r
144 SincroRequiresNew notreentrant = new SincroRequiresNew ();
\r
148 public void TestSynchronization ()
\r
150 Thread tr = new Thread (new ThreadStart (FirstSyncThread));
\r
152 Thread.Sleep (200);
\r
153 SecondSyncThread ();
\r
156 Assert ("Concurrency detected in FirstSyncThread", !otResult);
\r
159 void FirstSyncThread ()
\r
161 otResult = sincob.CheckConcurrency ();
\r
164 void SecondSyncThread ()
\r
166 bool concurrent = sincob.CheckConcurrency ();
\r
167 Assert ("Concurrency detected", !concurrent);
\r
171 public void TestSupported ()
\r
173 SincroRequiresNew ob = new SincroRequiresNew ();
\r
174 SynchRes res = ob.CheckContextTransition (typeof(SincroSupported));
\r
175 Assert ("Synchronizaton context expected", res == SynchRes.SameSync);
\r
177 SincroSupported ob2 = new SincroSupported ();
\r
178 res = ob2.CheckContext (Thread.CurrentContext);
\r
179 Assert ("Synchronizaton context not expected", res == SynchRes.NoSync);
\r
183 public void TestRequired ()
\r
185 SincroRequiresNew ob = new SincroRequiresNew ();
\r
186 SynchRes res = ob.CheckContextTransition (typeof(SincroRequired));
\r
187 Assert ("Synchronizaton context expected 1", res == SynchRes.SameSync);
\r
189 SincroRequired ob2 = new SincroRequired ();
\r
190 res = ob2.CheckContext (Thread.CurrentContext);
\r
191 Assert ("Synchronizaton context expected 2", res == SynchRes.NewSync);
\r
195 public void TestRequiresNew ()
\r
197 SincroRequiresNew ob = new SincroRequiresNew ();
\r
198 SynchRes res = ob.CheckContextTransition (typeof(SincroRequiresNew));
\r
199 Assert ("New synchronizaton context expected", res == SynchRes.NewSync);
\r
201 SincroRequiresNew ob2 = new SincroRequiresNew ();
\r
202 res = ob2.CheckContext (Thread.CurrentContext);
\r
203 Assert ("Synchronizaton context not expected", res == SynchRes.NewSync);
\r
207 public void TestNotSupported ()
\r
209 SincroRequiresNew ob = new SincroRequiresNew ();
\r
210 SynchRes res = ob.CheckContextTransition (typeof(SincroNotSupported));
\r
211 Assert ("Synchronizaton context not expected 1", res == SynchRes.NoSync);
\r
213 SincroNotSupported ob2 = new SincroNotSupported ();
\r
214 res = ob2.CheckContext (Thread.CurrentContext);
\r
215 Assert ("Synchronizaton context not expected 2", res == SynchRes.NoSync);
\r
219 public void TestLocked1 ()
\r
221 sincob.Lock (false);
\r
222 Thread tr = new Thread (new ThreadStart (FirstSyncThread));
\r
224 Thread.Sleep (200);
\r
225 SecondSyncThread ();
\r
228 Assert ("Concurrency detected in FirstSyncThread", !otResult);
\r
232 public void TestLocked2 ()
\r
234 Thread tr = new Thread (new ThreadStart (FirstNotSyncThread));
\r
236 Thread.Sleep (200);
\r
237 SecondNotSyncThread ();
\r
240 Assert ("Concurrency not detected in FirstReentryThread", otResult);
\r
243 void FirstNotSyncThread ()
\r
245 otResult = sincob.CheckUnlockedConcurrency ();
\r
248 void SecondNotSyncThread ()
\r
250 bool concurrent = sincob.CheckConcurrency ();
\r
251 Assert ("Concurrency not detected", concurrent);
\r
255 public void TestLocked3 ()
\r
257 Thread tr = new Thread (new ThreadStart (Lock1Thread));
\r
259 Thread.Sleep (200);
\r
263 void Lock1Thread ()
\r
265 sincob.CheckLock1 ();
\r
268 void Lock2Thread ()
\r
270 sincob.CheckLock2 ();
\r
274 public void TestReentry ()
\r
276 Thread tr = new Thread (new ThreadStart (FirstReentryThread));
\r
278 Thread.Sleep (200);
\r
279 SecondReentryThread ();
\r
282 Assert ("Concurrency not detected in FirstReentryThread", otResult);
\r
285 void FirstReentryThread ()
\r
287 otResult = reentrant.CheckCalloutConcurrency (notsup);
\r
290 void SecondReentryThread ()
\r
292 bool concurrent = reentrant.CheckCalloutConcurrency (notsup);
\r
293 Assert ("Concurrency not detected", concurrent);
\r
297 public void TestNoReentry ()
\r
299 Thread tr = new Thread (new ThreadStart (FirstNoReentryThread));
\r
301 Thread.Sleep (200);
\r
302 SecondNoReentryThread ();
\r
305 Assert ("Concurrency detected in FirstNoReentryThread", !otResult);
\r
308 void FirstNoReentryThread ()
\r
310 otResult = notreentrant.CheckCalloutConcurrency (notsup);
\r
313 void SecondNoReentryThread ()
\r
315 bool concurrent = notreentrant.CheckCalloutConcurrency (notsup);
\r
316 Assert ("Concurrency detected", !concurrent);
\r
320 public void TestCallback ()
\r
322 Thread tr = new Thread (new ThreadStart (CallbackThread));
\r
324 Thread.Sleep (200);
\r
325 bool concurrent = notreentrant.CheckConcurrency ();
\r
326 Assert ("Concurrency detected", !concurrent);
\r
327 notreentrant.CheckContext (Thread.CurrentContext);
\r
330 Assert ("Concurrency detected in CallbackThread", !otResult);
\r
333 void CallbackThread ()
\r
335 otResult = notreentrant.TestCallback ();
\r
339 [Category("NotDotNet")]
\r
340 public void TestMonitorWait ()
\r
342 Thread tr = new Thread (new ThreadStart (DoMonitorPulse));
\r
345 bool r = sincob.CheckMonitorWait (true);
\r
346 Assert ("Wait timeout", r);
\r
348 r = tr.Join (1000);
\r
349 Assert ("Join timeout", r);
\r
351 tr = new Thread (new ThreadStart (DoMonitorPulse));
\r
354 r = sincob.CheckMonitorWait (false);
\r
355 Assert ("Expected wait timeout", !r);
\r
357 r = tr.Join (1000);
\r
358 Assert ("Join timeout 2", r);
\r
361 void DoMonitorPulse ()
\r
363 Thread.Sleep (100);
\r
364 sincob.CheckMonitorPulse ();
\r