def test_not_setted_connection_pool_connection_keepalive(
        pingpong_thrift_client, pingpong_service_key, pingpong_thrift_service,
        fake_datetime):
    pool = ClientPool(
        pingpong_thrift_service,
        pingpong_thrift_client.host,
        pingpong_thrift_client.port,
        name=pingpong_service_key,
        raise_empty=False, max_conn=3,
        connection_class=pingpong_thrift_client.pool.connection_class,
    )
    assert pool.keepalive is None
    with pool.connection_ctx() as conn:
        now = time.time()
        assert conn.alive_until is None
        assert conn.test_connection()
        old_connection = conn

    fake_datetime.FAKE_TIME = now + 0.1
    with pool.connection_ctx() as conn:
        assert conn is old_connection

    fake_datetime.FAKE_TIME = now + timedelta(days=100).seconds
    assert old_connection.test_connection()

    with pool.connection_ctx() as conn:
        assert old_connection is conn
def test_set_timeout(pingpong_thrift_client, pingpong_service_key,
                     pingpong_thrift_service, fake_time):
    pool = ClientPool(
        pingpong_thrift_service,
        pingpong_thrift_client.host,
        pingpong_thrift_client.port,
        name=pingpong_service_key,
        raise_empty=False,
        max_conn=3,
        connection_class=pingpong_thrift_client.pool.connection_class,
    )
    client = pool.get_client()

    client.set_client_timeout(0.5 * 1000)
    assert client.sleep(0.2) == 'good morning'

    with pytest.raises(socket.timeout) as e:
        client.sleep(1)
    assert 'timed out' in str(e.value)
    client.close()

    with pytest.raises(socket.timeout) as e:
        with pool.connection_ctx(timeout=1) as client:
            client.sleep(2)
    assert 'timed out' in str(e.value)
def test_setted_connection_pool_connection_keepalive(
        pingpong_thrift_client, pingpong_service_key, pingpong_thrift_service,
        fake_datetime):
    keep_alive = 1
    pool = ClientPool(
        pingpong_thrift_service,
        pingpong_thrift_client.host,
        pingpong_thrift_client.port,
        name=pingpong_service_key,
        raise_empty=False, max_conn=3,
        connection_class=pingpong_thrift_client.pool.connection_class,
        keepalive=keep_alive
    )
    assert pool.keepalive == keep_alive
    with pool.connection_ctx() as conn:
        now = time.time()
        assert conn.alive_until == now + keep_alive
        assert conn.test_connection()
        old_connection = conn

    fake_datetime.FAKE_TIME = now + 0.1
    with pool.connection_ctx() as conn:
        assert conn is old_connection

    fake_datetime.FAKE_TIME = now + keep_alive + 2
    assert not old_connection.test_connection()

    with pool.connection_ctx() as conn:
        assert old_connection is not conn
def test_connection_pool_generation(pingpong_thrift_client,
                                    pingpong_service_key,
                                    pingpong_thrift_service, fake_datetime):
    pool = ClientPool(
        pingpong_thrift_service,
        pingpong_thrift_client.host,
        pingpong_thrift_client.port,
        name=pingpong_service_key,
        raise_empty=False,
        max_conn=3,
        connection_class=pingpong_thrift_client.pool.connection_class,
    )
    c = pool.produce_client()
    assert c.pool_generation == pool.generation == 0

    pool.clear()

    c2 = pool.produce_client()
    assert c2.pool_generation == pool.generation == 1

    pool.put_back_connection(c)
    pool.put_back_connection(c2)

    for c in pool.connections:
        assert c.pool_generation == pool.generation
def test_not_setted_connection_pool_connection_keepalive(
        pingpong_thrift_client, pingpong_service_key, pingpong_thrift_service,
        fake_datetime):
    pool = ClientPool(
        pingpong_thrift_service,
        pingpong_thrift_client.host,
        pingpong_thrift_client.port,
        name=pingpong_service_key,
        raise_empty=False, max_conn=3,
        connection_class=pingpong_thrift_client.pool.connection_class,
    )
    assert pool.keepalive is None
    with pool.connection_ctx() as conn:
        now = time.time()
        assert conn.alive_until is None
        assert conn.test_connection()
        old_connection = conn

    fake_datetime.FAKE_TIME = now + 0.1
    with pool.connection_ctx() as conn:
        assert conn is old_connection

    fake_datetime.FAKE_TIME = now + timedelta(days=100).seconds
    assert old_connection.test_connection()

    with pool.connection_ctx() as conn:
        assert old_connection is conn
def test_setted_connection_pool_connection_keepalive(
        pingpong_thrift_client, pingpong_service_key, pingpong_thrift_service,
        fake_datetime):
    keep_alive = 1
    pool = ClientPool(
        pingpong_thrift_service,
        pingpong_thrift_client.host,
        pingpong_thrift_client.port,
        name=pingpong_service_key,
        raise_empty=False, max_conn=3,
        connection_class=pingpong_thrift_client.pool.connection_class,
        keepalive=keep_alive
    )
    assert pool.keepalive == keep_alive
    with pool.connection_ctx() as conn:
        now = time.time()
        assert conn.alive_until == now + keep_alive
        assert conn.test_connection()
        old_connection = conn

    fake_datetime.FAKE_TIME = now + 0.1
    with pool.connection_ctx() as conn:
        assert conn is old_connection

    fake_datetime.FAKE_TIME = now + keep_alive + 2
    assert not old_connection.test_connection()

    with pool.connection_ctx() as conn:
        assert old_connection is not conn
def test_conn_close_hook(pingpong_thrift_client, pingpong_service_key,
                         pingpong_thrift_service, fake_time):
    pool = ClientPool(
        pingpong_thrift_service,
        pingpong_thrift_client.host,
        pingpong_thrift_client.port,
        name=pingpong_service_key,
        raise_empty=False, max_conn=3,
        connection_class=pingpong_thrift_client.pool.connection_class,
    )
    close_mock = Mock()
    pool.register_after_close_func(close_mock)
    client = pool.get_client()
    client.close()
    close_mock.assert_called_with(pool, client)
def test_conn_close_hook(pingpong_thrift_client, pingpong_service_key,
                         pingpong_thrift_service, fake_time):
    pool = ClientPool(
        pingpong_thrift_service,
        pingpong_thrift_client.host,
        pingpong_thrift_client.port,
        name=pingpong_service_key,
        raise_empty=False, max_conn=3,
        connection_class=pingpong_thrift_client.pool.connection_class,
    )
    close_mock = Mock()
    pool.register_after_close_func(close_mock)
    client = pool.get_client()
    client.close()
    close_mock.assert_called_with(pool, client)
def test_api_call_context(
        pingpong_thrift_client, pingpong_service_key, pingpong_thrift_service,
        fake_time):
    from thrift_connector.hooks import before_call, after_call

    mock_before_hook = Mock()
    mock_after_hook = Mock()
    before_call.register(mock_before_hook)
    after_call.register(mock_after_hook)

    pool = ClientPool(
        pingpong_thrift_service,
        pingpong_thrift_client.host,
        pingpong_thrift_client.port,
        name=pingpong_service_key,
        raise_empty=False, max_conn=3,
        connction_class=pingpong_thrift_client.pool.connction_class,
    )
    pool.ping()

    # get one client manually, there should be one client in pool,
    # since there's only one call
    client = pool.get_client()
    assert client.test_connection()
    pool.put_back_connection(client)

    mock_before_hook.assert_called_with(pool, client, 'ping', fake_time.time())
    mock_after_hook.assert_called_with(pool, client, 'ping', fake_time.time(),
                                       0, 'pong')
def test_set_timeout(pingpong_thrift_client, pingpong_service_key,
                     pingpong_thrift_service, fake_time):
    pool = ClientPool(
        pingpong_thrift_service,
        pingpong_thrift_client.host,
        pingpong_thrift_client.port,
        name=pingpong_service_key,
        raise_empty=False, max_conn=3,
        connection_class=pingpong_thrift_client.pool.connection_class,
    )
    client = pool.get_client()

    client.set_client_timeout(0.5 * 1000)
    assert client.sleep(0.2) == 'good morning'

    with pytest.raises(socket.timeout) as e:
        client.sleep(1)
    assert 'timed out' in str(e.value)
    client.close()

    with pytest.raises(socket.timeout) as e:
        with pool.connection_ctx(timeout=1) as client:
            client.sleep(2)
    assert 'timed out' in str(e.value)
def test_connection_pool_generation(
        pingpong_thrift_client, pingpong_service_key, pingpong_thrift_service,
        fake_datetime):
    pool = ClientPool(
        pingpong_thrift_service,
        pingpong_thrift_client.host,
        pingpong_thrift_client.port,
        name=pingpong_service_key,
        raise_empty=False, max_conn=3,
        connection_class=pingpong_thrift_client.pool.connection_class,
    )
    c = pool.produce_client()
    assert c.pool_generation == pool.generation == 0

    pool.clear()

    c2 = pool.produce_client()
    assert c2.pool_generation == pool.generation == 1

    pool.put_back_connection(c)
    pool.put_back_connection(c2)

    for c in pool.connections:
        assert c.pool_generation == pool.generation
def test_api_call_context(pingpong_thrift_client, pingpong_service_key,
                          pingpong_thrift_service, fake_time):
    from thrift_connector.hooks import before_call, after_call

    mock_before_hook = Mock()
    mock_after_hook = Mock()
    before_call.register(mock_before_hook)
    after_call.register(mock_after_hook)

    pool = ClientPool(
        pingpong_thrift_service,
        pingpong_thrift_client.host,
        pingpong_thrift_client.port,
        name=pingpong_service_key,
        raise_empty=False,
        max_conn=3,
        connection_class=pingpong_thrift_client.pool.connection_class,
    )
    pool.ping()

    # get one client manually, there should be one client in pool,
    # since there's only one call
    client = pool.get_client()
    assert client.test_connection()
    pool.put_back_connection(client)

    mock_before_hook.assert_called_with(pool, client, 'ping', fake_time.time())
    mock_after_hook.assert_called_with(pool, client, 'ping', fake_time.time(),
                                       0, 'pong')

    # raise Exception when raises specified
    mock_before_hook_with_err = Mock(side_effect=TypeError('test'))
    before_call.register(mock_before_hook_with_err, raises=(TypeError, ))
    with pool.connection_ctx() as client:
        with pytest.raises(TypeError) as exc_info:
            client.win()

    assert "test" in str(exc_info.value)
    before_call.callbacks.clear()
    after_call.callbacks.clear()
def test_api_call_context(
        pingpong_thrift_client, pingpong_service_key, pingpong_thrift_service,
        fake_time):
    from thrift_connector.hooks import before_call, after_call

    mock_before_hook = Mock()
    mock_after_hook = Mock()
    before_call.register(mock_before_hook)
    after_call.register(mock_after_hook)

    pool = ClientPool(
        pingpong_thrift_service,
        pingpong_thrift_client.host,
        pingpong_thrift_client.port,
        name=pingpong_service_key,
        raise_empty=False, max_conn=3,
        connection_class=pingpong_thrift_client.pool.connection_class,
    )
    pool.ping()

    # get one client manually, there should be one client in pool,
    # since there's only one call
    client = pool.get_client()
    assert client.test_connection()
    pool.put_back_connection(client)

    mock_before_hook.assert_called_with(pool, client, 'ping', fake_time.time())
    mock_after_hook.assert_called_with(pool, client, 'ping', fake_time.time(),
                                       0, 'pong')

    # raise Exception when raises specified
    mock_before_hook_with_err = Mock(side_effect=TypeError('test'))
    before_call.register(mock_before_hook_with_err, raises=(TypeError,))
    with pool.connection_ctx() as client:
        with pytest.raises(TypeError) as exc_info:
            client.win()
        assert "test" in str(exc_info.value)
    before_call.callbacks.clear()
    after_call.callbacks.clear()

    mock_before_hook_with_err = Mock(side_effect=TypeError('test'))
    before_call.register(mock_before_hook_with_err, raises=[TypeError,
                                                            RuntimeError])
    with pool.connection_ctx() as client:
        with pytest.raises(TypeError) as exc_info:
            client.win()
        assert "test" in str(exc_info.value)
    before_call.callbacks.clear()
    after_call.callbacks.clear()

    mock_before_hook_with_err = Mock(side_effect=TypeError('test'))
    before_call.register(mock_before_hook_with_err, raises=(TypeError,
                                                            RuntimeError))
    with pool.connection_ctx() as client:
        with pytest.raises(TypeError) as exc_info:
            client.win()
        assert "test" in str(exc_info.value)
    before_call.callbacks.clear()
    after_call.callbacks.clear()

    before_call.register(mock_before_hook_with_err, raises=TypeError)
    with pool.connection_ctx() as client:
        with pytest.raises(TypeError) as exc_info:
            client.win()
        assert "test" in str(exc_info.value)
    before_call.callbacks.clear()
    after_call.callbacks.clear()