From 4251100eafe075f310f61aed28a3566b90810e0d Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Fri, 4 Dec 2026 05:52:42 -0800 Subject: [PATCH 1/3] Small fixes: async disposal in UsingAsync, blank return-parameter names, argument names, one enumeration message UsingAsync on a connection factory or a function now disposes the connection the way a connection scope does: DisposeAsync when the connection implements IAsyncDisposable (every DbConnection), Dispose otherwise. It called Dispose before, so a provider's asynchronous disposal was never used. The cost is one type check per call. AddReturnParameter with a DbType, and the SQL Server one with a SqlDbType, now treat an empty or white-space name as no name, as the overload without a type and a null name already did. They threw ArgumentException for a blank name before. AsGeneric on a null IDbConnectionPool now throws ArgumentNullException named connectionPool (it named an internal parameter, source). ToChannel(query, channel) with both arguments null now names the first parameter, source, as the ChannelWriter overload does; it named channel. The message for a second enumeration of a stream is one constant, StallStream.EnumeratedTwiceMessage, used by the row stream and by the chunk stream; the chunk stream had its own text. The tests refer to the constant. The private RowMap method ColumnIndex is renamed OrdinalsByName, so it no longer shares a name with the internal ColumnIndex type. No behavior change. Each behavior change has a test that failed before the change. The release notes list the four changes a caller can see. Co-Authored-By: Claude Sonnet 5.5 --- Source/Channel/ToChannel.Target.cs | 1 + Source/Core/ChunkStream.cs | 6 +-- Source/Core/Core/RowMap.cs | 6 +-- .../Core/Extensions/Command.AddParameter.cs | 8 ++-- .../Extensions/ConnectionProvider.Using.cs | 30 +++++++++------ Source/Core/IDbConnectionPool.cs | 7 +++- Source/Core/StallStream.cs | 9 +++-- .../Extensions/Command.AddParameter.cs | 9 +++-- .../CaptureStreamTests.cs | 3 +- .../CommandParameterExtensionsTests.cs | 37 +++++++++++++++++++ .../ConnectionOpenUsingTests.cs | 33 +++++++++++++++++ .../NameCacheSourceTests.cs | 2 +- .../NullGuardDefectTests.cs | 9 +++++ .../PumpStreamOpenTests.cs | 26 +++++++++++++ .../SqlServerPackageTests.cs | 14 +++++++ .../ToChannelTargetTests.cs | 13 +++++++ docsite/articles/release-notes.md | 4 ++ 17 files changed, 185 insertions(+), 32 deletions(-) diff --git a/Source/Channel/ToChannel.Target.cs b/Source/Channel/ToChannel.Target.cs index aa384574..063eebc8 100644 --- a/Source/Channel/ToChannel.Target.cs +++ b/Source/Channel/ToChannel.Target.cs @@ -65,6 +65,7 @@ public static ValueTask ToChannel( bool complete, CancellationToken cancellationToken = default) { + if (source is null) throw new ArgumentNullException(nameof(source)); if (channel is null) throw new ArgumentNullException(nameof(channel)); Contract.EndContractBlock(); diff --git a/Source/Core/ChunkStream.cs b/Source/Core/ChunkStream.cs index 8cfd0d22..b1d487cb 100644 --- a/Source/Core/ChunkStream.cs +++ b/Source/Core/ChunkStream.cs @@ -116,11 +116,7 @@ public IAsyncEnumerator> GetAsyncEnumerator( // The one-shot guard is here, not in the iterator: an iterator body runs // at the first step, and the second call must throw at the call. if (Interlocked.Exchange(ref _taken, 1) != 0) - { - throw new InvalidOperationException( - "This stream can be enumerated only once." - + " Call ToPooledBuffer() to read it many times."); - } + throw StallStream.EnumeratedTwice(); return Read(cancellationToken); } diff --git a/Source/Core/Core/RowMap.cs b/Source/Core/Core/RowMap.cs index b371b712..a1b7c331 100644 --- a/Source/Core/Core/RowMap.cs +++ b/Source/Core/Core/RowMap.cs @@ -849,8 +849,8 @@ internal static void ThrowIfUnmappable() // A column a member declares by ordinal ([Column(Order = n)]) is its before any tier runs, so none gives it away; a column that is not its throws. int claimed = HasDecorators ? ClaimOrdinals(names, feeds, filled) : 0; - int unmatched = names.Length - claimed - Feed(ColumnIndex(StringComparer.Ordinal), names, feeds, filled); - if (unmatched > 0) unmatched -= Feed(ColumnIndex(StringComparer.OrdinalIgnoreCase), names, feeds, filled); + int unmatched = names.Length - claimed - Feed(OrdinalsByName(StringComparer.Ordinal), names, feeds, filled); + if (unmatched > 0) unmatched -= Feed(OrdinalsByName(StringComparer.OrdinalIgnoreCase), names, feeds, filled); if (unmatched > 0 && NameIndex(filled) is { } byName) Feed(byName, names, feeds, filled); return feeds; @@ -884,7 +884,7 @@ int Feed(Dictionary index, string[] names, Slot?[] feeds, bool[] fi /// /// A member that declares its column ([Column(Name)]) is asked first: another member reading the same name does not take it. /// - Dictionary ColumnIndex(StringComparer comparer) + Dictionary OrdinalsByName(StringComparer comparer) { var index = new Dictionary(_columns.Length, comparer); diff --git a/Source/Core/Extensions/Command.AddParameter.cs b/Source/Core/Extensions/Command.AddParameter.cs index 152db228..e0c86103 100644 --- a/Source/Core/Extensions/Command.AddParameter.cs +++ b/Source/Core/Extensions/Command.AddParameter.cs @@ -185,15 +185,17 @@ public static IDbDataParameter AddParameterType( /// /// /// - /// The optional name of the parameter. + /// The optional name of the parameter. It is ignored if it is , empty, or white space. /// The created parameter, which has already been added to the command's parameter collection. Read its value after the command has executed. /// is . - /// public static IDbDataParameter AddReturnParameter( this IDbCommand target, DbType type, string? name = null) - => target.AddParameterType(name, type, ParameterDirection.ReturnValue); + => target.AddParameterType( + string.IsNullOrWhiteSpace(name) ? null : name, + type, + ParameterDirection.ReturnValue); /// /// Adds a return-value parameter to the command, leaving the provider to determine its type. diff --git a/Source/Core/Extensions/ConnectionProvider.Using.cs b/Source/Core/Extensions/ConnectionProvider.Using.cs index caef2ebd..3fc67e10 100644 --- a/Source/Core/Extensions/ConnectionProvider.Using.cs +++ b/Source/Core/Extensions/ConnectionProvider.Using.cs @@ -187,8 +187,9 @@ public static async ValueTask UsingAsync(this IDbConnectionFactory connect if (action is null) throw new ArgumentNullException(nameof(action)); Contract.EndContractBlock(); - using IDbConnection conn = connectionFactory.Create(); - return await action(conn).ConfigureAwait(false); + var lease = Seal(connectionFactory.Create(), default, null); + await using var leaseDisposal = lease.ConfigureAwait(false); + return await action(lease.Connection).ConfigureAwait(false); } /// @@ -203,8 +204,9 @@ public static async ValueTask UsingAsync(this IDbConnectionFactory connectionFac if (action is null) throw new ArgumentNullException(nameof(action)); Contract.EndContractBlock(); - using IDbConnection conn = connectionFactory.Create(); - await action(conn).ConfigureAwait(false); + var lease = Seal(connectionFactory.Create(), default, null); + await using var leaseDisposal = lease.ConfigureAwait(false); + await action(lease.Connection).ConfigureAwait(false); } /// @@ -222,8 +224,9 @@ public static async ValueTask UsingAsync(this IDbConnectionFa if (action is null) throw new ArgumentNullException(nameof(action)); Contract.EndContractBlock(); - using TConnection conn = connectionFactory.Create(); - return await action(conn).ConfigureAwait(false); + var lease = Seal(connectionFactory.Create(), default, null); + await using var leaseDisposal = lease.ConfigureAwait(false); + return await action(lease.Connection).ConfigureAwait(false); } /// @@ -240,8 +243,9 @@ public static async ValueTask UsingAsync(this IDbConnectionFactory< if (action is null) throw new ArgumentNullException(nameof(action)); Contract.EndContractBlock(); - using TConnection conn = connectionFactory.Create(); - await action(conn).ConfigureAwait(false); + var lease = Seal(connectionFactory.Create(), default, null); + await using var leaseDisposal = lease.ConfigureAwait(false); + await action(lease.Connection).ConfigureAwait(false); } /// @@ -333,8 +337,9 @@ public static async ValueTask UsingAsync(this Func @@ -351,7 +356,8 @@ public static async ValueTask UsingAsync(this Func con if (action is null) throw new ArgumentNullException(nameof(action)); Contract.EndContractBlock(); - using TConnection conn = connectionFactory(); - await action(conn).ConfigureAwait(false); + var lease = Seal(connectionFactory(), default, null); + await using var leaseDisposal = lease.ConfigureAwait(false); + await action(lease.Connection).ConfigureAwait(false); } } diff --git a/Source/Core/IDbConnectionPool.cs b/Source/Core/IDbConnectionPool.cs index 89b073f3..1feb0c9a 100644 --- a/Source/Core/IDbConnectionPool.cs +++ b/Source/Core/IDbConnectionPool.cs @@ -100,5 +100,10 @@ object IConnectionIdentity.Identity /// /// is . public static IDbConnectionPool AsGeneric(this IDbConnectionPool connectionPool) - => connectionPool is IDbConnectionPool p ? p : new GenericPool(connectionPool); + { + if (connectionPool is null) throw new ArgumentNullException(nameof(connectionPool)); + Contract.EndContractBlock(); + + return connectionPool is IDbConnectionPool p ? p : new GenericPool(connectionPool); + } } diff --git a/Source/Core/StallStream.cs b/Source/Core/StallStream.cs index 9d783903..47975ac6 100644 --- a/Source/Core/StallStream.cs +++ b/Source/Core/StallStream.cs @@ -55,10 +55,13 @@ internal static class StallStream public static TimeoutException TimeoutFor(TimeSpan stall) => new($"The consumer made no pull for {stall} while the reader and the connection were held; the read was stopped."); + /// A stream is enumerated a second time. + public const string EnumeratedTwiceMessage + = "This stream can be enumerated only once. " + + "Call ToListAsync() or ToPooledBuffer() to read the rows more than once."; + public static InvalidOperationException EnumeratedTwice() - => new( - "This stream can be enumerated only once." - + " Call ToListAsync() or ToPooledBuffer() to read the rows more than once."); + => new(EnumeratedTwiceMessage); /// /// Gets the buffer size, in rows, unless the caller asks for another one. diff --git a/Source/MSSqlClient/Extensions/Command.AddParameter.cs b/Source/MSSqlClient/Extensions/Command.AddParameter.cs index bba76c7e..129d75d0 100644 --- a/Source/MSSqlClient/Extensions/Command.AddParameter.cs +++ b/Source/MSSqlClient/Extensions/Command.AddParameter.cs @@ -64,11 +64,14 @@ public static SqlParameter AddParameterType( /// /// /// - /// The optional name of the parameter. + /// The optional name of the parameter. It is ignored if it is , empty, or white space. /// The created parameter, which has already been added to the command's parameter collection. Read its value after the command has executed. /// is . - /// public static SqlParameter AddReturnParameter(this SqlCommand target, SqlDbType type, string? name = null) - => AddParameterType(target, name, type, ParameterDirection.ReturnValue); + => AddParameterType( + target, + string.IsNullOrWhiteSpace(name) ? null : name, + type, + ParameterDirection.ReturnValue); } diff --git a/Tests/Open.Database.Extensions.Core.Tests/CaptureStreamTests.cs b/Tests/Open.Database.Extensions.Core.Tests/CaptureStreamTests.cs index 4d646ec3..4bca471f 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/CaptureStreamTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/CaptureStreamTests.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks.Dataflow; +using Open.Database.Extensions.Core; #nullable enable @@ -154,7 +155,7 @@ public static async Task ASecondEnumeration_IsRefused_AndLeavesTheFirstRun() Contains( Assert.Throws( () => rows.GetAsyncEnumerator(Token)), - "can be enumerated only once"); + StallStream.EnumeratedTwiceMessage); Assert.Equal(3, count); Assert.Equal(9, await total); } diff --git a/Tests/Open.Database.Extensions.Core.Tests/CommandParameterExtensionsTests.cs b/Tests/Open.Database.Extensions.Core.Tests/CommandParameterExtensionsTests.cs index 7f14284f..5b7f61b6 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/CommandParameterExtensionsTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/CommandParameterExtensionsTests.cs @@ -51,6 +51,43 @@ public static void AddReturnParameter_SetsReturnDirection() Assert.Equal(ParameterDirection.ReturnValue, param.Direction); } + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + public static void AddReturnParameter_NullOrBlankName_AddsAnUnnamedParameter( + string? name) + { + (IDbCommand command, IDbDataParameter param, IDataParameterCollection parameters) + = NewCommand(); + param.ParameterName = "unset"; + + command.AddReturnParameter(DbType.Int32, name); + + Assert.Equal("unset", param.ParameterName); + Assert.Equal(DbType.Int32, param.DbType); + Assert.Equal(ParameterDirection.ReturnValue, param.Direction); + parameters.Received(1).Add(param); + } + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + public static void AddReturnParameter_Untyped_NullOrBlankName_AddsAnUnnamedParameter( + string? name) + { + (IDbCommand command, IDbDataParameter param, IDataParameterCollection parameters) + = NewCommand(); + param.ParameterName = "unset"; + + command.AddReturnParameter(name); + + Assert.Equal("unset", param.ParameterName); + Assert.Equal(ParameterDirection.ReturnValue, param.Direction); + parameters.Received(1).Add(param); + } + [Fact] public static void AddParameter_BlankName_Throws() { diff --git a/Tests/Open.Database.Extensions.Core.Tests/ConnectionOpenUsingTests.cs b/Tests/Open.Database.Extensions.Core.Tests/ConnectionOpenUsingTests.cs index cecee2f4..8dc1eea0 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/ConnectionOpenUsingTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/ConnectionOpenUsingTests.cs @@ -250,6 +250,39 @@ await Assert.ThrowsAsync(async () => Assert.Equal("Create,DisposeConnection", probe.Log); } + [Fact] + public static async Task FactoryUsingAsync_AsyncDisposableConnection_IsDisposedAsynchronously() + { + int synchronous = 0; + int asynchronous = 0; + var connection = Substitute.For(); + connection.When(c => c.Dispose()).Do(_ => ++synchronous); +#pragma warning disable CA2012 // NSubstitute is told what the call returns here; nothing consumes the ValueTask. + ((IAsyncDisposable)connection).DisposeAsync().Returns(_ => + { + ++asynchronous; + return default; + }); +#pragma warning restore CA2012 + + var factory = Substitute.For(); + factory.Create().Returns(connection); + var generic = Substitute.For>(); + generic.Create().Returns(connection); + Func function = () => connection; + + await factory.UsingAsync(_ => ValueTask.CompletedTask); + await factory.UsingAsync(_ => new ValueTask(1)); + await generic.UsingAsync(_ => ValueTask.CompletedTask); + await generic.UsingAsync(_ => new ValueTask(1)); + await function.UsingAsync(_ => ValueTask.CompletedTask); + await function.UsingAsync(_ => new ValueTask(1)); + + // One asynchronous disposal for each of the six calls, and no other. + Assert.Equal(6, asynchronous); + Assert.Equal(0, synchronous); + } + #endregion #region Argument validation diff --git a/Tests/Open.Database.Extensions.Core.Tests/NameCacheSourceTests.cs b/Tests/Open.Database.Extensions.Core.Tests/NameCacheSourceTests.cs index 590fdcb4..cb24cc19 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/NameCacheSourceTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/NameCacheSourceTests.cs @@ -538,7 +538,7 @@ static string Refusal(Action act) public static void ANullSource_IsRefused_ByNamingTheParameter() { Assert.Equal("ArgumentNullException:connectionFactory", Refusal(() => _ = new ExpressiveCommand((IDbConnectionFactory)null!, CommandType.StoredProcedure, "proc"))); - Assert.Equal("ArgumentNullException:source", Refusal(() => _ = new ExpressiveCommand((IDbConnectionPool)null!, CommandType.StoredProcedure, "proc"))); + Assert.Equal("ArgumentNullException:connectionPool", Refusal(() => _ = new ExpressiveCommand((IDbConnectionPool)null!, CommandType.StoredProcedure, "proc"))); Assert.Equal("ArgumentNullException:connFactory", Refusal(() => _ = new ExpressiveCommand((Func)null!, CommandType.StoredProcedure, "proc"))); } diff --git a/Tests/Open.Database.Extensions.Core.Tests/NullGuardDefectTests.cs b/Tests/Open.Database.Extensions.Core.Tests/NullGuardDefectTests.cs index 8660f6ee..9f67d72b 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/NullGuardDefectTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/NullGuardDefectTests.cs @@ -70,6 +70,15 @@ public static void AsPool_NullSource_NamesTheSource(string call) Assert.Equal("factory", ex.ParamName); } + [Fact] + public static void AsGeneric_NullPool_NamesTheConnectionPool() + { + Assert.Equal("connectionPool", Assert.Throws( + () => ((IDbConnectionPool)null!).AsGeneric()).ParamName); + Assert.Equal("connectionFactory", Assert.Throws( + () => ((IDbConnectionFactory)null!).AsGeneric()).ParamName); + } + [Fact] public static void AsPool_NullFactory_NamesTheFactory() { diff --git a/Tests/Open.Database.Extensions.Core.Tests/PumpStreamOpenTests.cs b/Tests/Open.Database.Extensions.Core.Tests/PumpStreamOpenTests.cs index 54cf1ef3..58202084 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/PumpStreamOpenTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/PumpStreamOpenTests.cs @@ -53,6 +53,32 @@ IAsyncEnumerable stream await TakeAndDispose(stream, tally); } + [Fact] + public static void ChunkStream_ASecondEnumeration_SaysWhatToCallInstead() + { + IAsyncEnumerable> stream + = Read(Tally(3)).ToPooledBufferStream(Ct); + stream.GetAsyncEnumerator(Ct); + + var ex = Assert.Throws( + () => stream.GetAsyncEnumerator(Ct)); + + Assert.Equal(StallStream.EnumeratedTwiceMessage, ex.Message); + } + + [Fact] + public static void StallStream_ASecondEnumeration_SaysWhatToCallInstead() + { + IAsyncEnumerable stream + = Read(Tally(3)).StreamBuffered(Long, 1, Ct); + stream.GetAsyncEnumerator(Ct); + + var ex = Assert.Throws( + () => stream.GetAsyncEnumerator(Ct)); + + Assert.Equal(StallStream.EnumeratedTwiceMessage, ex.Message); + } + [Fact] public static async Task CapturedStream_OverChunks_StartsNothing_UntilTheFirstPull() { diff --git a/Tests/Open.Database.Extensions.Core.Tests/SqlServerPackageTests.cs b/Tests/Open.Database.Extensions.Core.Tests/SqlServerPackageTests.cs index 1ff7a8fb..3190ff0d 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/SqlServerPackageTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/SqlServerPackageTests.cs @@ -157,6 +157,20 @@ public static void AddParameterType_ANullNameIsAllowedForTheReturnValue() Assert.StartsWith("Parameter", p.ParameterName); // the collection names a parameter that has no name } + [Theory] + [InlineData("")] + [InlineData(" ")] + public static void AddReturnParameter_ABlankNameIsTheSameAsNone(string name) + { + using var command = new SqlCommand(); + + SqlParameter p = SqlCmd.AddReturnParameter(command, SqlDbType.Int, name); + + Assert.Same(p, Assert.Single(command.Parameters)); + Assert.Equal(ParameterDirection.ReturnValue, p.Direction); + Assert.StartsWith("Parameter", p.ParameterName); // the collection names a parameter that has no name + } + [Fact] public static void AddParameterType_SetsTheTypeAndDirection_AndReturnsTheParameterInTheCollection() { diff --git a/Tests/Open.Database.Extensions.Core.Tests/ToChannelTargetTests.cs b/Tests/Open.Database.Extensions.Core.Tests/ToChannelTargetTests.cs index ad73e7b6..44006492 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/ToChannelTargetTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/ToChannelTargetTests.cs @@ -322,6 +322,19 @@ public static void TheGuards_ThrowAtTheCall() Assert.Empty(tally.Connections); } + [Fact] + public static void WhenEveryArgumentIsNull_TheGuardNamesTheSource() + { + IMappedQuery none = null; + + Assert.Equal("source", Assert.Throws( + () => Drop(none.ToChannel((Channel)null, Ct))).ParamName); + Assert.Equal("source", Assert.Throws( + () => Drop(none.ToChannel((Channel)null, true, Ct))).ParamName); + Assert.Equal("source", Assert.Throws( + () => Drop(none.ToChannel((ChannelWriter)null, Ct))).ParamName); + } + [Fact] public static async Task AUsersOwnSource_Works() { diff --git a/docsite/articles/release-notes.md b/docsite/articles/release-notes.md index 72b9b5f1..3bf0a29c 100644 --- a/docsite/articles/release-notes.md +++ b/docsite/articles/release-notes.md @@ -8,6 +8,10 @@ Version 11 is `11.0.0-rc.17`. For moving a 10.x codebase, read [Coming from earl - **Fixed: a command made from a factory, a pool or a function read a procedure's parameter names again for every command.** A command made from a factory or pool that implements only the non-generic interface, or from a `Func` or a `Func` through `Command` or `StoredProcedure`, kept a separate cache per command, so the first run of every command asked the database for the names. Commands made from the same factory, pool or function now share one cache. Different factories, pools and functions still do not share. A function is told apart by the object its method belongs to: lambdas that capture nothing, in one class, are one source and share; a lambda that captures a local variable or a parameter and is built again for each call is a new source each time, so keep one factory for the application. See [Parameter names are read once](expressive-commands.md#parameter-names-are-read-once). - **Fixed: a stored procedure's parameter names with an ordinal that is not a number.** A row of the `ProcedureParameters` schema collection whose `ORDINAL_POSITION` is not a number that fits an `int` failed the call that looks up the names; the row is now skipped, and the other parameters are still found. +- **Fixed: `UsingAsync` on a factory or a function disposes the connection asynchronously.** It called `Dispose`; a connection that implements `IAsyncDisposable` (every `DbConnection`) is now disposed with `DisposeAsync`, as the connection scopes do, and any other connection with `Dispose`. +- **Changed: `AddReturnParameter` treats a blank name as no name.** The overload that takes a `DbType` (and the SQL Server one that takes a `SqlDbType`) threw `ArgumentException` for an empty or white-space name; like the overload without a type, it now adds an unnamed return parameter, as it does for `null`. +- **Fixed: the name of the `ArgumentNullException` from `AsGeneric(IDbConnectionPool)`.** A `null` pool threw with the parameter name `source`; it is now `connectionPool`. +- **Fixed: the name of the `ArgumentNullException` from `ToChannel(query, channel)` when both arguments are `null`.** It named `channel`; it now names `source`, the first parameter, as the `ChannelWriter` overload does. ### Changes since rc.16 From 5e176895f6f5a871addb2a25ad60c55ccbb74013 Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Fri, 4 Dec 2026 06:30:09 -0800 Subject: [PATCH 2/3] One guard for each argument: leave the ToChannel source check to its callee, and the pool wrapper's to AsGeneric ToChannel(query, channel) checked the query itself before the call that takes the channel's writer, which checks it again. The first overload now checks only what it uses before the callee does (the channel), with a comment that the query is guarded by the ChannelWriter overload. With both arguments null the exception names the channel again; the test pins that, and the release note for the name is dropped. The private wrapper that AsGeneric makes around a non-generic pool no longer throws for a null pool: AsGeneric, its only caller, checks the argument and names it, and the wrapper asserts it in debug builds. No public API change. Co-Authored-By: Claude Sonnet 5.5 --- Source/Channel/ToChannel.Target.cs | 2 +- Source/Core/IDbConnectionPool.cs | 20 ++++++++++++++----- .../ToChannelTargetTests.cs | 6 +++--- docsite/articles/release-notes.md | 1 - 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Source/Channel/ToChannel.Target.cs b/Source/Channel/ToChannel.Target.cs index 063eebc8..c7629a37 100644 --- a/Source/Channel/ToChannel.Target.cs +++ b/Source/Channel/ToChannel.Target.cs @@ -65,7 +65,7 @@ public static ValueTask ToChannel( bool complete, CancellationToken cancellationToken = default) { - if (source is null) throw new ArgumentNullException(nameof(source)); + // The source is guarded by the ChannelWriter overload. if (channel is null) throw new ArgumentNullException(nameof(channel)); Contract.EndContractBlock(); diff --git a/Source/Core/IDbConnectionPool.cs b/Source/Core/IDbConnectionPool.cs index 1feb0c9a..f22db6c9 100644 --- a/Source/Core/IDbConnectionPool.cs +++ b/Source/Core/IDbConnectionPool.cs @@ -1,3 +1,5 @@ +using System.Diagnostics; + namespace Open.Database.Extensions; /// @@ -69,12 +71,20 @@ public static class ConnectionPoolExtensions /// /// Presents a non-generic pool as an of by forwarding to it. /// - /// The pool to forward to. - /// is . - class GenericPool(IDbConnectionPool source) : IDbConnectionPool, IConnectionIdentity + class GenericPool : IDbConnectionPool, IConnectionIdentity { - private readonly IDbConnectionPool _source - = source ?? throw new ArgumentNullException(nameof(source)); + private readonly IDbConnectionPool _source; + + /// + /// Initializes a new instance that forwards to the pool. + /// + /// The pool to forward to. + public GenericPool(IDbConnectionPool source) + { + // Guarded by AsGeneric, the only place that makes one. + Debug.Assert(source is not null); + _source = source; + } /// public IDbConnection Take() diff --git a/Tests/Open.Database.Extensions.Core.Tests/ToChannelTargetTests.cs b/Tests/Open.Database.Extensions.Core.Tests/ToChannelTargetTests.cs index 44006492..50665b75 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/ToChannelTargetTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/ToChannelTargetTests.cs @@ -323,13 +323,13 @@ public static void TheGuards_ThrowAtTheCall() } [Fact] - public static void WhenEveryArgumentIsNull_TheGuardNamesTheSource() + public static void WhenEveryArgumentIsNull_EachOverloadNamesItsFirstGuard() { IMappedQuery none = null; - Assert.Equal("source", Assert.Throws( + Assert.Equal("channel", Assert.Throws( () => Drop(none.ToChannel((Channel)null, Ct))).ParamName); - Assert.Equal("source", Assert.Throws( + Assert.Equal("channel", Assert.Throws( () => Drop(none.ToChannel((Channel)null, true, Ct))).ParamName); Assert.Equal("source", Assert.Throws( () => Drop(none.ToChannel((ChannelWriter)null, Ct))).ParamName); diff --git a/docsite/articles/release-notes.md b/docsite/articles/release-notes.md index 3bf0a29c..be973526 100644 --- a/docsite/articles/release-notes.md +++ b/docsite/articles/release-notes.md @@ -11,7 +11,6 @@ Version 11 is `11.0.0-rc.17`. For moving a 10.x codebase, read [Coming from earl - **Fixed: `UsingAsync` on a factory or a function disposes the connection asynchronously.** It called `Dispose`; a connection that implements `IAsyncDisposable` (every `DbConnection`) is now disposed with `DisposeAsync`, as the connection scopes do, and any other connection with `Dispose`. - **Changed: `AddReturnParameter` treats a blank name as no name.** The overload that takes a `DbType` (and the SQL Server one that takes a `SqlDbType`) threw `ArgumentException` for an empty or white-space name; like the overload without a type, it now adds an unnamed return parameter, as it does for `null`. - **Fixed: the name of the `ArgumentNullException` from `AsGeneric(IDbConnectionPool)`.** A `null` pool threw with the parameter name `source`; it is now `connectionPool`. -- **Fixed: the name of the `ArgumentNullException` from `ToChannel(query, channel)` when both arguments are `null`.** It named `channel`; it now names `source`, the first parameter, as the `ChannelWriter` overload does. ### Changes since rc.16 From 6d7d44a59b5e0c8dae0093a32becc1ef2f6fbc24 Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Fri, 4 Dec 2026 06:57:56 -0800 Subject: [PATCH 3/3] Stop a schema-names test from depending on test order The theory that checks how a procedure name is split into the restrictions of the schema read used the name dbo.names1. The test helper that gives each test a fresh procedure name hands out dbo.names1 too, as its first name. A qualified name is now remembered for every connection of one kind, so when a test that uses the helper ran first, the theory's call found the names already remembered, read no schema and recorded no restrictions, and the assertion failed. Whether it failed depended on the order the tests ran in, and it did in CI. The theory now uses names the helper never hands out. Tests only: no run-time change. Co-Authored-By: Claude Sonnet 5.5 --- .../ProcedureSchemaNamesTests.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs b/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs index f2bdfabd..66673903 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs @@ -156,15 +156,17 @@ public static async Task Async_FindsTheSameNames() // ---- the procedure name: which restrictions are asked ---- + // These names are not ones ParameterNameHarness.NewProcedure hands out (dbo.namesN): a qualified name + // is remembered for every connection of this kind, so one that another test has used is not read again. [Theory] - [InlineData("names1", null, "names1")] - [InlineData("dbo.names1", "dbo", "names1")] - [InlineData("[dbo].[names1]", "dbo", "names1")] - [InlineData("\"dbo\".\"names1\"", "dbo", "names1")] + [InlineData("split1", null, "split1")] + [InlineData("dbo.split1", "dbo", "split1")] + [InlineData("[dbo].[split1]", "dbo", "split1")] + [InlineData("\"dbo\".\"split1\"", "dbo", "split1")] [InlineData("[my.schema].[my proc]", "my.schema", "my proc")] [InlineData("\"dbo\".\"my.proc\"", "dbo", "my.proc")] [InlineData("[sales].[a]]b]", "sales", "a]b")] - [InlineData("names1;1", null, "names1;1")] // nothing is stripped that the provider could still resolve + [InlineData("split1;1", null, "split1;1")] // nothing is stripped that the provider could still resolve public static void TheProcedureName_IsSplitIntoSchemaAndName_ForTheRestrictions(string procedure, string? schema, string name) { const int orderId = 5;