From ef426dff95fab29d78b848ddc636a3c59abe8402 Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Fri, 4 Dec 2026 07:17:29 -0800 Subject: [PATCH] Give every test fake connection a server name of its own, and fail when one is fixed The remembered parameter names of a schema-qualified procedure are kept for the whole process, by server, database, kind of connection and procedure text. Most test fakes reported the same fixed server name ("fake", "gate", "output" and so on), so a test could find names another test had left for the same procedure text, and pass or fail by the order the tests ran in. The theory in ProcedureSchemaNamesTests that failed in CI was one such case; its names no longer collide with the ones the test helper hands out. Each fake connection now reports a server name from NameServer.Unique. A fake that stands for one server across several connections of one test (the tracked connections of a tally, the connections of a token rig, the connections of a database rig) takes the name once for the tally or rig instead of once per connection, so what a test counts across those connections keeps its meaning. A fake that reports no server at all (the gated connection of the ConfigureAwait test) stays as it is: a connection that names no server is never shared. A new test reads the test project's own source files and fails when a type reports a string literal as its server name (except the one allowed fake, with its reason), or when a test file uses a procedure name of the form the helper hands out (dbo.names followed by a number). The failure message says what is wrong and the fix. Tests only: no run-time change. Co-Authored-By: Claude Sonnet 5.5 --- .../ConnectionScopeTests.cs | 2 +- .../DbQueryProcedureNamesTests.cs | 2 +- .../DerivedNameCostTests.cs | 2 +- .../DerivedNamesEveryRunTests.cs | 7 +- .../DerivedParameterNameTests.cs | 2 +- .../ExecuteScalarNullTests.cs | 2 +- .../NameCacheSharedTierTests.cs | 5 +- .../OutputMetadataTests.cs | 2 +- .../OutputParameterTests.cs | 2 +- .../ProcedureSchemaNamesTests.cs | 2 +- .../ScalarAndNonQueryRunTests.cs | 2 +- .../SharedTierIsolationTests.cs | 89 +++++++++++++++++++ .../TestHelpers/CaptureProvider.cs | 2 +- .../TestHelpers/FakeDbConnection.cs | 2 +- .../TestHelpers/LeakTracking.cs | 5 +- .../TestHelpers/TokenRig.cs | 5 +- .../TransactionCreateCommandLeakTests.cs | 2 +- .../UnmappableTypeTests.cs | 2 +- 18 files changed, 116 insertions(+), 21 deletions(-) create mode 100644 Tests/Open.Database.Extensions.Core.Tests/SharedTierIsolationTests.cs diff --git a/Tests/Open.Database.Extensions.Core.Tests/ConnectionScopeTests.cs b/Tests/Open.Database.Extensions.Core.Tests/ConnectionScopeTests.cs index d3ae0056..d476b00a 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/ConnectionScopeTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/ConnectionScopeTests.cs @@ -33,7 +33,7 @@ internal sealed class ScopeConnection : DbConnection [AllowNull] public override string ConnectionString { get; set; } = ""; public override string Database => "scope"; - public override string DataSource => "scope"; + public override string DataSource { get; } = NameServer.Unique(); public override string ServerVersion => "1.0"; public override ConnectionState State => _state; public override void ChangeDatabase(string databaseName) { } diff --git a/Tests/Open.Database.Extensions.Core.Tests/DbQueryProcedureNamesTests.cs b/Tests/Open.Database.Extensions.Core.Tests/DbQueryProcedureNamesTests.cs index 7befdd0c..6e9c92eb 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/DbQueryProcedureNamesTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/DbQueryProcedureNamesTests.cs @@ -18,7 +18,7 @@ sealed class Counting : DbConnection [AllowNull] public override string ConnectionString { get; set; } = ""; public override string Database => "fake"; - public override string DataSource => "fake"; + public override string DataSource { get; } = NameServer.Unique(); public override string ServerVersion => "1.0"; public override ConnectionState State => ConnectionState.Open; public override void ChangeDatabase(string databaseName) { } diff --git a/Tests/Open.Database.Extensions.Core.Tests/DerivedNameCostTests.cs b/Tests/Open.Database.Extensions.Core.Tests/DerivedNameCostTests.cs index d6d595bd..868e8af0 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/DerivedNameCostTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/DerivedNameCostTests.cs @@ -730,7 +730,7 @@ sealed class ThreadConnection : DbConnection [AllowNull] public override string ConnectionString { get; set; } = ""; public override string Database => "fake"; - public override string DataSource => "fake"; + public override string DataSource { get; } = NameServer.Unique(); public override string ServerVersion => "1.0"; public override ConnectionState State => ConnectionState.Open; public override void ChangeDatabase(string databaseName) { } diff --git a/Tests/Open.Database.Extensions.Core.Tests/DerivedNamesEveryRunTests.cs b/Tests/Open.Database.Extensions.Core.Tests/DerivedNamesEveryRunTests.cs index 2f630c5e..06d80f69 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/DerivedNamesEveryRunTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/DerivedNamesEveryRunTests.cs @@ -25,13 +25,13 @@ sealed class Slot(string database) public int Disposals; } - sealed class DatabaseConnection(string database, DbCommand command) + sealed class DatabaseConnection(string server, string database, DbCommand command) : DbConnection { [AllowNull] public override string ConnectionString { get; set; } = ""; public override string Database => database; - public override string DataSource => "fake"; + public override string DataSource => server; public override string ServerVersion => "1.0"; public override ConnectionState State => ConnectionState.Open; public override void ChangeDatabase(string databaseName) { } @@ -50,6 +50,7 @@ protected override DbTransaction BeginDbTransaction( sealed class Rig { readonly AsyncLocal _current = new(); + readonly string _server = NameServer.Unique(); public Slot Use(string database) { @@ -65,7 +66,7 @@ public DatabaseConnection Create() command.Disposed += (_, _) => Interlocked.Increment(ref slot.Disposals); slot.Command = command; - return new DatabaseConnection(slot.Database, command); + return new DatabaseConnection(_server, slot.Database, command); } } diff --git a/Tests/Open.Database.Extensions.Core.Tests/DerivedParameterNameTests.cs b/Tests/Open.Database.Extensions.Core.Tests/DerivedParameterNameTests.cs index a5a42423..a26ecfc1 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/DerivedParameterNameTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/DerivedParameterNameTests.cs @@ -411,7 +411,7 @@ internal sealed class OpenDbConnection(DbCommand command) : DbConnection [AllowNull] public override string ConnectionString { get; set; } = ""; public override string Database => "fake"; - public override string DataSource => "fake"; + public override string DataSource { get; } = NameServer.Unique(); public override string ServerVersion => "1.0"; public override ConnectionState State => ConnectionState.Open; public override void ChangeDatabase(string databaseName) { } diff --git a/Tests/Open.Database.Extensions.Core.Tests/ExecuteScalarNullTests.cs b/Tests/Open.Database.Extensions.Core.Tests/ExecuteScalarNullTests.cs index 5eee16f1..4f73a408 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/ExecuteScalarNullTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/ExecuteScalarNullTests.cs @@ -27,7 +27,7 @@ sealed class ScalarConnection(DbCommand command) : DbConnection [AllowNull] public override string ConnectionString { get; set; } = ""; public override string Database => "fake"; - public override string DataSource => "fake"; + public override string DataSource { get; } = NameServer.Unique(); public override string ServerVersion => "1.0"; public override ConnectionState State => ConnectionState.Open; public override void ChangeDatabase(string databaseName) { } diff --git a/Tests/Open.Database.Extensions.Core.Tests/NameCacheSharedTierTests.cs b/Tests/Open.Database.Extensions.Core.Tests/NameCacheSharedTierTests.cs index e77e4376..d4155b00 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/NameCacheSharedTierTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/NameCacheSharedTierTests.cs @@ -15,9 +15,8 @@ namespace Open.Database.Extensions.Tests; // collection that disables parallelization runs after the parallel ones, one test at a time, so // no test runs while the bound test has the process-wide tier full or cleared. Each test uses a // server name of its own (NameServer.Unique), so what another test shared is never found. -// Tests in other collections: the fakes that read a qualified name give themselves a server -// name of their own too; the others keep a fixed server name and use names without a schema, -// names unique to them, or command types of their own, whose names are never shared. +// Tests in other collections: every fake connection reports a server name of its own +// (NameServer.Unique), and SharedTierIsolationTests fails when one reports a fixed name. [CollectionDefinition(Name, DisableParallelization = true)] public sealed class SharedNameTierCollection { diff --git a/Tests/Open.Database.Extensions.Core.Tests/OutputMetadataTests.cs b/Tests/Open.Database.Extensions.Core.Tests/OutputMetadataTests.cs index 4dd93f4e..851f5298 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/OutputMetadataTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/OutputMetadataTests.cs @@ -426,7 +426,7 @@ sealed class CancelingConnection(CancellationTokenSource source) : DbConnection [AllowNull] public override string ConnectionString { get; set; } = "cancel"; public override string Database => "cancel"; - public override string DataSource => "cancel"; + public override string DataSource { get; } = NameServer.Unique(); public override string ServerVersion => "1.0"; public override ConnectionState State => ConnectionState.Open; public override void ChangeDatabase(string databaseName) { } diff --git a/Tests/Open.Database.Extensions.Core.Tests/OutputParameterTests.cs b/Tests/Open.Database.Extensions.Core.Tests/OutputParameterTests.cs index 6292066a..eba09511 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/OutputParameterTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/OutputParameterTests.cs @@ -83,7 +83,7 @@ sealed class OutputConnection(Action? onExecute = null) : DbConne [AllowNull] public override string ConnectionString { get; set; } = "output"; public override string Database => "output"; - public override string DataSource => "output"; + public override string DataSource { get; } = NameServer.Unique(); public override string ServerVersion => "1.0"; public override ConnectionState State => ConnectionState.Open; public override void ChangeDatabase(string databaseName) { } diff --git a/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs b/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs index 66673903..67af27d9 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs @@ -25,7 +25,7 @@ sealed class SchemaConnection(DataTable? table, Exception? failure) : DbConnecti [AllowNull] public override string ConnectionString { get; set; } = ""; public override string Database => "fake"; - public override string DataSource => "fake"; + public override string DataSource { get; } = NameServer.Unique(); public override string ServerVersion => "1.0"; public override ConnectionState State => ConnectionState.Open; public override void ChangeDatabase(string databaseName) { } diff --git a/Tests/Open.Database.Extensions.Core.Tests/ScalarAndNonQueryRunTests.cs b/Tests/Open.Database.Extensions.Core.Tests/ScalarAndNonQueryRunTests.cs index c211e629..9bd3ddc6 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/ScalarAndNonQueryRunTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/ScalarAndNonQueryRunTests.cs @@ -260,7 +260,7 @@ sealed class Gate : DbConnection [AllowNull] public override string ConnectionString { get; set; } = ""; public override string Database => "gate"; - public override string DataSource => "gate"; + public override string DataSource { get; } = NameServer.Unique(); public override string ServerVersion => "1.0"; public override ConnectionState State => _state; public override void ChangeDatabase(string databaseName) { } diff --git a/Tests/Open.Database.Extensions.Core.Tests/SharedTierIsolationTests.cs b/Tests/Open.Database.Extensions.Core.Tests/SharedTierIsolationTests.cs new file mode 100644 index 00000000..c9906651 --- /dev/null +++ b/Tests/Open.Database.Extensions.Core.Tests/SharedTierIsolationTests.cs @@ -0,0 +1,89 @@ +using System.Runtime.CompilerServices; +using System.Text.RegularExpressions; + +namespace Open.Database.Extensions.Tests; + +#nullable enable + +// What the library remembers about a schema-qualified procedure is kept for the whole process, +// by server, database, kind of connection and procedure text (ProcedureParameterNames.Shared). +// A fake connection that always reports the same server, or a test that uses a procedure name +// the harness also hands out, can find the names another test left, so it passes or fails by +// the order the tests ran in. These tests read the test project's own source files and fail +// when either comes back. +[ExcludeFromCodeCoverage] +public static partial class SharedTierIsolationTests +{ + // A server name written as a string literal: as an expression body or as an initializer. + [GeneratedRegex(@"\bstring\s+DataSource\s*(?:=>|\{\s*get;\s*\}\s*=)\s*""(?[^""]*)""")] + private static partial Regex FixedServerName(); + + // A procedure name of the kind ParameterNameHarness.NewProcedure hands out, not as the end of + // a longer name (a database part makes it another text). + [GeneratedRegex(@"(? NoServerAllowed = new() + { + ["ConfigureAwaitTests.cs"] = "GatedConnection: reports no server, and never creates a command.", + }; + + static IEnumerable SourceFiles([CallerFilePath] string here = "") + { + string? directory = Path.GetDirectoryName(here); + if (directory is null || !Directory.Exists(directory)) + { + throw new InvalidOperationException( + $"The test project's source folder '{directory}' is not there. This test reads the .cs files of Open.Database.Extensions.Core.Tests: run it from a build made in the repository."); + } + + string separator = Path.DirectorySeparatorChar.ToString(); + return Directory.EnumerateFiles(directory, "*.cs", SearchOption.AllDirectories) + .Where(path => !path.Contains(separator + "obj" + separator) + && !path.Contains(separator + "bin" + separator)); + } + + [Fact] + public static void NoFakeConnection_ReportsAFixedServerName() + { + List found = []; + foreach (string path in SourceFiles()) + { + string file = Path.GetFileName(path); + foreach (Match match in FixedServerName().Matches(File.ReadAllText(path))) + { + string name = match.Groups["name"].Value; + if (name.Length == 0 && NoServerAllowed.ContainsKey(file)) continue; + + found.Add($"{file}: DataSource is the fixed name \"{name}\""); + } + } + + Assert.True( + found.Count == 0, + "A fake connection reports a fixed server name, so a procedure name another test used on a fake of the same name can be found there already remembered, and the result depends on the order the tests run in. Report a name of the test's own: DataSource { get; } = NameServer.Unique(), or a name made once for a rig that hands out several connections. Found: " + + string.Join("; ", found)); + } + + [Fact] + public static void NoTestUses_AProcedureNameTheHarnessHandsOut() + { + List found = []; + foreach (string path in SourceFiles()) + { + if (HarnessProcedureName().IsMatch(File.ReadAllText(path))) + { + found.Add(Path.GetFileName(path)); + } + } + + Assert.True( + found.Count == 0, + "A test uses a procedure name of the form ParameterNameHarness.NewProcedure hands out (dbo.names followed by a number), so it can meet the names remembered for another test's procedure. Use a name of its own, or call ParameterNameHarness.NewProcedure. Found in: " + + string.Join(", ", found)); + } +} diff --git a/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/CaptureProvider.cs b/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/CaptureProvider.cs index 7fa18a55..fcb73f35 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/CaptureProvider.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/CaptureProvider.cs @@ -29,7 +29,7 @@ internal sealed class CaptureConnection( [AllowNull] public override string ConnectionString { get; set; } = "capture"; public override string Database => "capture"; - public override string DataSource => "capture"; + public override string DataSource { get; } = NameServer.Unique(); public override string ServerVersion => "1.0"; public override ConnectionState State => ConnectionState.Open; public override void ChangeDatabase(string databaseName) { } diff --git a/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/FakeDbConnection.cs b/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/FakeDbConnection.cs index 543a7bd2..7c47ffa3 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/FakeDbConnection.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/FakeDbConnection.cs @@ -32,7 +32,7 @@ internal sealed class FakeDbConnection : DbConnection [AllowNull] public override string ConnectionString { get; set; } = ""; public override string Database => "fake"; - public override string DataSource => "fake"; + public override string DataSource { get; } = NameServer.Unique(); public override string ServerVersion => "1.0"; public override ConnectionState State => _state; public override void ChangeDatabase(string databaseName) { } diff --git a/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/LeakTracking.cs b/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/LeakTracking.cs index 67ae85e4..8d07adc8 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/LeakTracking.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/LeakTracking.cs @@ -55,6 +55,9 @@ internal sealed class DbTally(DbScript? script = null) { public DbScript Script { get; } = script ?? new(); + /// Gets the server name every connection of this tally reports; no other test's connections report it. + public string Server { get; } = NameServer.Unique(); + public List Connections { get; } = []; public List Commands { get; } = []; public List Readers { get; } = []; @@ -141,7 +144,7 @@ public void ResetCounters() [AllowNull] public override string ConnectionString { get; set; } = ""; public override string Database => "tracked"; - public override string DataSource => "tracked"; + public override string DataSource => tally.Server; public override string ServerVersion => "1.0"; public override ConnectionState State => _state; public override void ChangeDatabase(string databaseName) { } diff --git a/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/TokenRig.cs b/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/TokenRig.cs index 2eb25ad5..ec04a605 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/TokenRig.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/TokenRig.cs @@ -29,6 +29,9 @@ internal sealed class TokenRig public List Execute { get; } = []; public List Read { get; } = []; + /// Gets the server name every connection of this rig reports; no other test's connections report it. + public string Server { get; } = NameServer.Unique(); + /// What was done to the connection and the command, in order. public List Log { get; } = []; @@ -77,7 +80,7 @@ internal sealed class TokenConnection(TokenRig rig, ConnectionState initial = Co [AllowNull] public override string ConnectionString { get; set; } = ""; public override string Database => "fake"; - public override string DataSource => "fake"; + public override string DataSource => rig.Server; public override string ServerVersion => "1.0"; public override ConnectionState State => _state; public override void ChangeDatabase(string databaseName) { } diff --git a/Tests/Open.Database.Extensions.Core.Tests/TransactionCreateCommandLeakTests.cs b/Tests/Open.Database.Extensions.Core.Tests/TransactionCreateCommandLeakTests.cs index 83280f84..1c90125d 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/TransactionCreateCommandLeakTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/TransactionCreateCommandLeakTests.cs @@ -49,7 +49,7 @@ sealed class RejectingConnection : DbConnection [AllowNull] public override string ConnectionString { get; set; } = ""; public override string Database => "rejecting"; - public override string DataSource => "rejecting"; + public override string DataSource { get; } = NameServer.Unique(); public override string ServerVersion => "1.0"; public override ConnectionState State => ConnectionState.Open; public override void ChangeDatabase(string databaseName) { } diff --git a/Tests/Open.Database.Extensions.Core.Tests/UnmappableTypeTests.cs b/Tests/Open.Database.Extensions.Core.Tests/UnmappableTypeTests.cs index be4cb30b..d45bc1bb 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/UnmappableTypeTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/UnmappableTypeTests.cs @@ -402,7 +402,7 @@ sealed class ClosedConnection : DbConnection [AllowNull] public override string ConnectionString { get; set; } = ""; public override string Database => "fake"; - public override string DataSource => "fake"; + public override string DataSource { get; } = NameServer.Unique(); public override string ServerVersion => "1.0"; public override ConnectionState State => ConnectionState.Closed; public override void ChangeDatabase(string databaseName) { }