2 // MonoTests.System.Runtime.Remoting.SynchronizationAttributeTest.cs
4 // Author: Lluis Sanchez Gual (lluis@ximian.com)
6 // 2003 (C) Copyright, Novell, Inc.
10 using System.Threading;
11 using System.Runtime.Remoting.Contexts;
12 using NUnit.Framework;
14 namespace MonoTests.System.Runtime.Remoting
16 enum SynchRes { SameSync, NewSync, NoSync }
18 class SincroBase: ContextBoundObject
22 public bool CheckConcurrency ()
25 for (int n=0; n<40; n++)
33 public bool CheckUnlockedConcurrency ()
36 return CheckConcurrency ();
39 public SynchRes CheckContext (Context ctx)
41 object otherp = ctx.GetProperty ("Synchronization");
42 object thisp = Thread.CurrentContext.GetProperty ("Synchronization");
44 if (thisp == null) return SynchRes.NoSync;
45 if (thisp == otherp) return SynchRes.SameSync;
46 return SynchRes.NewSync;
49 public SynchRes CheckContextTransition (Type type)
51 SincroBase bob = (SincroBase)Activator.CreateInstance (type);
52 return bob.CheckContext (Thread.CurrentContext);
55 public bool CheckCalloutConcurrency (SincroBase bob)
57 bool res = bob.CheckConcurrency ();
61 public void CheckLock1 ()
68 public void CheckLock2 ()
75 public void Lock (bool b)
77 SynchronizationAttribute thisp = (SynchronizationAttribute) Thread.CurrentContext.GetProperty ("Synchronization");
81 public bool GetLocked ()
83 SynchronizationAttribute thisp = (SynchronizationAttribute) Thread.CurrentContext.GetProperty ("Synchronization");
87 public bool CheckMonitorWait (bool exitContext)
91 return Monitor.Wait (this, 1000, exitContext);
95 public void CheckMonitorPulse ()
104 [Synchronization (SynchronizationAttribute.SUPPORTED)]
105 class SincroSupported: SincroBase
109 [Synchronization (SynchronizationAttribute.REQUIRED)]
110 class SincroRequired: SincroBase
114 [Synchronization (SynchronizationAttribute.REQUIRES_NEW)]
115 class SincroRequiresNew: SincroBase
117 public bool TestCallback ()
119 SincroNotSupported bob = new SincroNotSupported ();
120 return bob.CallBack (this);
124 [Synchronization (SynchronizationAttribute.NOT_SUPPORTED)]
125 class SincroNotSupported: SincroBase
127 public bool CallBack (SincroRequiresNew bob)
129 return bob.CheckConcurrency ();
133 [Synchronization (SynchronizationAttribute.REQUIRES_NEW, true)]
134 class SincroRequiresNewReentrant: SincroBase
139 [Category ("MobileNotWorking")] // Bug #10267
140 public class SynchronizationAttributeTest
142 SincroRequiresNew sincob = new SincroRequiresNew ();
143 SincroNotSupported notsup = new SincroNotSupported ();
144 SincroRequiresNewReentrant reentrant = new SincroRequiresNewReentrant ();
145 SincroRequiresNew notreentrant = new SincroRequiresNew ();
149 public void TestSynchronization ()
151 Thread tr = new Thread (new ThreadStart (FirstSyncThread));
157 Assert.IsTrue (!otResult, "Concurrency detected in FirstSyncThread");
160 void FirstSyncThread ()
162 otResult = sincob.CheckConcurrency ();
165 void SecondSyncThread ()
167 bool concurrent = sincob.CheckConcurrency ();
168 Assert.IsTrue (!concurrent, "Concurrency detected");
172 public void TestSupported ()
174 SincroRequiresNew ob = new SincroRequiresNew ();
175 SynchRes res = ob.CheckContextTransition (typeof(SincroSupported));
176 Assert.IsTrue (res == SynchRes.SameSync, "Synchronizaton context expected");
178 SincroSupported ob2 = new SincroSupported ();
179 res = ob2.CheckContext (Thread.CurrentContext);
180 Assert.IsTrue (res == SynchRes.NoSync, "Synchronizaton context not expected");
184 public void TestRequired ()
186 SincroRequiresNew ob = new SincroRequiresNew ();
187 SynchRes res = ob.CheckContextTransition (typeof(SincroRequired));
188 Assert.IsTrue (res == SynchRes.SameSync, "Synchronizaton context expected 1");
190 SincroRequired ob2 = new SincroRequired ();
191 res = ob2.CheckContext (Thread.CurrentContext);
192 Assert.IsTrue (res == SynchRes.NewSync, "Synchronizaton context expected 2");
196 public void TestRequiresNew ()
198 SincroRequiresNew ob = new SincroRequiresNew ();
199 SynchRes res = ob.CheckContextTransition (typeof(SincroRequiresNew));
200 Assert.IsTrue (res == SynchRes.NewSync, "New synchronizaton context expected");
202 SincroRequiresNew ob2 = new SincroRequiresNew ();
203 res = ob2.CheckContext (Thread.CurrentContext);
204 Assert.IsTrue (res == SynchRes.NewSync, "Synchronizaton context not expected");
208 public void TestNotSupported ()
210 SincroRequiresNew ob = new SincroRequiresNew ();
211 SynchRes res = ob.CheckContextTransition (typeof(SincroNotSupported));
212 Assert.IsTrue (res == SynchRes.NoSync, "Synchronizaton context not expected 1");
214 SincroNotSupported ob2 = new SincroNotSupported ();
215 res = ob2.CheckContext (Thread.CurrentContext);
216 Assert.IsTrue (res == SynchRes.NoSync, "Synchronizaton context not expected 2");
220 public void TestLocked1 ()
223 Thread tr = new Thread (new ThreadStart (FirstSyncThread));
229 Assert.IsTrue (!otResult, "Concurrency detected in FirstSyncThread");
233 public void TestLocked2 ()
235 Thread tr = new Thread (new ThreadStart (FirstNotSyncThread));
238 SecondNotSyncThread ();
241 Assert.IsTrue (otResult, "Concurrency not detected in FirstReentryThread");
244 void FirstNotSyncThread ()
246 otResult = sincob.CheckUnlockedConcurrency ();
249 void SecondNotSyncThread ()
251 bool concurrent = sincob.CheckConcurrency ();
252 Assert.IsTrue (concurrent, "Concurrency not detected");
256 public void TestLocked3 ()
258 Thread tr = new Thread (new ThreadStart (Lock1Thread));
266 sincob.CheckLock1 ();
271 sincob.CheckLock2 ();
275 public void TestReentry ()
277 Thread tr = new Thread (new ThreadStart (FirstReentryThread));
280 SecondReentryThread ();
283 Assert.IsTrue (otResult, "Concurrency not detected in FirstReentryThread");
286 void FirstReentryThread ()
288 otResult = reentrant.CheckCalloutConcurrency (notsup);
291 void SecondReentryThread ()
293 bool concurrent = reentrant.CheckCalloutConcurrency (notsup);
294 Assert.IsTrue (concurrent, "Concurrency not detected");
298 public void TestNoReentry ()
300 Thread tr = new Thread (new ThreadStart (FirstNoReentryThread));
303 SecondNoReentryThread ();
306 Assert.IsTrue (!otResult, "Concurrency detected in FirstNoReentryThread");
309 void FirstNoReentryThread ()
311 otResult = notreentrant.CheckCalloutConcurrency (notsup);
314 void SecondNoReentryThread ()
316 bool concurrent = notreentrant.CheckCalloutConcurrency (notsup);
317 Assert.IsTrue (!concurrent, "Concurrency detected");
321 public void TestCallback ()
323 Thread tr = new Thread (new ThreadStart (CallbackThread));
326 bool concurrent = notreentrant.CheckConcurrency ();
327 Assert.IsTrue (!concurrent, "Concurrency detected");
328 notreentrant.CheckContext (Thread.CurrentContext);
331 Assert.IsTrue (!otResult, "Concurrency detected in CallbackThread");
334 void CallbackThread ()
336 otResult = notreentrant.TestCallback ();
340 [Category("NotDotNet")]
341 [Category ("MobileNotWorking")]
342 public void TestMonitorWait ()
344 Thread tr = new Thread (new ThreadStart (DoMonitorPulse));
347 bool r = sincob.CheckMonitorWait (true);
348 Assert.IsTrue (r, "Wait timeout");
351 Assert.IsTrue (r, "Join timeout");
353 tr = new Thread (new ThreadStart (DoMonitorPulse));
356 r = sincob.CheckMonitorWait (false);
357 Assert.IsTrue (!r, "Expected wait timeout");
360 Assert.IsTrue (r, "Join timeout 2");
363 void DoMonitorPulse ()
366 sincob.CheckMonitorPulse ();