Visitar URL original
Scope the database info test factory to its own data source by Brar · Pull Request #6691 · npgsql/npgsql · GitHub
Skip to content

Scope the database info test factory to its own data source - #6691

Open
Brar wants to merge 1 commit into
npgsql:mainfrom
Brar:tests/database-info-factory-scope
Open

Brar wants to merge 1 commit into
npgsql:mainfrom
Brar:tests/database-info-factory-scope

Conversation

@Brar

@Brar Brar commented Oct 25, 2026

Copy link
Copy Markdown
Member

Allow_to_query_while_loading_database_info (added in #6634) registers its database info factory globally. Despite its [NonParallelizable], NUnit runs the cases of a parameterized method while other tests run (nunit/nunit#3371), and every data source that bootstrapped in the meantime ran the factory's SELECT 1. Mock servers never answer it, so opening a connection to one failed after about 32 s, and on real servers it counted as a use for auto-prepare, which made AutoPrepareTests.Basic and Candidate_eject fail.

The factory now handles only the connectors of the test's own data source and returns null for all others, so that the default factories load their database info. Its query and assertion no longer run in other tests.

Allow_to_query_while_loading_database_info (added in npgsql#6634) registers
its database info factory globally. Despite its [NonParallelizable],
NUnit runs the cases of a parameterized method while other tests run
(nunit/nunit#3371), and every data source that bootstrapped in the
meantime ran the factory's SELECT 1. Mock servers never answer it, so
opening a connection to one failed after about 32 s, and on real servers
it counted as a use for auto-prepare, which made AutoPrepareTests.Basic
and Candidate_eject fail.

The factory now handles only the connectors of the test's own data
source and returns null for all others, so that the default factories
load their database info. Its query and assertion no longer run in
other tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Brar
Brar requested review from roji and vonzshik as code owners October 25, 2026 14:09

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Recommendation

Take this change as-is. No actionable findings.

Problem. The existing test factory is registered process-wide and executes SELECT 1 for every connector bootstrapped during its registration. This is a real test-isolation defect: unrelated connections gain an extra query, which can perturb auto-prepare accounting or interfere with mock-server protocols. The defect exists whenever another source bootstraps during that window; reproducing the exact NUnit scheduling issue is not necessary to demonstrate it.

Expected approach. Restrict this test helper to the exact data-source instance created by its test, returning null for other sources so the existing factory chain continues normally. This should be a few lines of C# test code, not a production API redesign. No documentation changes are warranted.

PR assessment. The data-source capture and identity guard implement precisely that scope:

if (conn.DataSource != dataSource)
    return null;

The owning source still executes and asserts the bootstrap query. Retaining [NonParallelizable] and the finally reset is appropriate because registration remains global. There is no unrelated plumbing or scope expansion.

Gaps. No blocking gaps found. The existing parameterized test exercises the owning-source path rather than the foreign-source exclusion; I checked that exclusion directly below.

Manual validation. I wrote and ran a standalone console harness against local PostgreSQL 18.4, using two distinct data sources with identical connection strings and Max Auto Prepare=10;Auto Prepare Min Usages=2. With an equivalent of the baseline unscoped factory registered, opening the unrelated source executes the hidden query, and its first caller-issued SELECT 1 is already prepared. Using the actual nested factory from this PR, instantiated from the built test assembly, the unrelated open leaves QueryExecuted false; its first caller-issued query is unprepared and its second becomes prepared, as expected. Opening the owning source then sets QueryExecuted true. All four combinations of baseline/PR and sync/async behaved as described. This was direct API execution, not only static inspection or unit tests. Separately, both factory-test cases plus AutoPrepareTests.Basic and Candidate_eject passed (4 tests). The SDK pinned by the repository was unavailable locally, so validation used the installed .NET 11 RC SDK targeting net10.0.

Posted by copilot

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approving, test-only change.

This branch has not been deployed

No deployments
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.

2 participants