Пример #1
0
    public void Existing_connection_can_be_opened_and_closed_externally()
    {
        var dbConnection = new FakeDbConnection(
            "Database=FrodoLives");

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

        connection.Open();

        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);

        connection.Open();

        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);

        connection.Open();

        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);

        connection.Open();

        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);

        connection.Open();
        connection.Open();

        Assert.Equal(3, dbConnection.OpenCount);

        dbConnection.SetState(ConnectionState.Closed);

        connection.Open();

        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);
    }
Пример #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);
            }
        }