GivenExponentialRetry.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. using Microsoft.DotNet.Cli.Utils;
  4. namespace Microsoft.DotNet.Tests
  5. {
  6. public class GivenExponentialRetry : SdkTest
  7. {
  8. public GivenExponentialRetry(ITestOutputHelper log) : base(log)
  9. {
  10. }
  11. [Fact]
  12. public async Task ItReturnsOnSuccess()
  13. {
  14. var retryCount = 0;
  15. Func<Task<string>> action = () =>
  16. {
  17. retryCount++;
  18. return Task.FromResult("done");
  19. };
  20. var res = await ExponentialRetry.ExecuteWithRetryOnFailure<string>(action);
  21. retryCount.Should().Be(1);
  22. }
  23. [Fact(Skip = "Don't want to retry on exceptions")]
  24. public async Task ItRetriesOnError()
  25. {
  26. var retryCount = 0;
  27. Func<Task<string>> action = () =>
  28. {
  29. retryCount++;
  30. throw new Exception();
  31. };
  32. await Assert.ThrowsAsync<AggregateException>(async () => await ExponentialRetry.ExecuteWithRetryOnFailure<string>(action, 2, timer: () => ExponentialRetry.Timer(ExponentialRetry.TestingIntervals)));
  33. retryCount.Should().Be(2);
  34. }
  35. }
  36. }