ChannelDispatcherTest.cs 12 KB

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