async def test_hostname(self) -> None: async with server_in_event_loop() as addr: async with get_client(TestService, host="localhost", port=addr.port) as client: sum = await client.add(1, 2) self.assertEqual(3, sum)
async def test_oneway(self) -> None: async with server_in_event_loop() as addr: async with get_client(TestService, host=addr.ip, port=addr.port) as client: res = await client.oneway() self.assertIsNone(res) await asyncio.sleep(1) # wait for server to clear the queue
async def test_exception(self) -> None: async with server_in_event_loop() as addr: async with get_client(TestService, host=addr.ip, port=addr.port) as client: res = await client.divide(6, 3) self.assertAlmostEqual(2, res) with self.assertRaises(ArithmeticException): await client.divide(1, 0)
async def test_derived_service(self) -> None: async with server_in_event_loop() as addr: async with get_client(EchoService, host=addr.ip, port=addr.port) as client: out = await client.echo("hello") self.assertEqual("hello", out) sum = await client.add(1, 2) self.assertEqual(3, sum)
async def test_persistent_header(self) -> None: async with server_in_event_loop() as addr: async with get_client(TestService, host="localhost", port=addr.port) as client: client.set_persistent_header(TEST_HEADER_KEY, TEST_HEADER_VALUE) value = await client.readHeader(TEST_HEADER_KEY) self.assertEqual(TEST_HEADER_VALUE, value)
async def test_deriving_from_external_service(self) -> None: async with server_in_event_loop() as addr: async with get_client(LeafService, host=addr.ip, port=addr.port) as client: rev = await client.reverse([1, 2, 3]) self.assertEqual([3, 2, 1], list(rev)) out = await client.echo("hello") self.assertEqual("hello", out) sum = await client.add(1, 2) self.assertEqual(3, sum)
async def test_unexpected_exception(self) -> None: async with server_in_event_loop() as addr: async with get_client(TestService, host=addr.ip, port=addr.port) as client: with self.assertRaises(ApplicationError) as ex: await client.surprise() self.assertEqual(ex.exception.message, "ValueError('Surprise!')") self.assertEqual(ex.exception.type, ApplicationErrorType.UNKNOWN)
async def test_stream_sumAndNums_throws(self) -> None: async with server_in_event_loop() as addr: async with get_client( TestService, host="localhost", port=addr.port, client_type=ClientType.THRIFT_ROCKET_CLIENT_TYPE, ) as client: with self.assertRaises(ArithmeticException) as e: await client.nums(4, 2) self.assertEqual(e.exception.msg, "from outside of stream")
async def test_oneway_with_rocket(self) -> None: async with server_in_event_loop() as addr: async with get_client( TestService, host=addr.ip, port=addr.port, client_type=ClientType.THRIFT_ROCKET_CLIENT_TYPE, ) as client: res = await client.oneway() self.assertIsNone(res) await asyncio.sleep(1) # wait for server to clear the queue
async def test_client_type_and_protocol(self) -> None: async with server_in_event_loop() as addr: async with get_client( TestService, host=addr.ip, port=addr.port, client_type=ClientType.THRIFT_ROCKET_CLIENT_TYPE, protocol=Protocol.BINARY, ) as client: sum = await client.add(1, 2) self.assertEqual(3, sum)
async def test_stream_nums(self) -> None: async with server_in_event_loop() as addr: async with get_client( TestService, host="localhost", port=addr.port, client_type=ClientType.THRIFT_ROCKET_CLIENT_TYPE, ) as client: stream = await client.nums(2, 4) result = [] async for num in stream: result.append(num) self.assertEqual( result, [SimpleResponse(value=f"{i}") for i in (2, 3, 4)])
async def test_proxy_factory(self) -> None: # Should be empty before we assign it self.assertEqual(get_proxy_factory(), None) # Should be able to assign/get a test factory install_proxy_factory(test_proxy_factory) self.assertEqual(get_proxy_factory(), test_proxy_factory) async with server_in_event_loop() as addr: self.assertIsInstance( get_client(TestService, host=addr.ip, port=addr.port), ThriftClientTestProxy, ) # Should be able to unhook a factory install_proxy_factory(None) self.assertEqual(get_proxy_factory(), None)
async def test_stream_nums_throws_inside(self) -> None: async with server_in_event_loop() as addr: async with get_client( TestService, host="localhost", port=addr.port, client_type=ClientType.THRIFT_ROCKET_CLIENT_TYPE, ) as client: stream = await client.nums(8, 11) result = [] with self.assertRaises(ArithmeticException) as e: async for num in stream: result.append(num) self.assertEqual( result, [SimpleResponse(value=f"{i}") for i in (8, 9, 10)]) self.assertEqual(e.exception.msg, "from inside of stream")
async def test_stream_nums_throws_undeclared(self) -> None: async with server_in_event_loop() as addr: async with get_client( TestService, host="localhost", port=addr.port, client_type=ClientType.THRIFT_ROCKET_CLIENT_TYPE, ) as client: stream = await client.nums(-1, 2) result = [] with self.assertRaises(ApplicationError) as e: async for num in stream: result.append(num) self.assertEqual( result, [SimpleResponse(value=f"{i}") for i in (-1, 0, 1, 2)]) self.assertEqual( e.exception.message, "apache::thrift::TApplicationException: ValueError('from is negative')", )
async def test_exit_callback(self) -> None: class Callback: def __init__(self): self.triggered = False def trigger(self): self.triggered = True async def async_trigger(self): self.triggered = True cb1 = Callback() cb2 = Callback() async with server_in_event_loop() as addr: async with get_client(TestService, host=addr.ip, port=addr.port) as client: client._at_aexit(cb1.trigger) client._at_aexit(cb2.async_trigger) self.assertTrue(cb1.triggered) self.assertTrue(cb2.triggered)
async def test_void_return_with_exception(self) -> None: async with server_in_event_loop() as addr: async with get_client(TestService, host=addr.ip, port=addr.port) as client: with self.assertRaises(EmptyException): await client.oops()
async def test_void_return(self) -> None: async with server_in_event_loop() as addr: async with get_client(TestService, host=addr.ip, port=addr.port) as client: res = await client.noop() self.assertIsNone(res)