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)
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)
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, connction_class=pingpong_thrift_client.pool.connction_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_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, connction_class=pingpong_thrift_client.pool.connction_class, ) close_mock = Mock() pool.register_after_close_func(close_mock) client = pool.get_client() client.close() close_mock.assert_called_with(pool, client)