public void multiple_message_handle_invocations_are_correct() { BusMessages .AssertNext <TestDomainEvent>(TestCorrelationId) .AssertNext <ParentTestDomainEvent>(TestCorrelationId) .AssertNext <ChildTestDomainEvent>(TestCorrelationId) .AssertNext <GrandChildTestDomainEvent>(TestCorrelationId) .AssertEmpty(); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref MessageSubscriber.TestDomainEventHandleCount) == 1, 1000, $"Expected 1 Test Domain Event Handled, found {MessageSubscriber.TestDomainEventHandleCount}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref MessageSubscriber.GrandChildTestDomainEventHandleCount) == 1, 1000, $"Expected 1 GrandChildTestDomainEvent handled , found {MessageSubscriber.ChildTestDomainEventHandleCount}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref MessageSubscriber.ChildTestDomainEventHandleCount) == 2, 1000, $"Expected 2 ChildTestDomainEvent handled , found {MessageSubscriber.ChildTestDomainEventHandleCount}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref MessageSubscriber.ParentTestDomainEventHandleCount) == 3, 1000, $"Expected 3 Parent Test Domain Event handled, found {MessageSubscriber.ParentTestDomainEventHandleCount}"); }
public void child_invokes_parent_and_child_handlers_once() { using (var sub = new TestInheritedMessageSubscriber(Bus)) { var subscription = sub; TestQueue.Clear(); Bus.Publish(new ChildTestDomainEvent(TestCorrelationId, ChildMsgId)); BusMessages .AssertNext <ChildTestDomainEvent>(TestCorrelationId) .AssertEmpty(); Assert.IsOrBecomesTrue(() => subscription.Starving, 3000); Assert.IsOrBecomesTrue(() => subscription.TestDomainEventHandleCount == 0, 1000, $"Expected 0 Test Domain Event Handled, found {subscription.TestDomainEventHandleCount}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref subscription.GrandChildTestDomainEventHandleCount) == 0, 1000, $"Expected 0 GrandChildTestDomainEvent handled , found {subscription.ChildTestDomainEventHandleCount}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref subscription.ChildTestDomainEventHandleCount) == 1, 1000, $"Expected 1 ChildTestDomainEvent handled , found {subscription.ChildTestDomainEventHandleCount}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref subscription.ParentTestDomainEventHandleCount) == 1, 1000, $"Expected 1 Parent Test Domain Event handled, found {subscription.ParentTestDomainEventHandleCount}"); } }
public void grand_child_invokes_all_handlers_thrice() { using (var sub = new TestInheritedMessageSubscriber(Bus, false)) { var subscriber = sub; TestQueue.Clear(); Bus.Publish(new GrandChildTestDomainEvent(TestCorrelationId, ChildMsgId)); BusMessages .AssertNext <GrandChildTestDomainEvent>(TestCorrelationId) .AssertEmpty(); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref subscriber.TestDomainEventHandleCount) == 0, 2000, $"Expected 0 Test Domain Event Handled, found {subscriber.TestDomainEventHandleCount}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref subscriber.GrandChildTestDomainEventHandleCount) == 3, 3000, $"Expected 3 GrandChildTestDomainEvent handled , found {subscriber.ChildTestDomainEventHandleCount}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref subscriber.ChildTestDomainEventHandleCount) == 3, 3000, $"Expected 3 ChildTestDomainEvent handled , found {subscriber.ChildTestDomainEventHandleCount}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref subscriber.ParentTestDomainEventHandleCount) == 3, 3000, $"Expected 3 Parent Test Domain Event handled, found {subscriber.ParentTestDomainEventHandleCount}"); } }
public void can_handle_child_messages() { var msg3 = new ChildTestMessage(); Bus.Publish(msg3); Assert.Equal(BusMessages.Count, 2); Message deQdMsg; if (BusMessages.TryDequeue(out deQdMsg)) { Assert.IsType <TestMessage>(deQdMsg); } if (BusMessages.TryDequeue(out deQdMsg)) { Assert.IsType <ChildTestMessage>(deQdMsg); } Assert.False(BusMessages.TryDequeue(out deQdMsg)); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref MessageSubscriber.TimesTestMessageHandled) == 1, 1000, $"Expected 1 TestMessage, found {MessageSubscriber.TimesTestMessageHandled}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref MessageSubscriber.TimesChildTestMessageHandled) == 1, null, $"Expected 1 ChildTestMessage, found {MessageSubscriber.TimesChildTestMessageHandled}"); }
public void can_handle_two_different_messages() { var msg2 = new TestMessage2(); Bus.Publish(msg2); Assert.IsOrBecomesTrue( () => BusMessages.Count == 2, null, $"Expected 2 Messages on bus, found {BusMessages.Count}"); Message deQdMsg; if (BusMessages.TryDequeue(out deQdMsg)) { Assert.IsType <TestMessage>(deQdMsg); } if (BusMessages.TryDequeue(out deQdMsg)) { Assert.IsType <TestMessage2>(deQdMsg); } Assert.False(BusMessages.TryDequeue(out deQdMsg)); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref MessageSubscriber.TimesTestMessageHandled) == 1, 1000, $"Expected 1 TestMessage, found {MessageSubscriber.TimesTestMessageHandled}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref MessageSubscriber.TimesTestMessage2Handled) == 1, 100, $"Expected 1 TestMessage2, found {MessageSubscriber.TimesTestMessage2Handled}"); }
public Bus(IMessageTransport busTransport, IDependencyResolver dependencyResolver, MessageScanRules rules, Func <MessageHandlingExceptionRaisedEventArgs, Task> messageHandlingExceptionHandler = null) { this.busTransport = busTransport; this.dependencyResolver = dependencyResolver; this.busMessages = new BusMessages(rules); this.messageHandlingExceptionHandler = messageHandlingExceptionHandler; }
public Task Send <T>(T commandMessage, SendOptions options = null) { var type = typeof(T); if (!busMessages.IsSendable(type)) { throw BusMessages.CreateMessageNotFoundException(type); } return(busTransport.Send(commandMessage, MessageTypeToNameSendingCommandMap[type], options)); }
public Task Publish <T>(T eventMessage, PublishOptions options = null) { var type = typeof(T); if (!busMessages.IsPublishable(type)) { throw BusMessages.CreateMessageNotFoundException(type); } return(busTransport.Publish(eventMessage, MessageTypeToNamePublishingEventMap[type], options)); }
public void can_handle_multiple_subscribers() { TestQueuedSubscriber secondSubscriber = new TestQueuedSubscriber(Bus); var msg2 = new TestMessage2(); Bus.Publish(msg2); Assert.Equal(BusMessages.Count, 2); var msg3 = new TestMessage2(); Bus.Publish(msg3); Assert.Equal(BusMessages.Count, 3); Message deQdMsg; if (BusMessages.TryDequeue(out deQdMsg)) { Assert.IsType <TestMessage>(deQdMsg); } if (BusMessages.TryDequeue(out deQdMsg)) { Assert.IsType <TestMessage2>(deQdMsg); } if (BusMessages.TryDequeue(out deQdMsg)) { Assert.IsType <TestMessage2>(deQdMsg); } Assert.False(BusMessages.TryDequeue(out deQdMsg)); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref MessageSubscriber.TimesTestMessageHandled) == 1, 1000, $"Expected 1 TestMessage, handled by first subscriber {MessageSubscriber.TimesTestMessageHandled}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref secondSubscriber.TimesTestMessageHandled) == 0, 1000, $"Expected 0 TestMessage handled by second subscriber, found {secondSubscriber.TimesTestMessageHandled}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref MessageSubscriber.TimesTestMessage2Handled) == 2, 1000, $"Expected 2 TimesTestMessage2Handled by secondSubscriber, found {MessageSubscriber.TimesTestMessage2Handled}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref secondSubscriber.TimesTestMessage2Handled) == 2, 1000, $"Expected 2 TimesTestMessage2Handled by second subscriber, found {secondSubscriber.TimesTestMessage2Handled}"); }
public Task Publish <T>(T eventMessage) { if (!started) { throw new InvalidOperationException("The bus has not been started."); } var type = typeof(T); if (!busMessages.IsAnEvent(type)) { throw BusMessages.CreateMessageNotFoundException(type); } return(busTransport.Publish(eventMessage, busMessages.GetMessageTypeNameByType(type))); }
public Task Send <T>(T commandMessage) { if (!started) { throw new InvalidOperationException("The bus has not been started."); } var type = typeof(T); if (!busMessages.IsACommand(type)) { throw BusMessages.CreateMessageNotFoundException(type); } return(busTransport.Send(commandMessage, busMessages.GetMessageTypeNameByType(type))); }
public Task Send <T>(T commandMessage, SendOptions options) { if (!started) { throw new InvalidOperationException("The bus has not been started."); } var type = typeof(T); if (!busMessages.IsSendable(type)) { throw BusMessages.CreateMessageNotFoundException(type); } return(options == null ? busTransport.Send(commandMessage, MessageTypeToNameSendingCommandMap[type]) : busTransport.Send(commandMessage, MessageTypeToNameSendingCommandMap[type], options)); }
public Task Publish <T>(T eventMessage, PublishOptions options) { if (!started) { throw new InvalidOperationException("The bus has not been started."); } var type = typeof(T); if (!busMessages.IsPublishable(type)) { throw BusMessages.CreateMessageNotFoundException(type); } return(options == null ? busTransport.Publish(eventMessage, MessageTypeToNamePublishingEventMap[type]) : busTransport.Publish(eventMessage, MessageTypeToNamePublishingEventMap[type], options)); }
public void no_dependency_resolver_should_mark_as_either_sendable_or_publishable() { var busMessages = new BusMessages( new MessageScanRules() .DefineEventScanRuleWith(t => t.Namespace == "RockyBus.DemoMessages.Events") .DefineCommandScanRuleWith(t => t.Namespace == "RockyBus.DemoMessages.Commands"), null); busMessages.IsSendable(typeof(Apple)).Should().BeTrue(); busMessages.IsSendable(typeof(Banana)).Should().BeTrue(); busMessages.IsSendable(typeof(Cat)).Should().BeFalse(); busMessages.IsSendable(typeof(Dog)).Should().BeFalse(); busMessages.IsPublishable(typeof(Apple)).Should().BeFalse(); busMessages.IsPublishable(typeof(Banana)).Should().BeFalse(); busMessages.IsPublishable(typeof(Cat)).Should().BeTrue(); busMessages.IsPublishable(typeof(Dog)).Should().BeTrue(); }
public void grand_child_invokes_all_handlers_four_times() { //n.b. the number of duplications matched the number of times we have subscribed to the hierarchy //see the next test where we do not subscribe to Message // the subscription to Test Event does not matter because it tis outside the heirarchy using (var sub = new TestInheritedMessageSubscriber(Bus, false)) { var subscriber = sub; subscriber.Subscribe <Message>(subscriber); TestQueue.Clear(); Bus.Publish(new GrandChildTestDomainEvent(TestCorrelationId, ChildMsgId)); BusMessages .AssertNext <GrandChildTestDomainEvent>(TestCorrelationId) .AssertEmpty(); Assert.IsOrBecomesTrue(() => subscriber.Starving, 3000); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref subscriber.TestDomainEventHandleCount) == 0, 1000, $"Expected 0 Test Domain Event Handled, found {subscriber.TestDomainEventHandleCount}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref subscriber.GrandChildTestDomainEventHandleCount) == 4, 3000, $"Expected 4 GrandChildTestDomainEvent handled , found {subscriber.ChildTestDomainEventHandleCount}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref subscriber.ChildTestDomainEventHandleCount) == 4, 3000, $"Expected 4 ChildTestDomainEvent handled , found {subscriber.ChildTestDomainEventHandleCount}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref subscriber.ParentTestDomainEventHandleCount) == 4, 3000, $"Expected 4 Parent Test Domain Event handled, found {subscriber.ParentTestDomainEventHandleCount}"); Assert.IsOrBecomesTrue( () => Interlocked.Read(ref subscriber.MessageHandleCount) == 4, 3000, $"Expected 4 Message Test Domain Event handled, found {subscriber.MessageHandleCount}"); } }
public void can_handle_all_messages() { TestQueue.Clear(); var cmdHandler = new TestCommandSubscriber(Bus); // this is just an example command - choice to fire this one was random var cmd = new TestCommands.TestCommand2( Guid.NewGuid(), null); Bus.Fire(cmd, "exception message", TimeSpan.FromSeconds(5)); Assert.IsOrBecomesTrue( () => BusMessages.Count == 3, 1000, $"Expected 3 bus messages for TestCommand2, found {BusMessages.Count}"); Message deQdMsg; if (BusMessages.TryDequeue(out deQdMsg)) { Assert.IsAssignableFrom <Message>(deQdMsg); } if (BusMessages.TryDequeue(out deQdMsg)) { Assert.IsAssignableFrom <Message>(deQdMsg); } if (BusMessages.TryDequeue(out deQdMsg)) { Assert.IsAssignableFrom <Message>(deQdMsg); } }
void when_bus_messages_is_created() { _busMessages = new BusMessages(_messageScanRules); }