Пример #1
0
        public async Task Lazy_connection_is_async_opened_and_closed_when_necessary()
        {
            using (var connection = new FakeRelationalConnection(
                       CreateOptions(new FakeRelationalOptionsExtension {
                ConnectionString = "Database=FrodoLives"
            })))
            {
                Assert.Equal(0, connection.DbConnections.Count);

                var cancellationToken = new CancellationTokenSource().Token;
                await connection.OpenAsync(cancellationToken);

                Assert.Equal(1, connection.DbConnections.Count);

                var dbConnection = connection.DbConnections[0];
                Assert.Equal(1, dbConnection.OpenAsyncCount);

                await connection.OpenAsync(cancellationToken);

                await connection.OpenAsync(cancellationToken);

                Assert.Equal(1, dbConnection.OpenAsyncCount);

                connection.Close();
                connection.Close();

                Assert.Equal(1, dbConnection.OpenAsyncCount);
                Assert.Equal(0, dbConnection.CloseCount);

                connection.Close();

                Assert.Equal(1, dbConnection.OpenAsyncCount);
                Assert.Equal(1, dbConnection.CloseCount);

                await connection.OpenAsync(cancellationToken);

                Assert.Equal(2, dbConnection.OpenAsyncCount);

                connection.Close();

                Assert.Equal(2, dbConnection.OpenAsyncCount);
                Assert.Equal(2, dbConnection.CloseCount);
            }
        }
Пример #2
0
        public async Task Existing_connection_can_be_opened_and_closed_externally_async()
        {
            var dbConnection = new FakeDbConnection(
                "Database=FrodoLives",
                state: ConnectionState.Closed);

            using (var connection = new FakeRelationalConnection(
                       CreateOptions(new FakeRelationalOptionsExtension().WithConnection(dbConnection))))
            {
                Assert.Equal(0, connection.DbConnections.Count);

                await connection.OpenAsync(default(CancellationToken));

                Assert.Equal(0, connection.DbConnections.Count);

                Assert.Equal(1, dbConnection.OpenCount);

                connection.Close();

                Assert.Equal(1, dbConnection.OpenCount);
                Assert.Equal(1, dbConnection.CloseCount);

                dbConnection.SetState(ConnectionState.Open);

                await connection.OpenAsync(default(CancellationToken));

                Assert.Equal(1, dbConnection.OpenCount);
                Assert.Equal(1, dbConnection.CloseCount);

                connection.Close();

                Assert.Equal(1, dbConnection.OpenCount);
                Assert.Equal(1, dbConnection.CloseCount);

                dbConnection.SetState(ConnectionState.Closed);

                await connection.OpenAsync(default(CancellationToken));

                Assert.Equal(2, dbConnection.OpenCount);
                Assert.Equal(1, dbConnection.CloseCount);

                connection.Close();

                Assert.Equal(2, dbConnection.OpenCount);
                Assert.Equal(2, dbConnection.CloseCount);

                dbConnection.SetState(ConnectionState.Open);

                await connection.OpenAsync(default(CancellationToken));

                Assert.Equal(2, dbConnection.OpenCount);
                Assert.Equal(2, dbConnection.CloseCount);

                dbConnection.SetState(ConnectionState.Closed);

                connection.Close();

                Assert.Equal(2, dbConnection.OpenCount);
                Assert.Equal(2, dbConnection.CloseCount);

                await connection.OpenAsync(default(CancellationToken));

                await connection.OpenAsync(default(CancellationToken));

                Assert.Equal(3, dbConnection.OpenCount);

                dbConnection.SetState(ConnectionState.Closed);

                await connection.OpenAsync(default(CancellationToken));

                Assert.Equal(4, dbConnection.OpenCount);
                Assert.Equal(2, dbConnection.CloseCount);

                dbConnection.SetState(ConnectionState.Closed);

                connection.Close();

                Assert.Equal(4, dbConnection.OpenCount);
                Assert.Equal(2, dbConnection.CloseCount);
            }
        }