diff --git a/src/EFCore.Relational/Diagnostics/Internal/DbConnectionInterceptorAggregator.cs b/src/EFCore.Relational/Diagnostics/Internal/DbConnectionInterceptorAggregator.cs index 6bc0e8d5dc4..7c9550ad12e 100644 --- a/src/EFCore.Relational/Diagnostics/Internal/DbConnectionInterceptorAggregator.cs +++ b/src/EFCore.Relational/Diagnostics/Internal/DbConnectionInterceptorAggregator.cs @@ -194,6 +194,28 @@ await _interceptors[i].ConnectionDisposedAsync(connection, eventData) } } + public void ConnectionCanceled( + DbConnection connection, + ConnectionEndEventData eventData) + { + for (var i = 0; i < _interceptors.Length; i++) + { + _interceptors[i].ConnectionCanceled(connection, eventData); + } + } + + public async Task ConnectionCanceledAsync( + DbConnection connection, + ConnectionEndEventData eventData, + CancellationToken cancellationToken = default) + { + for (var i = 0; i < _interceptors.Length; i++) + { + await _interceptors[i].ConnectionCanceledAsync(connection, eventData, cancellationToken) + .ConfigureAwait(false); + } + } + public void ConnectionFailed( DbConnection connection, ConnectionErrorEventData eventData) diff --git a/src/EFCore.Relational/Diagnostics/Internal/RelationalConnectionDiagnosticsLogger.cs b/src/EFCore.Relational/Diagnostics/Internal/RelationalConnectionDiagnosticsLogger.cs index 0418bdc277f..53504a32b23 100644 --- a/src/EFCore.Relational/Diagnostics/Internal/RelationalConnectionDiagnosticsLogger.cs +++ b/src/EFCore.Relational/Diagnostics/Internal/RelationalConnectionDiagnosticsLogger.cs @@ -953,7 +953,10 @@ public virtual Task ConnectionCanceledAsync( diagnosticSourceEnabled, simpleLogEnabled); - interceptor?.ConnectionCanceled(connection.DbConnection, eventData); + if (interceptor != null) + { + return interceptor.ConnectionCanceledAsync(connection.DbConnection, eventData, cancellationToken); + } } return Task.CompletedTask; diff --git a/test/EFCore.Relational.Tests/RelationalConnectionTest.cs b/test/EFCore.Relational.Tests/RelationalConnectionTest.cs index d4ca81eb238..34f4a3da935 100644 --- a/test/EFCore.Relational.Tests/RelationalConnectionTest.cs +++ b/test/EFCore.Relational.Tests/RelationalConnectionTest.cs @@ -1084,6 +1084,125 @@ public async Task Reports_command_diagnostic_on_cancellation() }); } + [Theory] + [InlineData(typeof(IDbCommandInterceptor), typeof(Diagnostics.Internal.DbCommandInterceptorAggregator))] + [InlineData(typeof(IDbConnectionInterceptor), typeof(Diagnostics.Internal.DbConnectionInterceptorAggregator))] + [InlineData(typeof(IDbTransactionInterceptor), typeof(Diagnostics.Internal.DbTransactionInterceptorAggregator))] + [InlineData(typeof(ISaveChangesInterceptor), typeof(Diagnostics.Internal.SaveChangesInterceptorAggregator))] + [InlineData(typeof(IIdentityResolutionInterceptor), typeof(Diagnostics.Internal.IdentityResolutionInterceptorAggregator))] + [InlineData(typeof(IMaterializationInterceptor), typeof(Diagnostics.Internal.MaterializationInterceptorAggregator))] + [InlineData(typeof(IQueryExpressionInterceptor), typeof(Diagnostics.Internal.QueryExpressionInterceptorAggregator))] + public void Interceptor_aggregators_implement_all_interface_methods(Type interceptorType, Type aggregatorType) + { + var compositeType = Assert.Single( + aggregatorType.GetNestedTypes(BindingFlags.NonPublic), interceptorType.IsAssignableFrom); + + Assert.All( + compositeType.GetInterfaceMap(interceptorType).TargetMethods, + method => Assert.Equal(compositeType, method.DeclaringType)); + } + + [Theory] + [InlineData(false, 1, false)] + [InlineData(false, 2, false)] + [InlineData(true, 1, false)] + [InlineData(true, 2, false)] + [InlineData(true, 1, true)] + [InlineData(true, 2, true)] + public async Task Reports_connection_cancellation_to_interceptors(bool async, int interceptorCount, bool cancelWhileOpening) + { + var callbacks = new List(); + var interceptors = Enumerable.Range(0, interceptorCount).Select(_ => new CancellationInterceptor(callbacks)).ToArray(); + using var cancellationTokenSource = new CancellationTokenSource(); + using var dbConnection = new CancelingDbConnection(cancelWhileOpening ? cancellationTokenSource : null); + using var context = FakeRelationalTestHelpers.Instance.CreateContext( + new DbContextOptionsBuilder().UseFakeRelational(dbConnection).AddInterceptors(interceptors).Options); + using var connection = new CancelingRelationalConnection( + context.GetService()); + + var exception = async + ? await Assert.ThrowsAsync(() => connection.OpenAsync(cancellationTokenSource.Token)) + : Assert.Throws(() => connection.Open()); + + Assert.Same(dbConnection.Exception, exception); + Assert.Equal(ConnectionState.Closed, dbConnection.State); + Assert.Equal(interceptors, callbacks); + foreach (var interceptor in interceptors) + { + Assert.Equal(async, interceptor.AsyncCalled); + Assert.NotEqual(async, interceptor.SyncCalled); + Assert.Equal(async ? cancellationTokenSource.Token : default, interceptor.CancellationToken); + var eventData = Assert.IsType(interceptor.EventData); + Assert.Equal(async, eventData.IsAsync); + Assert.Same(dbConnection, eventData.Connection); + Assert.Same(context, eventData.Context); + Assert.Equal(connection.ConnectionId, eventData.ConnectionId); + Assert.Same(interceptors[0].EventData, eventData); + } + } + + private class CancelingRelationalConnection(RelationalConnectionDependencies dependencies) + : RelationalConnection(dependencies) + { + protected override DbConnection CreateDbConnection() + => throw new NotImplementedException(); + } + + private class CancelingDbConnection(CancellationTokenSource? cancellationTokenSource) : FakeDbConnection("Database=FrodoLives") + { + public OperationCanceledException Exception { get; } = new(); + + public override void Open() + => throw Exception; + + public override Task OpenAsync(CancellationToken cancellationToken) + { + cancellationTokenSource?.Cancel(); + return Task.FromException(Exception); + } + } + + private class CancellationInterceptor(List callbacks) : DbConnectionInterceptor + { + public bool SyncCalled { get; private set; } + public bool AsyncCalled { get; private set; } + public CancellationToken CancellationToken { get; private set; } + public ConnectionEndEventData? EventData { get; private set; } + + public override void ConnectionCanceled(DbConnection connection, ConnectionEndEventData eventData) + { + Assert.Same(connection, eventData.Connection); + SyncCalled = true; + EventData = eventData; + callbacks.Add(this); + } + + public override async Task ConnectionCanceledAsync( + DbConnection connection, + ConnectionEndEventData eventData, + CancellationToken cancellationToken = default) + { + await Task.Yield(); + Assert.Same(connection, eventData.Connection); + AsyncCalled = true; + CancellationToken = cancellationToken; + EventData = eventData; + callbacks.Add(this); + } + + public override void ConnectionFailed(DbConnection connection, ConnectionErrorEventData eventData) + => Assert.Fail("Cancellation should not be reported as a failure."); + + public override Task ConnectionFailedAsync( + DbConnection connection, + ConnectionErrorEventData eventData, + CancellationToken cancellationToken = default) + { + Assert.Fail("Cancellation should not be reported as a failure."); + return Task.CompletedTask; + } + } + [Fact] public async Task HandleTransactionCompleted_with_concurrent_ClearTransactions_is_thread_safe() {