Beispiel #1
0
 public StateMachineTests()
 {
     _keyValueStoreMock      = new Mock <IKeyValueStore>();
     _mockPublisher          = new Mock <Func <IEnumerable <ISagaMessageIdentifier>, Task> >();
     _mockEventloggerFactory = new Mock <IEventLoggerFactory>();
     _mockEventLogger        = new Mock <IEventLogger>();
     _mockEventloggerFactory.Setup(o => o.GetRequestEventLogger(It.IsAny <string>())).Returns(_mockEventLogger.Object);
     _sagaMachine = new SagaMachine <TestState>(_keyValueStoreMock.Object, _mockPublisher.Object, _mockEventloggerFactory.Object);
 }
Beispiel #2
0
        public void SagaMachine_Should_Not_Stop_Saga_If_An_Exception_Happens()
        {
            //Arrange
            string dummyLockToken;

            _keyValueStoreMock.Setup(o => o.TakeLockWithDefaultExpiryTime(It.IsAny <string>(), out dummyLockToken)).Returns(true);
            _keyValueStoreMock.Setup(o => o.ReleaseLock(It.IsAny <string>(), It.IsAny <string>())).Returns(true);

            _keyValueStoreMock.Setup(o => o.GetValue <TestState>(It.IsAny <string>()))
            .Returns(new HashedValue <TestState>
            {
                Hash  = Guid.Empty.ToString(),
                Value = new TestState()
            });

            // We need the below to set the return value
            _keyValueStoreMock.Setup(kv => kv.Remove(It.IsAny <string>(), It.IsAny <string>())).Returns(true);

            Func <IEnumerable <ISagaMessageIdentifier>, Task> publisher = async messages =>
            {
                foreach (var message in messages)
                {
                    if (message is GoodbyeMessage)
                    {
                        await Task.Delay(1000);

                        throw new Exception();
                    }

                    await Task.Delay(500);
                }
            };

            //Act
            _sagaMachine = new SagaMachine <TestState>(_keyValueStoreMock.Object, publisher, _mockEventloggerFactory.Object);
            _sagaMachine
            .WithMessage <HelloMessage>((proccess, msg) => proccess
                                        .Publish((msgForPub, state) => new[] { new GoodbyeMessage() })
                                        .StopSaga()
                                        .Execute()
                                        );

            Func <Task> act = async() => { await _sagaMachine.Handle(new HelloMessage()).ConfigureAwait(false); };

            act.ShouldThrow <Exception>();

            //Assert
            _keyValueStoreMock.Verify(o => o.TrySetValue(It.IsAny <string>(), It.IsAny <TestState>(), It.IsAny <string>()), Times.Never);
            _keyValueStoreMock.Verify(kv => kv.Remove(It.IsAny <string>(), It.IsAny <string>()), Times.Never);
        }