Skip to content

Partially covered await foreach method with EnumeratorCancellation attribute set #1275

@ipetrushevskiy

Description

@ipetrushevskiy

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%

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingwith reproIssue with repro

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions