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

Replace public async void with public async Task for XUnit #2948

Merged
merged 2 commits into from
Nov 5, 2024

Conversation

MichelZ
Copy link
Contributor

@MichelZ MichelZ commented Nov 1, 2024

Fixes #2947

@MichelZ
Copy link
Contributor Author

MichelZ commented Nov 1, 2024

@dotnet-policy-service agree

Copy link
Contributor

@benrr101 benrr101 left a comment

Choose a reason for hiding this comment

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

Thanks for both reporting the issue and fixing the issue :D I think the likelihood that we'll upgrade xunit versions anytime soon is sorta slim - iirc there's a dependency issue with a library provided by the dotnet team, that won't work with a newer version. It's been like 7 months since I looked at that, so it might not be a concern anymore. But, regardless, this is the proper way to do async tests, so we should be doing this.

I'll kick off a CI build, and assuming it passes without issue (no reason it shouldn't), we'll get it merged 🚢

@benrr101
Copy link
Contributor

benrr101 commented Nov 1, 2024

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@benrr101 benrr101 added this to the 6.0-preview3 milestone Nov 1, 2024
@benrr101 benrr101 added the ➕ Code Health Issues/PRs that are targeted to source code quality improvements. label Nov 1, 2024
Copy link

codecov bot commented Nov 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 72.29%. Comparing base (699f6ab) to head (0fb95ae).
Report is 30 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2948      +/-   ##
==========================================
+ Coverage   72.23%   72.29%   +0.06%     
==========================================
  Files         291      288       -3     
  Lines       59769    59660     -109     
==========================================
- Hits        43172    43131      -41     
+ Misses      16597    16529      -68     
Flag Coverage Δ
addons 92.58% <ø> (-0.33%) ⬇️
netcore 75.38% <ø> (-0.42%) ⬇️
netfx 70.69% <ø> (+0.09%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@edwardneal edwardneal left a comment

Choose a reason for hiding this comment

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

Thanks for this - I didn't realise we were using async void anywhere.

I can see a bundle of other async void tests when I search for it in the solution. I can submit a PR to your branch with them if you want, unless they were excluded deliberately?

@MichelZ
Copy link
Contributor Author

MichelZ commented Nov 1, 2024

Hmm. I upgraded XUnit and it gave Warnings about these. I‘m not sure why it wouldn‘t warn about the others, but of course I/we can change them as well

@MichelZ
Copy link
Contributor Author

MichelZ commented Nov 2, 2024

I think the reason the others are not flagged is that they don't use the [Fact] or [Theory] attributes, but the [ConditionalTheory]. The analyzer probably ignores those. I'll update them as well

@MichelZ
Copy link
Contributor Author

MichelZ commented Nov 2, 2024

Done.

There are also async void's in the BenchmarkRunners for Benchmark.NET, I'm not too familiar with Benchmark.NET, so this might be on purpose? If not, I can also fix them in a separate PR.

Additionally, there is one async void in SNIPacket.cs here:

public async void WriteToStreamAsync(Stream stream, SNIAsyncCallback callback, SNIProviders provider)

Not sure if that needs fixing, too (in another PR)

@MichelZ
Copy link
Contributor Author

MichelZ commented Nov 2, 2024

Thanks for both reporting the issue and fixing the issue :D I think the likelihood that we'll upgrade xunit versions anytime soon is sorta slim - iirc there's a dependency issue with a library provided by the dotnet team, that won't work with a newer version. It's been like 7 months since I looked at that, so it might not be a concern anymore. But, regardless, this is the proper way to do async tests, so we should be doing this.

I'll kick off a CI build, and assuming it passes without issue (no reason it shouldn't), we'll get it merged 🚢

You mean this comment?
#2379 (comment)

Wasn't this fixed by upgrading to .NET8 now? (.NET 6 was removed)
Do you think I can add a PR to upgrade to the latest versions?

@edwardneal
Copy link
Contributor

Thanks. I don't think the BenchmarkRunners will make much difference, but it's worth testing with/without.

Additionally, there is one async void in SNIPacket.cs here:

Unfortunately this is a different case. It's intentional - you can see that WriteToStreamAsync invokes the callback itself. WriteToStreamAsync is called by SNITcpHandle.SendAsync, which supplies the handle's send callback. SendAsync effectively starts the task and abandons it until the callback is invoked. It does that because that's how the native SNI layer works.

In theory, I think it'd be possible to move to a Task-first approach; (where the managed SNI layer uses Tasks in line with normal .NET, and the native SNI layer takes on the responsibility for marshalling between its master "write complete" callback and TaskCompletionSources) I think this'd be an improvement and if practical, I'd help with that. In practice, I'd want to do that on a merged and stable TdsParser and TdsParserStateObject layer; it's possible that SqlClientX is a more promising option by the time that the merge/stabilisation is done.

@MichelZ
Copy link
Contributor Author

MichelZ commented Nov 2, 2024

Let's see how TdsParser goes...
I'm curious about SqlClientX and what the roadmap/timeframe looks for that

@benrr101
Copy link
Contributor

benrr101 commented Nov 5, 2024

Do you think I can add a PR to upgrade to the latest versions?

Yknow what, I think you're right. Since we dropped net6 we should be able to take the latest version of Microsoft.DotNet.XUnitExtensions and probably take the latest of XUnit (might even be a requirement for the aforementioned). I'm always a bit hesitant with this codebase, but once this one is checked in, I would support a PR that upgrades XUnit/extensions

I'm curious about SqlClientX and what the roadmap/timeframe looks for that

... same 😅

@benrr101
Copy link
Contributor

benrr101 commented Nov 5, 2024

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@benrr101 benrr101 merged commit 4f4d922 into dotnet:main Nov 5, 2024
76 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
➕ Code Health Issues/PRs that are targeted to source code quality improvements.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

XUnit does not support public async void in next version
4 participants