Remove DNS lookups of the local hostname in tests (#18059)
[mono-project.git] / mcs / class / System.Runtime.Remoting / Test / GenericTest.cs
blob79edd736627af59334f6d00d41e466e64610329a
1 //
2 // MonoTests.Remoting.GenericTest.cs
3 //
4 // Authors:
5 // Robert Jordan <robertj@gmx.net>
6 //
9 using System;
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
25 int Test ();
26 int Test (int i);
27 int Test (int a, int b);
28 V Test <V> (V v);
29 V Test <V, T> (V v, T t);
32 public interface ITest
34 V TestIface<V> (V v);
35 int TestDirectIfaceImpl (int i);
36 INested GetNested ();
37 INested GetNestedMbr ();
40 public class ServerBase<T> : MarshalByRefObject, ITest
42 public virtual V TestVirt<V> (V v)
44 return default (V);
47 public V TestIface<V> (V v)
49 return v;
52 int ITest.TestDirectIfaceImpl (int i)
54 return i;
57 public int TestDirectIfaceImpl (int i)
59 return -1;
62 public INested GetNested ()
64 return new Nested ();
67 public INested GetNested (string s)
69 return new Nested ();
72 public INested GetNestedMbr ()
74 return new NestedMbr ();
78 public class Server<T> : ServerBase<T>
80 public override V TestVirt<V> (V v)
82 return v;
86 [Serializable]
87 public class Nested : INested
89 public int Test ()
91 return 47;
94 int INested.Test ()
96 return 42;
99 public int Test (int i)
101 return i + 500;
104 int INested.Test (int a, int b)
106 return a + b;
109 public V Test <V> (V v)
111 return v;
114 V INested.Test <V, T> (V v, T t)
116 return default (V);
120 public class NestedMbr : MarshalByRefObject, INested
122 public int Test ()
124 return 47;
127 int INested.Test ()
129 return 42;
132 public int Test (int i)
134 return i + 500;
137 int INested.Test (int a, int b)
139 return a + b;
142 public V Test <V> (V v)
144 return v;
147 V INested.Test <V, T> (V v, T t)
149 return default (V);
154 [TestFixture]
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")]
162 [Test]
163 public void TestCrossAppDomainChannel ()
165 RunTests (RegisterAndConnect <Server<object>> ());
168 [Test]
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);
179 try {
180 Register <Server<object>> ("gentcptest.rem");
181 RunTests (Connect <Server<object>> ($"tcp://localhost:{port}/gentcptest.rem"));
182 } finally {
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");
214 ITest i = rem;
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>");