PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/src/Tests/RockBus.Samples.3DynamicPublishSubscribe/Sample3DynamicPublishSubscribeTests.cs

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