Advertisement
Guest User

Untitled

a guest
Jun 16th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.67 KB | None | 0 0
  1. [Test]
  2. public void BasicBackplaneTest()
  3. {
  4. SubsciberTestServerNode nodeA = null;
  5. SubsciberTestServerNode nodeB = null;
  6.  
  7. string messageA = null;
  8. string messageB = null;
  9. try
  10. {
  11. Log("Given I have a WorkCenter Dispatch Publisher");
  12. var publisher = new WcDispatchPublisher(ConnectionString);
  13.  
  14. Log("And I have multiple server nodes subscribed to the backplane");
  15. nodeA = new SubsciberTestServerNode("nodeA").Start().Result;
  16. nodeB = new SubsciberTestServerNode("nodeB").Start().Result;
  17.  
  18. Log("And I wait 5 seconds");
  19. Thread.Sleep(5000);
  20.  
  21. Log("When I publish a message: {0}", TestPayload);
  22. publisher.Publish(TestPayload);
  23.  
  24. Log("And I wait 60 seconds");
  25. Thread.Sleep(TimeSpan.FromSeconds(60));
  26.  
  27. messageA = nodeA.Message;
  28. messageB = nodeB.Message;
  29. }
  30. catch (AggregateException exception)
  31. {
  32. Log("Exception Occurred: {0}", exception.Flatten().Message);
  33. Exception = exception;
  34. }
  35. catch (Exception exception)
  36. {
  37. Log("Exception Occurred: {0}", exception.Message);
  38. Exception = exception;
  39. }
  40. finally
  41. {
  42. nodeA?.Dispose();
  43. nodeB?.Dispose();
  44.  
  45. Log("Then no exceptions should have been thrown.");
  46. Exception.Should().BeNull();
  47.  
  48. Log("Then the message should have been added to the Message Queue");
  49. messageA.Should().NotBeNullOrWhiteSpace();
  50. messageB.Should().NotBeNullOrWhiteSpace();
  51. }
  52.  
  53. internal class SubsciberTestServerNode : IDisposable
  54. {
  55. private readonly string _nodeName;
  56. private readonly string _url;
  57. private WcDispatchSubscriber _subscriber;
  58. private IDisposable _webApp;
  59.  
  60. public SubsciberTestServerNode(string nodeName)
  61. {
  62. _nodeName = nodeName;
  63. _url = $"http://localhost:9999/{nodeName}";
  64. MessageList = new List<string>();
  65. }
  66.  
  67. public string Message { get; set; }
  68. public List<string> MessageList { get; set; }
  69.  
  70. public void Dispose()
  71. {
  72. if (_webApp != null)
  73. {
  74. _webApp.Dispose();
  75. _webApp = null;
  76. _subscriber.Dispose();
  77. _subscriber = null;
  78. }
  79. }
  80.  
  81. public async Task<SubsciberTestServerNode> Start()
  82. {
  83. _webApp = WebApp.Start(_url, app =>
  84. {
  85. new Startup(_nodeName).Configuration(app);
  86. Thread.Sleep(TimeSpan.FromSeconds(5));
  87. //Place this code into your Application_Start() method.
  88. var factory = new ConnectionFactory
  89. {
  90. UserName = "guest",
  91. Password = "guest",
  92. HostName = "localhost"
  93. };
  94.  
  95. var exchangeName = "WC_LeadDispatch_Exchange";
  96.  
  97. var configuration = new RabbitMqScaleoutConfiguration(factory, exchangeName);
  98. GlobalHost.DependencyResolver.UseRabbitMq(configuration);
  99. GlobalHost.Configuration.TransportConnectTimeout = TimeSpan.FromSeconds(10);
  100.  
  101.  
  102. Thread.Sleep(TimeSpan.FromSeconds(5));
  103. });
  104.  
  105. _subscriber = new WcDispatchSubscriber();
  106. await _subscriber.Subscribe(_url, msg =>
  107. {
  108. string message = $"Message received at Node: {_nodeName}. Message: {msg}.";
  109. Console.WriteLine(message);
  110. Message = message;
  111. MessageList.Add(message);
  112. });
  113. return this;
  114. }
  115. }
  116.  
  117. public class WcDispatchSubscriber : IDisposable
  118. {
  119. private const string HubName = "DispatchUpdateHub";
  120. private const string MessageEventName = "addMessage";
  121. private readonly int _connectionLimitInt;
  122. private IDisposable _hubProxySubscription;
  123.  
  124. public WcDispatchSubscriber()
  125. {
  126. string connectionLimit = ConfigurationManager.AppSettings.Get("SignalRConnectionLimit");
  127. int.TryParse(connectionLimit, out _connectionLimitInt);
  128. _connectionLimitInt = _connectionLimitInt == 0 ? 100 : _connectionLimitInt;
  129. }
  130.  
  131. public void Dispose()
  132. {
  133. _hubProxySubscription.Dispose();
  134. }
  135.  
  136. public async Task Subscribe(string hubConnectionString, Action<string> messageReceived)
  137. {
  138. var hubConnection = new HubConnection(hubConnectionString);
  139. IHubProxy dispatchHubProxy = hubConnection.CreateHubProxy(HubName);
  140. _hubProxySubscription = dispatchHubProxy.On(MessageEventName, messageReceived);
  141. ServicePointManager.DefaultConnectionLimit = _connectionLimitInt;
  142. await hubConnection.Start();
  143. }
  144. }
  145.  
  146. public class WcDispatchPublisher
  147. {
  148. private const string ExchangeName = "WC_LeadDispatch_Exchange";
  149. private readonly IHubContext _hubContext;
  150.  
  151. public WcDispatchPublisher(string connectionString)
  152. {
  153. //actual string will look like this. we may need to overload the other constructors in the Rabbit/SigR.
  154. //_rabbitConnectionString =
  155. // "host=cprmqsrvt02vn01:5672;publisherConfirms=true;username=unittest;password=Un1t735t;virtualhost=UnitTest-NotificationService";
  156. var configuration = new RabbitMqScaleoutConfiguration(connectionString, ExchangeName);
  157. GlobalHost.DependencyResolver.UseRabbitMq(configuration);
  158.  
  159. _hubContext = GlobalHost.ConnectionManager.GetHubContext<DispatchUpdateHub>();
  160. }
  161.  
  162. /// <summary>
  163. /// </summary>
  164. /// <param name="payload"></param>
  165. public void Publish(string payload)
  166. {
  167. Task.Factory.StartNew(() =>
  168. {
  169. _hubContext.Clients.All.addMessage(payload);
  170. }).Wait();
  171. }
  172. }
  173.  
  174. Given I have a WorkCenter Dispatch Publisher
  175. And I have multiple server nodes subscribed to the backplane
  176. And I wait 5 seconds
  177. When I publish a message: test-payload
  178. And I wait 60 seconds
  179. Message received at Node: nodeA. Message: test-payload.
  180. Message received at Node: nodeB. Message: test-payload.
  181. Message received at Node: nodeB. Message: {"LeadId":37252,"DispatchId":153595,"NotificationTypeId":1,"NotificationTitle":" Leads Dispatch","DispatchDateTime":"2016-06- 16T17:16:26.187","AlertMessage":"Lead number 37252 dispatched 6/16/2016 at 7:16 AM CST","DispatchedToFranchise":5576,"FranchiseOpsId":130}.
  182. Message received at Node: nodeB. Message: {"LeadId":37252,"DispatchId":153595,"NotificationTypeId":1,"NotificationTitle":" Leads Dispatch","DispatchDateTime":"2016-06- 16T17:16:26.187","AlertMessage":"Lead number 37252 dispatched 6/16/2016 at 7:16 AM CST","DispatchedToFranchise":5576,"FranchiseOpsId":130}.
  183. Message received at Node: nodeB. Message: {"LeadId":37252,"DispatchId":153595,"NotificationTypeId":1,"NotificationTitle":" Leads Dispatch","DispatchDateTime":"2016-06- 16T17:16:26.187","AlertMessage":"Lead number 37252 dispatched 6/16/2016 at 7:16 AM CST","DispatchedToFranchise":5576,"FranchiseOpsId":130}.
  184. Message received at Node: nodeB. Message: {"LeadId":37252,"DispatchId":153595,"NotificationTypeId":1,"NotificationTitle":" Leads Dispatch","DispatchDateTime":"2016-06- 16T17:16:26.187","AlertMessage":"Lead number 37252 dispatched 6/16/2016 at 7:16 AM CST","DispatchedToFranchise":5576,"FranchiseOpsId":130}.
  185. Message received at Node: nodeB. Message: {"LeadId":37252,"DispatchId":153595,"NotificationTypeId":1,"NotificationTitle":"Leads Dispatch","DispatchDateTime":"2016-06-16T17:16:26.187","AlertMessage":"Lead number 37252 dispatched 6/16/2016 at 7:16 AM CST","DispatchedToFranchise":5576,"FranchiseOpsId":130}.
  186. Message received at Node: nodeB. Message: {"LeadId":37252,"DispatchId":153595,"NotificationTypeId":1,"NotificationTitle":"Leads Dispatch","DispatchDateTime":"2016-06-16T17:16:26.187","AlertMessage":"Lead number 37252 dispatched 6/16/2016 at 7:16 AM CST","DispatchedToFranchise":5576,"FranchiseOpsId":130}.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement