2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / corlib / Test / System.Runtime.Remoting / ContextTest.cs
1 //
2 // MonoTests.System.Runtime.Remoting.BaseCalls.cs
3 //
4 // Author: Lluis Sanchez Gual (lluis@ximian.com)
5 //
6 // 2003 (C) Copyright, Novell, Inc.
7 //
8
9 using System;
10 using System.Threading;
11 using System.Collections;
12 using System.Runtime.Remoting;
13 using System.Runtime.Remoting.Activation;
14 using System.Runtime.Remoting.Contexts;
15 using NUnit.Framework;
16
17 namespace MonoTests.System.Runtime.Remoting
18 {
19         
20         public class NeedsContextAttribute: Attribute, IContextAttribute 
21         {
22                 public void GetPropertiesForNewContext (IConstructionCallMessage msg) {}
23                 public bool IsContextOK (Context ctx, IConstructionCallMessage msg) { return false; }
24         }
25         
26         [NeedsContextAttribute]
27         public class TestCbo: ContextBoundObject
28         {
29                 public Context GetContext ()
30                 {
31                         return Thread.CurrentContext;
32                 }
33         }
34         
35         [TestFixture]
36         public class ContextText
37         {
38                 TestCbo cbo = new TestCbo ();
39                 Context otherCtx;
40                 LocalDataStoreSlot slot;
41                 
42                 [Test]
43                 public void TestDoCallback ()
44                 {
45                         otherCtx = cbo.GetContext ();
46                         Assert.IsTrue (Thread.CurrentContext != otherCtx, "New context not created");
47                         
48                         otherCtx.DoCallBack (new CrossContextDelegate (DelegateTarget));
49                 }
50                 
51                 void DelegateTarget ()
52                 {
53                         Assert.IsTrue (Thread.CurrentContext == otherCtx, "Wrong context");
54                 }
55                 
56                 [Test]
57                 public void TestDatastore ()
58                 {
59                         otherCtx = cbo.GetContext ();
60                         
61                         slot = Context.AllocateDataSlot ();
62                         LocalDataStoreSlot namedSlot1 = Context.AllocateNamedDataSlot ("slot1");
63                         LocalDataStoreSlot namedSlot2 = Context.GetNamedDataSlot ("slot2");
64                         
65                         Context.SetData (slot, "data");
66                         Context.SetData (namedSlot1, "data1");
67                         Context.SetData (namedSlot2, "data2");
68                         
69                         otherCtx.DoCallBack (new CrossContextDelegate (CheckOtherContextDatastore));
70                         
71                         Assert.IsTrue (Context.GetData (slot).Equals ("data"), "Wrong data 1");
72                         Assert.IsTrue (Context.GetData (namedSlot1).Equals ("data1"), "Wrong data 2");
73                         Assert.IsTrue (Context.GetData (namedSlot2).Equals ("data2"), "Wrong data 3");
74                         
75                         try
76                         {
77                                 namedSlot1 = Context.AllocateNamedDataSlot ("slot1");
78                                 Assert.Fail ("Exception expected");
79                         }
80                         catch {}
81                         
82                         Context.FreeNamedDataSlot ("slot1");
83                         Context.FreeNamedDataSlot ("slot2");
84                         
85                         try
86                         {
87                                 namedSlot1 = Context.AllocateNamedDataSlot ("slot1");
88                         }
89                         catch 
90                         {
91                                 Assert.Fail ("Exception not expected");
92                         }
93                         
94                         Context.FreeNamedDataSlot ("slot1");
95                 }
96                 
97                 void CheckOtherContextDatastore ()
98                 {
99                         LocalDataStoreSlot namedSlot1 = Context.GetNamedDataSlot ("slot1");
100                         LocalDataStoreSlot namedSlot2 = Context.GetNamedDataSlot ("slot2");
101                         
102                         Assert.IsTrue (Context.GetData (slot) == null, "Slot already has data");
103                         Assert.IsTrue (Context.GetData (namedSlot1) == null, "Slot already has data");
104                         Assert.IsTrue (Context.GetData (namedSlot2) == null, "Slot already has data");
105                         
106                         Context.SetData (slot, "other data");
107                         Context.SetData (namedSlot1, "other data1");
108                         Context.SetData (namedSlot2, "other data2");
109                 }
110                 
111         }
112 }