-
Notifications
You must be signed in to change notification settings - Fork 317
Fix LocalAppContextSwitches race conditions in tests #3853
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
…lper. - Removed Tristate in favour of bool?. - Gave the Common test project access to MDS internals and cleaned up the Helper a lot.
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.
Pull request overview
This PR refactors the LocalAppContextSwitchesHelper to eliminate race conditions and simplify the implementation. The changes replace the custom Tristate enum with nullable bool (bool?), remove reflection-based field access in favor of direct access via InternalsVisibleTo, and introduce a semaphore-based locking mechanism to ensure only one test helper instance exists at a time. The Common test project now has access to Microsoft.Data.SqlClient internals, and nullable reference types have been enabled for better null safety.
Key Changes
- Replaced Tristate enum with
bool?throughout LocalAppContextSwitches and test helper code - Introduced semaphore-based mutual exclusion to prevent concurrent LocalAppContextSwitchesHelper instances
- Granted Common test project access to MDS internals via InternalsVisibleTo, eliminating need for reflection
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 18 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs | Removed Tristate enum, changed fields from private to internal, converted all switch backing fields to bool? |
| src/Microsoft.Data.SqlClient/tests/Common/LocalAppContextSwitchesHelper.cs | Replaced reflection-based access with direct field access, added semaphore locking, renamed properties from *Field to *Value suffix |
| src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionTests.cs | Updated test code to use new property names (EnableUserAgentValue instead of EnableUserAgentField) |
| src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionFailoverTests.cs | Updated test code to use new property naming convention with Value suffix |
| src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlConnectionStringTest.cs | Converted Tristate parameters to bool? in test data and updated property references |
| src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/ParametersTest.cs | Simplified switch assignments by removing ternary operators converting bool to Tristate |
| src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/DataReaderTest/DataReaderTest.cs | Updated legacy row version test helper usage to new bool? based API |
| src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlParameterTest.cs | Simplified legacy scale behavior switch assignment to use bool directly |
| src/Microsoft.Data.SqlClient/tests/Common/Fixtures/CspCertificateFixture.cs | Added nullable annotations for RSA and path properties |
| src/Microsoft.Data.SqlClient/tests/Common/Fixtures/ColumnMasterKeyCertificateFixture.cs | Made certificate property nullable |
| src/Microsoft.Data.SqlClient/tests/Common/Fixtures/ColumnEncryptionCertificateFixture.cs | Added nullable annotation for local machine certificate and improved null handling in GetCertificate |
| src/Microsoft.Data.SqlClient/tests/Common/Fixtures/CertificateFixtureBase.cs | Added nullable annotations for password and store context variables |
| src/Microsoft.Data.SqlClient/tests/Common/Common.csproj | Enabled nullable reference types for the Common test project |
| src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj | Added InternalsVisibleTo for Common test assembly |
| src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj | Added InternalsVisibleTo for Common test assembly |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/tests/Common/LocalAppContextSwitchesHelper.cs
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/tests/Common/Fixtures/ColumnEncryptionCertificateFixture.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Show resolved
Hide resolved
| DateTimeOffset notBefore = DateTimeOffset.UtcNow.AddDays(-1); | ||
| DateTimeOffset notAfter = DateTimeOffset.UtcNow.AddDays(1); | ||
| byte[] passwordBytes = new byte[32]; | ||
| string password = null; |
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 enabled nullable context for the entire Common test project, so there are a few changes like this related to that.
src/Microsoft.Data.SqlClient/tests/Common/LocalAppContextSwitchesHelper.cs
Show resolved
Hide resolved
…st projects that don't have access to MDS internals.
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.
Pull request overview
Copilot reviewed 16 out of 16 changed files in this pull request and generated 14 comments.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/tests/FunctionalTests/TdsParserStateObject.TestHarness.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/tests/FunctionalTests/TdsParserStateObject.TestHarness.cs
Outdated
Show resolved
Hide resolved
...Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlConnectionStringTest.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
…asta. - Addressed some Copilot comments.
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.
Pull request overview
Copilot reviewed 16 out of 16 changed files in this pull request and generated 1 comment.
Description
TODO:
The first 4 commits can be reviewed together. The remaining commits (that do the cleanup work) are best reviewed individually.
Testing
PR and CI pipelines will test this. The failures have been intermittent, so it may take a few runs even after this hits main to declare it fixed.