Skip to content

Commit

Permalink
Fix CancellationToken overloads of ForEachAsync.
Browse files Browse the repository at this point in the history
  • Loading branch information
bartdesmet committed Feb 22, 2019
1 parent 89b9338 commit 1d87e7a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public async Task ForEachAsync_Async_Null()
{
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync<int>(default, x => Task.CompletedTask));
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync(Return42, default(Func<int, Task>)));
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync(default, (int x, int i) => Task.CompletedTask));
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync<int>(default, (x, i) => Task.CompletedTask));
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync(Return42, default(Func<int, int, Task>)));

await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync<int>(default, x => Task.CompletedTask, CancellationToken.None));
Expand Down Expand Up @@ -122,14 +122,9 @@ public async Task ForEachAsync_Async_Indexed_Throws_Action()
[Fact]
public async Task ForEachAsync_Cancel_Async_Cancel_Null()
{
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync(default, (int x, CancellationToken ct) => Task.CompletedTask));
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync(Return42, default(Func<int, CancellationToken, Task>)));
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync(default, (int x, int i, CancellationToken ct) => Task.CompletedTask));
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync(Return42, default(Func<int, int, CancellationToken, Task>)));

await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync(default, (int x, CancellationToken ct) => Task.CompletedTask, CancellationToken.None));
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync(Return42, default(Func<int, CancellationToken, Task>), CancellationToken.None));
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync(default, (int x, int i, CancellationToken ct) => Task.CompletedTask, CancellationToken.None));
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync<int>(default, (x, i, ct) => Task.CompletedTask, CancellationToken.None));
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ForEachAsync(Return42, default(Func<int, int, CancellationToken, Task>), CancellationToken.None));
}

Expand All @@ -139,7 +134,7 @@ public async Task ForEachAsync_Cancel_Async_Cancel_Simple()
var sum = 0;
var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();

await xs.ForEachAsync((int x, CancellationToken ct) => { sum += x; return Task.CompletedTask; });
await xs.ForEachAsync((int x, CancellationToken ct) => { sum += x; return Task.CompletedTask; }, CancellationToken.None);
Assert.Equal(10, sum);
}

Expand All @@ -149,7 +144,7 @@ public async Task ForEachAsync_Cancel_Async_Cancel_Indexed()
var sum = 0;
var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();

await xs.ForEachAsync((x, i, ct) => { sum += x * i; return Task.CompletedTask; });
await xs.ForEachAsync((x, i, ct) => { sum += x * i; return Task.CompletedTask; }, CancellationToken.None);
Assert.Equal(1 * 0 + 2 * 1 + 3 * 2 + 4 * 3, sum);
}

Expand All @@ -159,7 +154,7 @@ public async Task ForEachAsync_Cancel_Async_Cancel_Throws_Action()
var ex = new Exception("Bang");
var xs = Throw<int>(ex);

await AssertThrowsAsync(xs.ForEachAsync((int x, CancellationToken ct) => Task.FromException(ex)), ex);
await AssertThrowsAsync(xs.ForEachAsync((int x, CancellationToken ct) => Task.FromException(ex), CancellationToken.None), ex);
}

[Fact]
Expand All @@ -168,7 +163,7 @@ public async Task ForEachAsync_Cancel_Async_Cancel_Indexed_Throws_Action()
var ex = new Exception("Bang");
var xs = Throw<int>(ex);

await AssertThrowsAsync(xs.ForEachAsync((x, i, ct) => Task.FromException(ex)), ex);
await AssertThrowsAsync(xs.ForEachAsync((x, i, ct) => Task.FromException(ex), CancellationToken.None), ex);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static async Task Core(IAsyncEnumerable<TSource> _source, Func<TSource, Task> _a
}
}

public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> action, CancellationToken cancellationToken = default)
public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> action, CancellationToken cancellationToken)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand Down Expand Up @@ -111,7 +111,7 @@ static async Task Core(IAsyncEnumerable<TSource> _source, Func<TSource, int, Tas
}
}

public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, Task> action, CancellationToken cancellationToken = default)
public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, Task> action, CancellationToken cancellationToken)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand Down

0 comments on commit 1d87e7a

Please sign in to comment.