public async Task StartAsync_starts_the_engine([Frozen] IBusEngine engine, NybusHost sut) { await sut.StartAsync(); Mock.Get(engine).Verify(p => p.StartAsync(), Times.Once); }
public async Task RaiseEventAsync_forwards_message_to_engine([Frozen] IBusEngine engine, NybusHost sut, FirstTestEvent testEvent, Guid correlationId, IDictionary <string, string> headers) { await sut.RaiseEventAsync(testEvent, correlationId, headers); Mock.Get(engine).Verify(p => p.SendMessageAsync(It.Is <EventMessage <FirstTestEvent> >(m => ReferenceEquals(m.Event, testEvent) && m.Headers.CorrelationId == correlationId)), Times.Once); }
public async Task RaiseEventAsync_forwards_given_headers([Frozen] IBusEngine engine, NybusHost sut, FirstTestEvent testEvent, Guid correlationId, string headerKey, string headerValue) { var headers = new Dictionary <string, string> { [headerKey] = headerValue }; await sut.RaiseEventAsync(testEvent, correlationId, headers); Mock.Get(engine).Verify(p => p.SendMessageAsync(It.Is <Message>(message => message.Headers.ContainsKey(headerKey) && message.Headers[headerKey] == headerValue))); }
public void Bus_returns_self(NybusHost sut) { Assert.That(sut.Bus, Is.SameAs(sut)); }
public async Task InvokeCommandAsync_forwards_message_to_engine([Frozen] IBusEngine engine, NybusHost sut, FirstTestCommand testCommand, Guid correlationId, IDictionary <string, string> headers) { await sut.InvokeCommandAsync(testCommand, correlationId, headers); Mock.Get(engine).Verify(p => p.SendMessageAsync(It.Is <CommandMessage <FirstTestCommand> >(m => ReferenceEquals(m.Command, testCommand) && m.Headers.CorrelationId == correlationId)), Times.Once); }
public async Task ExecuteEventHandlerAsync_notifies_engine_on_success([Frozen] IServiceProvider serviceProvider, [Frozen] IBusEngine engine, NybusHost sut, IDispatcher dispatcher, EventMessage <FirstTestEvent> eventMessage, IServiceScopeFactory scopeFactory, IEventHandler <FirstTestEvent> handler) { var handlerType = handler.GetType(); var context = new NybusEventContext <FirstTestEvent>(eventMessage); Mock.Get(serviceProvider).Setup(p => p.GetService(typeof(IServiceScopeFactory))).Returns(scopeFactory); Mock.Get(serviceProvider).Setup(p => p.GetService(handlerType)).Returns(handler); await sut.ExecuteEventHandlerAsync(dispatcher, context, handlerType); Mock.Get(engine).Verify(p => p.NotifySuccessAsync(context.Message)); }
public async Task ExecuteEventHandlerAsync_executes_error_filter_on_fail([Frozen] IServiceProvider serviceProvider, [Frozen] IBusEngine engine, [Frozen] INybusConfiguration configuration, NybusHost sut, IDispatcher dispatcher, EventMessage <FirstTestEvent> eventMessage, IServiceScopeFactory scopeFactory, IEventHandler <FirstTestEvent> handler, Exception error, IErrorFilter errorFilter) { configuration.EventErrorFilters = new[] { errorFilter }; var handlerType = handler.GetType(); var context = new NybusEventContext <FirstTestEvent>(eventMessage); Mock.Get(serviceProvider).Setup(p => p.GetService(typeof(IServiceScopeFactory))).Returns(scopeFactory); Mock.Get(serviceProvider).Setup(p => p.GetService(handlerType)).Returns(handler); Mock.Get(handler).Setup(p => p.HandleAsync(It.IsAny <IDispatcher>(), It.IsAny <IEventContext <FirstTestEvent> >())).Throws(error); await sut.ExecuteEventHandlerAsync(dispatcher, context, handlerType); Mock.Get(errorFilter).Verify(p => p.HandleErrorAsync(context, error, It.IsAny <EventErrorDelegate <FirstTestEvent> >())); }
public async Task ExecuteEventHandler_executes_handler([Frozen] IServiceProvider serviceProvider, NybusHost sut, IDispatcher dispatcher, IEventContext <FirstTestEvent> eventContext, IServiceScopeFactory scopeFactory, IEventHandler <FirstTestEvent> handler) { var handlerType = handler.GetType(); Mock.Get(serviceProvider).Setup(p => p.GetService(typeof(IServiceScopeFactory))).Returns(scopeFactory); Mock.Get(serviceProvider).Setup(p => p.GetService(handlerType)).Returns(handler); await sut.ExecuteEventHandlerAsync(dispatcher, eventContext, handlerType); Mock.Get(handler).Verify(p => p.HandleAsync(dispatcher, eventContext), Times.Once); }
public void ExecuteEventHandlerAsync_throws_if_handler_is_not_registered([Frozen] IServiceProvider serviceProvider, NybusHost sut, IDispatcher dispatcher, IEventContext <FirstTestEvent> eventContext, IServiceScopeFactory scopeFactory) { var handlerType = typeof(FirstTestEventHandler); Mock.Get(serviceProvider).Setup(p => p.GetService(typeof(IServiceScopeFactory))).Returns(scopeFactory); Mock.Get(serviceProvider).Setup(p => p.GetService(handlerType)).Returns(null as FirstTestEventHandler); Assert.ThrowsAsync <MissingHandlerException>(() => sut.ExecuteEventHandlerAsync(dispatcher, eventContext, handlerType)); }
public async Task ExecuteCommandHandler_creates_new_scope_for_execution([Frozen] IServiceProvider serviceProvider, NybusHost sut, IDispatcher dispatcher, ICommandContext <FirstTestCommand> commandContext, IServiceScopeFactory scopeFactory, ICommandHandler <FirstTestCommand> handler) { var handlerType = handler.GetType(); Mock.Get(serviceProvider).Setup(p => p.GetService(typeof(IServiceScopeFactory))).Returns(scopeFactory); Mock.Get(serviceProvider).Setup(p => p.GetService(handlerType)).Returns(handler); await sut.ExecuteCommandHandlerAsync(dispatcher, commandContext, handlerType); Mock.Get(scopeFactory).Verify(p => p.CreateScope(), Times.Once); }
public void ExecutionEnvironment_returns_self(NybusHost sut) { Assert.That(sut.ExecutionEnvironment, Is.SameAs(sut)); }
public async Task Null_messages_delivered_from_engine_are_discarded([Frozen] IBusEngine engine, NybusHost sut, EventMessage <FirstTestEvent> testMessage, EventReceivedAsync <FirstTestEvent> receivedMessage) { var subject = new Subject <Message>(); Mock.Get(engine).Setup(p => p.StartAsync()).ReturnsAsync(subject); sut.SubscribeToEvent(receivedMessage); await sut.StartAsync(); subject.OnNext(null); await sut.StopAsync(); Mock.Get(receivedMessage).Verify(p => p(It.IsAny <IDispatcher>(), It.IsAny <IEventContext <FirstTestEvent> >()), Times.Never); }
public async Task Handler_is_executed_when_eventMessages_are_processed([Frozen] IBusEngine engine, NybusHost sut, EventMessage <FirstTestEvent> testMessage, EventReceivedAsync <FirstTestEvent> receivedMessage) { var subject = new Subject <Message>(); Mock.Get(engine).Setup(p => p.StartAsync()).ReturnsAsync(subject); sut.SubscribeToEvent(receivedMessage); await sut.StartAsync(); subject.OnNext(testMessage); await sut.StopAsync(); Mock.Get(receivedMessage).Verify(p => p(It.IsAny <IDispatcher>(), It.IsAny <IEventContext <FirstTestEvent> >()), Times.Once); }
public async Task StopAsync_is_ignored_if_not_started([Frozen] IBusEngine engine, NybusHost sut) { await sut.StopAsync(); Mock.Get(engine).Verify(p => p.StopAsync(), Times.Never); }