Example #1
0
        public async Task StartAsync_starts_the_engine([Frozen] IBusEngine engine, NybusHost sut)
        {
            await sut.StartAsync();

            Mock.Get(engine).Verify(p => p.StartAsync(), Times.Once);
        }
Example #2
0
        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);
        }
Example #3
0
        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)));
        }
Example #4
0
 public void Bus_returns_self(NybusHost sut)
 {
     Assert.That(sut.Bus, Is.SameAs(sut));
 }
Example #5
0
        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);
        }
Example #6
0
        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));
        }
Example #7
0
        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> >()));
        }
Example #8
0
        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);
        }
Example #9
0
        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));
        }
Example #10
0
        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);
        }
Example #11
0
 public void ExecutionEnvironment_returns_self(NybusHost sut)
 {
     Assert.That(sut.ExecutionEnvironment, Is.SameAs(sut));
 }
Example #12
0
        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);
        }
Example #13
0
        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);
        }
Example #14
0
        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);
        }