--- /dev/null
+//
+// MonoTests.System.Runtime.Remoting.BaseCalls.cs
+//
+// Author: Lluis Sanchez Gual (lluis@ximian.com)
+//
+// 2003 (C) Copyright, Novell, Inc.
+//
+
+using System;
+using System.Threading;
+using System.Collections;
+using System.Runtime.Remoting;
+using System.Runtime.Remoting.Activation;
+using System.Runtime.Remoting.Contexts;
+using NUnit.Framework;
+
+namespace MonoTests.System.Runtime.Remoting
+{
+
+ public class NeedsContextAttribute: Attribute, IContextAttribute
+ {
+ public void GetPropertiesForNewContext (IConstructionCallMessage msg) {}
+ public bool IsContextOK (Context ctx, IConstructionCallMessage msg) { return false; }
+ }
+
+ [NeedsContextAttribute]
+ public class TestCbo: ContextBoundObject
+ {
+ public Context GetContext ()
+ {
+ return Thread.CurrentContext;
+ }
+ }
+
+ [TestFixture]
+ public class ContextText: Assertion
+ {
+ TestCbo cbo = new TestCbo ();
+ Context otherCtx;
+ LocalDataStoreSlot slot;
+
+ [Test]
+ public void TestDoCallback ()
+ {
+ otherCtx = cbo.GetContext ();
+ Assert ("New context not created", Thread.CurrentContext != otherCtx);
+
+ otherCtx.DoCallBack (new CrossContextDelegate (DelegateTarget));
+ }
+
+ void DelegateTarget ()
+ {
+ Assert ("Wrong context", Thread.CurrentContext == otherCtx);
+ }
+
+ [Test]
+ public void TestDatastore ()
+ {
+ otherCtx = cbo.GetContext ();
+
+ slot = Context.AllocateDataSlot ();
+ LocalDataStoreSlot namedSlot1 = Context.AllocateNamedDataSlot ("slot1");
+ LocalDataStoreSlot namedSlot2 = Context.GetNamedDataSlot ("slot2");
+
+ Context.SetData (slot, "data");
+ Context.SetData (namedSlot1, "data1");
+ Context.SetData (namedSlot2, "data2");
+
+ otherCtx.DoCallBack (new CrossContextDelegate (CheckOtherContextDatastore));
+
+ Assert ("Wrong data 1", Context.GetData (slot).Equals ("data"));
+ Assert ("Wrong data 2", Context.GetData (namedSlot1).Equals ("data1"));
+ Assert ("Wrong data 3", Context.GetData (namedSlot2).Equals ("data2"));
+
+ try
+ {
+ namedSlot1 = Context.AllocateNamedDataSlot ("slot1");
+ Assert ("Exception expected",false);
+ }
+ catch {}
+
+ Context.FreeNamedDataSlot ("slot1");
+ Context.FreeNamedDataSlot ("slot2");
+
+ try
+ {
+ namedSlot1 = Context.AllocateNamedDataSlot ("slot1");
+ }
+ catch
+ {
+ Assert ("Exception not expected",false);
+ }
+
+ Context.FreeNamedDataSlot ("slot1");
+ }
+
+ void CheckOtherContextDatastore ()
+ {
+ LocalDataStoreSlot namedSlot1 = Context.GetNamedDataSlot ("slot1");
+ LocalDataStoreSlot namedSlot2 = Context.GetNamedDataSlot ("slot2");
+
+ Assert ("Slot already has data", Context.GetData (slot) == null);
+ Assert ("Slot already has data", Context.GetData (namedSlot1) == null);
+ Assert ("Slot already has data", Context.GetData (namedSlot2) == null);
+
+ Context.SetData (slot, "other data");
+ Context.SetData (namedSlot1, "other data1");
+ Context.SetData (namedSlot2, "other data2");
+ }
+
+ }
+}
--- /dev/null
+//\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
+\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
+ [TestFixture]\r
+ 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
+\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
+\r
+ void FirstSyncThread ()\r
+ {\r
+ bool concurrent = sincob.CheckConcurrency ();\r
+ Assert ("Concurrency detected", !concurrent);\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
+\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
+\r
+ void FirstNotSyncThread ()\r
+ {\r
+ bool concurrent = sincob.CheckUnlockedConcurrency ();\r
+ Assert ("Concurrency not detected", concurrent);\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
+\r
+ void FirstReentryThread ()\r
+ {\r
+ bool concurrent = reentrant.CheckCalloutConcurrency (notsup);\r
+ Assert ("Concurrency not detected", concurrent);\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
+\r
+ void FirstNoReentryThread ()\r
+ {\r
+ bool concurrent = notreentrant.CheckCalloutConcurrency (notsup);\r
+ Assert ("Concurrency detected", !concurrent);\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
+\r
+ void CallbackThread ()\r
+ {\r
+ bool concurrent = notreentrant.TestCallback ();\r
+ Assert ("Concurrency detected", !concurrent);\r
+ }\r
+ }\r
+}\r