2 // MonoTests.Remoting.GenericTest.cs
5 // Robert Jordan <robertj@gmx.net>
10 using System.Collections;
11 using System.Runtime.Remoting;
12 using System.Runtime.Remoting.Channels;
13 using System.Runtime.Remoting.Channels.Tcp;
14 using System.Runtime.Remoting.Channels.Http;
15 using System.Runtime.Remoting.Channels.Ipc;
16 using System.Threading;
17 using NUnit.Framework;
19 namespace MonoTests.Remoting
21 public interface INested
25 int Test (int a, int b);
27 V Test <V, T> (V v, T t);
30 public interface ITest
33 int TestDirectIfaceImpl (int i);
35 INested GetNestedMbr ();
38 public class ServerBase<T> : MarshalByRefObject, ITest
40 public virtual V TestVirt<V> (V v)
45 public V TestIface<V> (V v)
50 int ITest.TestDirectIfaceImpl (int i)
55 public int TestDirectIfaceImpl (int i)
60 public INested GetNested ()
65 public INested GetNested (string s)
70 public INested GetNestedMbr ()
72 return new NestedMbr ();
76 public class Server<T> : ServerBase<T>
78 public override V TestVirt<V> (V v)
85 public class Nested : INested
97 public int Test (int i)
102 int INested.Test (int a, int b)
107 public V Test <V> (V v)
112 V INested.Test <V, T> (V v, T t)
118 public class NestedMbr : MarshalByRefObject, INested
130 public int Test (int i)
135 int INested.Test (int a, int b)
140 public V Test <V> (V v)
145 V INested.Test <V, T> (V v, T t)
153 public class GenericTest
155 // Under MS.NET, INested.Test<V>(V v) isn't supported over the
156 // xappdom channel anymore (as of .NET 3.5). The stacktrace
157 // looks like if INested.Test(int) is invoked in place of
158 // INested.Test<int>(int).
159 [Category("NotDotNet")]
161 public void TestCrossAppDomainChannel ()
163 RunTests (RegisterAndConnect <Server<object>> ());
167 public void TestTcpChannel ()
169 IDictionary props = new Hashtable ();
170 props ["name"] = Guid.NewGuid ().ToString("N");
171 props ["port"] = 18191;
172 TcpChannel chan = new TcpChannel (props, null, null);
173 ChannelServices.RegisterChannel (chan);
176 Register <Server<object>> ("gentcptest.rem");
177 RunTests (Connect <Server<object>> ("tcp://localhost:18191/gentcptest.rem"));
179 ChannelServices.UnregisterChannel (chan);
183 static T RegisterAndConnect <T> () where T: MarshalByRefObject
185 AppDomain d = BaseCallTest.CreateDomain ("GenericTests");
186 return (T) d.CreateInstanceAndUnwrap (
187 typeof (T).Assembly.FullName,
188 typeof (T).FullName);
191 static void Register <T> (string uri) where T: MarshalByRefObject
193 object obj = Activator.CreateInstance (typeof(T));
194 RemotingServices.Marshal ((MarshalByRefObject)obj, uri);
197 static T Connect <T> (string uri) where T: MarshalByRefObject
199 return (T) RemotingServices.Connect (typeof (T), uri);
202 static void RunTests (ServerBase<object> rem)
204 Assert.AreEqual (42, rem.TestIface<int>(42),
205 "#1 calling TestIface on object instance");
207 Assert.AreEqual (42, rem.TestVirt<int>(42),
208 "#2 calling TestVirt");
211 Assert.AreEqual (42, i.TestIface<int>(42),
212 "#3 calling TestIface on interface");
214 Assert.AreEqual (42, i.TestDirectIfaceImpl (42),
215 "#4 calling TestDirectIfaceImp");
217 INested cao = rem.GetNested ();
218 Assert.AreEqual (42, cao.Test (),
219 "#5a calling INested.Test ()");
221 Assert.AreEqual (42 + 500, cao.Test (42),
222 "#5 calling INested.Test (int)");
224 Assert.AreEqual (42, cao.Test (21, 21),
225 "#6 calling INested.Test (int, int)");
227 Assert.AreEqual (42, cao.Test<int> (42),
228 "#7 calling INested.Test<V>");
230 Assert.AreEqual (0, cao.Test<int, string> (42, "bar"),
231 "#8 calling INested.Test<V, T>");
233 cao = rem.GetNestedMbr ();
234 Assert.AreEqual (42, cao.Test (),
235 "#9a calling INested.Test ()");
237 Assert.AreEqual (42 + 500, cao.Test (42),
238 "#9 calling INested.Test (int)");
240 Assert.AreEqual (42, cao.Test (21, 21),
241 "#10 calling INested.Test (int, int)");
243 Assert.AreEqual (42, cao.Test<int> (42),
244 "#11 calling INested.Test<V>");
246 Assert.AreEqual (0, cao.Test<int, string> (42, "bar"),
247 "#12 calling INested.Test<V, T>");