Exemple #1
0
        public virtual async Task Intercept_connection_that_throws_on_open(bool async)
        {
            var interceptor = new ConnectionInterceptor();

            using (var context = CreateBadUniverse(new DbContextOptionsBuilder().AddInterceptors(interceptor)))
            {
                try
                {
                    if (async)
                    {
                        await context.Database.OpenConnectionAsync();
                    }
                    else
                    {
                        context.Database.OpenConnection();
                    }

                    Assert.False(true);
                }
                catch (Exception exception)
                {
                    Assert.Same(interceptor.Exception, exception);
                }

                AssertErrorOnOpen(context, interceptor, async);
            }
        }
Exemple #2
0
        public virtual async Task Intercept_connection_with_multiple_interceptors(bool async)
        {
            var interceptor1 = new ConnectionInterceptor();
            var interceptor2 = new ConnectionOverridingInterceptor();
            var interceptor3 = new ConnectionInterceptor();
            var interceptor4 = new ConnectionOverridingInterceptor();

            using (var context = CreateContext(
                       new IInterceptor[]
            {
                new NoOpConnectionInterceptor(), interceptor1, interceptor2
            },
                       new IInterceptor[]
            {
                interceptor3, interceptor4, new NoOpConnectionInterceptor()
            }))
            {
                // Test infrastructure uses an open connection, so close it first.
                var connection  = context.Database.GetDbConnection();
                var startedOpen = connection.State == ConnectionState.Open;
                if (startedOpen)
                {
                    connection.Close();
                }

                using (var listener = Fixture.SubscribeToDiagnosticListener(context.ContextId))
                {
                    if (async)
                    {
                        await context.Database.OpenConnectionAsync();
                    }
                    else
                    {
                        context.Database.OpenConnection();
                    }

                    AssertNormalOpen(context, interceptor1, async);
                    AssertNormalOpen(context, interceptor2, async);
                    AssertNormalOpen(context, interceptor3, async);
                    AssertNormalOpen(context, interceptor4, async);

                    interceptor1.Reset();
                    interceptor2.Reset();
                    interceptor3.Reset();
                    interceptor4.Reset();

                    if (async)
                    {
                        await context.Database.CloseConnectionAsync();
                    }
                    else
                    {
                        context.Database.CloseConnection();
                    }

                    AssertNormalClose(context, interceptor1, async);
                    AssertNormalClose(context, interceptor2, async);
                    AssertNormalClose(context, interceptor3, async);
                    AssertNormalClose(context, interceptor4, async);

                    AsertOpenCloseEvents(listener);
                }

                if (startedOpen)
                {
                    connection.Open();
                }
            }
        }