Skip to content

Tests | Address additional flaky tests#4305

Open
edwardneal wants to merge 8 commits into
dotnet:mainfrom
edwardneal:tests/flaky-tests
Open

Tests | Address additional flaky tests#4305
edwardneal wants to merge 8 commits into
dotnet:mainfrom
edwardneal:tests/flaky-tests

Conversation

@edwardneal
Copy link
Copy Markdown
Contributor

Description

This corrects a number of other flaky tests which I noticed while looking at the XEvents tests; these fixes are small enough to bundle together.

LocalAppContextSwitchesTest

This is a standalone test, in an assembly where tests in the same class run in sequence, and classes run in parallel. That presents a problem if the AppContext switch tests are running in parallel with tests in ConnectionFailoverTests or SqlConnectionOptionsTest.

LocalAppContextSwitchesTest can find itself in a position where it's asserting EnableMultiSubnetFailoverByDefault or IgnoreServerProvidedFailoverPartner at the same time that SqlConnectionOptionsTest.TestDefaultMultiSubnetFailover or ConnectionFailoverTests.TransientFault_IgnoreServerProvidedFailoverPartner_ShouldConnectToUserProvidedPartner are setting these switches to true.

LocalAppContextSwitchesHelper has its own way to deal with this - a static SemaphoreSlim which only allows one writer at a time. I've chosen to use this in order to maintain consistency when dealing with AppContext switches.

BatchTests.ParameterInOutAndReturn

This test manually created a stored procedure with a hardcoded name. There were a few instances where this already existed. I've replaced this with the StoredProcedure RAII primitive.

AKVUnitTests, EnclaveAzureDatabaseTests

Surprisingly, it looks like there have been occasions where a deleted key with the same name already existed. I've switched the RNG to a more robust implementation, and added some limited retry logic.

The simpler solution would probably have been to purge the key and retry, but I'm not sure what permissions this runs with in CI, and if the key has been deleted then there's no point - it'll be purged in due course.

It's also worth noting the differing approach to generating key names. This is because AKV names have different requirements to database objects.

SqlCommandCancelTest

This depended upon always catching a SqlException when an operation was cancelled. Sometimes this can be an InvalidOperationException.

Issues

None.

Testing

Modified tests continue to pass.

This prevents checks of the default AppContext switch values which would run in parallel to other tests which modify these values via the RAII helper.
* Swap System.Random with the same RNG which SqlClient uses.
* Make a fixed number of attempts to reattempt key creation if a key with the generated name already exists.
@edwardneal edwardneal requested a review from a team as a code owner May 21, 2026 18:49
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board May 21, 2026
mdaigle
mdaigle previously approved these changes May 22, 2026
Copy link
Copy Markdown
Contributor

@mdaigle mdaigle left a comment

Choose a reason for hiding this comment

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

🚀

@mdaigle mdaigle added this to the 7.1.0-preview2 milestone May 22, 2026
@mdaigle mdaigle assigned mdaigle and benrr101 and unassigned mdaigle May 22, 2026
@mdaigle
Copy link
Copy Markdown
Contributor

mdaigle commented May 22, 2026

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

LocalAppContextSwitchesHelper was returning the field values, skipping the default values exposed by LocalAppContextSwitches when the AppContext switch had not been set one way or another.
@apoorvdeshmukh
Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

@cheenamalhotra cheenamalhotra moved this from To triage to In review in SqlClient Board May 26, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.82%. Comparing base (c724554) to head (b41069b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4305      +/-   ##
==========================================
- Coverage   66.52%   65.82%   -0.71%     
==========================================
  Files         284      279       -5     
  Lines       43301    66219   +22918     
==========================================
+ Hits        28807    43586   +14779     
- Misses      14494    22633    +8139     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 65.82% <ø> (?)

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.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

5 participants