示例#1
0
        public void EntityConnection_StateChangeEvents_are_fired_when_state_changes()
        {
            using (var context = new SimpleModelContext())
            {
                EntityConnection entityConnection = (EntityConnection)((IObjectContextAdapter)context).ObjectContext.Connection;

                ConnectionEventsTracker dbConnectionTracker = new ConnectionEventsTracker(entityConnection.StoreConnection);
                ConnectionEventsTracker entityConnectionTracker = new ConnectionEventsTracker(entityConnection);
                ConnectionState state = ConnectionState.Closed;

                // verify that the open and close events have not been fired yet
                dbConnectionTracker.VerifyNoConnectionEventsWereFired();
                entityConnectionTracker.VerifyNoConnectionEventsWereFired();

                DbConnection storeConnection = entityConnection.StoreConnection;
                storeConnection.Open();

                // verify the open event has been fired on the store connection but not yet on the EntityConnection
                dbConnectionTracker.VerifyConnectionOpenedEventWasFired();
                entityConnectionTracker.VerifyNoConnectionEventsWereFired();

                // look at the EntityConnection's State (side-effect is we fire an event)
                state = entityConnection.State;
                Assert.Equal(ConnectionState.Open, state);

                // now the open event should have been fired on both connections
                dbConnectionTracker.VerifyConnectionOpenedEventWasFired();
                entityConnectionTracker.VerifyConnectionOpenedEventWasFired();

                storeConnection.Close();

                // verify the close event has been fired on the store connection but not yet on the EntityConnection
                dbConnectionTracker.VerifyConnectionOpenCloseEventsWereFired();
                entityConnectionTracker.VerifyConnectionOpenedEventWasFired();

                // look at the EntityConnection's State (side-effect is we fire an event)
                state = entityConnection.State;
                Assert.Equal(ConnectionState.Closed, state);

                // now the close events should have been fired on both connections
                dbConnectionTracker.VerifyConnectionOpenCloseEventsWereFired();
                entityConnectionTracker.VerifyConnectionOpenCloseEventsWereFired();
            }
        }
        public void EntityConnection_StateChangeEvents_are_fired_when_state_changes()
        {
            using (var context = new SimpleModelContext())
            {
                var entityConnection = (EntityConnection)((IObjectContextAdapter)context).ObjectContext.Connection;

                var dbConnectionTracker = new ConnectionEventsTracker(entityConnection.StoreConnection);
                var entityConnectionTracker = new ConnectionEventsTracker(entityConnection);

                // verify that the open and close events have not been fired yet
                dbConnectionTracker.VerifyNoConnectionEventsWereFired();
                entityConnectionTracker.VerifyNoConnectionEventsWereFired();

                var storeConnection = entityConnection.StoreConnection;
                storeConnection.Open();

                // verify the open event has been fired on the store connection and the EntityConnection
                // was subscribed so it updated too
                dbConnectionTracker.VerifyConnectionOpenedEventWasFired();
                entityConnectionTracker.VerifyConnectionOpenedEventWasFired();

                storeConnection.Close();

                // verify the close event has been fired on the store connection and the EntityConnection
                // was subscribed so it updated too
                dbConnectionTracker.VerifyConnectionOpenCloseEventsWereFired();
                entityConnectionTracker.VerifyConnectionOpenCloseEventsWereFired();
            }
        }
示例#3
0
        public void Implicit_EntityConnection_throws_if_close_EntityConnection_during_query()
        {
            using (var context = new SimpleModelContext())
            {
                EntityConnection entityConnection = (EntityConnection)((IObjectContextAdapter)context).ObjectContext.Connection;

                Assert.True(context.Products.Count() >= 2, "Need at least 2 product entries for test to work below");

                ConnectionEventsTracker dbConnectionTracker = new ConnectionEventsTracker(entityConnection.StoreConnection);
                ConnectionEventsTracker entityConnectionTracker = new ConnectionEventsTracker(entityConnection);

                var query = from p in context.Products
                            select p.Name;

                Assert.Equal(ConnectionState.Closed, entityConnection.State); // entityConnection state
                Assert.Equal(ConnectionState.Closed, entityConnection.StoreConnection.State); // underlying storeConnection state

                IEnumerator<string> enumerator = query.GetEnumerator();
                enumerator.MoveNext();

                // close the entity connection explicitly (i.e. not through context) in middle of query
                entityConnection.Close();
                Assert.Equal(ConnectionState.Closed, entityConnection.State);
                Assert.Equal(ConnectionState.Closed, entityConnection.StoreConnection.State);

                // verify that the open and close events have been fired once and only once on both EntityConnection and underlying DbConnection
                dbConnectionTracker.VerifyConnectionOpenCloseEventsWereFired();
                entityConnectionTracker.VerifyConnectionOpenCloseEventsWereFired();

                // check that we throw when we attempt to use the implicitly-opened entityConnection
                Exception e = Assert.Throws<InvalidOperationException>(() =>
                {
                    enumerator.MoveNext();
                });
                Assert.True("Calling 'Read' when the data reader is closed is not a valid operation." == e.Message);

                enumerator.Dispose();
                Assert.Equal(ConnectionState.Closed, entityConnection.State);
                Assert.Equal(ConnectionState.Closed, entityConnection.StoreConnection.State);

                // verify that the open and close events are not fired again by the second MoveNext() above
                dbConnectionTracker.VerifyConnectionOpenCloseEventsWereFired();
                entityConnectionTracker.VerifyConnectionOpenCloseEventsWereFired();

                // prove that can still re-use the connection even after the above
                Assert.True(context.Products.Count() > 0); // this will check that the query will still execute

                // and show that the entity connection and the store connection are once again closed
                Assert.Equal(ConnectionState.Closed, entityConnection.State);
                Assert.Equal(ConnectionState.Closed, entityConnection.StoreConnection.State);
            }
        }
        public void Implicit_EntityConnection_throws_if_close_underlying_StoreConnection()
        {
            using (var context = new SimpleModelContext())
            {
                var entityConnection = (EntityConnection)((IObjectContextAdapter)context).ObjectContext.Connection;

                Assert.True(context.Products.Count() >= 2, "Need at least 2 product entries for test to work below");

                var dbConnectionTracker = new ConnectionEventsTracker(entityConnection.StoreConnection);
                var entityConnectionTracker = new ConnectionEventsTracker(entityConnection);

                var query = from p in context.Products
                    select p.Name;

                Assert.Equal(ConnectionState.Closed, entityConnection.State); // entityConnection state
                Assert.Equal(ConnectionState.Closed, entityConnection.StoreConnection.State); // underlying storeConnection state

                var enumerator = query.GetEnumerator();
                enumerator.MoveNext();

                Assert.Equal(ConnectionState.Open, entityConnection.State);
                Assert.Equal(ConnectionState.Open, entityConnection.StoreConnection.State);

                // close the underlying store connection without explicitly closing entityConnection
                // (but entityConnection state is updated automatically)
                entityConnection.StoreConnection.Close();
                Assert.Equal(ConnectionState.Closed, entityConnection.State);
                Assert.Equal(ConnectionState.Closed, entityConnection.StoreConnection.State);

                // verify that the open and close events have been fired once and only once on both EntityConnection and underlying DbConnection
                dbConnectionTracker.VerifyConnectionOpenCloseEventsWereFired();
                entityConnectionTracker.VerifyConnectionOpenCloseEventsWereFired();

                // check that we throw when we attempt to use the implicitly-opened entityConnection with closed underlying store connection
                Assert.Throws<EntityCommandExecutionException>(() => enumerator.MoveNext()).ValidateMessage("ADP_DataReaderClosed", "Read");

                enumerator.Dispose();
                Assert.Equal(ConnectionState.Closed, entityConnection.State);
                Assert.Equal(ConnectionState.Closed, entityConnection.StoreConnection.State);

                // verify that the open and close events are not fired again by the second MoveNext() above
                dbConnectionTracker.VerifyConnectionOpenCloseEventsWereFired();
                entityConnectionTracker.VerifyConnectionOpenCloseEventsWereFired();

                // prove that can still re-use the connection even after the above
                Assert.True(context.Products.Count() > 0); // this will check that the query will still execute

                // and show that the entity connection and the store connection are once again closed
                Assert.Equal(ConnectionState.Closed, entityConnection.State);
                Assert.Equal(ConnectionState.Closed, entityConnection.StoreConnection.State);
            }
        }