Retry.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. public class Retry : AsyncEnumerableExTests
  12. {
  13. [Fact]
  14. public void Retry_Null()
  15. {
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Retry<int>(default));
  17. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Retry<int>(default, 1));
  18. Assert.Throws<ArgumentOutOfRangeException>(() => AsyncEnumerableEx.Retry<int>(Return42, -1));
  19. }
  20. [Fact]
  21. public async Task Retry1Async()
  22. {
  23. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  24. var res = xs.Retry();
  25. var e = res.GetAsyncEnumerator();
  26. await HasNextAsync(e, 1);
  27. await HasNextAsync(e, 2);
  28. await HasNextAsync(e, 3);
  29. await NoNextAsync(e);
  30. }
  31. [Fact]
  32. public async Task Retry2Async()
  33. {
  34. var ex = new InvalidOperationException("Bang!");
  35. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Concat(Throw<int>(ex));
  36. var res = xs.Retry();
  37. var e = res.GetAsyncEnumerator();
  38. await HasNextAsync(e, 1);
  39. await HasNextAsync(e, 2);
  40. await HasNextAsync(e, 3);
  41. await HasNextAsync(e, 1);
  42. await HasNextAsync(e, 2);
  43. await HasNextAsync(e, 3);
  44. await HasNextAsync(e, 1);
  45. await HasNextAsync(e, 2);
  46. await HasNextAsync(e, 3);
  47. }
  48. }
  49. }