Skip to content

Coverage for "await foreach" loops and compiler-generated async iterators (issue #1104) #1107

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

Merged
merged 4 commits into from
Mar 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
293 changes: 288 additions & 5 deletions src/coverlet.core/Symbols/CecilSymbolHelper.cs

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions test/coverlet.core.tests/Coverage/CoverageTests.AsyncForeach.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

using Coverlet.Core.Samples.Tests;
using Coverlet.Tests.Xunit.Extensions;
using Xunit;

namespace Coverlet.Core.Tests
{
public partial class CoverageTests
{
[Fact]
public void AsyncForeach()
{
string path = Path.GetTempFileName();
try
{
FunctionExecutor.Run(async (string[] pathSerialize) =>
{
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<AsyncForeach>(instance =>
{
int res = ((ValueTask<int>)instance.SumWithATwist(AsyncEnumerable.Range(1, 5))).GetAwaiter().GetResult();
res += ((ValueTask<int>)instance.Sum(AsyncEnumerable.Range(1, 3))).GetAwaiter().GetResult();
res += ((ValueTask<int>)instance.SumEmpty()).GetAwaiter().GetResult();

return Task.CompletedTask;
}, persistPrepareResultToFile: pathSerialize[0]);
return 0;
}, new string[] { path });

TestInstrumentationHelper.GetCoverageResult(path)
.Document("Instrumentation.AsyncForeach.cs")
.AssertLinesCovered(BuildConfiguration.Debug,
// SumWithATwist(IAsyncEnumerable<int>)
// Apparently due to entering and exiting the async state machine, line 17
// (the top of an "await foreach" loop) is reached three times *plus* twice
// per loop iteration. So, in this case, with five loop iterations, we end
// up with 3 + 5 * 2 = 13 hits.
(14, 1), (15, 1), (17, 13), (18, 5), (19, 5), (20, 5), (21, 5), (22, 5),
(24, 0), (25, 0), (26, 0), (27, 5), (29, 1), (30, 1),
// Sum(IAsyncEnumerable<int>)
(34, 1), (35, 1), (37, 9), (38, 3), (39, 3), (40, 3), (42, 1), (43, 1),
// SumEmpty()
(47, 1), (48, 1), (50, 3), (51, 0), (52, 0), (53, 0), (55, 1), (56, 1)
)
.AssertBranchesCovered(BuildConfiguration.Debug,
// SumWithATwist(IAsyncEnumerable<int>)
(17, 2, 1), (17, 3, 5), (19, 0, 5), (19, 1, 0),
// Sum(IAsyncEnumerable<int>)
(37, 0, 1), (37, 1, 3),
// SumEmpty()
// If we never entered the loop, that's a branch not taken, which is
// what we want to see.
(50, 0, 1), (50, 1, 0)
)
.ExpectedTotalNumberOfBranches(BuildConfiguration.Debug, 4);
}
finally
{
File.Delete(path);
}
}
}
}
52 changes: 52 additions & 0 deletions test/coverlet.core.tests/Coverage/CoverageTests.AsyncIterator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

using Coverlet.Core.Samples.Tests;
using Coverlet.Tests.Xunit.Extensions;
using Xunit;

namespace Coverlet.Core.Tests
{
public partial class CoverageTests
{
[Fact]
public void AsyncIterator()
{
string path = Path.GetTempFileName();
try
{
FunctionExecutor.Run(async (string[] pathSerialize) =>
{
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<AsyncIterator>(instance =>
{
int res = ((Task<int>)instance.Issue1104_Repro()).GetAwaiter().GetResult();

return Task.CompletedTask;
}, persistPrepareResultToFile: pathSerialize[0]);
return 0;
}, new string[] { path });

TestInstrumentationHelper.GetCoverageResult(path)
.Document("Instrumentation.AsyncIterator.cs")
.AssertLinesCovered(BuildConfiguration.Debug,
// Issue1104_Repro()
(14, 1), (15, 1), (17, 203), (18, 100), (19, 100), (20, 100), (22, 1), (23, 1),
// CreateSequenceAsync()
(26, 1), (27, 202), (28, 100), (29, 100), (30, 100), (31, 100), (32, 1)
)
.AssertBranchesCovered(BuildConfiguration.Debug,
// Issue1104_Repro(),
(17, 0, 1), (17, 1, 100),
// CreateSequenceAsync()
(27, 0, 1), (27, 1, 100)
)
.ExpectedTotalNumberOfBranches(BuildConfiguration.Debug, 2);
}
finally
{
File.Delete(path);
}
}
}
}
58 changes: 58 additions & 0 deletions test/coverlet.core.tests/Samples/Instrumentation.AsyncForeach.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Remember to use full name because adding new using directives change line numbers

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Coverlet.Core.Samples.Tests
{
public class AsyncForeach
{
async public ValueTask<int> SumWithATwist(IAsyncEnumerable<int> ints)
{
int sum = 0;

await foreach (int i in ints)
{
if (i > 0)
{
sum += i;
}
else
{
sum = 0;
}
}

return sum;
}


async public ValueTask<int> Sum(IAsyncEnumerable<int> ints)
{
int sum = 0;

await foreach (int i in ints)
{
sum += i;
}

return sum;
}


async public ValueTask<int> SumEmpty()
{
int sum = 0;

await foreach (int i in AsyncEnumerable.Empty<int>())
{
sum += i;
}

return sum;
}
}
}
34 changes: 34 additions & 0 deletions test/coverlet.core.tests/Samples/Instrumentation.AsyncIterator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Remember to use full name because adding new using directives change line numbers

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Coverlet.Core.Samples.Tests
{
public class AsyncIterator
{
async public Task<int> Issue1104_Repro()
{
int sum = 0;

await foreach (int result in CreateSequenceAsync())
{
sum += result;
}

return sum;
}

async private IAsyncEnumerable<int> CreateSequenceAsync()
{
for (int i = 0; i < 100; ++i)
{
await Task.CompletedTask;
yield return i;
}
}
}
}
45 changes: 45 additions & 0 deletions test/coverlet.core.tests/Samples/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,51 @@ async public ValueTask AsyncAwait()
}
}

public class AwaitForeachStateMachine
{
async public ValueTask AsyncAwait(IAsyncEnumerable<int> ints)
{
await foreach (int i in ints)
{
await default(ValueTask);
}
}
}

public class AwaitForeachStateMachine_WithBranches
{
async public ValueTask<int> SumWithATwist(IAsyncEnumerable<int> ints)
{
int sum = 0;

await foreach (int i in ints)
{
if (i > 0)
{
sum += i;
}
else
{
sum = 0;
}
}

return sum;
}
}

public class AsyncIteratorStateMachine
{
async public IAsyncEnumerable<int> CreateSequenceAsync()
{
for (int i = 0; i < 100; ++i)
{
await Task.CompletedTask;
yield return i;
}
}
}

[ExcludeFromCoverage]
public class ClassExcludedByCoverletCodeCoverageAttr
{
Expand Down
69 changes: 69 additions & 0 deletions test/coverlet.core.tests/Symbols/CecilSymbolHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,75 @@ public void GetBranchPoints_IgnoresBranchesIn_AsyncAwaitValueTaskStateMachine()
Assert.Empty(points);
}

[Fact]
public void GetBranchPoints_IgnoresMostBranchesIn_AwaitForeachStateMachine()
{
// arrange
var nestedName = typeof(AwaitForeachStateMachine).GetNestedTypes(BindingFlags.NonPublic).First().Name;
var type = _module.Types.FirstOrDefault(x => x.FullName == typeof(AwaitForeachStateMachine).FullName);
var nestedType = type.NestedTypes.FirstOrDefault(x => x.FullName.EndsWith(nestedName));
var method = nestedType.Methods.First(x => x.FullName.EndsWith("::MoveNext()"));

// act
var points = _cecilSymbolHelper.GetBranchPoints(method);

// assert
// We do expect there to be a two-way branch (stay in the loop or not?) on
// the line containing "await foreach".
Assert.NotNull(points);
Assert.Equal(2, points.Count());
Assert.Equal(points[0].Offset, points[1].Offset);
Assert.Equal(204, points[0].StartLine);
Assert.Equal(204, points[1].StartLine);
}

[Fact]
public void GetBranchPoints_IgnoresMostBranchesIn_AwaitForeachStateMachine_WithBranchesWithinIt()
{
// arrange
var nestedName = typeof(AwaitForeachStateMachine_WithBranches).GetNestedTypes(BindingFlags.NonPublic).First().Name;
var type = _module.Types.FirstOrDefault(x => x.FullName == typeof(AwaitForeachStateMachine_WithBranches).FullName);
var nestedType = type.NestedTypes.FirstOrDefault(x => x.FullName.EndsWith(nestedName));
var method = nestedType.Methods.First(x => x.FullName.EndsWith("::MoveNext()"));

// act
var points = _cecilSymbolHelper.GetBranchPoints(method);

// assert
// We do expect there to be four branch points (two places where we can branch
// two ways), one being the "stay in the loop or not?" branch on the line
// containing "await foreach" and the other being the "if" statement inside
// the loop.
Assert.NotNull(points);
Assert.Equal(4, points.Count());
Assert.Equal(points[0].Offset, points[1].Offset);
Assert.Equal(points[2].Offset, points[3].Offset);
Assert.Equal(219, points[0].StartLine);
Assert.Equal(219, points[1].StartLine);
Assert.Equal(217, points[2].StartLine);
Assert.Equal(217, points[3].StartLine);
}

[Fact]
public void GetBranchesPoints_IgnoresExtraBranchesIn_AsyncIteratorStateMachine()
{
// arrange
var nestedName = typeof(AsyncIteratorStateMachine).GetNestedTypes(BindingFlags.NonPublic).First().Name;
var type = _module.Types.FirstOrDefault(x => x.FullName == typeof(AsyncIteratorStateMachine).FullName);
var nestedType = type.NestedTypes.FirstOrDefault(x => x.FullName.EndsWith(nestedName));
var method = nestedType.Methods.First(x => x.FullName.EndsWith("::MoveNext()"));

// act
var points = _cecilSymbolHelper.GetBranchPoints(method);

// assert
// We do expect the "for" loop to be a branch with two branch points, but that's it.
Assert.NotNull(points);
Assert.Equal(2, points.Count());
Assert.Equal(237, points[0].StartLine);
Assert.Equal(237, points[1].StartLine);
}

[Fact]
public void GetBranchPoints_ExceptionFilter()
{
Expand Down
6 changes: 4 additions & 2 deletions test/coverlet.core.tests/coverlet.core.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@

<ItemGroup>
<!--For test TestInstrument_NetstandardAwareAssemblyResolver_PreserveCompilationContext-->
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<!--For test issue 809 https://github.com/coverlet-coverage/coverlet/issues/809-->
<PackageReference Include="LinqKit.Microsoft.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="LinqKit.Microsoft.EntityFrameworkCore" Version="5.0.23" />
<!--To test issue 1104 https://github.com/coverlet-coverage/coverlet/issues/1104-->
<PackageReference Include="System.Linq.Async" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down