Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partially covered await foreach method with EnumeratorCancellation attribute set #1275

Closed
ipetrushevskiy opened this issue Jan 6, 2022 · 2 comments · Fixed by #1277
Closed
Assignees
Labels
bug Something isn't working with repro Issue with repro

Comments

@ipetrushevskiy
Copy link

ipetrushevskiy commented Jan 6, 2022

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%

@daveMueller daveMueller added untriaged To be investigated with repro Issue with repro labels Jan 6, 2022
@daveMueller
Copy link
Collaborator

Thanks for reporting this.

@daveMueller daveMueller added bug Something isn't working and removed untriaged To be investigated labels Jan 7, 2022
@daveMueller
Copy link
Collaborator

I will work on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working with repro Issue with repro
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants