Example #1
0
        public void ReceivedOn_is_attached_to_context_from_clock(CommandMessage <FirstTestCommand> commandMessage, DateTimeOffset now)
        {
            Clock.SetTo(new TestClock(now));

            var sut = new NybusCommandContext <FirstTestCommand>(commandMessage);

            Assert.That(sut.ReceivedOn, Is.EqualTo(now));

            Clock.Reset();
        }
Example #2
0
        public async Task ExecuteCommandHandlerAsync_notifies_engine_on_success([Frozen] IServiceProvider serviceProvider, [Frozen] IBusEngine engine, NybusHost sut, IDispatcher dispatcher, CommandMessage <FirstTestCommand> commandMessage, IServiceScopeFactory scopeFactory, ICommandHandler <FirstTestCommand> handler)
        {
            var handlerType = handler.GetType();

            var context = new NybusCommandContext <FirstTestCommand>(commandMessage);

            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, context, handlerType);

            Mock.Get(engine).Verify(p => p.NotifySuccessAsync(context.Message));
        }
Example #3
0
        public async Task ExecuteCommandHandlerAsync_executes_error_filter_on_fail([Frozen] IServiceProvider serviceProvider, [Frozen] IBusEngine engine, [Frozen] INybusConfiguration configuration, NybusHost sut, IDispatcher dispatcher, CommandMessage <FirstTestCommand> commandMessage, IServiceScopeFactory scopeFactory, ICommandHandler <FirstTestCommand> handler, Exception error, IErrorFilter errorFilter)
        {
            configuration.CommandErrorFilters = new[] { errorFilter };

            var handlerType = handler.GetType();

            var context = new NybusCommandContext <FirstTestCommand>(commandMessage);

            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 <ICommandContext <FirstTestCommand> >())).Throws(error);

            await sut.ExecuteCommandHandlerAsync(dispatcher, context, handlerType);

            Mock.Get(errorFilter).Verify(p => p.HandleErrorAsync(context, error, It.IsAny <CommandErrorDelegate <FirstTestCommand> >()));
        }
Example #4
0
        public async Task HandleError_adds_retry_count_if_retry_count_not_present([Frozen] IBusEngine engine, RetryErrorFilter sut, Exception error, NybusCommandContext <FirstTestCommand> context, CommandErrorDelegate <FirstTestCommand> next)
        {
            await sut.HandleErrorAsync(context, error, next);

            Assert.That(context.Message.Headers.ContainsKey(Headers.RetryCount));
        }
Example #5
0
        public async Task HandleError_retries_if_retry_count_not_present([Frozen] IBusEngine engine, RetryErrorFilter sut, Exception error, NybusCommandContext <FirstTestCommand> context, CommandErrorDelegate <FirstTestCommand> next)
        {
            await sut.HandleErrorAsync(context, error, next);

            Mock.Get(engine).Verify(p => p.SendMessageAsync(context.CommandMessage));
        }
Example #6
0
        public async Task HandleError_increments_retry_count_if_retry_count_present([Frozen] RetryErrorFilterOptions options, [Frozen] IBusEngine engine, RetryErrorFilter sut, Exception error, NybusCommandContext <FirstTestCommand> context, CommandErrorDelegate <FirstTestCommand> next)
        {
            context.Message.Headers[Headers.RetryCount] = (options.MaxRetries - 2).Stringfy();

            await sut.HandleErrorAsync(context, error, next);

            Assert.That(context.Message.Headers.ContainsKey(Headers.RetryCount));
            Assert.That(context.Message.Headers[Headers.RetryCount], Is.EqualTo((options.MaxRetries - 1).Stringfy()));
        }
Example #7
0
        public async Task HandleError_retries_if_retry_count_less_than_maxRetries([Frozen] RetryErrorFilterOptions options, [Frozen] IBusEngine engine, RetryErrorFilter sut, Exception error, NybusCommandContext <FirstTestCommand> context, CommandErrorDelegate <FirstTestCommand> next)
        {
            context.Message.Headers[Headers.RetryCount] = (options.MaxRetries - 2).Stringfy();

            await sut.HandleErrorAsync(context, error, next);

            Mock.Get(engine).Verify(p => p.SendMessageAsync(context.CommandMessage));
        }
Example #8
0
        public async Task HandleError_invokes_next_if_retry_count_equal_or_greater_than_maxRetries([Frozen] RetryErrorFilterOptions options, [Frozen] IBusEngine engine, RetryErrorFilter sut, Exception error, NybusCommandContext <FirstTestCommand> context, CommandErrorDelegate <FirstTestCommand> next)
        {
            context.Message.Headers[Headers.RetryCount] = options.MaxRetries.Stringfy();

            await sut.HandleErrorAsync(context, error, next);

            Mock.Get(next).Verify(p => p(context, error));
        }
Example #9
0
        public void Message_SentOn_is_attached_to_context(CommandMessage <FirstTestCommand> commandMessage)
        {
            var sut = new NybusCommandContext <FirstTestCommand>(commandMessage);

            Assert.That(sut.SentOn, Is.EqualTo(commandMessage.Headers.SentOn));
        }
Example #10
0
        public void Message_correlationId_is_attached_to_context(CommandMessage <FirstTestCommand> commandMessage)
        {
            var sut = new NybusCommandContext <FirstTestCommand>(commandMessage);

            Assert.That(sut.CorrelationId, Is.EqualTo(commandMessage.Headers.CorrelationId));
        }
Example #11
0
        public void Command_is_attached_to_context(CommandMessage <FirstTestCommand> commandMessage)
        {
            var sut = new NybusCommandContext <FirstTestCommand>(commandMessage);

            Assert.That(sut.Command, Is.SameAs(commandMessage.Command));
        }