AsyncTests.Exceptions.cs 1.6 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 partial class AsyncTests
  12. {
  13. [Fact]
  14. public void Retry_Null()
  15. {
  16. AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Retry<int>(default(IAsyncEnumerable<int>)));
  17. AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Retry<int>(default(IAsyncEnumerable<int>), 1));
  18. AssertThrows<ArgumentOutOfRangeException>(() => AsyncEnumerableEx.Retry<int>(AsyncEnumerable.Return(42), -1));
  19. }
  20. [Fact]
  21. public void Retry1()
  22. {
  23. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  24. var res = xs.Retry();
  25. var e = res.GetAsyncEnumerator();
  26. HasNext(e, 1);
  27. HasNext(e, 2);
  28. HasNext(e, 3);
  29. NoNext(e);
  30. }
  31. [Fact]
  32. public void Retry2()
  33. {
  34. var ex = new InvalidOperationException("Bang!");
  35. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Concat(AsyncEnumerable.Throw<int>(ex));
  36. var res = xs.Retry();
  37. var e = res.GetAsyncEnumerator();
  38. HasNext(e, 1);
  39. HasNext(e, 2);
  40. HasNext(e, 3);
  41. HasNext(e, 1);
  42. HasNext(e, 2);
  43. HasNext(e, 3);
  44. HasNext(e, 1);
  45. HasNext(e, 2);
  46. HasNext(e, 3);
  47. }
  48. }
  49. }