PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.ServiceModel/Test/System.ServiceModel.Dispatcher/ChannelDispatcherTest.cs

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