Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand All @@ -50,6 +50,7 @@ protected override DbTransaction BeginDbTransaction(
sealed class Rig
{
readonly AsyncLocal<Slot?> _current = new();
readonly string _server = NameServer.Unique();

public Slot Use(string database)
{
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ sealed class OutputConnection(Action<OutputCommand>? 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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
@@ -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*""(?<name>[^""]*)""")]
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(@"(?<![\w.])dbo\.names\d")]
private static partial Regex HarnessProcedureName();

// The files whose fake connection reports an empty server name, and why that is safe: a
// connection that names no server is never told apart from another, so the library keeps
// nothing of it in the shared tier. Any other fixed name is the harness's to hand out
// (NameServer.Unique).
static readonly Dictionary<string, string> NoServerAllowed = new()
{
["ConfigureAwaitTests.cs"] = "GatedConnection: reports no server, and never creates a command.",
};

static IEnumerable<string> 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<string> 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<string> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ internal sealed class DbTally(DbScript? script = null)
{
public DbScript Script { get; } = script ?? new();

/// <summary>Gets the server name every connection of this tally reports; no other test's connections report it.</summary>
public string Server { get; } = NameServer.Unique();

public List<TrackedConnection> Connections { get; } = [];
public List<TrackedCommand> Commands { get; } = [];
public List<TrackedReader> Readers { get; } = [];
Expand Down Expand Up @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ internal sealed class TokenRig
public List<CancellationToken> Execute { get; } = [];
public List<CancellationToken> Read { get; } = [];

/// <summary>Gets the server name every connection of this rig reports; no other test's connections report it.</summary>
public string Server { get; } = NameServer.Unique();

/// <summary>What was done to the connection and the command, in order.</summary>
public List<string> Log { get; } = [];

Expand Down Expand Up @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down