Exemple #1
0
        public async Task Retry_NoException()
        {
            var count  = 0;
            var result = await Retry.Async(() =>
            {
                ++count;
                return(42);
            })
                         .WithAttempts(int.MaxValue)
                         .WithDelay(TimeSpan.FromMilliseconds(100))
                         .When <Exception>();

            Assert.AreEqual(42, result);
            Assert.AreEqual(1, count);
        }
Exemple #2
0
 public async Task Retry_ReachTheMaximalNumberOfAttempts()
 {
     var count  = 0;
     var result = await Retry.Async(() =>
     {
         ++count;
         if (count <= 3)
         {
             throw new IOException();
         }
         return(42);
     })
                  .WithAttempts(3)
                  .WithDelay(TimeSpan.FromMilliseconds(10))
                  .When <IOException>();
 }
Exemple #3
0
        public async Task Retry_RetryWhenTheExceptionIsAssignable()
        {
            var count  = 0;
            var result = await Retry.Async(() =>
            {
                ++count;
                if (count <= 2)
                {
                    throw new FileNotFoundException();
                }
                return(42);
            })
                         .WithAttempts(int.MaxValue)
                         .WithDelay(TimeSpan.FromMilliseconds(10))
                         .When <IOException>();

            Assert.AreEqual(42, result);
            Assert.AreEqual(3, count);
        }
Exemple #4
0
        public async Task Retry_FinishOnLastAttempt()
        {
            var count  = 0;
            var result = await Retry.Async(() =>
            {
                ++count;
                if (count <= 2)
                {
                    throw new IOException();
                }
                return(42);
            })
                         .WithAttempts(3)
                         .WithDelay(TimeSpan.FromMilliseconds(10))
                         .When <IOException>();

            Assert.AreEqual(42, result);
            Assert.AreEqual(3, count);
        }
Exemple #5
0
        public async Task Retry_CustomExceptionPredicateIsMatched()
        {
            var count  = 0;
            var result = await Retry.Async(() =>
            {
                ++count;
                if (count <= 1)
                {
                    throw new IOException("test");
                }
                return(42);
            })
                         .WithAttempts(int.MaxValue)
                         .WithDelay(TimeSpan.FromMilliseconds(10))
                         .WhenExactly <IOException>(e => e.Message == "test");

            Assert.AreEqual(2, count);
            Assert.AreEqual(42, result);
        }
Exemple #6
0
 private async void FileWatcherOnCreated(object sender, FileSystemEventArgs e)
 {
     if (!IsEntityEvent(e))
     {
         return; // skip this event
     }
     try
     {
         await Retry
         .Async(() => CheckAndAdd(e.FullPath))
         .WithAttempts(MaxRetryCount)
         .WithDelay(RetryDelay)
         .WithCancellationToken(Cancellation.Token)
         .WhenExactly <IOException>();
     }
     catch (OperationCanceledException)
     {
     }
     catch (IOException ex)
     {
         Logger.Debug(ex, "All attempts have failed.");
     }
 }
Exemple #7
0
        public async Task Retry_ExceptionHasDifferentType()
        {
            var count = 0;

            try
            {
                await Retry.Async(() =>
                {
                    ++count;
                    if (count <= 1)
                    {
                        throw new FileNotFoundException();
                    }
                    return(42);
                })
                .WithAttempts(int.MaxValue)
                .WithDelay(TimeSpan.FromMilliseconds(10))
                .WhenExactly <IOException>();
            }
            finally
            {
                Assert.AreEqual(1, count);
            }
        }
Exemple #8
0
        public async Task Retry_ExceptionCannotBeAssignable()
        {
            var count = 0;

            try
            {
                await Retry.Async(() =>
                {
                    ++count;
                    if (count <= 1)
                    {
                        throw new Exception();
                    }
                    return(42);
                })
                .WithAttempts(int.MaxValue)
                .WithDelay(TimeSpan.FromMilliseconds(10))
                .When <IOException>();
            }
            finally
            {
                Assert.AreEqual(1, count);
            }
        }