public WhenMessageHandlerThrowsExceptionsDefinedByRetryPolicy()
        {
            var fixture = new Fixture()
                          .CustomizeConnectedProjectionIdentifiers();

            var exceptionSequence = fixture
                                    .CreateMany <RetryException>(2, 10)
                                    .ToArray <Exception>();

            _loggerMock           = new FakeLoggerFactory().ResolveLoggerMock <MessageHandlerWithExecutionTracking>();
            _projection           = fixture.Create <ConnectedProjectionIdentifier>();
            _handlerWithoutPolicy = new MessageHandlerWithExecutionTracking(
                _projection,
                _loggerMock.AsLogger(),
                exceptionSequence);

            _numberOfRetries          = exceptionSequence.Length;
            _numberOfExpectedAttempts = Times.Exactly(1 + _numberOfRetries);
            _initialWait = TimeSpan.FromMilliseconds(fixture.CreatePositive <int>());
            _messages    = fixture.CreateMany <StreamMessage>().ToList();

            new LinearBackOff <RetryException>(_numberOfRetries, _initialWait)
            .ApplyOn(_handlerWithoutPolicy)
            .HandleAsync(_messages, Mock.Of <IStreamGapStrategy>(), CancellationToken.None)
            .GetAwaiter()
            .GetResult();
        }
        public WhenApplyingALinearBackoffPolicyOnAHandler()
        {
            _fixture = new Fixture()
                       .CustomizeConnectedProjectionIdentifiers();

            _handlerWithoutPolicy = new MessageHandlerWithExecutionTracking(
                _fixture.Create <ConnectedProjectionIdentifier>(),
                new Mock <ILogger>().Object);

            var numberOfRetries = _fixture.CreatePositive <int>() + 1;

            _numberOfExpectedAttempts = Times.Once();
            var initialWait         = TimeSpan.FromMilliseconds(_fixture.CreatePositive <int>());
            var linearBackoffPolicy = new LinearBackOff <RetryException>(numberOfRetries, initialWait);

            _sut = linearBackoffPolicy.ApplyOn(_handlerWithoutPolicy);
        }
        public WhenMessageHandlerThrowsAnExceptionThatWasNotDefinedToRetry()
        {
            var fixture = new Fixture()
                          .CustomizeConnectedProjectionIdentifiers();

            _handlerWithoutPolicy = new MessageHandlerWithExecutionTracking(
                fixture.Create <ConnectedProjectionIdentifier>(),
                new FakeLoggerFactory().ResolveLoggerMock <MessageHandlerWithExecutionTracking>().AsLogger(),
                new DoNotRetryException());

            var numberOfRetries = fixture.CreatePositive <int>() + 1;
            var initialWait     = TimeSpan.FromMilliseconds(fixture.CreatePositive <int>());

            _messages = fixture.CreateMany <StreamMessage>().ToList();

            _sut = new LinearBackOff <RetryException>(numberOfRetries, initialWait)
                   .ApplyOn(_handlerWithoutPolicy);
        }
        public WhenMessageHandlerThrowsAnExceptionThatWasNotDefinedToRetryAfterRetrying()
        {
            var fixture = new Fixture()
                          .CustomizeConnectedProjectionIdentifiers();

            _exceptionSequence = new Exception[] { new RetryException(), new DoNotRetryException() };

            _loggerMock           = new FakeLoggerFactory().ResolveLoggerMock <MessageHandlerWithExecutionTracking>();
            _projection           = fixture.Create <ConnectedProjectionIdentifier>();
            _handlerWithoutPolicy = new MessageHandlerWithExecutionTracking(
                _projection,
                _loggerMock.AsLogger(),
                _exceptionSequence);

            var numberOfRetries = _exceptionSequence.Count(exception => exception is RetryException);

            _numberOfExpectedAttempts = Times.Exactly(1 + numberOfRetries);
            _initialWait = TimeSpan.FromMilliseconds(fixture.CreatePositive <int>());
            _messages    = fixture.CreateMany <StreamMessage>().ToList();

            _sut = new LinearBackOff <RetryException>(numberOfRetries, _initialWait)
                   .ApplyOn(_handlerWithoutPolicy);
        }
        public WhenMessageHandlerThrowsTheExceptionDefinedInThePolicyMoreThanTheNumberOfRetries()
        {
            var fixture = new Fixture()
                          .CustomizeConnectedProjectionIdentifiers();

            var exceptionSequence = fixture
                                    .CreateMany <RetryException>(2, 10)
                                    .ToArray <Exception>();

            _loggerMock           = new FakeLoggerFactory().ResolveLoggerMock <MessageHandlerWithExecutionTracking>();
            _projection           = fixture.Create <ConnectedProjectionIdentifier>();
            _handlerWithoutPolicy = new MessageHandlerWithExecutionTracking(
                _projection,
                _loggerMock.AsLogger(),
                exceptionSequence);

            _numberOfRetries          = exceptionSequence.Length - 1;
            _numberOfExpectedAttempts = Times.Exactly(1 + _numberOfRetries);
            _initialWait = TimeSpan.FromMilliseconds(fixture.CreatePositive <int>());
            _messages    = fixture.CreateMany <StreamMessage>().ToList();

            _sut = new LinearBackOff <RetryException>(_numberOfRetries, _initialWait)
                   .ApplyOn(_handlerWithoutPolicy);
        }