public void When_multiple_consequenter_handlers_are_chained_then_the_last_added_is_called_first() { // arrange var bus = new InProcessEventBus(); var handlerCalls = new List <string>(); // act var handler = new TestConsequenter() .WrapAll((e, next) => { handlerCalls.Add("c"); next(e); }) .WrapAll((e, next) => { handlerCalls.Add("b"); next(e); }) .WrapAll((e, next) => { handlerCalls.Add("a"); next(e); }); bus.Subscribe(handler); bus.PublishAsync(new Order.Created()).Wait(); // assert handlerCalls.Should().BeInAscendingOrder(); }
public async Task When_a_handler_chain_throws_then_subsequent_events_are_still_published() { // arrange var bus = new InProcessEventBus(); var errors = new List <EventHandlingError>(); bus.Errors.Subscribe(errors.Add); var callCount = 0; // act var handler = new TestConsequenter() .WrapAll((e, next) => { callCount++; if (callCount == 1) { throw new Exception("oops!"); } }); bus.Subscribe(handler); await bus.PublishAsync(new Order.Created()); await bus.PublishAsync(new Order.Created()); // assert callCount.Should().Be(2); }
public void When_a_consequenter_that_has_been_chained_throws_then_the_EventHandlingError_Handler_references_the_inner_handler() { // arrange var bus = new InProcessEventBus(); var errors = new List <EventHandlingError>(); bus.Errors.Subscribe(errors.Add); // act var handler = new TestConsequenter(onCreated: e => { throw new Exception("oops!"); }); bus.Subscribe(handler); bus.PublishAsync(new Order.Created()).Wait(); // assert errors.Should().ContainSingle(e => e.Handler is TestConsequenter); }
public void When_a_handler_chain_throws_then_an_EventHandlingError_is_published() { // arrange var bus = new InProcessEventBus(); var errors = new List <EventHandlingError>(); bus.Errors.Subscribe(errors.Add); // act var handler = new TestConsequenter() .WrapAll((e, next) => { throw new Exception("oops!"); }); bus.Subscribe(handler); bus.PublishAsync(new Order.Created()).Wait(); // assert errors.Should().ContainSingle(e => e.StreamName == "Order" && e.Event.EventName() == "Created" && e.Exception.Message.Contains("oops!")); }
public void Consequenter_can_be_short_circuited_using_handler_chains() { // arrange var bus = new InProcessEventBus(); var consequenterWasCalled = false; var handlerCalls = 0; // act var handler = new TestConsequenter(onCreated: e => consequenterWasCalled = true) .WrapAll((e, next) => { handlerCalls++; // by not calling next, we short circuit the call to the remaining handler chain }); bus.Subscribe(handler); bus.PublishAsync(new Order.Created()).Wait(); // assert handlerCalls.Should().Be(1); consequenterWasCalled.Should().BeFalse(); }