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 using MonoTests
.Helpers
;
21 namespace MonoTests
.Remoting
23 public interface INested
27 int Test (int a
, int b
);
29 V Test
<V
, T
> (V v
, T t
);
32 public interface ITest
35 int TestDirectIfaceImpl (int i
);
37 INested
GetNestedMbr ();
40 public class ServerBase
<T
> : MarshalByRefObject
, ITest
42 public virtual V TestVirt
<V
> (V v
)
47 public V TestIface
<V
> (V v
)
52 int ITest
.TestDirectIfaceImpl (int i
)
57 public int TestDirectIfaceImpl (int i
)
62 public INested
GetNested ()
67 public INested
GetNested (string s
)
72 public INested
GetNestedMbr ()
74 return new NestedMbr ();
78 public class Server
<T
> : ServerBase
<T
>
80 public override V TestVirt
<V
> (V v
)
87 public class Nested
: INested
99 public int Test (int i
)
104 int INested
.Test (int a
, int b
)
109 public V Test
<V
> (V v
)
114 V INested
.Test
<V
, T
> (V v
, T t
)
120 public class NestedMbr
: MarshalByRefObject
, INested
132 public int Test (int i
)
137 int INested
.Test (int a
, int b
)
142 public V Test
<V
> (V v
)
147 V INested
.Test
<V
, T
> (V v
, T t
)
155 public class GenericTest
157 // Under MS.NET, INested.Test<V>(V v) isn't supported over the
158 // xappdom channel anymore (as of .NET 3.5). The stacktrace
159 // looks like if INested.Test(int) is invoked in place of
160 // INested.Test<int>(int).
161 [Category("NotDotNet")]
163 public void TestCrossAppDomainChannel ()
165 RunTests (RegisterAndConnect
<Server
<object>> ());
169 public void TestTcpChannel ()
171 var port
= NetworkHelpers
.FindFreePort ();
172 IDictionary props
= new Hashtable ();
173 props
["name"] = Guid
.NewGuid ().ToString("N");
174 props
["port"] = port
;
175 props
["bindTo"] = "127.0.0.1";
176 TcpChannel chan
= new TcpChannel (props
, null, null);
177 ChannelServices
.RegisterChannel (chan
);
180 Register
<Server
<object>> ("gentcptest.rem");
181 RunTests (Connect
<Server
<object>> ($"tcp://localhost:{port}/gentcptest.rem"));
183 ChannelServices
.UnregisterChannel (chan
);
187 static T RegisterAndConnect
<T
> () where T
: MarshalByRefObject
189 AppDomain d
= BaseCallTest
.CreateDomain ("GenericTests");
190 return (T
) d
.CreateInstanceAndUnwrap (
191 typeof (T
).Assembly
.FullName
,
192 typeof (T
).FullName
);
195 static void Register
<T
> (string uri
) where T
: MarshalByRefObject
197 object obj
= Activator
.CreateInstance (typeof(T
));
198 RemotingServices
.Marshal ((MarshalByRefObject
)obj
, uri
);
201 static T Connect
<T
> (string uri
) where T
: MarshalByRefObject
203 return (T
) RemotingServices
.Connect (typeof (T
), uri
);
206 static void RunTests (ServerBase
<object> rem
)
208 Assert
.AreEqual (42, rem
.TestIface
<int>(42),
209 "#1 calling TestIface on object instance");
211 Assert
.AreEqual (42, rem
.TestVirt
<int>(42),
212 "#2 calling TestVirt");
215 Assert
.AreEqual (42, i
.TestIface
<int>(42),
216 "#3 calling TestIface on interface");
218 Assert
.AreEqual (42, i
.TestDirectIfaceImpl (42),
219 "#4 calling TestDirectIfaceImp");
221 INested cao
= rem
.GetNested ();
222 Assert
.AreEqual (42, cao
.Test (),
223 "#5a calling INested.Test ()");
225 Assert
.AreEqual (42 + 500, cao
.Test (42),
226 "#5 calling INested.Test (int)");
228 Assert
.AreEqual (42, cao
.Test (21, 21),
229 "#6 calling INested.Test (int, int)");
231 Assert
.AreEqual (42, cao
.Test
<int> (42),
232 "#7 calling INested.Test<V>");
234 Assert
.AreEqual (0, cao
.Test
<int, string> (42, "bar"),
235 "#8 calling INested.Test<V, T>");
237 cao
= rem
.GetNestedMbr ();
238 Assert
.AreEqual (42, cao
.Test (),
239 "#9a calling INested.Test ()");
241 Assert
.AreEqual (42 + 500, cao
.Test (42),
242 "#9 calling INested.Test (int)");
244 Assert
.AreEqual (42, cao
.Test (21, 21),
245 "#10 calling INested.Test (int, int)");
247 Assert
.AreEqual (42, cao
.Test
<int> (42),
248 "#11 calling INested.Test<V>");
250 Assert
.AreEqual (0, cao
.Test
<int, string> (42, "bar"),
251 "#12 calling INested.Test<V, T>");