ChannelDispatcherTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using NUnit.Framework;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Channels;
  10. using System.ServiceModel.Description;
  11. using System.ServiceModel.Dispatcher;
  12. namespace MonoTests.System.ServiceModel.Dispatcher
  13. {
  14. [TestFixture]
  15. public class ChannelDispatcherTest
  16. {
  17. Uri CreateAvailableUri (string uriString)
  18. {
  19. var uri = new Uri (uriString);
  20. try {
  21. var t = new TcpListener (uri.Port);
  22. t.Start ();
  23. t.Stop ();
  24. } catch (Exception ex) {
  25. Assert.Fail ("Port {0} is not open. It is likely previous tests have failed and the port is kept opened");
  26. }
  27. return uri;
  28. }
  29. [Test]
  30. public void ConstructorNullBindingName ()
  31. {
  32. new ChannelDispatcher (new MyChannelListener (new Uri ("urn:foo")), null);
  33. new ChannelDispatcher (new MyChannelListener (new Uri ("urn:foo")), null, null);
  34. }
  35. [Test]
  36. public void ServiceThrottle ()
  37. {
  38. var cd = new ChannelDispatcher (new MyChannelListener<IReplyChannel> (new Uri ("urn:foo")));
  39. var st = cd.ServiceThrottle;
  40. Assert.IsNull (st, "#0");
  41. var uri = CreateAvailableUri ("http://localhost:37564");
  42. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  43. h.AddServiceEndpoint (typeof (TestContract).FullName, new BasicHttpBinding (), "address");
  44. h.ChannelDispatchers.Add (cd);
  45. Assert.IsNull (st, "#1");
  46. var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
  47. ed.DispatchRuntime.Type = typeof (TestContract);
  48. cd.Endpoints.Add (ed);
  49. cd.MessageVersion = MessageVersion.Default;
  50. {
  51. cd.Open (TimeSpan.FromSeconds (10));
  52. try {
  53. Assert.IsNull (st, "#2");
  54. // so, can't really test actual slot values as it is null.
  55. } finally {
  56. cd.Close (TimeSpan.FromSeconds (10));
  57. }
  58. return;
  59. }
  60. }
  61. [Test]
  62. public void Collection_Add_Remove () {
  63. Console.WriteLine ("STart test Collection_Add_Remove");
  64. var uri = CreateAvailableUri ("http://localhost:37564");
  65. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  66. h.AddServiceEndpoint (typeof (TestContract).FullName, new BasicHttpBinding (), "address");
  67. MyChannelDispatcher d = new MyChannelDispatcher (new MyChannelListener (uri));
  68. h.ChannelDispatchers.Add (d);
  69. Assert.IsTrue (d.Attached, "#1");
  70. h.ChannelDispatchers.Remove (d);
  71. Assert.IsFalse (d.Attached, "#2");
  72. h.ChannelDispatchers.Insert (0, d);
  73. Assert.IsTrue (d.Attached, "#3");
  74. h.ChannelDispatchers.Add (new MyChannelDispatcher (new MyChannelListener (uri)));
  75. h.ChannelDispatchers.Clear ();
  76. Assert.IsFalse (d.Attached, "#4");
  77. }
  78. [Test]
  79. public void EndpointDispatcherAddTest ()
  80. {
  81. var uri = CreateAvailableUri ("http://localhost:37564");
  82. MyChannelDispatcher d = new MyChannelDispatcher (new MyChannelListener (uri));
  83. d.Endpoints.Add (new EndpointDispatcher (new EndpointAddress (uri), "", ""));
  84. }
  85. [Test]
  86. [ExpectedException (typeof (InvalidOperationException))]
  87. public void EndpointDispatcherAddTest2 () {
  88. var uri = CreateAvailableUri ("http://localhost:37564");
  89. MyChannelDispatcher d = new MyChannelDispatcher (new MyChannelListener (uri));
  90. d.Endpoints.Add (new EndpointDispatcher (new EndpointAddress (uri), "", ""));
  91. d.Open (); // the dispatcher must be attached.
  92. }
  93. [Test]
  94. [ExpectedException (typeof (InvalidOperationException))]
  95. public void EndpointDispatcherAddTest3 ()
  96. {
  97. var uri = CreateAvailableUri ("http://localhost:37564");
  98. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  99. MyChannelDispatcher d = new MyChannelDispatcher (new MyChannelListener (uri));
  100. d.Endpoints.Add (new EndpointDispatcher (new EndpointAddress (uri), "", ""));
  101. h.ChannelDispatchers.Add (d);
  102. d.Open (); // missing MessageVersion
  103. }
  104. [Test]
  105. [ExpectedException (typeof (InvalidOperationException))] // i.e. it is thrown synchronously in current thread.
  106. public void EndpointDispatcherAddTest4 ()
  107. {
  108. var uri = CreateAvailableUri ("http://localhost:37564");
  109. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  110. var listener = new MyChannelListener (uri);
  111. MyChannelDispatcher d = new MyChannelDispatcher (listener);
  112. var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
  113. Assert.IsNotNull (ed.DispatchRuntime, "#1");
  114. Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#2");
  115. Assert.IsNull (ed.DispatchRuntime.InstanceContextProvider, "#3");
  116. Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#3.2");
  117. Assert.IsNull (ed.DispatchRuntime.SingletonInstanceContext, "#4");
  118. d.Endpoints.Add (ed);
  119. d.MessageVersion = MessageVersion.Default;
  120. h.ChannelDispatchers.Add (d);
  121. // it misses DispatchRuntime.Type, which seems set
  122. // automatically when the dispatcher is created in
  123. // ordinal process but need to be set manually in this case.
  124. try {
  125. d.Open ();
  126. try {
  127. // should not reach here, but in case it didn't, it must be closed.
  128. d.Close (TimeSpan.FromSeconds (10));
  129. } catch {
  130. }
  131. } finally {
  132. Assert.AreEqual (CommunicationState.Opened, listener.State, "#5");
  133. }
  134. }
  135. [Test]
  136. [ExpectedException (typeof (InvalidOperationException))] // i.e. it is thrown synchronously in current thread.
  137. public void EndpointDispatcherAddTest5 ()
  138. {
  139. var uri = CreateAvailableUri ("http://localhost:37564");
  140. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  141. var binding = new BasicHttpBinding ();
  142. var listener = new MyChannelListener (uri);
  143. MyChannelDispatcher d = new MyChannelDispatcher (listener);
  144. var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
  145. d.Endpoints.Add (ed);
  146. ed.DispatchRuntime.Type = typeof (TestContract); // different from Test4
  147. d.MessageVersion = MessageVersion.Default;
  148. h.ChannelDispatchers.Add (d);
  149. // It rejects "unrecognized type" of the channel listener.
  150. // Test6 uses IChannelListener<IReplyChannel> and works.
  151. d.Open ();
  152. // should not reach here, but in case it didn't, it must be closed.
  153. d.Close (TimeSpan.FromSeconds (10));
  154. }
  155. [Test]
  156. public void EndpointDispatcherAddTest6 ()
  157. {
  158. var uri = CreateAvailableUri ("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. Assert.IsFalse (d.Attached, "#x1");
  166. ed.DispatchRuntime.Type = typeof (TestContract);
  167. d.MessageVersion = MessageVersion.Default;
  168. h.ChannelDispatchers.Add (d);
  169. Assert.IsTrue (d.Attached, "#x2");
  170. d.Open (); // At this state, it does *not* call AcceptChannel() yet.
  171. Assert.IsFalse (listener.AcceptChannelTried, "#1");
  172. Assert.IsFalse (listener.WaitForChannelTried, "#2");
  173. Assert.IsNotNull (ed.DispatchRuntime, "#3");
  174. Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#4");
  175. Assert.IsNull (ed.DispatchRuntime.InstanceContextProvider, "#5"); // it is not still set after ChannelDispatcher.Open().
  176. Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#5.2");
  177. Assert.IsNull (ed.DispatchRuntime.SingletonInstanceContext, "#6");
  178. d.Close (); // we don't have to even close it.
  179. }
  180. [Test]
  181. [ExpectedException (typeof (InvalidOperationException))]
  182. public void EndpointDispatcherAddTest7 ()
  183. {
  184. var uri = CreateAvailableUri ("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. d.Open (); // At this state, it does *not* call AcceptChannel() yet.
  199. // This rejects already-opened ChannelDispatcher.
  200. h.Open (TimeSpan.FromSeconds (10));
  201. // should not reach here, but in case it didn't, it must be closed.
  202. h.Close (TimeSpan.FromSeconds (10));
  203. }
  204. [Test]
  205. public void EndpointDispatcherAddTest8 ()
  206. {
  207. var uri = CreateAvailableUri ("http://localhost:37564");
  208. ServiceHost h = new ServiceHost (typeof (TestContract), uri);
  209. var listener = new MyChannelListener<IReplyChannel> (uri);
  210. MyChannelDispatcher d = new MyChannelDispatcher (listener);
  211. var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
  212. d.Endpoints.Add (ed);
  213. ed.DispatchRuntime.Type = typeof (TestContract);
  214. d.MessageVersion = MessageVersion.Default;
  215. // add service endpoint to open the host (unlike all tests above).
  216. h.AddServiceEndpoint (typeof (TestContract),
  217. new BasicHttpBinding (), uri.ToString ());
  218. h.ChannelDispatchers.Clear ();
  219. h.ChannelDispatchers.Add (d);
  220. Assert.AreEqual (h, d.Host, "#0");
  221. try {
  222. h.Open (TimeSpan.FromSeconds (10));
  223. Assert.IsTrue (listener.AcceptChannelTried, "#1"); // while it throws NIE ...
  224. Assert.IsFalse (listener.WaitForChannelTried, "#2");
  225. Assert.IsNotNull (ed.DispatchRuntime, "#3");
  226. Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#4");
  227. Assert.IsNotNull (ed.DispatchRuntime.InstanceContextProvider, "#5"); // it was set after ServiceHost.Open().
  228. Assert.IsNull (ed.DispatchRuntime.SingletonInstanceContext, "#6");
  229. /*
  230. var l = new HttpListener ();
  231. l.Prefixes.Add (uri.ToString ());
  232. l.Start ();
  233. l.Stop ();
  234. */
  235. } finally {
  236. h.Close (TimeSpan.FromSeconds (10));
  237. h.Abort ();
  238. }
  239. }
  240. // FIXME: this test itself indeed passes, but some weird conflict that blocks correspoding port happens between this and somewhere (probably above)
  241. // [Test]
  242. public void EndpointDispatcherAddTest9 () // test singleton service
  243. {
  244. var uri = CreateAvailableUri ("http://localhost:37564");
  245. ServiceHost h = new ServiceHost (new TestContract (), uri);
  246. h.Description.Behaviors.Find<ServiceBehaviorAttribute> ().InstanceContextMode = InstanceContextMode.Single;
  247. var listener = new MyChannelListener<IReplyChannel> (uri);
  248. MyChannelDispatcher d = new MyChannelDispatcher (listener);
  249. var ed = new EndpointDispatcher (new EndpointAddress (uri), "", "");
  250. d.Endpoints.Add (ed);
  251. ed.DispatchRuntime.Type = typeof (TestContract);
  252. d.MessageVersion = MessageVersion.Default;
  253. h.AddServiceEndpoint (typeof (TestContract), new BasicHttpBinding (), uri.ToString ());
  254. h.ChannelDispatchers.Clear ();
  255. Assert.IsNull (ed.DispatchRuntime.SingletonInstanceContext, "#1");
  256. h.ChannelDispatchers.Add (d);
  257. Assert.IsNull (ed.DispatchRuntime.SingletonInstanceContext, "#2");
  258. try {
  259. h.Open (TimeSpan.FromSeconds (10));
  260. Assert.IsNull (ed.DispatchRuntime.InstanceProvider, "#4");
  261. Assert.IsNotNull (ed.DispatchRuntime.InstanceContextProvider, "#5"); // it was set after ServiceHost.Open().
  262. Assert.IsNotNull (ed.DispatchRuntime.SingletonInstanceContext, "#6");
  263. } finally {
  264. h.Close (TimeSpan.FromSeconds (10));
  265. h.Abort ();
  266. }
  267. }
  268. [ServiceContract]
  269. public class TestContract
  270. {
  271. [OperationContract]
  272. public void Process (string input) {
  273. }
  274. }
  275. class MyChannelDispatcher : ChannelDispatcher
  276. {
  277. public bool Attached = false;
  278. public MyChannelDispatcher (IChannelListener l) : base (l) { }
  279. protected override void Attach (ServiceHostBase host) {
  280. base.Attach (host);
  281. Attached = true;
  282. }
  283. protected override void Detach (ServiceHostBase host) {
  284. base.Detach (host);
  285. Attached = false;
  286. }
  287. }
  288. class MyChannelListener<TChannel> : MyChannelListener, IChannelListener<TChannel> where TChannel : class, IChannel
  289. {
  290. public MyChannelListener (Uri uri)
  291. : base (uri)
  292. {
  293. }
  294. public bool AcceptChannelTried { get; set; }
  295. public TChannel AcceptChannel ()
  296. {
  297. AcceptChannelTried = true;
  298. throw new NotImplementedException ();
  299. }
  300. public TChannel AcceptChannel (TimeSpan timeout)
  301. {
  302. AcceptChannelTried = true;
  303. throw new NotImplementedException ();
  304. }
  305. public IAsyncResult BeginAcceptChannel (AsyncCallback callback, object state)
  306. {
  307. AcceptChannelTried = true;
  308. throw new NotImplementedException ();
  309. }
  310. public IAsyncResult BeginAcceptChannel (TimeSpan timeout, AsyncCallback callback, object state)
  311. {
  312. AcceptChannelTried = true;
  313. throw new NotImplementedException ();
  314. }
  315. public TChannel EndAcceptChannel (IAsyncResult result)
  316. {
  317. throw new NotImplementedException ();
  318. }
  319. }
  320. class MyChannelListener : IChannelListener
  321. {
  322. public MyChannelListener (Uri uri)
  323. {
  324. Uri = uri;
  325. }
  326. public bool WaitForChannelTried { get; set; }
  327. public CommunicationState State { get; set; }
  328. #region IChannelListener Members
  329. public IAsyncResult BeginWaitForChannel (TimeSpan timeout, AsyncCallback callback, object state)
  330. {
  331. WaitForChannelTried = true;
  332. throw new NotImplementedException ();
  333. }
  334. public bool EndWaitForChannel (IAsyncResult result)
  335. {
  336. throw new NotImplementedException ();
  337. }
  338. public T GetProperty<T> () where T : class
  339. {
  340. throw new NotImplementedException ();
  341. }
  342. public Uri Uri { get; set; }
  343. public bool WaitForChannel (TimeSpan timeout)
  344. {
  345. WaitForChannelTried = true;
  346. throw new NotImplementedException ();
  347. }
  348. #endregion
  349. #region ICommunicationObject Members
  350. public void Abort ()
  351. {
  352. State = CommunicationState.Closed;
  353. }
  354. public IAsyncResult BeginClose (TimeSpan timeout, AsyncCallback callback, object state) {
  355. throw new NotImplementedException ();
  356. }
  357. public IAsyncResult BeginClose (AsyncCallback callback, object state) {
  358. throw new NotImplementedException ();
  359. }
  360. public IAsyncResult BeginOpen (TimeSpan timeout, AsyncCallback callback, object state) {
  361. throw new NotImplementedException ();
  362. }
  363. public IAsyncResult BeginOpen (AsyncCallback callback, object state) {
  364. throw new NotImplementedException ();
  365. }
  366. public void Close (TimeSpan timeout)
  367. {
  368. State = CommunicationState.Closed;
  369. }
  370. public void Close ()
  371. {
  372. State = CommunicationState.Closed;
  373. }
  374. public event EventHandler Closed;
  375. public event EventHandler Closing;
  376. public void EndClose (IAsyncResult result) {
  377. throw new NotImplementedException ();
  378. }
  379. public void EndOpen (IAsyncResult result) {
  380. throw new NotImplementedException ();
  381. }
  382. public event EventHandler Faulted;
  383. public void Open (TimeSpan timeout)
  384. {
  385. State = CommunicationState.Opened;
  386. }
  387. public void Open ()
  388. {
  389. State = CommunicationState.Opened;
  390. }
  391. public event EventHandler Opened;
  392. public event EventHandler Opening;
  393. #endregion
  394. }
  395. }
  396. }