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 // http://bugzilla.ximian.com/show_bug.cgi?id=72576
\r
140 [Category ("NotWorking")]
\r
141 public class SynchronizationAttributeTest: Assertion
\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
150 public void TestSynchronization ()
\r
152 Thread tr = new Thread (new ThreadStart (FirstSyncThread));
\r
154 Thread.Sleep (200);
\r
155 SecondSyncThread ();
\r
158 Assert ("Concurrency detected in FirstSyncThread", !otResult);
\r
161 void FirstSyncThread ()
\r
163 otResult = sincob.CheckConcurrency ();
\r
166 void SecondSyncThread ()
\r
168 bool concurrent = sincob.CheckConcurrency ();
\r
169 Assert ("Concurrency detected", !concurrent);
\r
173 public void TestSupported ()
\r
175 SincroRequiresNew ob = new SincroRequiresNew ();
\r
176 SynchRes res = ob.CheckContextTransition (typeof(SincroSupported));
\r
177 Assert ("Synchronizaton context expected", res == SynchRes.SameSync);
\r
179 SincroSupported ob2 = new SincroSupported ();
\r
180 res = ob2.CheckContext (Thread.CurrentContext);
\r
181 Assert ("Synchronizaton context not expected", res == SynchRes.NoSync);
\r
185 public void TestRequired ()
\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
191 SincroRequired ob2 = new SincroRequired ();
\r
192 res = ob2.CheckContext (Thread.CurrentContext);
\r
193 Assert ("Synchronizaton context expected 2", res == SynchRes.NewSync);
\r
197 public void TestRequiresNew ()
\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
203 SincroRequiresNew ob2 = new SincroRequiresNew ();
\r
204 res = ob2.CheckContext (Thread.CurrentContext);
\r
205 Assert ("Synchronizaton context not expected", res == SynchRes.NewSync);
\r
209 public void TestNotSupported ()
\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
215 SincroNotSupported ob2 = new SincroNotSupported ();
\r
216 res = ob2.CheckContext (Thread.CurrentContext);
\r
217 Assert ("Synchronizaton context not expected 2", res == SynchRes.NoSync);
\r
221 public void TestLocked1 ()
\r
223 sincob.Lock (false);
\r
224 Thread tr = new Thread (new ThreadStart (FirstSyncThread));
\r
226 Thread.Sleep (200);
\r
227 SecondSyncThread ();
\r
230 Assert ("Concurrency detected in FirstSyncThread", !otResult);
\r
234 public void TestLocked2 ()
\r
236 Thread tr = new Thread (new ThreadStart (FirstNotSyncThread));
\r
238 Thread.Sleep (200);
\r
239 SecondNotSyncThread ();
\r
242 Assert ("Concurrency not detected in FirstReentryThread", otResult);
\r
245 void FirstNotSyncThread ()
\r
247 otResult = sincob.CheckUnlockedConcurrency ();
\r
250 void SecondNotSyncThread ()
\r
252 bool concurrent = sincob.CheckConcurrency ();
\r
253 Assert ("Concurrency not detected", concurrent);
\r
257 public void TestLocked3 ()
\r
259 Thread tr = new Thread (new ThreadStart (Lock1Thread));
\r
261 Thread.Sleep (200);
\r
265 void Lock1Thread ()
\r
267 sincob.CheckLock1 ();
\r
270 void Lock2Thread ()
\r
272 sincob.CheckLock2 ();
\r
276 public void TestReentry ()
\r
278 Thread tr = new Thread (new ThreadStart (FirstReentryThread));
\r
280 Thread.Sleep (200);
\r
281 SecondReentryThread ();
\r
284 Assert ("Concurrency not detected in FirstReentryThread", otResult);
\r
287 void FirstReentryThread ()
\r
289 otResult = reentrant.CheckCalloutConcurrency (notsup);
\r
292 void SecondReentryThread ()
\r
294 bool concurrent = reentrant.CheckCalloutConcurrency (notsup);
\r
295 Assert ("Concurrency not detected", concurrent);
\r
299 public void TestNoReentry ()
\r
301 Thread tr = new Thread (new ThreadStart (FirstNoReentryThread));
\r
303 Thread.Sleep (200);
\r
304 SecondNoReentryThread ();
\r
307 Assert ("Concurrency detected in FirstNoReentryThread", !otResult);
\r
310 void FirstNoReentryThread ()
\r
312 otResult = notreentrant.CheckCalloutConcurrency (notsup);
\r
315 void SecondNoReentryThread ()
\r
317 bool concurrent = notreentrant.CheckCalloutConcurrency (notsup);
\r
318 Assert ("Concurrency detected", !concurrent);
\r
322 public void TestCallback ()
\r
324 Thread tr = new Thread (new ThreadStart (CallbackThread));
\r
326 Thread.Sleep (200);
\r
327 bool concurrent = notreentrant.CheckConcurrency ();
\r
328 Assert ("Concurrency detected", !concurrent);
\r
329 notreentrant.CheckContext (Thread.CurrentContext);
\r
332 Assert ("Concurrency detected in CallbackThread", !otResult);
\r
335 void CallbackThread ()
\r
337 otResult = notreentrant.TestCallback ();
\r
341 [Category("NotDotNet")]
\r
342 public void TestMonitorWait ()
\r
344 Thread tr = new Thread (new ThreadStart (DoMonitorPulse));
\r
347 bool r = sincob.CheckMonitorWait (true);
\r
348 Assert ("Wait timeout", r);
\r
350 r = tr.Join (1000);
\r
351 Assert ("Join timeout", r);
\r
353 tr = new Thread (new ThreadStart (DoMonitorPulse));
\r
356 r = sincob.CheckMonitorWait (false);
\r
357 Assert ("Expected wait timeout", !r);
\r
359 r = tr.Join (1000);
\r
360 Assert ("Join timeout 2", r);
\r
363 void DoMonitorPulse ()
\r
365 Thread.Sleep (100);
\r
366 sincob.CheckMonitorPulse ();
\r