mono-2.6.7-for-amiga /mcs/class/System.ServiceModel/Test/System.ServiceModel.Dispatcher/ChannelDispatcherTest.cs

Language C# Lines 471
MD5 Hash 211d151fa923071673031547fd9e4372 Estimated Cost $9,755 (why?)
Repository https://bitbucket.org/steenlund/mono-2.6.7-for-amiga View Raw File View Project SPDX
  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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using NUnit.Framework;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace MonoTests.System.ServiceModel.Dispatcher
{
	[TestFixture]
	public class ChannelDispatcherTest
	{
		Uri CreateAvailableUri (string uriString)
		{
			var uri = new Uri (uriString);
			try {
				var t = new TcpListener (uri.Port);
				t.Start ();
				t.Stop ();
			} catch (Exception ex) {
				Assert.Fail (String.Format ("Port {0} is not open. It is likely previous tests have failed and the port is kept opened", uri.Port));
			}
			return uri;
		}

		[Test]
		public void ConstructorNullBindingName ()
		{
			new ChannelDispatcher (new MyChannelListener (new Uri ("urn:foo")), null);
			new ChannelDispatcher (new MyChannelListener (new Uri ("urn:foo")), null, null);
		}

		[Test]
		public void ServiceThrottle ()
		{
			var cd = new ChannelDispatcher (new MyChannelListener<IReplyChannel> (new Uri ("urn:foo")));
			var st = cd.ServiceThrottle;
			Assert.IsNull (st, "#0");

			var uri = CreateAvailableUri ("http://localhost:37564");
			ServiceHost h = new ServiceHost (typeof (TestContract), uri);
			h.AddServiceEndpoint (typeof (TestContract).FullName, new BasicHttpBinding (), "address");
			h.ChannelDispatchers.Add (cd);
			Assert.IsNull (st, "#1");
			var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
			Assert.IsNull (ed.ChannelDispatcher, "#1-2");
			ed.DispatchRuntime.Type = typeof (TestContract);
			cd.Endpoints.Add (ed);
			Assert.AreEqual (cd, ed.ChannelDispatcher, "#1-3");
			cd.MessageVersion = MessageVersion.Default;

			{
				cd.Open (TimeSpan.FromSeconds (10));
				try {
					Assert.IsNull (st, "#2");
					// so, can't really test actual slot values as it is null.
				} finally {
					cd.Close (TimeSpan.FromSeconds (10));
				}
				return;
			}
		}

		[Test]			
		public void Collection_Add_Remove () {
			Console.WriteLine ("STart test Collection_Add_Remove");
			var uri = CreateAvailableUri ("http://localhost:37564");
			ServiceHost h = new ServiceHost (typeof (TestContract), uri);
			h.AddServiceEndpoint (typeof (TestContract).FullName, new BasicHttpBinding (), "address");
			MyChannelDispatcher d = new MyChannelDispatcher (new MyChannelListener (uri));
			h.ChannelDispatchers.Add (d);
			Assert.IsTrue (d.Attached, "#1");
			h.ChannelDispatchers.Remove (d);
			Assert.IsFalse (d.Attached, "#2");
			h.ChannelDispatchers.Insert (0, d);
			Assert.IsTrue (d.Attached, "#3");
			h.ChannelDispatchers.Add (new MyChannelDispatcher (new MyChannelListener (uri)));
			h.ChannelDispatchers.Clear ();
			Assert.IsFalse (d.Attached, "#4");
		}

		[Test]
		public void EndpointDispatcherAddTest ()
		{
			var uri = CreateAvailableUri ("http://localhost:37564");
			MyChannelDispatcher d = new MyChannelDispatcher (new MyChannelListener (uri));
			d.Endpoints.Add (new EndpointDispatcher (new EndpointAddress (uri), "", ""));
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))] 
		public void EndpointDispatcherAddTest2 () {
			var uri = CreateAvailableUri ("http://localhost:37564");
			MyChannelDispatcher d = new MyChannelDispatcher (new MyChannelListener (uri));
			d.Endpoints.Add (new EndpointDispatcher (new EndpointAddress (uri), "", ""));
			d.Open (); // the dispatcher must be attached.
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void EndpointDispatcherAddTest3 ()
		{
			var uri = CreateAvailableUri ("http://localhost:37564");
			ServiceHost h = new ServiceHost (typeof (TestContract), uri);
			MyChannelDispatcher d = new MyChannelDispatcher (new MyChannelListener (uri));
			d.Endpoints.Add (new EndpointDispatcher (new EndpointAddress (uri), "", ""));
			h.ChannelDispatchers.Add (d);
			d.Open (); // missing MessageVersion
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))] // i.e. it is thrown synchronously in current thread.
		public void EndpointDispatcherAddTest4 ()
		{
			var uri = CreateAvailableUri ("http://localhost:37564");
			ServiceHost h = new ServiceHost (typeof (TestContract), uri);
			var listener = new MyChannelListener (uri);
			MyChannelDispatcher d = new MyChannelDispatcher (listener);
			var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
			Assert.IsNotNull (ed.DispatchRuntime, "#1");
			Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#2");
			Assert.IsNull (ed.DispatchRuntime.InstanceContextProvider, "#3");
			Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#3.2");
			Assert.IsNull (ed.DispatchRuntime.SingletonInstanceContext, "#4");
			d.Endpoints.Add (ed);
			d.MessageVersion = MessageVersion.Default;
			h.ChannelDispatchers.Add (d);
			// it misses DispatchRuntime.Type, which seems set
			// automatically when the dispatcher is created in
			// ordinal process but need to be set manually in this case.
			try {
				d.Open ();
				try {
					// should not reach here, but in case it didn't, it must be closed.
					d.Close (TimeSpan.FromSeconds (10));
				} catch {
				}
			} finally {
				Assert.AreEqual (CommunicationState.Opened, listener.State, "#5");
			}
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))] // i.e. it is thrown synchronously in current thread.
		public void EndpointDispatcherAddTest5 ()
		{
			var uri = CreateAvailableUri ("http://localhost:37564");
			ServiceHost h = new ServiceHost (typeof (TestContract), uri);
			var binding = new BasicHttpBinding ();
			var listener = new MyChannelListener (uri);
			MyChannelDispatcher d = new MyChannelDispatcher (listener);
			var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
			d.Endpoints.Add (ed);

			ed.DispatchRuntime.Type = typeof (TestContract); // different from Test4

			d.MessageVersion = MessageVersion.Default;
			h.ChannelDispatchers.Add (d);
			// It rejects "unrecognized type" of the channel listener.
			// Test6 uses IChannelListener<IReplyChannel> and works.
			d.Open ();
			// should not reach here, but in case it didn't, it must be closed.
			d.Close (TimeSpan.FromSeconds (10));
		}

		[Test]
		public void EndpointDispatcherAddTest6 ()
		{
			var uri = CreateAvailableUri ("http://localhost:37564");
			ServiceHost h = new ServiceHost (typeof (TestContract), uri);
			var binding = new BasicHttpBinding ();
			var listener = new MyChannelListener<IReplyChannel> (uri);
			MyChannelDispatcher d = new MyChannelDispatcher (listener);
			var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
			d.Endpoints.Add (ed);
			Assert.IsFalse (d.Attached, "#x1");

			ed.DispatchRuntime.Type = typeof (TestContract);

			d.MessageVersion = MessageVersion.Default;
			h.ChannelDispatchers.Add (d);
			Assert.IsTrue (d.Attached, "#x2");
			d.Open (); // At this state, it does *not* call AcceptChannel() yet.
			Assert.IsFalse (listener.AcceptChannelTried, "#1");
			Assert.IsFalse (listener.WaitForChannelTried, "#2");

			Assert.IsNotNull (ed.DispatchRuntime, "#3");
			Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#4");
			Assert.IsNull (ed.DispatchRuntime.InstanceContextProvider, "#5"); // it is not still set after ChannelDispatcher.Open().
			Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#5.2");
			Assert.IsNull (ed.DispatchRuntime.SingletonInstanceContext, "#6");

			d.Close (); // we don't have to even close it.
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void EndpointDispatcherAddTest7 ()
		{
			var uri = CreateAvailableUri ("http://localhost:37564");
			ServiceHost h = new ServiceHost (typeof (TestContract), uri);
			var binding = new BasicHttpBinding ();
			var listener = new MyChannelListener<IReplyChannel> (uri);
			MyChannelDispatcher d = new MyChannelDispatcher (listener);
			var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
			d.Endpoints.Add (ed);

			ed.DispatchRuntime.Type = typeof (TestContract);

			d.MessageVersion = MessageVersion.Default;

			// add service endpoint to open the host (unlike all tests above).
			h.AddServiceEndpoint (typeof (TestContract),
				new BasicHttpBinding (), uri.ToString ());
			h.ChannelDispatchers.Clear ();

			h.ChannelDispatchers.Add (d);
			d.Open (); // At this state, it does *not* call AcceptChannel() yet.

			// This rejects already-opened ChannelDispatcher.
			h.Open (TimeSpan.FromSeconds (10));
			// should not reach here, but in case it didn't, it must be closed.
			h.Close (TimeSpan.FromSeconds (10));
		}

		[Test]
		public void EndpointDispatcherAddTest8 ()
		{
			var uri = CreateAvailableUri ("http://localhost:37564");
			ServiceHost h = new ServiceHost (typeof (TestContract), uri);
			var listener = new MyChannelListener<IReplyChannel> (uri);
			MyChannelDispatcher d = new MyChannelDispatcher (listener);
			var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
			d.Endpoints.Add (ed);

			ed.DispatchRuntime.Type = typeof (TestContract);

			d.MessageVersion = MessageVersion.Default;

			// add service endpoint to open the host (unlike all tests above).
			h.AddServiceEndpoint (typeof (TestContract),
				new BasicHttpBinding (), uri.ToString ());
			h.ChannelDispatchers.Clear ();

			h.ChannelDispatchers.Add (d);

			Assert.AreEqual (h, d.Host, "#0");

			try {
				h.Open (TimeSpan.FromSeconds (10));
				Assert.IsTrue (listener.BeginAcceptChannelTried, "#1"); // while it throws NIE ...
				Assert.IsFalse (listener.WaitForChannelTried, "#2");
				Assert.IsNotNull (ed.DispatchRuntime, "#3");
				Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#4");
				Assert.IsNotNull (ed.DispatchRuntime.InstanceContextProvider, "#5"); // it was set after ServiceHost.Open().
				Assert.IsNull (ed.DispatchRuntime.SingletonInstanceContext, "#6");
				/*
				var l = new HttpListener ();
				l.Prefixes.Add (uri.ToString ());
				l.Start ();
				l.Stop ();
				*/
			} finally {
				h.Close (TimeSpan.FromSeconds (10));
				h.Abort ();
			}
		}

		// FIXME: this test itself indeed passes, but some weird conflict that blocks correspoding port happens between this and somewhere (probably above)
//		[Test]
		public void EndpointDispatcherAddTest9 () // test singleton service
		{
			var uri = CreateAvailableUri ("http://localhost:37564");
			ServiceHost h = new ServiceHost (new TestContract (), uri);
			h.Description.Behaviors.Find<ServiceBehaviorAttribute> ().InstanceContextMode = InstanceContextMode.Single;
			var listener = new MyChannelListener<IReplyChannel> (uri);
			MyChannelDispatcher d = new MyChannelDispatcher (listener);
			var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
			d.Endpoints.Add (ed);
			ed.DispatchRuntime.Type = typeof (TestContract);
			d.MessageVersion = MessageVersion.Default;
			h.AddServiceEndpoint (typeof (TestContract), new BasicHttpBinding (), uri.ToString ());
			h.ChannelDispatchers.Clear ();
			Assert.IsNull (ed.DispatchRuntime.SingletonInstanceContext, "#1");
			h.ChannelDispatchers.Add (d);
			Assert.IsNull (ed.DispatchRuntime.SingletonInstanceContext, "#2");
			try {
				h.Open (TimeSpan.FromSeconds (10));
				Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#4");
				Assert.IsNotNull (ed.DispatchRuntime.InstanceContextProvider, "#5"); // it was set after ServiceHost.Open().
				Assert.IsNotNull (ed.DispatchRuntime.SingletonInstanceContext, "#6");
			} finally {
				h.Close (TimeSpan.FromSeconds (10));
				h.Abort ();
			}
		}

		[ServiceContract]
		public class TestContract
		{
			[OperationContract]
			public void Process (string input) {
			}
		}

		class MyChannelDispatcher : ChannelDispatcher
		{
			public bool Attached = false;

			public MyChannelDispatcher (IChannelListener l) : base (l) { }
			protected override void Attach (ServiceHostBase host) {
				base.Attach (host);
				Attached = true;				
			}

			protected override void Detach (ServiceHostBase host) {
				base.Detach (host);
				Attached = false;				
			}
		}

		class MyChannelListener<TChannel> : MyChannelListener, IChannelListener<TChannel> where TChannel : class, IChannel
		{
			public MyChannelListener (Uri uri)
				: base (uri)
			{
			}

			public bool AcceptChannelTried { get; set; }
			public bool BeginAcceptChannelTried { get; set; }

			public TChannel AcceptChannel ()
			{
				AcceptChannelTried = true;
				throw new NotImplementedException ();
			}

			public TChannel AcceptChannel (TimeSpan timeout)
			{
				AcceptChannelTried = true;
				throw new NotImplementedException ();
			}

			public IAsyncResult BeginAcceptChannel (AsyncCallback callback, object state)
			{
				BeginAcceptChannelTried = true;
				throw new NotImplementedException ();
			}

			public IAsyncResult BeginAcceptChannel (TimeSpan timeout, AsyncCallback callback, object state)
			{
				BeginAcceptChannelTried = true;
				throw new NotImplementedException ();
			}

			public TChannel EndAcceptChannel (IAsyncResult result)
			{
				throw new NotImplementedException ();
			}
		}

		class MyChannelListener : IChannelListener
		{
			public MyChannelListener (Uri uri)
			{
				Uri = uri;
			}

			public bool WaitForChannelTried { get; set; }

			public CommunicationState State { get; set; }

			#region IChannelListener Members

			public IAsyncResult BeginWaitForChannel (TimeSpan timeout, AsyncCallback callback, object state)
			{
				WaitForChannelTried = true;
				throw new NotImplementedException ();
			}

			public bool EndWaitForChannel (IAsyncResult result)
			{
				throw new NotImplementedException ();
			}

			public T GetProperty<T> () where T : class
			{
				throw new NotImplementedException ();
			}

			public Uri Uri { get; set; }

			public bool WaitForChannel (TimeSpan timeout)
			{
				WaitForChannelTried = true;
				throw new NotImplementedException ();
			}

			#endregion

			#region ICommunicationObject Members

			public void Abort ()
			{
				State = CommunicationState.Closed;
			}

			public IAsyncResult BeginClose (TimeSpan timeout, AsyncCallback callback, object state) {
				throw new NotImplementedException ();
			}

			public IAsyncResult BeginClose (AsyncCallback callback, object state) {
				throw new NotImplementedException ();
			}

			public IAsyncResult BeginOpen (TimeSpan timeout, AsyncCallback callback, object state) {
				throw new NotImplementedException ();
			}

			public IAsyncResult BeginOpen (AsyncCallback callback, object state) {
				throw new NotImplementedException ();
			}

			public void Close (TimeSpan timeout)
			{
				State = CommunicationState.Closed;
			}

			public void Close ()
			{
				State = CommunicationState.Closed;
			}

			public event EventHandler Closed;

			public event EventHandler Closing;

			public void EndClose (IAsyncResult result) {
				throw new NotImplementedException ();
			}

			public void EndOpen (IAsyncResult result) {
				throw new NotImplementedException ();
			}

			public event EventHandler Faulted;

			public void Open (TimeSpan timeout)
			{
				State = CommunicationState.Opened;
			}

			public void Open () 
			{
				State = CommunicationState.Opened;
			}

			public event EventHandler Opened;

			public event EventHandler Opening;

			#endregion
		}
	}
}
Back to Top