Exemplo n.º 1
0
        public async Task ShouldNotRetryOnSuccess()
        {
            var retryLogic = new AsyncRetryLogic(TimeSpan.FromSeconds(5), null);
            var work       = CreateFailingWork(5);

            var result = await retryLogic.RetryAsync(() => work.Work(null));

            result.Should().Be(5);
            work.Invocations.Should().Be(1);
        }
Exemplo n.º 2
0
        public async Task ShouldRetryOnTransientErrors(Exception error)
        {
            var retryLogic = new AsyncRetryLogic(TimeSpan.FromSeconds(5), null);
            var work       = CreateFailingWork(5, error);

            var result = await retryLogic.RetryAsync(() => work.Work(null));

            result.Should().Be(5);
            work.Invocations.Should().Be(2);
        }
Exemplo n.º 3
0
        public async Task ShouldNotRetryOnNonTransientErrors(Exception error)
        {
            var retryLogic = new AsyncRetryLogic(TimeSpan.FromSeconds(5), null);
            var work       = CreateFailingWork(0, error);

            var exc = await Record.ExceptionAsync(() => retryLogic.RetryAsync(() => work.Work(null)));

            exc.Should().Be(error);
            work.Invocations.Should().Be(1);
        }
Exemplo n.º 4
0
        public async Task ShouldRetryAtLeastTwice()
        {
            var error      = new TransientException("code", "message");
            var logger     = new Mock <ILogger>();
            var retryLogic = new AsyncRetryLogic(TimeSpan.FromSeconds(1), logger.Object);
            var work       = CreateFailingWork(TimeSpan.FromSeconds(2), 1, error);

            var result = await retryLogic.RetryAsync(() => work.Work(null));

            result.Should().Be(1);
            logger.Verify(x => x.Warn(error,
                                      It.Is <string>(s => s.StartsWith("Transaction failed and will be retried in"))),
                          Times.Once);
        }
Exemplo n.º 5
0
        public async Task ShouldLogRetries(int errorCount)
        {
            var error      = new TransientException("code", "message");
            var logger     = new Mock <ILogger>();
            var retryLogic = new AsyncRetryLogic(TimeSpan.FromMinutes(1), logger.Object);
            var work       = CreateFailingWork(1,
                                               Enumerable.Range(1, errorCount).Select(x => error).Cast <Exception>().ToArray());

            var result = await retryLogic.RetryAsync(() => work.Work(null));

            result.Should().Be(1);
            logger.Verify(x => x.Warn(error,
                                      It.Is <string>(s => s.StartsWith("Transaction failed and will be retried in"))),
                          Times.Exactly(errorCount));
        }
Exemplo n.º 6
0
        public async Task ShouldThrowServiceUnavailableWhenRetriesTimedOut()
        {
            var errorCount = 3;
            var exceptions = Enumerable.Range(1, errorCount).Select(i => new TransientException($"{i}", $"{i}"))
                             .Cast <Exception>().ToArray();
            var logger     = new Mock <ILogger>();
            var retryLogic = new AsyncRetryLogic(TimeSpan.FromSeconds(2), logger.Object);
            var work       = CreateFailingWork(TimeSpan.FromSeconds(1), 1, exceptions);

            var exc = await Record.ExceptionAsync(() => retryLogic.RetryAsync(() => work.Work(null)));

            exc.Should().BeOfType <ServiceUnavailableException>()
            .Which.InnerException.Should().BeOfType <AggregateException>()
            .Which.InnerExceptions.Should().BeSubsetOf(exceptions);
        }