Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
    def test_api_call_context(self):
        from thrift_connector.hooks import before_call, after_call

        pool = self.pingpong_thrift_client.pool

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

        fake_time = time.time()
        with fake_datetime(fake_time):
            with (yield pool.connection_ctx()) as conn:
                ping_future = conn.ping()
                yield ping_future

            mock_before_hook.assert_called_with(pool, conn, 'ping', fake_time)
            mock_after_hook.assert_called_with(pool, conn, 'ping', fake_time,
                                               0, ping_future)

        # 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 (yield pool.connection_ctx()) as client:
            with pytest.raises(TypeError) as exc_info:
                yield 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,
        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)
Ejemplo n.º 4
0
    def test_api_call_context(self):
        from thrift_connector.hooks import before_call, after_call

        pool = self.pingpong_thrift_client.pool

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

        fake_time = time.time()
        with fake_datetime(fake_time):
            with (yield pool.connection_ctx()) as conn:
                ping_future = conn.ping()
                yield ping_future

            mock_before_hook.assert_called_with(pool, conn, 'ping', fake_time)
            mock_after_hook.assert_called_with(
                pool, conn, 'ping', fake_time, 0, ping_future)

        # 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 (yield pool.connection_ctx()) as client:
            with pytest.raises(TypeError) as exc_info:
                yield 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()
Ejemplo n.º 6
0
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()