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

/Main/src/Tests/RockBus.Samples.5DatabaseTransport.Dapper.SqlServer/Sample5DatabaseTransportTests.cs

#
C# | 259 lines | 207 code | 42 blank | 10 comment | 50 complexity | 321ce24a0d9d5b6e8bb4d0c78abedee0 MD5 | raw file
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using RockBus.Clients.Interfaces;
  3. using RockBus.Clients.LoanProcessor;
  4. using RockBus.Composition;
  5. using RockBus.Composition.Interfaces;
  6. using RockBus.Initialization.Interfaces;
  7. using RockBus.Logging.Trace;
  8. using RockBus.Routing.Interfaces;
  9. using RockBus.ServiceBus;
  10. using RockBus.ServiceModel;
  11. using RockBus.TestUtils;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.ServiceModel;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. namespace RockBus.Samples._5DatabaseTransport.Dapper.SqlServer
  19. {
  20. [TestClass]
  21. public class Sample5DatabaseTransportTests
  22. {
  23. private static ServiceHost _cbrServiceHost;
  24. private static ServiceHost _loanProcessorServiceHost;
  25. private static ServiceBusEnvironment _sbe;
  26. static Sample5DatabaseTransportTests()
  27. {
  28. // Make sure dependencies are copied to test location
  29. var loggerType = typeof(TraceLogger);
  30. var dmType = typeof(DependencyManagerBase);
  31. }
  32. [TestInitialize()]
  33. public void TestInitialize()
  34. {
  35. var initializationManager = DependencyManager.Instance.Resolve<IInitializationManager>();
  36. initializationManager.Initialize();
  37. _cbrServiceHost = ServiceHostUtils.OpenRockBusServiceHost(new TraceLogger());
  38. _loanProcessorServiceHost = ServiceHostUtils.OpenHost<LoanProcessorService>(new Uri[] { new Uri("net.pipe://localhost/LoanProcessorService/"), new Uri("http://localhost:9997/LoanProcessorService/") });
  39. _sbe = new ServiceBusEnvironment(new TraceLogger());
  40. _sbe.ConfigureSubscribersAsync().Wait();
  41. }
  42. [TestCleanup()]
  43. public void TestCleanup()
  44. {
  45. _sbe.UnconfigureSubscribersAsync().Wait();
  46. List<ServiceHost> hosts = new List<ServiceHost>(2)
  47. {
  48. _cbrServiceHost,
  49. _loanProcessorServiceHost,
  50. };
  51. var closingTasks = new List<TaskCompletionSource<bool>>();
  52. foreach (ServiceHost s in hosts)
  53. {
  54. var closingTask = new TaskCompletionSource<bool>();
  55. closingTasks.Add(closingTask);
  56. s.Closed += delegate(Object o, System.EventArgs n)
  57. {
  58. closingTask.TrySetResult(true);
  59. };
  60. s.Close();
  61. }
  62. Task[] taskArray = closingTasks.Select((tcs) => tcs.Task).ToArray();
  63. Task.WaitAll(taskArray, 2000);
  64. }
  65. [TestMethod]
  66. [TestCategory("Samples")]
  67. [DeploymentItem("RockBus.Composition.dll")]
  68. [DeploymentItem("RockBus.Logging.Trace.dll")]
  69. public void TestNewInterestRateEvent()
  70. {
  71. IPublisherManager pm = DependencyManager.Instance.Resolve<IPublisherManager>();
  72. pm.EnableAll();
  73. // We have to wait here in order for the subscriptions to become active
  74. // See <internalConfiguration reinitializationIntervalMs="2000" reinitLockTimeoutMs="5000" />
  75. Thread.Sleep(3000);
  76. LoanProcessorService.Reset();
  77. string endpointName = "CBR_BasicHttpEndpointAsync";
  78. ClientBaseArgs args = new ClientBaseArgs(endpointName);
  79. ILoanProcessorAsync client = new LoanProcessorClientAsync(args);
  80. int id = 9996;
  81. int i = 0;
  82. NewInterestRateEvent newInterestRateEvent0 = new NewInterestRateEvent() { ID = 9996, SomeValue = "NewInterestRateEvent" + id + "_" + i };
  83. IAsyncResult asyncResult = client.BeginProcessNewInterestRateEvent(newInterestRateEvent0, ProcessNewInterestRateEventCallback, client);
  84. bool callbackCalled = asyncResult.AsyncWaitHandle.WaitOne(60000);
  85. Assert.IsTrue(callbackCalled);
  86. for (i = 0; i < 300; i++)
  87. {
  88. if (LoanProcessorService.CallStatsSet.Count >= 2)
  89. {
  90. break;
  91. }
  92. Thread.Sleep(100);
  93. }
  94. Assert.AreEqual<long>(2, LoanProcessorService.CallStatsSet.Count);
  95. Assert.AreEqual<int>(1, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9996 && cs.Count == 1 && cs.Address.StartsWith("net.pipe://")));
  96. Assert.AreEqual<int>(1, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9996 && cs.Count == 1 && cs.Address.StartsWith("http://")));
  97. }
  98. [TestMethod]
  99. [TestCategory("Samples")]
  100. [DeploymentItem("RockBus.Composition.dll")]
  101. [DeploymentItem("RockBus.Logging.Trace.dll")]
  102. public void TestLoanApplicationEvent()
  103. {
  104. var pm = DependencyManager.Instance.Resolve<IPublisherManager>();
  105. pm.EnableAll();
  106. // We have to wait here in order for the subscriptions to become active
  107. // See <internalConfiguration reinitializationIntervalMs="2000" reinitLockTimeoutMs="5000" />
  108. Thread.Sleep(3000);
  109. LoanProcessorService.Reset();
  110. string endpointName = "CBR_BasicHttpEndpointAsync";
  111. ClientBaseArgs args = new ClientBaseArgs(endpointName);
  112. ILoanProcessorAsync client = new LoanProcessorClientAsync(args);
  113. int id = 9996;
  114. int i = 0;
  115. LoanApplicationEvent loanApplicationEvent = new LoanApplicationEvent() { ID = id, SomeValue = "LoanApplicationEvent" + id + "_" + i };
  116. IAsyncResult asyncResult = client.BeginProcessLoanApplicationEvent(loanApplicationEvent, ProcessLoanApplicationEventCallback, client);
  117. bool callbackCalled = asyncResult.AsyncWaitHandle.WaitOne(60000);
  118. Assert.IsTrue(callbackCalled);
  119. for (i = 0; i < 600; i++)
  120. {
  121. if (LoanProcessorService.CallStatsSet.Count >= 1)
  122. {
  123. break;
  124. }
  125. Thread.Sleep(100);
  126. }
  127. Assert.AreEqual<long>(1, LoanProcessorService.CallStatsSet.Count);
  128. Assert.AreEqual<int>(1, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9996 && cs.Count == 1 && cs.Address.StartsWith("net.pipe://")));
  129. Assert.AreEqual<int>(0, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9997 && cs.Count == 1 && cs.Address.StartsWith("http://")));
  130. id = 9997;
  131. i = 0;
  132. loanApplicationEvent = new LoanApplicationEvent() { ID = id, SomeValue = "LoanApplicationEvent" + id + "_" + i };
  133. asyncResult = client.BeginProcessLoanApplicationEvent(loanApplicationEvent, ProcessLoanApplicationEventCallback, client);
  134. callbackCalled = asyncResult.AsyncWaitHandle.WaitOne(5000);
  135. Assert.IsTrue(callbackCalled);
  136. for (i = 0; i < 600; i++)
  137. {
  138. if (LoanProcessorService.CallStatsSet.Count >= 2)
  139. {
  140. break;
  141. }
  142. Thread.Sleep(100);
  143. }
  144. Assert.AreEqual<long>(2, LoanProcessorService.CallStatsSet.Count);
  145. Assert.AreEqual<int>(1, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9996 && cs.Count == 1 && cs.Address.StartsWith("net.pipe://")));
  146. Assert.AreEqual<int>(1, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9997 && cs.Count == 1 && cs.Address.StartsWith("http://")));
  147. }
  148. [TestMethod]
  149. [TestCategory("Samples")]
  150. [DeploymentItem("RockBus.Composition.dll")]
  151. [DeploymentItem("RockBus.Logging.Trace.dll")]
  152. public void TestLoanApplicationStatusRequest()
  153. {
  154. var pm = DependencyManager.Instance.Resolve<IPublisherManager>();
  155. pm.EnableAll();
  156. // We have to wait here in order for the subscriptions to become active
  157. // See <internalConfiguration reinitializationIntervalMs="2000" reinitLockTimeoutMs="5000" />
  158. Thread.Sleep(3000);
  159. LoanProcessorService.Reset();
  160. string endpointName = "CBR_BasicHttpEndpointAsync";
  161. ClientBaseArgs args = new ClientBaseArgs(endpointName);
  162. ILoanProcessorAsync client = new LoanProcessorClientAsync(args);
  163. int id = 9996;
  164. int i = 0;
  165. LoanApplicationStatusRequest request = new LoanApplicationStatusRequest() { ID = id, SomeValue = "LoanApplicationStatusRequest" + id + "_" + i };
  166. IAsyncResult asyncResult = client.BeginProcessLoanApplicationStatusRequest(request, ProcessLoanApplicationStatusRequestCallback, client);
  167. bool callbackCalled = asyncResult.AsyncWaitHandle.WaitOne(60000);
  168. Assert.IsTrue(callbackCalled);
  169. for (i = 0; i < 600; i++)
  170. {
  171. if (LoanProcessorService.CallStatsSet.Count >= 1)
  172. {
  173. break;
  174. }
  175. Thread.Sleep(100);
  176. }
  177. // Due to the way the RoundRobinIndex works, the 2nd one in the list is the first one to receive the message.
  178. Assert.AreEqual<long>(1, LoanProcessorService.CallStatsSet.Count);
  179. Assert.AreEqual<int>(1, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9996 && cs.Count == 1 && cs.Address.StartsWith("net.pipe://")));
  180. Assert.AreEqual<int>(0, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9997 && cs.Count == 1 && cs.Address.StartsWith("net.pipe://")));
  181. //Assert.AreEqual<int>(0, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9997 && cs.Count == 1 && cs.Address.StartsWith("http://")));
  182. id = 9997;
  183. i = 0;
  184. request = new LoanApplicationStatusRequest() { ID = id, SomeValue = "LoanApplicationStatusRequest" + id + "_" + i };
  185. asyncResult = client.BeginProcessLoanApplicationStatusRequest(request, ProcessLoanApplicationStatusRequestCallback, client);
  186. callbackCalled = asyncResult.AsyncWaitHandle.WaitOne(60000);
  187. Assert.IsTrue(callbackCalled);
  188. for (i = 0; i < 600; i++)
  189. {
  190. if (LoanProcessorService.CallStatsSet.Count >= 2)
  191. {
  192. break;
  193. }
  194. Thread.Sleep(100);
  195. }
  196. Assert.AreEqual<long>(2, LoanProcessorService.CallStatsSet.Count);
  197. Assert.AreEqual<int>(1, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9996 && cs.Count == 1 && cs.Address.StartsWith("net.pipe://")));
  198. Assert.AreEqual<int>(1, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9997 && cs.Count == 1 && cs.Address.StartsWith("net.pipe://")));
  199. //Assert.AreEqual<int>(0, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9997 && cs.Count == 1 && cs.Address.StartsWith("http://")));
  200. }
  201. private static void ProcessNewInterestRateEventCallback(IAsyncResult asyncResult)
  202. {
  203. ILoanProcessorAsync client = asyncResult.AsyncState as ILoanProcessorAsync;
  204. client.EndProcessNewInterestRateEvent(asyncResult);
  205. }
  206. private static void ProcessLoanApplicationEventCallback(IAsyncResult asyncResult)
  207. {
  208. ILoanProcessorAsync client = asyncResult.AsyncState as ILoanProcessorAsync;
  209. client.EndProcessLoanApplicationEvent(asyncResult);
  210. }
  211. private static void ProcessLoanApplicationStatusRequestCallback(IAsyncResult asyncResult)
  212. {
  213. ILoanProcessorAsync client = asyncResult.AsyncState as ILoanProcessorAsync;
  214. LoanApplicationStatusResponse loanApplicationStatusResponse = client.EndProcessLoanApplicationStatusRequest(asyncResult);
  215. }
  216. }
  217. }