BusTests.Core.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Abc.Zebus.Core;
  6. using Abc.Zebus.Directory;
  7. using Abc.Zebus.Routing;
  8. using Abc.Zebus.Testing;
  9. using Abc.Zebus.Testing.Comparison;
  10. using Abc.Zebus.Testing.Extensions;
  11. using Abc.Zebus.Testing.UnitTesting;
  12. using Abc.Zebus.Tests.Messages;
  13. using Abc.Zebus.Transport;
  14. using Moq;
  15. using NUnit.Framework;
  16. namespace Abc.Zebus.Tests.Core
  17. {
  18. public partial class BusTests
  19. {
  20. public class Core : BusTests
  21. {
  22. [Test]
  23. public void should_configure_transport_when_configured()
  24. {
  25. var transportMock = new Mock<ITransport>();
  26. var bus = new Bus(transportMock.Object, new Mock<IPeerDirectory>().Object, null, null, new DefaultMessageSendingStrategy(), new DefaultStoppingStrategy(), Mock.Of<IBindingKeyPredicateBuilder>(), _configuration.Object);
  27. bus.Configure(_self.Id, _environment);
  28. transportMock.Verify(trans => trans.Configure(It.Is<PeerId>(peerId => _self.Id.Equals(peerId)), _environment));
  29. }
  30. [Test]
  31. public void should_initialize_transport_and_register_on_directory()
  32. {
  33. AddInvoker<FakeCommand>(shouldBeSubscribedOnStartup: true);
  34. var sequence = new SetupSequence();
  35. _messageDispatcherMock.Setup(x => x.LoadMessageHandlerInvokers()).InSequence(sequence);
  36. _transport.Started += sequence.GetCallback();
  37. _directoryMock.Setup(x => x.RegisterAsync(_bus, It.Is<Peer>(p => p.DeepCompare(_self)), It.IsAny<IEnumerable<Subscription>>()))
  38. .InSequence(sequence)
  39. .Returns(Task.CompletedTask);
  40. _transport.Registered += sequence.GetCallback();
  41. _bus.Start();
  42. sequence.Verify();
  43. _transport.IsStarted.ShouldBeTrue();
  44. _transport.IsRegistered.ShouldBeTrue();
  45. }
  46. [Test]
  47. public void should_be_running_when_registering_on_directory()
  48. {
  49. var wasRunningDuringRegister = false;
  50. _directoryMock.Setup(x => x.RegisterAsync(_bus, It.IsAny<Peer>(), It.IsAny<IEnumerable<Subscription>>()))
  51. .Callback(() => wasRunningDuringRegister = _bus.IsRunning)
  52. .Returns(Task.CompletedTask);
  53. _bus.Start();
  54. wasRunningDuringRegister.ShouldBeTrue();
  55. }
  56. [Test]
  57. public void should_not_be_running_if_registration_failed()
  58. {
  59. try
  60. {
  61. _directoryMock.Setup(x => x.RegisterAsync(_bus, It.IsAny<Peer>(), It.IsAny<IEnumerable<Subscription>>()))
  62. .Returns(Task.FromException(new TimeoutException()));
  63. _bus.Start();
  64. }
  65. catch (AggregateException ex) when (ex.InnerException is TimeoutException)
  66. {
  67. _bus.IsRunning.ShouldBeFalse();
  68. }
  69. }
  70. [Test]
  71. public void should_stop_transport_and_unregister_from_directory()
  72. {
  73. var sequence = new SetupSequence();
  74. _directoryMock.Setup(x => x.UnregisterAsync(_bus)).Returns(Task.CompletedTask);
  75. _bus.Start();
  76. _bus.Stop();
  77. sequence.Verify();
  78. _transport.IsStopped.ShouldBeTrue();
  79. }
  80. [Test]
  81. public void should_stop_message_dispatcher()
  82. {
  83. _bus.Start();
  84. _bus.Stop();
  85. _messageDispatcherMock.Verify(x => x.Stop());
  86. }
  87. [Test]
  88. public void should_stop_message_dispatcher_when_directory_unsubscription_fails()
  89. {
  90. _directoryMock.Setup(i => i.UnregisterAsync(It.IsAny<IBus>()))
  91. .Returns(Task.FromException(new InvalidOperationException()));
  92. _bus.Start();
  93. _bus.Stop();
  94. _messageDispatcherMock.Verify(i => i.Stop());
  95. }
  96. [Test]
  97. public void should_fail_when_starting_started_bus()
  98. {
  99. _bus.Start();
  100. var exception = Assert.Throws<InvalidOperationException>(() => _bus.Start());
  101. exception.Message.ShouldContain("already running");
  102. }
  103. [Test]
  104. public void should_fail_when_stopping_non_started_bus()
  105. {
  106. var exception = Assert.Throws<InvalidOperationException>(() => _bus.Stop());
  107. exception.Message.ShouldContain("not running");
  108. }
  109. [Test]
  110. public void should_fire_events_starting_and_started_when_calling_start()
  111. {
  112. var startingEventCalled = 0;
  113. var startedEventCalled = 0;
  114. _bus.Starting += () => startingEventCalled = 1;
  115. _bus.Started += () => startedEventCalled = startingEventCalled + 1;
  116. _bus.Start();
  117. _bus.Stop();
  118. startingEventCalled.ShouldEqual(1);
  119. startedEventCalled.ShouldEqual(2);
  120. }
  121. [Test]
  122. public void should_fire_event_stopping_and_stopped_when_calling_Stop()
  123. {
  124. var stoppingEventCalled = 0;
  125. var stoppedEventCalled = 0;
  126. _bus.Stopping += () => stoppingEventCalled = 1;
  127. _bus.Stopped += () => stoppedEventCalled = stoppingEventCalled + 1;
  128. _bus.Start();
  129. _bus.Stop();
  130. stoppingEventCalled.ShouldEqual(1);
  131. stoppedEventCalled.ShouldEqual(2);
  132. }
  133. [Test]
  134. public void should_start_message_dispatcher()
  135. {
  136. _bus.Start();
  137. _messageDispatcherMock.Verify(x => x.Start());
  138. }
  139. [Test]
  140. public void should_forward_initiator_id()
  141. {
  142. _bus.Start();
  143. using (MessageId.PauseIdGeneration())
  144. {
  145. var receivedCommand = new FakeCommand(123);
  146. var eventToPublish = new FakeEvent(456);
  147. SetupDispatch(receivedCommand, _ => _bus.Publish(eventToPublish));
  148. SetupPeersHandlingMessage<FakeEvent>(_peerDown);
  149. using (MessageContext.SetCurrent(MessageContext.CreateTest(new OriginatorInfo(_peerUp.Id, _peerUp.EndPoint, null, "x.initiator"))))
  150. {
  151. var transportMessageReceived = receivedCommand.ToTransportMessage(_peerUp);
  152. transportMessageReceived.Originator.InitiatorUserName.ShouldEqual("x.initiator");
  153. _transport.RaiseMessageReceived(transportMessageReceived);
  154. }
  155. var sentMessage = _transport.Messages.Single(x => x.TransportMessage.MessageTypeId == eventToPublish.TypeId());
  156. sentMessage.TransportMessage.Originator.InitiatorUserName.ShouldEqual("x.initiator");
  157. }
  158. }
  159. [TestCase(PeerUpdateAction.Started)]
  160. [TestCase(PeerUpdateAction.Updated)]
  161. public void should_forward_peer_update_to_transport(PeerUpdateAction updateAction)
  162. {
  163. _directoryMock.Raise(x => x.PeerUpdated += null, _peerUp.Id, updateAction);
  164. var updatedPeer = _transport.UpdatedPeers.ExpectedSingle();
  165. updatedPeer.PeerId.ShouldEqual(_peerUp.Id);
  166. updatedPeer.UpdateAction.ShouldEqual(updateAction);
  167. }
  168. [Test]
  169. public void should_set_the_peerId_property()
  170. {
  171. _bus.PeerId.ShouldEqual(_self.Id);
  172. }
  173. [Test]
  174. public void should_set_the_environment_property()
  175. {
  176. _bus.Environment.ShouldEqual(_environment);
  177. }
  178. [Test]
  179. public void should_stop_if_starting_event_throws()
  180. {
  181. _bus.Starting += () => throw new DivideByZeroException();
  182. Assert.Throws<DivideByZeroException>(() => _bus.Start());
  183. _bus.IsRunning.ShouldBeFalse();
  184. }
  185. [Test]
  186. public void should_continue_running_if_stopping_event_throws()
  187. {
  188. _bus.Stopping += () => throw new DivideByZeroException();
  189. _bus.Start();
  190. Assert.Throws<DivideByZeroException>(() => _bus.Stop());
  191. _bus.IsRunning.ShouldBeTrue();
  192. }
  193. }
  194. }
  195. }