ForEachAsync.cs 3.5 KB

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