ChannelDispatcherTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using NUnit.Framework;
  7. using System.ServiceModel;
  8. using System.ServiceModel.Channels;
  9. using System.ServiceModel.Description;
  10. using System.ServiceModel.Dispatcher;
  11. namespace MonoTests.System.ServiceModel.Dispatcher
  12. {
  13. [TestFixture]
  14. public class ChannelDispatcherTest
  15. {
  16. [Test]
  17. public void ConstructorNullBindingName ()
  18. {
  19. new ChannelDispatcher (new MyChannelListener (new Uri ("urn:foo")), null);
  20. new ChannelDispatcher (new MyChannelListener (new Uri ("urn:foo")), null, null);
  21. }
  22. [Test]
  23. public void Collection_Add_Remove () {
  24. Console.WriteLine ("STart test Collection_Add_Remove");
  25. var uri = new Uri ("http://localhost:37564");
  26. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  27. h.AddServiceEndpoint (typeof (TestContract).FullName, new BasicHttpBinding (), "address");
  28. MyChannelDispatcher d = new MyChannelDispatcher (new MyChannelListener (uri));
  29. h.ChannelDispatchers.Add (d);
  30. Assert.IsTrue (d.Attached, "#1");
  31. h.ChannelDispatchers.Remove (d);
  32. Assert.IsFalse (d.Attached, "#2");
  33. h.ChannelDispatchers.Insert (0, d);
  34. Assert.IsTrue (d.Attached, "#3");
  35. h.ChannelDispatchers.Add (new MyChannelDispatcher (new MyChannelListener (uri)));
  36. h.ChannelDispatchers.Clear ();
  37. Assert.IsFalse (d.Attached, "#4");
  38. }
  39. [Test]
  40. public void EndpointDispatcherAddTest ()
  41. {
  42. var uri = new Uri ("http://localhost:8080");
  43. MyChannelDispatcher d = new MyChannelDispatcher (new MyChannelListener (uri));
  44. d.Endpoints.Add (new EndpointDispatcher (new EndpointAddress (uri), "", ""));
  45. }
  46. [Test]
  47. [ExpectedException (typeof (InvalidOperationException))]
  48. public void EndpointDispatcherAddTest2 () {
  49. var uri = new Uri ("http://localhost:8080");
  50. MyChannelDispatcher d = new MyChannelDispatcher (new MyChannelListener (uri));
  51. d.Endpoints.Add (new EndpointDispatcher (new EndpointAddress (uri), "", ""));
  52. d.Open (); // the dispatcher must be attached.
  53. }
  54. [Test]
  55. [ExpectedException (typeof (InvalidOperationException))]
  56. public void EndpointDispatcherAddTest3 ()
  57. {
  58. var uri = new Uri ("http://localhost:37564");
  59. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  60. MyChannelDispatcher d = new MyChannelDispatcher (new MyChannelListener (uri));
  61. d.Endpoints.Add (new EndpointDispatcher (new EndpointAddress (uri), "", ""));
  62. h.ChannelDispatchers.Add (d);
  63. d.Open (); // missing MessageVersion
  64. }
  65. [Test]
  66. [ExpectedException (typeof (InvalidOperationException))] // i.e. it is thrown synchronously in current thread.
  67. public void EndpointDispatcherAddTest4 ()
  68. {
  69. var uri = new Uri ("http://localhost:37564");
  70. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  71. var listener = new MyChannelListener (uri);
  72. MyChannelDispatcher d = new MyChannelDispatcher (listener);
  73. var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
  74. Assert.IsNotNull (ed.DispatchRuntime, "#1");
  75. Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#2");
  76. Assert.IsNull (ed.DispatchRuntime.InstanceContextProvider, "#3");
  77. Assert.IsNull (ed.DispatchRuntime.SingletonInstanceContext, "#4");
  78. d.Endpoints.Add (ed);
  79. d.MessageVersion = MessageVersion.Default;
  80. h.ChannelDispatchers.Add (d);
  81. // it misses DispatchRuntime.Type, which seems set
  82. // automatically when the dispatcher is created in
  83. // ordinal process but need to be set manually in this case.
  84. try {
  85. d.Open ();
  86. } finally {
  87. Assert.AreEqual (CommunicationState.Opened, listener.State, "#5");
  88. }
  89. }
  90. [Test]
  91. [ExpectedException (typeof (InvalidOperationException))] // i.e. it is thrown synchronously in current thread.
  92. public void EndpointDispatcherAddTest5 ()
  93. {
  94. var uri = new Uri ("http://localhost:37564");
  95. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  96. var binding = new BasicHttpBinding ();
  97. var listener = new MyChannelListener (uri);
  98. MyChannelDispatcher d = new MyChannelDispatcher (listener);
  99. var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
  100. d.Endpoints.Add (ed);
  101. ed.DispatchRuntime.Type = typeof (TestContract); // different from Test4
  102. d.MessageVersion = MessageVersion.Default;
  103. h.ChannelDispatchers.Add (d);
  104. // It rejects "unrecognized type" of the channel listener.
  105. // Test6 uses IChannelListener<IReplyChannel> and works.
  106. d.Open ();
  107. }
  108. [Test]
  109. public void EndpointDispatcherAddTest6 ()
  110. {
  111. var uri = new Uri ("http://localhost:37564");
  112. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  113. var binding = new BasicHttpBinding ();
  114. var listener = new MyChannelListener<IReplyChannel> (uri);
  115. MyChannelDispatcher d = new MyChannelDispatcher (listener);
  116. var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
  117. d.Endpoints.Add (ed);
  118. ed.DispatchRuntime.Type = typeof (TestContract);
  119. d.MessageVersion = MessageVersion.Default;
  120. h.ChannelDispatchers.Add (d);
  121. d.Open (); // At this state, it does *not* call AcceptChannel() yet.
  122. Assert.IsFalse (listener.AcceptChannelTried, "#1");
  123. Assert.IsFalse (listener.WaitForChannelTried, "#2");
  124. Assert.IsNotNull (ed.DispatchRuntime, "#3");
  125. Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#4");
  126. Assert.IsNull (ed.DispatchRuntime.InstanceContextProvider, "#5"); // it is not set after ChannelDispatcher.Open().
  127. Assert.IsNull (ed.DispatchRuntime.SingletonInstanceContext, "#6");
  128. // d.Close (); // we don't have to even close it.
  129. }
  130. [Test]
  131. [ExpectedException (typeof (InvalidOperationException))]
  132. public void EndpointDispatcherAddTest7 ()
  133. {
  134. var uri = new Uri ("http://localhost:37564");
  135. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  136. var binding = new BasicHttpBinding ();
  137. var listener = new MyChannelListener<IReplyChannel> (uri);
  138. MyChannelDispatcher d = new MyChannelDispatcher (listener);
  139. var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
  140. d.Endpoints.Add (ed);
  141. ed.DispatchRuntime.Type = typeof (TestContract);
  142. d.MessageVersion = MessageVersion.Default;
  143. // add service endpoint to open the host (unlike all tests above).
  144. h.AddServiceEndpoint (typeof (TestContract),
  145. new BasicHttpBinding (), uri.ToString ());
  146. h.ChannelDispatchers.Clear ();
  147. h.ChannelDispatchers.Add (d);
  148. d.Open (); // At this state, it does *not* call AcceptChannel() yet.
  149. // This rejects already-opened ChannelDispatcher.
  150. h.Open (TimeSpan.FromSeconds (10));
  151. // In case it is kept opened, it will block following tests, so close it explicitly.
  152. if (h.State == CommunicationState.Opened)
  153. h.Close ();
  154. }
  155. [Test]
  156. public void EndpointDispatcherAddTest8 ()
  157. {
  158. var uri = new Uri ("http://localhost:37564");
  159. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  160. var binding = new BasicHttpBinding ();
  161. var listener = new MyChannelListener<IReplyChannel> (uri);
  162. MyChannelDispatcher d = new MyChannelDispatcher (listener);
  163. var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
  164. d.Endpoints.Add (ed);
  165. ed.DispatchRuntime.Type = typeof (TestContract);
  166. d.MessageVersion = MessageVersion.Default;
  167. // add service endpoint to open the host (unlike all tests above).
  168. h.AddServiceEndpoint (typeof (TestContract),
  169. new BasicHttpBinding (), uri.ToString ());
  170. h.ChannelDispatchers.Clear ();
  171. h.ChannelDispatchers.Add (d);
  172. Assert.AreEqual (h, d.Host, "#0");
  173. try {
  174. h.Open (TimeSpan.FromSeconds (10));
  175. Assert.IsTrue (listener.AcceptChannelTried, "#1"); // while it throws NIE ...
  176. Assert.IsFalse (listener.WaitForChannelTried, "#2");
  177. Assert.IsNotNull (ed.DispatchRuntime, "#3");
  178. Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#4");
  179. Assert.IsNotNull (ed.DispatchRuntime.InstanceContextProvider, "#5"); // it was set after ServiceHost.Open().
  180. Assert.IsNull (ed.DispatchRuntime.SingletonInstanceContext, "#6");
  181. /*
  182. var l = new HttpListener ();
  183. l.Prefixes.Add (uri.ToString ());
  184. l.Start ();
  185. l.Stop ();
  186. */
  187. } finally {
  188. h.Close ();
  189. }
  190. }
  191. [ServiceContract]
  192. public class TestContract
  193. {
  194. [OperationContract]
  195. public void Process (string input) {
  196. }
  197. }
  198. class MyChannelDispatcher : ChannelDispatcher
  199. {
  200. public bool Attached = false;
  201. public MyChannelDispatcher (IChannelListener l) : base (l) { }
  202. protected override void Attach (ServiceHostBase host) {
  203. base.Attach (host);
  204. Attached = true;
  205. }
  206. protected override void Detach (ServiceHostBase host) {
  207. base.Detach (host);
  208. Attached = false;
  209. }
  210. }
  211. class MyChannelListener<TChannel> : MyChannelListener, IChannelListener<TChannel> where TChannel : class, IChannel
  212. {
  213. public MyChannelListener (Uri uri)
  214. : base (uri)
  215. {
  216. }
  217. public bool AcceptChannelTried { get; set; }
  218. public TChannel AcceptChannel ()
  219. {
  220. AcceptChannelTried = true;
  221. throw new NotImplementedException ();
  222. }
  223. public TChannel AcceptChannel (TimeSpan timeout)
  224. {
  225. AcceptChannelTried = true;
  226. throw new NotImplementedException ();
  227. }
  228. public IAsyncResult BeginAcceptChannel (AsyncCallback callback, object state)
  229. {
  230. AcceptChannelTried = true;
  231. throw new NotImplementedException ();
  232. }
  233. public IAsyncResult BeginAcceptChannel (TimeSpan timeout, AsyncCallback callback, object state)
  234. {
  235. AcceptChannelTried = true;
  236. throw new NotImplementedException ();
  237. }
  238. public TChannel EndAcceptChannel (IAsyncResult result)
  239. {
  240. throw new NotImplementedException ();
  241. }
  242. }
  243. class MyChannelListener : IChannelListener
  244. {
  245. public MyChannelListener (Uri uri)
  246. {
  247. Uri = uri;
  248. }
  249. public bool WaitForChannelTried { get; set; }
  250. public CommunicationState State { get; set; }
  251. #region IChannelListener Members
  252. public IAsyncResult BeginWaitForChannel (TimeSpan timeout, AsyncCallback callback, object state)
  253. {
  254. WaitForChannelTried = true;
  255. throw new NotImplementedException ();
  256. }
  257. public bool EndWaitForChannel (IAsyncResult result)
  258. {
  259. throw new NotImplementedException ();
  260. }
  261. public T GetProperty<T> () where T : class
  262. {
  263. throw new NotImplementedException ();
  264. }
  265. public Uri Uri { get; set; }
  266. public bool WaitForChannel (TimeSpan timeout)
  267. {
  268. WaitForChannelTried = true;
  269. throw new NotImplementedException ();
  270. }
  271. #endregion
  272. #region ICommunicationObject Members
  273. public void Abort ()
  274. {
  275. State = CommunicationState.Closed;
  276. }
  277. public IAsyncResult BeginClose (TimeSpan timeout, AsyncCallback callback, object state) {
  278. throw new NotImplementedException ();
  279. }
  280. public IAsyncResult BeginClose (AsyncCallback callback, object state) {
  281. throw new NotImplementedException ();
  282. }
  283. public IAsyncResult BeginOpen (TimeSpan timeout, AsyncCallback callback, object state) {
  284. throw new NotImplementedException ();
  285. }
  286. public IAsyncResult BeginOpen (AsyncCallback callback, object state) {
  287. throw new NotImplementedException ();
  288. }
  289. public void Close (TimeSpan timeout)
  290. {
  291. State = CommunicationState.Closed;
  292. }
  293. public void Close ()
  294. {
  295. State = CommunicationState.Closed;
  296. }
  297. public event EventHandler Closed;
  298. public event EventHandler Closing;
  299. public void EndClose (IAsyncResult result) {
  300. throw new NotImplementedException ();
  301. }
  302. public void EndOpen (IAsyncResult result) {
  303. throw new NotImplementedException ();
  304. }
  305. public event EventHandler Faulted;
  306. public void Open (TimeSpan timeout)
  307. {
  308. State = CommunicationState.Opened;
  309. }
  310. public void Open ()
  311. {
  312. State = CommunicationState.Opened;
  313. }
  314. public event EventHandler Opened;
  315. public event EventHandler Opening;
  316. #endregion
  317. }
  318. }
  319. }