ForEachAsync.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. public class ForEachAsync : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public async Task ForEachAsync_Null()
  15. {
  16. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync<int>(null, x => { }));
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync<int>(AsyncEnumerable.Return(42), default(Action<int>)));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync<int>(null, (x, i) => { }));
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync<int>(AsyncEnumerable.Return(42), default(Action<int, int>)));
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync<int>(null, x => { }, CancellationToken.None));
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync<int>(AsyncEnumerable.Return(42), default(Action<int>), CancellationToken.None));
  22. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync<int>(null, (x, i) => { }, CancellationToken.None));
  23. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync<int>(AsyncEnumerable.Return(42), default(Action<int, int>), CancellationToken.None));
  24. }
  25. [Fact]
  26. public void ForEachAsync1()
  27. {
  28. var sum = 0;
  29. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  30. xs.ForEachAsync(x => sum += x).Wait(WaitTimeoutMs);
  31. Assert.Equal(10, sum);
  32. }
  33. [Fact]
  34. public void ForEachAsync2()
  35. {
  36. var sum = 0;
  37. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  38. xs.ForEachAsync((x, i) => sum += x * i).Wait(WaitTimeoutMs);
  39. Assert.Equal(1 * 0 + 2 * 1 + 3 * 2 + 4 * 3, sum);
  40. }
  41. [Fact]
  42. public void ForEachAsync3()
  43. {
  44. var ex = new Exception("Bang");
  45. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  46. AssertThrows<Exception>(() => xs.ForEachAsync(x => { throw ex; }).Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  47. }
  48. [Fact]
  49. public void ForEachAsync4()
  50. {
  51. var ex = new Exception("Bang");
  52. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  53. AssertThrows<Exception>(() => xs.ForEachAsync((x, i) => { throw ex; }).Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  54. }
  55. [Fact]
  56. public void ForEachAsync5()
  57. {
  58. var ex = new Exception("Bang");
  59. var xs = AsyncEnumerable.Throw<int>(ex);
  60. AssertThrows<Exception>(() => xs.ForEachAsync(x => { throw ex; }).Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  61. }
  62. [Fact]
  63. public void ForEachAsync6()
  64. {
  65. var ex = new Exception("Bang");
  66. var xs = AsyncEnumerable.Throw<int>(ex);
  67. AssertThrows<Exception>(() => xs.ForEachAsync((x, i) => { throw ex; }).Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  68. }
  69. }
  70. }