1 // MarshalByRefObjectTest.cs Test class for
\r
2 // System.MarshalByRefObject class
\r
4 // Jean-Marc Andre (jean-marc.andre@polymtl.ca)
\r
8 using System.Runtime.Remoting;
\r
9 using System.Runtime.Remoting.Lifetime;
\r
10 using System.Runtime.Serialization;
\r
11 using System.Runtime.Remoting.Channels;
\r
12 using System.Runtime.Remoting.Channels.Tcp;
\r
14 // Just an internal test namespace for
\r
15 // the MarshalByRefObjectTest class
\r
16 namespace MonoTests.System.MarshalByRefObjectTestInternal
\r
19 // Object from this class can be marshaled
\r
20 public class MarshalObject: MarshalByRefObject
\r
22 public MarshalObject(){}
\r
24 public MarshalObject(int id)
\r
29 // This method override the default one
\r
30 // so we can set some properties of the lifetime
\r
31 // of the remot object
\r
32 public override object InitializeLifetimeService()
\r
34 ILease lease = (ILease) base.InitializeLifetimeService();
\r
36 // By default InitialLeaseTime is set to 5 minutes
\r
37 // we set it at 10 seconds
\r
38 if(lease.CurrentState == LeaseState.Initial)
\r
40 lease.InitialLeaseTime = TimeSpan.FromSeconds(10);
\r
53 } // MonoTests.System.MarshalByRefObjectTestInternal
\r
56 namespace MonoTests.System
\r
58 using MonoTests.System.MarshalByRefObjectTestInternal;
\r
59 using NUnit.Framework;
\r
61 // The main test class
\r
63 public class MarshalByRefObjectTest
\r
65 public MarshalByRefObjectTest()
\r
70 // This method test the CreateObjRef
\r
72 public void CreateObjRef()
\r
74 MarshalObject objMarshal = new MarshalObject();
\r
76 RemotingServices.SetObjectUriForMarshal(objMarshal, "MarshalByRefObjectTest.objMarshal1");
\r
77 RemotingServices.Marshal(objMarshal);
\r
79 ObjRef objRef = objMarshal.CreateObjRef(typeof(MarshalObject));
\r
80 Assertion.AssertEquals("#A01", objRef.URI, RemotingServices.GetObjectUri(objMarshal));
\r
82 // TODO: When implemented in the mono RemotingServices class
\r
83 //RemotingServices.Disconnect(objMarshal);
\r
87 [ExpectedException(typeof(RemotingException))]
\r
88 public void CreateObjRefThrowException()
\r
90 MarshalObject objMarshal = new MarshalObject();
\r
92 ObjRef objRef = objMarshal.CreateObjRef(typeof(MarshalObject));
\r
95 // This method both tests InitializeLifetimeService()
\r
96 // and GetLifetimeService()
\r
98 public void LifetimeService()
\r
100 MarshalObject objMarshal = new MarshalObject();
\r
102 RemotingServices.SetObjectUriForMarshal(objMarshal, "MarshalByRefObjectTest.objMarshal2");
\r
103 RemotingServices.Marshal(objMarshal);
\r
105 objMarshal.InitializeLifetimeService();
\r
106 ILease lease = (ILease) objMarshal.GetLifetimeService();
\r
107 Assertion.AssertEquals("#A02", lease.InitialLeaseTime, TimeSpan.FromSeconds(10));
\r
109 // TODO: When implemented in the mono RemotingServices class
\r
110 //RemotingServices.Disconnect(objMarshal);
\r
113 // Here we test if we a published object can be get
\r
114 // through a TcpChannel
\r
116 public void GetObject()
\r
118 MarshalObject objMarshal = new MarshalObject(1);
\r
120 RemotingServices.SetObjectUriForMarshal(objMarshal, "MarshalByRefObjectTest.objMarshal3");
\r
121 RemotingServices.Marshal(objMarshal);
\r
123 TcpChannel chn = new TcpChannel(1294);
\r
124 ChannelServices.RegisterChannel(chn);
\r
126 object objRem = Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1294/MarshalByRefObjectTest.objMarshal3");
\r
128 MarshalObject objMarshalRem = (MarshalObject) objRem;
\r
130 Assertion.AssertEquals("#A03", 1, objMarshalRem.Id);
\r
132 // TODO: When implemented in the mono RemotingServices class
\r
133 //RemotingServices.Disconnect(objMarshal);
\r
134 // chn.StopListening(null);
\r
135 ChannelServices.UnregisterChannel(chn);
\r