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

Coverage for awaiting ValueTasks #949

Merged

Conversation

alexthornton1
Copy link
Contributor

@alexthornton1 alexthornton1 commented Sep 12, 2020

For issue #907, first, I added a failing unit test, in two parts:

  • A new AsyncAwaitValueTaskStateMachine class in Samples.cs
    in coverlet.core.tests/Samples, in line with the existing
    AsyncAwaitStateMachine class, but returning a completed
    ValueTask instead of a completed Task.

  • A new test in CecilSymbolHelperTests.cs in
    coverlet.core.tests/Symbols that expects the "await" in
    that sample class not to have a branch in it.

After it failed, I updated CecilSymbolHelper to include
get_IsCompleted from System.Runtime.CompilerServices.ValueTaskAwaiter,
as suggested by @a-jackson in issue #907. The test passed.

One open question is whether it might be worth adding a
ValueTask-based analogue for the Instrumentation.AsyncAwait.cs
samples.

@dnfadmin
Copy link

dnfadmin commented Sep 12, 2020

CLA assistant check
All CLA requirements met.

@alexthornton1 alexthornton1 changed the title Fixing issue #907 Fixing issues #907 and #914 Sep 13, 2020
@alexthornton1
Copy link
Contributor Author

Also included in this branch is an attempted fix for issue #914, which was partly dependent on the fix for issue #907, anyway.

@alexthornton1
Copy link
Contributor Author

So, it turns out the fix for #914 works nicely in the Debug configuration, but not the Release configuration. The compiler-generated IL for await using is different in Release mode, so I'll need to adjust for that.

@MarcoRossignoli
Copy link
Collaborator

MarcoRossignoli commented Sep 13, 2020

Thanks for looking into the rabbit hole 😃 for tests please add also full coverage assertion like https://github.com/coverlet-coverage/coverlet/blob/master/test/coverlet.core.tests/Coverage/CoverageTests.SelectionStatements.cs#L18

You can use FunctionExecutor.RunInProcess to run/attach live during debugging and result.GenerateReport(show:true)

public static CoverageResult GenerateReport(this CoverageResult coverageResult, [CallerMemberName] string directory = "", bool show = false)
to get live report generator check

so I'll need to adjust for that.

Please add [WIP] to PR title if the work is in progress, so I'll review only if work is done, this kind of fix needs deeper look/testing so sorry in advance if I won't be super fast 🙇

@MarcoRossignoli MarcoRossignoli added the tenet-coverage Issue related to possible incorrect coverage label Sep 13, 2020
@alexthornton1 alexthornton1 changed the title Fixing issues #907 and #914 [WIP] Fixing issues #907 and #914 Sep 13, 2020
@alexthornton1
Copy link
Contributor Author

No worries. I had thought the work might be done, but I also thought there might be more to the story. I'll work on adding some higher-level tests and see where that leads. For now, I've changed the name of the PR to begin with [WIP]. I'll change it back when I think I'm done and we'll circle back again. Thanks for the response.

First, I added a failing unit test, in two parts:

* A new AsyncAwaitValueTaskStateMachine class in Samples.cs
  in coverlet.core.tests/Samples, in line with the existing
  AsyncAwaitStateMachine class, but returning a completed
  ValueTask instead of a completed Task.

* A new test in CecilSymbolHelperTests.cs in
  coverlet.core.tests/Symbols that expects the "await" in
  that sample class not to have a branch in it.

After it failed, I updated CecilSymbolHelper to include
get_IsCompleted from System.Runtime.CompilerServices.ValueTaskAwaiter,
as suggested by @a-jackson in issue coverlet-coverage#907.  The test passed.
So far, so good.

Additionally, coverage testing was added.  It was patterned from
the existing coverage testing for awaiting Tasks, with a few
differences:

(1) The Async() method calls an overload of MemoryStream's
    ReadAsync() that returns a ValueTask.

(2) ValueTask doesn't support ContinueWith(), so there's no coverage
    testing for calling ContinueWith().

(3) ValueTasks can be wrapped as Tasks, so there's coverage testing
    added for that.
@alexthornton1 alexthornton1 changed the title [WIP] Fixing issues #907 and #914 Coverage for awaiting ValueTasks (fixing issue #907) Sep 14, 2020
@alexthornton1
Copy link
Contributor Author

Let's pull these apart. Once I started adding coverage tests, the #907 fix worked fine, but #914 is a gnarlier beast than I had hoped. I'll still aim to attack it, but it seems clear it'll take longer, so no need to hold up the #907 fix -- which is what brought me here in the first place -- in the meantime. I moved #914 to another branch and will create a PR when I get a fuller solution together.

So this PR is now just a #907 fix with coverage testing. Happy to add whatever else is necessary; this has been fun so far.

Modified calls to GetAwaiter().GetResult() on a ValueTask<int> to use
"await" instead.

As it turns out, this is more of a stylistic change than anything else,
as we're not actually dependent on the result of the calls to the
various AsyncAwaitValueTask methods, since we're only interested in
assessing coverage, but it's a good change nonetheless.
Copy link
Collaborator

@MarcoRossignoli MarcoRossignoli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks a lot!

@MarcoRossignoli MarcoRossignoli changed the title Coverage for awaiting ValueTasks (fixing issue #907) Coverage for awaiting ValueTasks Oct 3, 2020
@MarcoRossignoli MarcoRossignoli merged commit c77b85c into coverlet-coverage:master Oct 3, 2020
@alexthornton1 alexthornton1 deleted the valuetask_await branch February 23, 2021 03:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tenet-coverage Issue related to possible incorrect coverage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants