示例#1
0
        public async Task RunAsync_should_throw_if_saga_cannot_handle_message()
        {
            var message = UnhandledMessage.New();

            var messageContext = NSubstitute.Substitute.For <IMessageContext <UnhandledMessage> >();

            messageContext.Message.Returns(message);

            var sagaStateService = NSubstitute.Substitute.For <ISagaStateService <DummySaga, DummySagaState> >();

            var state = new DummySagaState(message.CorrelationId);

            sagaStateService.GetAsync(messageContext, Arg.Any <CancellationToken>())
            .Returns((state, Guid.NewGuid()));

            var saga = NSubstitute.Substitute.ForPartsOf <DummySaga>(state);

            var sagaFactory = NSubstitute.Substitute.For <ISagaFactory <DummySaga, DummySagaState> >();

            sagaFactory.Create(state)
            .Returns(saga);

            var logger = NSubstitute.Substitute.For <ILogger <SagaRunner <DummySaga, DummySagaState> > >();

            var transactionManager = NSubstitute.Substitute.For <ITransactionManager>();
            var policyFactory      = NSubstitute.Substitute.For <ISagaPolicyFactory <DummySaga> >();

            var sut = new SagaRunner <DummySaga, DummySagaState>(sagaFactory, sagaStateService, transactionManager, policyFactory, logger);

            await Assert.ThrowsAsync <ConsumerNotFoundException>(() => sut.RunAsync(messageContext, CancellationToken.None));
        }
示例#2
0
        public async Task RunAsync_should_retry_if_saga_state_locked()
        {
            var message        = StartDummySaga.New();
            var messageContext = NSubstitute.Substitute.For <IMessageContext <StartDummySaga> >();

            messageContext.Message.Returns(message);

            var sagaStateService = NSubstitute.Substitute.For <ISagaStateService <DummySaga, DummySagaState> >();

            var state = new DummySagaState(message.CorrelationId);

            var firstCall = true;

            sagaStateService.When(s => s.GetAsync(messageContext, Arg.Any <CancellationToken>()))
            .Do(_ =>
            {
                if (firstCall)
                {
                    firstCall = false;
                    throw new LockException("lorem");
                }
            });
            sagaStateService.GetAsync(messageContext, Arg.Any <CancellationToken>())
            .Returns((state, Guid.NewGuid()));

            var saga = NSubstitute.Substitute.ForPartsOf <DummySaga>();

            saga.SetBus(NSubstitute.Substitute.For <IMessageBus>());
            saga.When(s => s.HandleAsync(Arg.Any <IMessageContext <StartDummySaga> >(), Arg.Any <CancellationToken>()))
            .DoNotCallBase();

            var sagaFactory = NSubstitute.Substitute.For <ISagaFactory <DummySaga, DummySagaState> >();

            sagaFactory.Create(state)
            .Returns(saga);

            var logger = NSubstitute.Substitute.For <ILogger <SagaRunner <DummySaga, DummySagaState> > >();

            var uow = NSubstitute.Substitute.For <IUnitOfWork>();

            var sut = new SagaRunner <DummySaga, DummySagaState>(sagaFactory, sagaStateService, uow, logger);

            await sut.RunAsync(messageContext, CancellationToken.None);

            await saga.Received(1)
            .HandleAsync(messageContext, Arg.Any <CancellationToken>());

            await sagaStateService.Received(2)
            .GetAsync(messageContext, Arg.Any <CancellationToken>());
        }
示例#3
0
        public async Task RunAsync_should_execute_saga_handler_with_policy_when_available()
        {
            var message = new StartDummySaga(Guid.NewGuid(), Guid.NewGuid());

            var messageContext = NSubstitute.Substitute.For <IMessageContext <StartDummySaga> >();

            messageContext.Message.Returns(message);

            var sagaStateService = NSubstitute.Substitute.For <ISagaStateService <DummySaga, DummySagaState> >();

            var state = new DummySagaState(message.CorrelationId);

            sagaStateService.GetAsync(messageContext, Arg.Any <CancellationToken>())
            .Returns((state, Guid.NewGuid()));

            var saga = NSubstitute.Substitute.ForPartsOf <DummySaga>();

            saga.SetBus(NSubstitute.Substitute.For <IMessageBus>());
            saga.When(s => s.HandleAsync(Arg.Any <IMessageContext <StartDummySaga> >(), Arg.Any <CancellationToken>()))
            .DoNotCallBase();

            var sagaFactory = NSubstitute.Substitute.For <ISagaFactory <DummySaga, DummySagaState> >();

            sagaFactory.Create(state)
            .Returns(saga);

            var logger = NSubstitute.Substitute.For <ILogger <SagaRunner <DummySaga, DummySagaState> > >();

            var transactionManager = NSubstitute.Substitute.For <ITransactionManager>();

            var policyFactory = NSubstitute.Substitute.For <ISagaPolicyFactory <DummySaga> >();
            var policy        = new FakePolicy();

            policyFactory.Create <StartDummySaga>().ReturnsForAnyArgs(policy);

            var sut = new SagaRunner <DummySaga, DummySagaState>(sagaFactory, sagaStateService, transactionManager, policyFactory, logger);

            await sut.RunAsync(messageContext, CancellationToken.None);

            await saga.Received(1)
            .HandleAsync(messageContext, Arg.Any <CancellationToken>());
        }
示例#4
0
        public async Task RunAsync_should_not_execute_handler_if_saga_is_marked_as_completed()
        {
            var message        = new StartDummySaga(Guid.NewGuid(), Guid.NewGuid());
            var messageContext = NSubstitute.Substitute.For <IMessageContext <StartDummySaga> >();

            messageContext.Message.Returns(message);

            var state = new DummySagaState(message.CorrelationId);

            state.MarkAsCompleted();

            var saga = NSubstitute.Substitute.ForPartsOf <DummySaga>(state);

            saga.When(s => s.HandleAsync(Arg.Any <IMessageContext <StartDummySaga> >(), Arg.Any <CancellationToken>()))
            .DoNotCallBase();

            var sagaFactory = NSubstitute.Substitute.For <ISagaFactory <DummySaga, DummySagaState> >();

            sagaFactory.Create(state)
            .Returns(saga);

            var sagaStateService = NSubstitute.Substitute.For <ISagaStateService <DummySaga, DummySagaState> >();

            sagaStateService.GetAsync(messageContext, Arg.Any <CancellationToken>())
            .Returns((state, Guid.NewGuid()));

            var logger = NSubstitute.Substitute.For <ILogger <SagaRunner <DummySaga, DummySagaState> > >();

            var transactionManager = NSubstitute.Substitute.For <ITransactionManager>();
            var policyFactory      = NSubstitute.Substitute.For <ISagaPolicyFactory <DummySaga> >();

            var sut = new SagaRunner <DummySaga, DummySagaState>(sagaFactory, sagaStateService, transactionManager, policyFactory, logger);

            await sut.RunAsync(messageContext, CancellationToken.None);

            sagaFactory.DidNotReceiveWithAnyArgs().Create(null);

            await saga.DidNotReceiveWithAnyArgs()
            .HandleAsync(messageContext, Arg.Any <CancellationToken>());
        }
示例#5
0
        public async Task RunAsync_should_not_execute_handler_if_message_already_processed()
        {
            var message        = new StartDummySaga(Guid.NewGuid(), Guid.NewGuid());
            var messageContext = NSubstitute.Substitute.For <IMessageContext <StartDummySaga> >();

            messageContext.Message.Returns(message);

            var saga = NSubstitute.Substitute.ForPartsOf <DummySaga>();

            saga.When(s => s.HandleAsync(Arg.Any <IMessageContext <StartDummySaga> >(), Arg.Any <CancellationToken>()))
            .DoNotCallBase();

            var state = new DummySagaState(message.CorrelationId);

            state.SetAsProcessed(message);

            var sagaFactory = NSubstitute.Substitute.For <ISagaFactory <DummySaga, DummySagaState> >();

            sagaFactory.Create(state)
            .Returns(saga);

            var sagaStateService = NSubstitute.Substitute.For <ISagaStateService <DummySaga, DummySagaState> >();

            sagaStateService.GetAsync(messageContext, Arg.Any <CancellationToken>())
            .Returns((state, Guid.NewGuid()));

            var logger = NSubstitute.Substitute.For <ILogger <SagaRunner <DummySaga, DummySagaState> > >();

            var uow = NSubstitute.Substitute.For <IUnitOfWork>();

            var sut = new SagaRunner <DummySaga, DummySagaState>(sagaFactory, sagaStateService, uow, logger);

            await sut.RunAsync(messageContext, CancellationToken.None);

            sagaFactory.DidNotReceiveWithAnyArgs().Create(null);

            await saga.DidNotReceiveWithAnyArgs()
            .HandleAsync(messageContext, Arg.Any <CancellationToken>());
        }
示例#6
0
        public async Task RunAsync_should_throw_SagaNotFoundException_if_saga_cannot_be_build()
        {
            var message        = new StartDummySaga(Guid.NewGuid(), Guid.NewGuid());
            var messageContext = NSubstitute.Substitute.For <IMessageContext <StartDummySaga> >();

            messageContext.Message.Returns(message);

            var sagaFactory = NSubstitute.Substitute.For <ISagaFactory <DummySaga, DummySagaState> >();

            var sagaStateService = NSubstitute.Substitute.For <ISagaStateService <DummySaga, DummySagaState> >();

            var state = new DummySagaState(message.CorrelationId);

            sagaStateService.GetAsync(messageContext, Arg.Any <CancellationToken>())
            .Returns((state, Guid.NewGuid()));

            var logger = NSubstitute.Substitute.For <ILogger <SagaRunner <DummySaga, DummySagaState> > >();

            var uow = NSubstitute.Substitute.For <IUnitOfWork>();

            var sut = new SagaRunner <DummySaga, DummySagaState>(sagaFactory, sagaStateService, uow, logger);

            await Assert.ThrowsAsync <SagaNotFoundException>(() => sut.RunAsync(messageContext, CancellationToken.None));
        }