-
Notifications
You must be signed in to change notification settings - Fork 390
Closed
Labels
Description
Hello
got issue similar to that one
Updated reproduction class:
public class AwaitForeachReproduction
{
public async Task<int> Execute(CancellationToken token)
{
int sum = 0;
await foreach (int result in AsyncEnumerable(token))
{
sum += result;
}
return sum;
}
private async IAsyncEnumerable<int> AsyncEnumerable([EnumeratorCancellation] CancellationToken cancellationToken)
{
for (int i = 0; i < 100; i++)
{
await Task.Delay(10, cancellationToken);
yield return i;
}
}
}
Test Case for it:
public class AwaitForeachReproductionFixture
{
[Test]
public async Task Execute_Should_Work()
{
// Arrange
var sut = new AwaitForeachReproduction();
// Act
var result = await sut.Execute(CancellationToken.None);
// Assert
Assert.AreEqual(result, 4950);
}
[Test]
public void Execute_Should_Be_Canceled()
{
// Arrange
var sut = new AwaitForeachReproduction();
var cts = new CancellationTokenSource();
// Act
var resultAsync = sut.Execute(cts.Token);
cts.Cancel();
// Assert
Assert.ThrowsAsync<TaskCanceledException>(async () => await resultAsync);
}
}
Reported BRANCH coverage is 75%
Without EnumeratorCancellation
attribute usage it is 100%