-
Notifications
You must be signed in to change notification settings - Fork 289
Add analyzer to detect Thread.Sleep and Task.Wait in test methods #6960
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
base: main
Are you sure you want to change the base?
Conversation
…detection Co-authored-by: Evangelink <[email protected]>
Co-authored-by: Evangelink <[email protected]>
| return; | ||
| } | ||
|
|
||
| // Check if the invocation is Task.Wait |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be looking at Task.Delay instead?
| /// MSTEST0059: <inheritdoc cref="Resources.AvoidBlockingCallsInTestsTitle"/>. | ||
| /// </summary> | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] | ||
| public sealed class AvoidBlockingCallsInTestsAnalyzer : DiagnosticAnalyzer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this name is accurate. It's not about blocking IMO. It's about arbitrary delays/sleeps.
| MessageFormat, | ||
| Description, | ||
| Category.Design, | ||
| DiagnosticSeverity.Warning, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't put as Warning by default. That's too aggressive IMO.
It should be Info. And not even sure if should be enabled by default or not.
Thread.Sleep() and Task.Wait() cause test flakiness when operations don't complete in fixed time windows. This analyzer detects these patterns at build time instead of via CodeQL, shifting feedback left.
Changes
New analyzer:
AvoidBlockingCallsInTestsAnalyzer(MSTEST0059)Thread.Sleep()andTask.Wait()invocations in test methods and fixturesTest coverage: 13 test cases covering all fixture types, positive/negative scenarios, and async alternatives
Localization: Added resources for all 13 supported languages
Example
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
11vvsblobprodcus336.vsblob.vsassets.iodotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --configuration Release(dns block)/home/REDACTED/work/testfx/testfx/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/testfx/testfx/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/testfx/testfx/artifacts/toolset/11.0.0-beta.25568.1.txt(dns block)rcxvsblobprodcus328.vsblob.vsassets.iodotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --configuration Release(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.