-//\r
-// MonoTests.System.Runtime.Remoting.SynchronizationAttributeTest.cs\r
-//\r
-// Author: Lluis Sanchez Gual (lluis@ximian.com)\r
-//\r
-// 2003 (C) Copyright, Novell, Inc.\r
-//\r
-\r
-using System;\r
-using System.Threading;\r
-using System.Runtime.Remoting.Contexts;\r
-using NUnit.Framework;\r
-\r
-namespace MonoTests.System.Runtime.Remoting\r
-{\r
- enum SynchRes { SameSync, NewSync, NoSync }\r
-\r
- class SincroBase: ContextBoundObject\r
- {\r
- public int idx = 0;\r
-\r
- public bool CheckConcurrency ()\r
- {\r
- int t = idx;\r
- for (int n=0; n<40; n++)\r
- {\r
- idx++;\r
- Thread.Sleep (25);\r
- }\r
- return (t+40 != idx);\r
- }\r
- \r
- public bool CheckUnlockedConcurrency ()\r
- {\r
- Lock (false);\r
- return CheckConcurrency ();\r
- }\r
- \r
- public SynchRes CheckContext (Context ctx)\r
- {\r
- object otherp = ctx.GetProperty ("Synchronization");\r
- object thisp = Thread.CurrentContext.GetProperty ("Synchronization");\r
-\r
- if (thisp == null) return SynchRes.NoSync;\r
- if (thisp == otherp) return SynchRes.SameSync;\r
- return SynchRes.NewSync;\r
- }\r
-\r
- public SynchRes CheckContextTransition (Type type)\r
- {\r
- SincroBase bob = (SincroBase)Activator.CreateInstance (type);\r
- return bob.CheckContext (Thread.CurrentContext);\r
- }\r
-\r
- public bool CheckCalloutConcurrency (SincroBase bob)\r
- {\r
- bool res = bob.CheckConcurrency ();\r
- return res;\r
- }\r
-\r
- public void CheckLock1 ()\r
- {\r
- Thread.Sleep (2000);\r
- Lock (false);\r
- Thread.Sleep (6000);\r
- }\r
-\r
- public void CheckLock2 ()\r
- {\r
- Thread.Sleep (1000);\r
- Lock (true);\r
- Thread.Sleep (2000);\r
- }\r
-\r
- public void Lock (bool b)\r
- {\r
- SynchronizationAttribute thisp = (SynchronizationAttribute) Thread.CurrentContext.GetProperty ("Synchronization");\r
- thisp.Locked = b;\r
- }\r
-\r
- public bool GetLocked ()\r
- {\r
- SynchronizationAttribute thisp = (SynchronizationAttribute) Thread.CurrentContext.GetProperty ("Synchronization");\r
- return thisp.Locked;\r
- }\r
- \r
- public bool CheckMonitorWait (bool exitContext)\r
- {\r
- lock (this)\r
- {\r
- return Monitor.Wait (this, 1000, exitContext);\r
- }\r
- }\r
- \r
- public void CheckMonitorPulse ()\r
- {\r
- lock (this)\r
- {\r
- Monitor.Pulse (this);\r
- }\r
- }\r
- }\r
-\r
- [Synchronization (SynchronizationAttribute.SUPPORTED)]\r
- class SincroSupported: SincroBase\r
- {\r
- }\r
-\r
- [Synchronization (SynchronizationAttribute.REQUIRED)]\r
- class SincroRequired: SincroBase\r
- {\r
- }\r
-\r
- [Synchronization (SynchronizationAttribute.REQUIRES_NEW)]\r
- class SincroRequiresNew: SincroBase\r
- {\r
- public bool TestCallback ()\r
- {\r
- SincroNotSupported bob = new SincroNotSupported ();\r
- return bob.CallBack (this);\r
- }\r
- }\r
-\r
- [Synchronization (SynchronizationAttribute.NOT_SUPPORTED)]\r
- class SincroNotSupported: SincroBase\r
- {\r
- public bool CallBack (SincroRequiresNew bob)\r
- {\r
- return bob.CheckConcurrency ();\r
- }\r
- }\r
-\r
- [Synchronization (SynchronizationAttribute.REQUIRES_NEW, true)]\r
- class SincroRequiresNewReentrant: SincroBase\r
- {\r
- }\r
-\r
+//
+// MonoTests.System.Runtime.Remoting.SynchronizationAttributeTest.cs
+//
+// Author: Lluis Sanchez Gual (lluis@ximian.com)
+//
+// 2003 (C) Copyright, Novell, Inc.
+//
+
+using System;
+using System.Threading;
+using System.Runtime.Remoting.Contexts;
+using NUnit.Framework;
+
+namespace MonoTests.System.Runtime.Remoting
+{
+ enum SynchRes { SameSync, NewSync, NoSync }
+
+ class SincroBase: ContextBoundObject
+ {
+ public int idx = 0;
+
+ public bool CheckConcurrency ()
+ {
+ int t = idx;
+ for (int n=0; n<40; n++)
+ {
+ idx++;
+ Thread.Sleep (25);
+ }
+ return (t+40 != idx);
+ }
+
+ public bool CheckUnlockedConcurrency ()
+ {
+ Lock (false);
+ return CheckConcurrency ();
+ }
+
+ public SynchRes CheckContext (Context ctx)
+ {
+ object otherp = ctx.GetProperty ("Synchronization");
+ object thisp = Thread.CurrentContext.GetProperty ("Synchronization");
+
+ if (thisp == null) return SynchRes.NoSync;
+ if (thisp == otherp) return SynchRes.SameSync;
+ return SynchRes.NewSync;
+ }
+
+ public SynchRes CheckContextTransition (Type type)
+ {
+ SincroBase bob = (SincroBase)Activator.CreateInstance (type);
+ return bob.CheckContext (Thread.CurrentContext);
+ }
+
+ public bool CheckCalloutConcurrency (SincroBase bob)
+ {
+ bool res = bob.CheckConcurrency ();
+ return res;
+ }
+
+ public void CheckLock1 ()
+ {
+ Thread.Sleep (2000);
+ Lock (false);
+ Thread.Sleep (6000);
+ }
+
+ public void CheckLock2 ()
+ {
+ Thread.Sleep (1000);
+ Lock (true);
+ Thread.Sleep (2000);
+ }
+
+ public void Lock (bool b)
+ {
+ SynchronizationAttribute thisp = (SynchronizationAttribute) Thread.CurrentContext.GetProperty ("Synchronization");
+ thisp.Locked = b;
+ }
+
+ public bool GetLocked ()
+ {
+ SynchronizationAttribute thisp = (SynchronizationAttribute) Thread.CurrentContext.GetProperty ("Synchronization");
+ return thisp.Locked;
+ }
+
+ public bool CheckMonitorWait (bool exitContext)
+ {
+ lock (this)
+ {
+ return Monitor.Wait (this, 1000, exitContext);
+ }
+ }
+
+ public void CheckMonitorPulse ()
+ {
+ lock (this)
+ {
+ Monitor.Pulse (this);
+ }
+ }
+ }
+
+ [Synchronization (SynchronizationAttribute.SUPPORTED)]
+ class SincroSupported: SincroBase
+ {
+ }
+
+ [Synchronization (SynchronizationAttribute.REQUIRED)]
+ class SincroRequired: SincroBase
+ {
+ }
+
+ [Synchronization (SynchronizationAttribute.REQUIRES_NEW)]
+ class SincroRequiresNew: SincroBase
+ {
+ public bool TestCallback ()
+ {
+ SincroNotSupported bob = new SincroNotSupported ();
+ return bob.CallBack (this);
+ }
+ }
+
+ [Synchronization (SynchronizationAttribute.NOT_SUPPORTED)]
+ class SincroNotSupported: SincroBase
+ {
+ public bool CallBack (SincroRequiresNew bob)
+ {
+ return bob.CheckConcurrency ();
+ }
+ }
+
+ [Synchronization (SynchronizationAttribute.REQUIRES_NEW, true)]
+ class SincroRequiresNewReentrant: SincroBase
+ {
+ }
+
[TestFixture]
- // http://bugzilla.ximian.com/show_bug.cgi?id=72576
- [Category ("NotWorking")]
- public class SynchronizationAttributeTest: Assertion\r
- {\r
- SincroRequiresNew sincob = new SincroRequiresNew ();\r
- SincroNotSupported notsup = new SincroNotSupported ();\r
- SincroRequiresNewReentrant reentrant = new SincroRequiresNewReentrant ();\r
- SincroRequiresNew notreentrant = new SincroRequiresNew ();\r
- bool otResult;\r
-\r
- [Test]\r
- public void TestSynchronization ()\r
- {\r
- Thread tr = new Thread (new ThreadStart (FirstSyncThread));\r
- tr.Start ();\r
- Thread.Sleep (200);\r
- SecondSyncThread ();\r
- \r
- tr.Join ();\r
- Assert ("Concurrency detected in FirstSyncThread", !otResult);\r
- }\r
-\r
- void FirstSyncThread ()\r
- {\r
- otResult = sincob.CheckConcurrency ();\r
- }\r
-\r
- void SecondSyncThread ()\r
- {\r
- bool concurrent = sincob.CheckConcurrency ();\r
- Assert ("Concurrency detected", !concurrent);\r
- }\r
-\r
- [Test]\r
- public void TestSupported ()\r
- {\r
- SincroRequiresNew ob = new SincroRequiresNew ();\r
- SynchRes res = ob.CheckContextTransition (typeof(SincroSupported));\r
- Assert ("Synchronizaton context expected", res == SynchRes.SameSync);\r
-\r
- SincroSupported ob2 = new SincroSupported ();\r
- res = ob2.CheckContext (Thread.CurrentContext);\r
- Assert ("Synchronizaton context not expected", res == SynchRes.NoSync);\r
- }\r
-\r
- [Test]\r
- public void TestRequired ()\r
- {\r
- SincroRequiresNew ob = new SincroRequiresNew ();\r
- SynchRes res = ob.CheckContextTransition (typeof(SincroRequired));\r
- Assert ("Synchronizaton context expected 1", res == SynchRes.SameSync);\r
-\r
- SincroRequired ob2 = new SincroRequired ();\r
- res = ob2.CheckContext (Thread.CurrentContext);\r
- Assert ("Synchronizaton context expected 2", res == SynchRes.NewSync);\r
- }\r
-\r
- [Test]\r
- public void TestRequiresNew ()\r
- {\r
- SincroRequiresNew ob = new SincroRequiresNew ();\r
- SynchRes res = ob.CheckContextTransition (typeof(SincroRequiresNew));\r
- Assert ("New synchronizaton context expected", res == SynchRes.NewSync);\r
-\r
- SincroRequiresNew ob2 = new SincroRequiresNew ();\r
- res = ob2.CheckContext (Thread.CurrentContext);\r
- Assert ("Synchronizaton context not expected", res == SynchRes.NewSync);\r
- }\r
-\r
- [Test]\r
- public void TestNotSupported ()\r
- {\r
- SincroRequiresNew ob = new SincroRequiresNew ();\r
- SynchRes res = ob.CheckContextTransition (typeof(SincroNotSupported));\r
- Assert ("Synchronizaton context not expected 1", res == SynchRes.NoSync);\r
-\r
- SincroNotSupported ob2 = new SincroNotSupported ();\r
- res = ob2.CheckContext (Thread.CurrentContext);\r
- Assert ("Synchronizaton context not expected 2", res == SynchRes.NoSync);\r
- }\r
-\r
- [Test]\r
- public void TestLocked1 ()\r
- {\r
- sincob.Lock (false);\r
- Thread tr = new Thread (new ThreadStart (FirstSyncThread));\r
- tr.Start ();\r
- Thread.Sleep (200);\r
- SecondSyncThread ();\r
- \r
- tr.Join ();\r
- Assert ("Concurrency detected in FirstSyncThread", !otResult);\r
- }\r
-\r
- [Test]\r
- public void TestLocked2 ()\r
- {\r
- Thread tr = new Thread (new ThreadStart (FirstNotSyncThread));\r
- tr.Start ();\r
- Thread.Sleep (200);\r
- SecondNotSyncThread ();\r
- \r
- tr.Join ();\r
- Assert ("Concurrency not detected in FirstReentryThread", otResult);\r
- }\r
-\r
- void FirstNotSyncThread ()\r
- {\r
- otResult = sincob.CheckUnlockedConcurrency ();\r
- }\r
-\r
- void SecondNotSyncThread ()\r
- {\r
- bool concurrent = sincob.CheckConcurrency ();\r
- Assert ("Concurrency not detected", concurrent);\r
- }\r
-\r
- [Test]\r
- public void TestLocked3 ()\r
- {\r
- Thread tr = new Thread (new ThreadStart (Lock1Thread));\r
- tr.Start ();\r
- Thread.Sleep (200);\r
- Lock2Thread ();\r
- }\r
-\r
- void Lock1Thread ()\r
- {\r
- sincob.CheckLock1 ();\r
- }\r
-\r
- void Lock2Thread ()\r
- {\r
- sincob.CheckLock2 ();\r
- }\r
-\r
- [Test]\r
- public void TestReentry ()\r
- {\r
- Thread tr = new Thread (new ThreadStart (FirstReentryThread));\r
- tr.Start ();\r
- Thread.Sleep (200);\r
- SecondReentryThread ();\r
- \r
- tr.Join ();\r
- Assert ("Concurrency not detected in FirstReentryThread", otResult);\r
- }\r
-\r
- void FirstReentryThread ()\r
- {\r
- otResult = reentrant.CheckCalloutConcurrency (notsup);\r
- }\r
-\r
- void SecondReentryThread ()\r
- {\r
- bool concurrent = reentrant.CheckCalloutConcurrency (notsup);\r
- Assert ("Concurrency not detected", concurrent);\r
- }\r
-\r
- [Test]\r
- public void TestNoReentry ()\r
- {\r
- Thread tr = new Thread (new ThreadStart (FirstNoReentryThread));\r
- tr.Start ();\r
- Thread.Sleep (200);\r
- SecondNoReentryThread ();\r
- \r
- tr.Join ();\r
- Assert ("Concurrency detected in FirstNoReentryThread", !otResult);\r
- }\r
-\r
- void FirstNoReentryThread ()\r
- {\r
- otResult = notreentrant.CheckCalloutConcurrency (notsup);\r
- }\r
-\r
- void SecondNoReentryThread ()\r
- {\r
- bool concurrent = notreentrant.CheckCalloutConcurrency (notsup);\r
- Assert ("Concurrency detected", !concurrent);\r
- }\r
-\r
- [Test]\r
- public void TestCallback ()\r
- {\r
- Thread tr = new Thread (new ThreadStart (CallbackThread));\r
- tr.Start ();\r
- Thread.Sleep (200);\r
- bool concurrent = notreentrant.CheckConcurrency ();\r
- Assert ("Concurrency detected", !concurrent);\r
- notreentrant.CheckContext (Thread.CurrentContext);\r
- \r
- tr.Join ();\r
- Assert ("Concurrency detected in CallbackThread", !otResult);\r
- }\r
-\r
- void CallbackThread ()\r
- {\r
- otResult = notreentrant.TestCallback ();\r
- }\r
- \r
+ [Category ("MobileNotWorking")] // Bug #10267
+ public class SynchronizationAttributeTest
+ {
+ SincroRequiresNew sincob = new SincroRequiresNew ();
+ SincroNotSupported notsup = new SincroNotSupported ();
+ SincroRequiresNewReentrant reentrant = new SincroRequiresNewReentrant ();
+ SincroRequiresNew notreentrant = new SincroRequiresNew ();
+ bool otResult;
+
[Test]
- [Category("NotDotNet")]\r
- public void TestMonitorWait ()\r
- {\r
- Thread tr = new Thread (new ThreadStart (DoMonitorPulse));\r
- tr.Start ();\r
- \r
- bool r = sincob.CheckMonitorWait (true);\r
- Assert ("Wait timeout", r);\r
- \r
- r = tr.Join (1000);\r
- Assert ("Join timeout", r);\r
- \r
- tr = new Thread (new ThreadStart (DoMonitorPulse));\r
- tr.Start ();\r
- \r
- r = sincob.CheckMonitorWait (false);\r
- Assert ("Expected wait timeout", !r);\r
- \r
- r = tr.Join (1000);\r
- Assert ("Join timeout 2", r);\r
- }\r
-\r
- void DoMonitorPulse ()\r
- {\r
- Thread.Sleep (100);\r
- sincob.CheckMonitorPulse ();\r
- }\r
- }\r
-}\r
+ public void TestSynchronization ()
+ {
+ Thread tr = new Thread (new ThreadStart (FirstSyncThread));
+ tr.Start ();
+ Thread.Sleep (200);
+ SecondSyncThread ();
+
+ tr.Join ();
+ Assert.IsTrue (!otResult, "Concurrency detected in FirstSyncThread");
+ }
+
+ void FirstSyncThread ()
+ {
+ otResult = sincob.CheckConcurrency ();
+ }
+
+ void SecondSyncThread ()
+ {
+ bool concurrent = sincob.CheckConcurrency ();
+ Assert.IsTrue (!concurrent, "Concurrency detected");
+ }
+
+ [Test]
+ public void TestSupported ()
+ {
+ SincroRequiresNew ob = new SincroRequiresNew ();
+ SynchRes res = ob.CheckContextTransition (typeof(SincroSupported));
+ Assert.IsTrue (res == SynchRes.SameSync, "Synchronizaton context expected");
+
+ SincroSupported ob2 = new SincroSupported ();
+ res = ob2.CheckContext (Thread.CurrentContext);
+ Assert.IsTrue (res == SynchRes.NoSync, "Synchronizaton context not expected");
+ }
+
+ [Test]
+ public void TestRequired ()
+ {
+ SincroRequiresNew ob = new SincroRequiresNew ();
+ SynchRes res = ob.CheckContextTransition (typeof(SincroRequired));
+ Assert.IsTrue (res == SynchRes.SameSync, "Synchronizaton context expected 1");
+
+ SincroRequired ob2 = new SincroRequired ();
+ res = ob2.CheckContext (Thread.CurrentContext);
+ Assert.IsTrue (res == SynchRes.NewSync, "Synchronizaton context expected 2");
+ }
+
+ [Test]
+ public void TestRequiresNew ()
+ {
+ SincroRequiresNew ob = new SincroRequiresNew ();
+ SynchRes res = ob.CheckContextTransition (typeof(SincroRequiresNew));
+ Assert.IsTrue (res == SynchRes.NewSync, "New synchronizaton context expected");
+
+ SincroRequiresNew ob2 = new SincroRequiresNew ();
+ res = ob2.CheckContext (Thread.CurrentContext);
+ Assert.IsTrue (res == SynchRes.NewSync, "Synchronizaton context not expected");
+ }
+
+ [Test]
+ public void TestNotSupported ()
+ {
+ SincroRequiresNew ob = new SincroRequiresNew ();
+ SynchRes res = ob.CheckContextTransition (typeof(SincroNotSupported));
+ Assert.IsTrue (res == SynchRes.NoSync, "Synchronizaton context not expected 1");
+
+ SincroNotSupported ob2 = new SincroNotSupported ();
+ res = ob2.CheckContext (Thread.CurrentContext);
+ Assert.IsTrue (res == SynchRes.NoSync, "Synchronizaton context not expected 2");
+ }
+
+ [Test]
+ public void TestLocked1 ()
+ {
+ sincob.Lock (false);
+
+ Thread tr = new Thread (new ThreadStart (FirstSyncThread));
+ tr.Start ();
+ Thread.Sleep (200);
+ SecondSyncThread ();
+
+ tr.Join ();
+ Assert.IsTrue (!otResult, "Concurrency detected in FirstSyncThread");
+ }
+
+ [Test]
+ public void TestLocked2 ()
+ {
+ Thread tr = new Thread (new ThreadStart (FirstNotSyncThread));
+ tr.Start ();
+ Thread.Sleep (200);
+ SecondNotSyncThread ();
+
+ tr.Join ();
+ Assert.IsTrue (otResult, "Concurrency not detected in FirstReentryThread");
+ }
+
+ void FirstNotSyncThread ()
+ {
+ otResult = sincob.CheckUnlockedConcurrency ();
+ }
+
+ void SecondNotSyncThread ()
+ {
+ bool concurrent = sincob.CheckConcurrency ();
+ Assert.IsTrue (concurrent, "Concurrency not detected");
+ }
+
+ [Test]
+ public void TestLocked3 ()
+ {
+ Thread tr = new Thread (new ThreadStart (Lock1Thread));
+ tr.Start ();
+ Thread.Sleep (200);
+ Lock2Thread ();
+ }
+
+ void Lock1Thread ()
+ {
+ sincob.CheckLock1 ();
+ }
+
+ void Lock2Thread ()
+ {
+ sincob.CheckLock2 ();
+ }
+
+ [Test]
+ public void TestReentry ()
+ {
+ Thread tr = new Thread (new ThreadStart (FirstReentryThread));
+ tr.Start ();
+ Thread.Sleep (200);
+ SecondReentryThread ();
+
+ tr.Join ();
+ Assert.IsTrue (otResult, "Concurrency not detected in FirstReentryThread");
+ }
+
+ void FirstReentryThread ()
+ {
+ otResult = reentrant.CheckCalloutConcurrency (notsup);
+ }
+
+ void SecondReentryThread ()
+ {
+ bool concurrent = reentrant.CheckCalloutConcurrency (notsup);
+ Assert.IsTrue (concurrent, "Concurrency not detected");
+ }
+
+ [Test]
+ public void TestNoReentry ()
+ {
+ Thread tr = new Thread (new ThreadStart (FirstNoReentryThread));
+ tr.Start ();
+ Thread.Sleep (200);
+ SecondNoReentryThread ();
+
+ tr.Join ();
+ Assert.IsTrue (!otResult, "Concurrency detected in FirstNoReentryThread");
+ }
+
+ void FirstNoReentryThread ()
+ {
+ otResult = notreentrant.CheckCalloutConcurrency (notsup);
+ }
+
+ void SecondNoReentryThread ()
+ {
+ bool concurrent = notreentrant.CheckCalloutConcurrency (notsup);
+ Assert.IsTrue (!concurrent, "Concurrency detected");
+ }
+
+ [Test]
+ public void TestCallback ()
+ {
+ Thread tr = new Thread (new ThreadStart (CallbackThread));
+ tr.Start ();
+ Thread.Sleep (200);
+ bool concurrent = notreentrant.CheckConcurrency ();
+ Assert.IsTrue (!concurrent, "Concurrency detected");
+ notreentrant.CheckContext (Thread.CurrentContext);
+
+ tr.Join ();
+ Assert.IsTrue (!otResult, "Concurrency detected in CallbackThread");
+ }
+
+ [Test]
+ public void TestSynchronizationReleasedOnMultipleAcquire ()
+ {
+
+ otResult = notreentrant.TestCallback ();
+
+ Thread tr = new Thread (new ThreadStart (CallbackThread));
+ tr.Start();
+
+ bool terminated = tr.Join(10000);
+ Assert.IsTrue(terminated, "Thread didn't get lock of context bound object.");
+
+ Assert.IsTrue (!otResult, "Concurrency detected in CallbackThread");
+ }
+
+ void CallbackThread ()
+ {
+ otResult = notreentrant.TestCallback ();
+ }
+
+ [Test]
+ [Category("NotDotNet")]
+ [Category ("MobileNotWorking")]
+ public void TestMonitorWait ()
+ {
+ Thread tr = new Thread (new ThreadStart (DoMonitorPulse));
+ tr.Start ();
+
+ bool r = sincob.CheckMonitorWait (true);
+ Assert.IsTrue (r, "Wait timeout");
+
+ r = tr.Join (1000);
+ Assert.IsTrue (r, "Join timeout");
+
+ tr = new Thread (new ThreadStart (DoMonitorPulse));
+ tr.Start ();
+
+ r = sincob.CheckMonitorWait (false);
+ Assert.IsTrue (!r, "Expected wait timeout");
+
+ r = tr.Join (1000);
+ Assert.IsTrue (r, "Join timeout 2");
+ }
+
+ void DoMonitorPulse ()
+ {
+ Thread.Sleep (100);
+ sincob.CheckMonitorPulse ();
+ }
+ }
+}