Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 20, 2025

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)

    • Detects Thread.Sleep() and Task.Wait() invocations in test methods and fixtures
    • Reports as Warning (enabled by default, Category: Design)
    • Scopes to: TestMethod, DataTestMethod, TestInitialize/Cleanup, ClassInitialize/Cleanup, AssemblyInitialize/Cleanup
    • Ignores non-test helper methods
  • Test coverage: 13 test cases covering all fixture types, positive/negative scenarios, and async alternatives

  • Localization: Added resources for all 13 supported languages

Example

// Before - no warning
[TestMethod]
public void MyTest()
{
    Thread.Sleep(1000);
    task.Wait();
}

// After - warnings at build time
[TestMethod]
public void MyTest()
{
    Thread.Sleep(1000);  // MSTEST0059: Avoid using 'Thread.Sleep' in test methods
    task.Wait();         // MSTEST0059: Avoid using 'Task.Wait' in test methods
}

// Suggested fix
[TestMethod]
public async Task MyTest()
{
    await Task.Delay(1000);
    await task;
}

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.io
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --configuration Release (dns block)
    • Triggering command: /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.io
    • Triggering command: dotnet 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

This section details on the original issue you should resolve

<issue_title>Detect Thread.Sleep(x) and Thread.Wait(x) inside test code to reduce flakiness</issue_title>
<issue_description>Detect Thread.Sleep(x) and Thread.Wait(x) inside test code to reduce flakiness

Background and Motivation

Thread.Sleep(x) and Thread.Wait(x) are causing test flakiness when operation is not completed in the specific time frame. In most of the cases asynchronous calls would be much better solution.

Proposed Feature

Detection of Thread.Sleep(x) and Thread.Wait(x) should be added for the test code so INFO or WARNING should inform developer during build time that there is possible flakiness in the test code.

Alternative Designs

Currently we are detecting above patterns using CodeQL inside the test code. Using test analyzer would be much better solution because it would shift the detection to the left (build time), speeding up detection and increasing quality.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Detect Thread.Sleep and Thread.Wait in test code Add analyzer to detect Thread.Sleep and Task.Wait in test methods Nov 20, 2025
Copilot AI requested a review from Evangelink November 20, 2025 10:06
Copilot finished work on behalf of Evangelink November 20, 2025 10:06
return;
}

// Check if the invocation is Task.Wait
Copy link
Member

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
Copy link
Member

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,
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detect Thread.Sleep(x) and Thread.Wait(x) inside test code to reduce flakiness

3 participants