File: GenericTest.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (255 lines) | stat: -rw-r--r-- 5,002 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// MonoTests.Remoting.GenericTest.cs
//
// Authors:
//     Robert Jordan  <robertj@gmx.net>
//


using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Ipc;
using System.Threading;
using NUnit.Framework;

using MonoTests.Helpers;

namespace MonoTests.Remoting
{
	public interface INested
	{
		int Test ();
		int Test (int i);
		int Test (int a, int b);
		V Test <V> (V v);
		V Test <V, T> (V v, T t);
	}

	public interface ITest
	{
		V TestIface<V> (V v);
		int TestDirectIfaceImpl (int i);
		INested GetNested ();
		INested GetNestedMbr ();
	}

	public class ServerBase<T> : MarshalByRefObject, ITest
	{
		public virtual V TestVirt<V> (V v)
		{
			return default (V);
		}

		public V TestIface<V> (V v)
		{
			return v;
		}

		int ITest.TestDirectIfaceImpl (int i)
		{
			return i;
		}

		public int TestDirectIfaceImpl (int i)
		{
			return -1;
		}

		public INested GetNested ()
		{
			return new Nested ();
		}

		public INested GetNested (string s)
		{
			return new Nested ();
		}

		public INested GetNestedMbr ()
		{
			return new NestedMbr ();
		}
	}

	public class Server<T> : ServerBase<T>
	{
		public override V TestVirt<V> (V v)
		{
			return v;
		}
	}

	[Serializable]
	public class Nested : INested
	{
		public int Test ()
		{
			return 47;
		}

		int INested.Test ()
		{
			return 42;
		}
		
		public int Test (int i)
		{
			return i + 500;
		}

		int INested.Test (int a, int b)
		{
			return a + b;
		}

		public V Test <V> (V v)
		{
			return v;
		}

		V INested.Test <V, T> (V v, T t)
		{
			return default (V);
		}
	}

	public class NestedMbr : MarshalByRefObject, INested
	{
		public int Test ()
		{
			return 47;
		}
		
		int INested.Test ()
		{
			return 42;
		}

		public int Test (int i)
		{
			return i + 500;
		}

		int INested.Test (int a, int b)
		{
			return a + b;
		}

		public V Test <V> (V v)
		{
			return v;
		}

		V INested.Test <V, T> (V v, T t)
		{
			return default (V);
		}
	}


	[TestFixture]
	public class GenericTest
	{
		// Under MS.NET, INested.Test<V>(V v) isn't supported over the
		// xappdom channel anymore (as of .NET 3.5). The stacktrace
		// looks like if INested.Test(int) is invoked in place of
		// INested.Test<int>(int).
		[Category("NotDotNet")]
		[Test]
		public void TestCrossAppDomainChannel ()
		{
			RunTests (RegisterAndConnect <Server<object>> ());
		}

		[Test]
		public void TestTcpChannel ()
		{
			var port = NetworkHelpers.FindFreePort ();
			IDictionary props = new Hashtable ();
			props ["name"] = Guid.NewGuid ().ToString("N");
			props ["port"] = port;
			props ["bindTo"] = "127.0.0.1";
			TcpChannel chan = new TcpChannel (props, null, null);
			ChannelServices.RegisterChannel (chan);
			
			try {
				Register <Server<object>> ("gentcptest.rem");
				RunTests (Connect <Server<object>> ($"tcp://localhost:{port}/gentcptest.rem"));
			} finally {
				ChannelServices.UnregisterChannel (chan);
			}
		}

		static T RegisterAndConnect <T> () where T: MarshalByRefObject
		{
			AppDomain d = BaseCallTest.CreateDomain ("GenericTests");
			return (T) d.CreateInstanceAndUnwrap (
				typeof (T).Assembly.FullName,
				typeof (T).FullName);
		}

		static void Register <T> (string uri) where T: MarshalByRefObject
		{
			object obj = Activator.CreateInstance (typeof(T));
			RemotingServices.Marshal ((MarshalByRefObject)obj, uri);
		}

		static T Connect <T> (string uri) where T: MarshalByRefObject
		{
			return (T) RemotingServices.Connect (typeof (T), uri);
		}

		static void RunTests (ServerBase<object> rem)
		{
			Assert.AreEqual (42, rem.TestIface<int>(42),
					 "#1 calling TestIface on object instance");

			Assert.AreEqual (42, rem.TestVirt<int>(42),
					 "#2 calling TestVirt");

			ITest i = rem;
			Assert.AreEqual (42, i.TestIface<int>(42),
					 "#3 calling TestIface on interface");

			Assert.AreEqual (42, i.TestDirectIfaceImpl (42),
					 "#4 calling TestDirectIfaceImp");

			INested cao = rem.GetNested ();
			Assert.AreEqual (42, cao.Test (),
					 "#5a calling INested.Test ()");

			Assert.AreEqual (42 + 500, cao.Test (42),
					 "#5 calling INested.Test (int)");

			Assert.AreEqual (42, cao.Test (21, 21),
					 "#6 calling INested.Test (int, int)");

			Assert.AreEqual (42, cao.Test<int> (42),
					 "#7 calling INested.Test<V>");

			Assert.AreEqual (0, cao.Test<int, string> (42, "bar"),
					 "#8 calling INested.Test<V, T>");

			cao = rem.GetNestedMbr ();
			Assert.AreEqual (42, cao.Test (),
					 "#9a calling INested.Test ()");

			Assert.AreEqual (42 + 500, cao.Test (42),
					 "#9 calling INested.Test (int)");

			Assert.AreEqual (42, cao.Test (21, 21),
					 "#10 calling INested.Test (int, int)");

			Assert.AreEqual (42, cao.Test<int> (42),
					 "#11 calling INested.Test<V>");

			Assert.AreEqual (0, cao.Test<int, string> (42, "bar"),
					 "#12 calling INested.Test<V, T>");
		}
	}
}