ChannelDispatcherTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 (TimeSpan.FromSeconds (10));
  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 (TimeSpan.FromSeconds (10));
  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. try {
  113. // should not reach here, but in case it didn't, it must be closed.
  114. d.Close (TimeSpan.FromSeconds (10));
  115. } catch {
  116. }
  117. } finally {
  118. Assert.AreEqual (CommunicationState.Opened, listener.State, "#5");
  119. }
  120. }
  121. [Test]
  122. [ExpectedException (typeof (InvalidOperationException))] // i.e. it is thrown synchronously in current thread.
  123. public void EndpointDispatcherAddTest5 ()
  124. {
  125. var uri = new Uri ("http://localhost:37564");
  126. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  127. var binding = new BasicHttpBinding ();
  128. var listener = new MyChannelListener (uri);
  129. MyChannelDispatcher d = new MyChannelDispatcher (listener);
  130. var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
  131. d.Endpoints.Add (ed);
  132. ed.DispatchRuntime.Type = typeof (TestContract); // different from Test4
  133. d.MessageVersion = MessageVersion.Default;
  134. h.ChannelDispatchers.Add (d);
  135. // It rejects "unrecognized type" of the channel listener.
  136. // Test6 uses IChannelListener<IReplyChannel> and works.
  137. d.Open ();
  138. // should not reach here, but in case it didn't, it must be closed.
  139. d.Close (TimeSpan.FromSeconds (10));
  140. }
  141. [Test]
  142. public void EndpointDispatcherAddTest6 ()
  143. {
  144. var uri = new Uri ("http://localhost:37564");
  145. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  146. var binding = new BasicHttpBinding ();
  147. var listener = new MyChannelListener<IReplyChannel> (uri);
  148. MyChannelDispatcher d = new MyChannelDispatcher (listener);
  149. var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
  150. d.Endpoints.Add (ed);
  151. ed.DispatchRuntime.Type = typeof (TestContract);
  152. d.MessageVersion = MessageVersion.Default;
  153. h.ChannelDispatchers.Add (d);
  154. d.Open (); // At this state, it does *not* call AcceptChannel() yet.
  155. Assert.IsFalse (listener.AcceptChannelTried, "#1");
  156. Assert.IsFalse (listener.WaitForChannelTried, "#2");
  157. Assert.IsNotNull (ed.DispatchRuntime, "#3");
  158. Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#4");
  159. Assert.IsNull (ed.DispatchRuntime.InstanceContextProvider, "#5"); // it is not set after ChannelDispatcher.Open().
  160. Assert.IsNull (ed.DispatchRuntime.SingletonInstanceContext, "#6");
  161. d.Close (); // we don't have to even close it.
  162. }
  163. [Test]
  164. [ExpectedException (typeof (InvalidOperationException))]
  165. public void EndpointDispatcherAddTest7 ()
  166. {
  167. var uri = new Uri ("http://localhost:37564");
  168. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  169. var binding = new BasicHttpBinding ();
  170. var listener = new MyChannelListener<IReplyChannel> (uri);
  171. MyChannelDispatcher d = new MyChannelDispatcher (listener);
  172. var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
  173. d.Endpoints.Add (ed);
  174. ed.DispatchRuntime.Type = typeof (TestContract);
  175. d.MessageVersion = MessageVersion.Default;
  176. // add service endpoint to open the host (unlike all tests above).
  177. h.AddServiceEndpoint (typeof (TestContract),
  178. new BasicHttpBinding (), uri.ToString ());
  179. h.ChannelDispatchers.Clear ();
  180. h.ChannelDispatchers.Add (d);
  181. d.Open (); // At this state, it does *not* call AcceptChannel() yet.
  182. // This rejects already-opened ChannelDispatcher.
  183. h.Open (TimeSpan.FromSeconds (10));
  184. // should not reach here, but in case it didn't, it must be closed.
  185. h.Close (TimeSpan.FromSeconds (10));
  186. }
  187. [Test]
  188. public void EndpointDispatcherAddTest8 ()
  189. {
  190. var uri = new Uri ("http://localhost:37564");
  191. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  192. var binding = new BasicHttpBinding ();
  193. var listener = new MyChannelListener<IReplyChannel> (uri);
  194. MyChannelDispatcher d = new MyChannelDispatcher (listener);
  195. var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
  196. d.Endpoints.Add (ed);
  197. ed.DispatchRuntime.Type = typeof (TestContract);
  198. d.MessageVersion = MessageVersion.Default;
  199. // add service endpoint to open the host (unlike all tests above).
  200. h.AddServiceEndpoint (typeof (TestContract),
  201. new BasicHttpBinding (), uri.ToString ());
  202. h.ChannelDispatchers.Clear ();
  203. h.ChannelDispatchers.Add (d);
  204. Assert.AreEqual (h, d.Host, "#0");
  205. try {
  206. h.Open (TimeSpan.FromSeconds (10));
  207. Assert.IsTrue (listener.AcceptChannelTried, "#1"); // while it throws NIE ...
  208. Assert.IsFalse (listener.WaitForChannelTried, "#2");
  209. Assert.IsNotNull (ed.DispatchRuntime, "#3");
  210. Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#4");
  211. Assert.IsNotNull (ed.DispatchRuntime.InstanceContextProvider, "#5"); // it was set after ServiceHost.Open().
  212. Assert.IsNull (ed.DispatchRuntime.SingletonInstanceContext, "#6");
  213. /*
  214. var l = new HttpListener ();
  215. l.Prefixes.Add (uri.ToString ());
  216. l.Start ();
  217. l.Stop ();
  218. */
  219. } finally {
  220. h.Close ();
  221. }
  222. }
  223. [ServiceContract]
  224. public class TestContract
  225. {
  226. [OperationContract]
  227. public void Process (string input) {
  228. }
  229. }
  230. class MyChannelDispatcher : ChannelDispatcher
  231. {
  232. public bool Attached = false;
  233. public MyChannelDispatcher (IChannelListener l) : base (l) { }
  234. protected override void Attach (ServiceHostBase host) {
  235. base.Attach (host);
  236. Attached = true;
  237. }
  238. protected override void Detach (ServiceHostBase host) {
  239. base.Detach (host);
  240. Attached = false;
  241. }
  242. }
  243. class MyChannelListener<TChannel> : MyChannelListener, IChannelListener<TChannel> where TChannel : class, IChannel
  244. {
  245. public MyChannelListener (Uri uri)
  246. : base (uri)
  247. {
  248. }
  249. public bool AcceptChannelTried { get; set; }
  250. public TChannel AcceptChannel ()
  251. {
  252. AcceptChannelTried = true;
  253. throw new NotImplementedException ();
  254. }
  255. public TChannel AcceptChannel (TimeSpan timeout)
  256. {
  257. AcceptChannelTried = true;
  258. throw new NotImplementedException ();
  259. }
  260. public IAsyncResult BeginAcceptChannel (AsyncCallback callback, object state)
  261. {
  262. AcceptChannelTried = true;
  263. throw new NotImplementedException ();
  264. }
  265. public IAsyncResult BeginAcceptChannel (TimeSpan timeout, AsyncCallback callback, object state)
  266. {
  267. AcceptChannelTried = true;
  268. throw new NotImplementedException ();
  269. }
  270. public TChannel EndAcceptChannel (IAsyncResult result)
  271. {
  272. throw new NotImplementedException ();
  273. }
  274. }
  275. class MyChannelListener : IChannelListener
  276. {
  277. public MyChannelListener (Uri uri)
  278. {
  279. Uri = uri;
  280. }
  281. public bool WaitForChannelTried { get; set; }
  282. public CommunicationState State { get; set; }
  283. #region IChannelListener Members
  284. public IAsyncResult BeginWaitForChannel (TimeSpan timeout, AsyncCallback callback, object state)
  285. {
  286. WaitForChannelTried = true;
  287. throw new NotImplementedException ();
  288. }
  289. public bool EndWaitForChannel (IAsyncResult result)
  290. {
  291. throw new NotImplementedException ();
  292. }
  293. public T GetProperty<T> () where T : class
  294. {
  295. throw new NotImplementedException ();
  296. }
  297. public Uri Uri { get; set; }
  298. public bool WaitForChannel (TimeSpan timeout)
  299. {
  300. WaitForChannelTried = true;
  301. throw new NotImplementedException ();
  302. }
  303. #endregion
  304. #region ICommunicationObject Members
  305. public void Abort ()
  306. {
  307. State = CommunicationState.Closed;
  308. }
  309. public IAsyncResult BeginClose (TimeSpan timeout, AsyncCallback callback, object state) {
  310. throw new NotImplementedException ();
  311. }
  312. public IAsyncResult BeginClose (AsyncCallback callback, object state) {
  313. throw new NotImplementedException ();
  314. }
  315. public IAsyncResult BeginOpen (TimeSpan timeout, AsyncCallback callback, object state) {
  316. throw new NotImplementedException ();
  317. }
  318. public IAsyncResult BeginOpen (AsyncCallback callback, object state) {
  319. throw new NotImplementedException ();
  320. }
  321. public void Close (TimeSpan timeout)
  322. {
  323. State = CommunicationState.Closed;
  324. }
  325. public void Close ()
  326. {
  327. State = CommunicationState.Closed;
  328. }
  329. public event EventHandler Closed;
  330. public event EventHandler Closing;
  331. public void EndClose (IAsyncResult result) {
  332. throw new NotImplementedException ();
  333. }
  334. public void EndOpen (IAsyncResult result) {
  335. throw new NotImplementedException ();
  336. }
  337. public event EventHandler Faulted;
  338. public void Open (TimeSpan timeout)
  339. {
  340. State = CommunicationState.Opened;
  341. }
  342. public void Open ()
  343. {
  344. State = CommunicationState.Opened;
  345. }
  346. public event EventHandler Opened;
  347. public event EventHandler Opening;
  348. #endregion
  349. }
  350. }
  351. }