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 ()
224 Thread tr = new Thread (new ThreadStart (FirstSyncThread));
230 Assert.IsTrue (!otResult, "Concurrency detected in FirstSyncThread");
234 public void TestLocked2 ()
236 Thread tr = new Thread (new ThreadStart (FirstNotSyncThread));
239 SecondNotSyncThread ();
242 Assert.IsTrue (otResult, "Concurrency not detected in FirstReentryThread");
245 void FirstNotSyncThread ()
247 otResult = sincob.CheckUnlockedConcurrency ();
250 void SecondNotSyncThread ()
252 bool concurrent = sincob.CheckConcurrency ();
253 Assert.IsTrue (concurrent, "Concurrency not detected");
257 public void TestLocked3 ()
259 Thread tr = new Thread (new ThreadStart (Lock1Thread));
267 sincob.CheckLock1 ();
272 sincob.CheckLock2 ();
276 public void TestReentry ()
278 Thread tr = new Thread (new ThreadStart (FirstReentryThread));
281 SecondReentryThread ();
284 Assert.IsTrue (otResult, "Concurrency not detected in FirstReentryThread");
287 void FirstReentryThread ()
289 otResult = reentrant.CheckCalloutConcurrency (notsup);
292 void SecondReentryThread ()
294 bool concurrent = reentrant.CheckCalloutConcurrency (notsup);
295 Assert.IsTrue (concurrent, "Concurrency not detected");
299 public void TestNoReentry ()
301 Thread tr = new Thread (new ThreadStart (FirstNoReentryThread));
304 SecondNoReentryThread ();
307 Assert.IsTrue (!otResult, "Concurrency detected in FirstNoReentryThread");
310 void FirstNoReentryThread ()
312 otResult = notreentrant.CheckCalloutConcurrency (notsup);
315 void SecondNoReentryThread ()
317 bool concurrent = notreentrant.CheckCalloutConcurrency (notsup);
318 Assert.IsTrue (!concurrent, "Concurrency detected");
322 public void TestCallback ()
324 Thread tr = new Thread (new ThreadStart (CallbackThread));
327 bool concurrent = notreentrant.CheckConcurrency ();
328 Assert.IsTrue (!concurrent, "Concurrency detected");
329 notreentrant.CheckContext (Thread.CurrentContext);
332 Assert.IsTrue (!otResult, "Concurrency detected in CallbackThread");
336 public void TestSynchronizationReleasedOnMultipleAcquire ()
339 otResult = notreentrant.TestCallback ();
341 Thread tr = new Thread (new ThreadStart (CallbackThread));
344 bool terminated = tr.Join(2000);
345 Assert.IsTrue(terminated, "Thread didn't get lock of context bound object.");
347 Assert.IsTrue (!otResult, "Concurrency detected in CallbackThread");
350 void CallbackThread ()
352 otResult = notreentrant.TestCallback ();
356 [Category("NotDotNet")]
357 [Category ("MobileNotWorking")]
358 public void TestMonitorWait ()
360 Thread tr = new Thread (new ThreadStart (DoMonitorPulse));
363 bool r = sincob.CheckMonitorWait (true);
364 Assert.IsTrue (r, "Wait timeout");
367 Assert.IsTrue (r, "Join timeout");
369 tr = new Thread (new ThreadStart (DoMonitorPulse));
372 r = sincob.CheckMonitorWait (false);
373 Assert.IsTrue (!r, "Expected wait timeout");
376 Assert.IsTrue (r, "Join timeout 2");
379 void DoMonitorPulse ()
382 sincob.CheckMonitorPulse ();