def test_end_to_end(self): def do_test(client, server, server_serve): url = 'inproc://%s' % uuid.uuid4() server.socket.listen(url) client.socket.dial(url) server_task = tasks.spawn(server_serve) client_task = tasks.spawn(client.m.greet(name='world')) with self.assertRaises(kernels.KernelTimeout): kernels.run(timeout=0.005) self.assertTrue(client_task.is_completed()) self.assertEqual(client_task.get_result_nonblocking(), 'Hello, world') self.assertFalse(server_task.is_completed()) server.socket.close() kernels.run(timeout=1) self.assertTrue(server_task.is_completed()) self.assertIsNone(server_task.get_result_nonblocking()) app = TestApplication() with servers.Server(app, Request, Response, WIRE_DATA) as server: with clients.Client(Request, Response, WIRE_DATA) as client: do_test(client, server, server.serve) app = TestApplication() server = servers.Server(app, Request, Response, WIRE_DATA) with clients.Client(Request, Response, WIRE_DATA) as client: with server: do_test(client, server, server.serve)
def test_timeout(self): with clients.Client(Request, Response, WIRE_DATA) as client: client.socket.send_timeout = 1 # Unit: milliseconds. client.socket.dial('inproc://%s' % uuid.uuid4()) task = tasks.spawn(client.m.greet(name='world')) kernels.run(timeout=0.01) self.assertTrue(task.is_completed()) with self.assertRaises(clients.ServerTimeoutError): task.get_result_nonblocking() task = tasks.spawn( client.m.greet.on_timeout_return(42)(name='world')) kernels.run(timeout=0.01) self.assertTrue(task.is_completed()) self.assertEqual(task.get_result_nonblocking(), 42)
def test_invalid_response(self): with clients.Client(Request, Response, WIRE_DATA) as client: with nng.Socket(nng.Protocols.REP0) as socket: url = 'inproc://%s' % uuid.uuid4() socket.listen(url) client.socket.dial(url) task = tasks.spawn(client.m.greet(name='world')) with self.assertRaises(kernels.KernelTimeout): kernels.run(timeout=0) socket.recv() socket.send(b'{"result": {"greet": 42}, "error": null}') kernels.run(timeout=1) self.assertTrue(task.is_completed()) with self.assertRaisesRegex(AssertionError, r'expect.*str'): task.get_result_nonblocking()
def test_success(self): with clients.Client(Request, Response, WIRE_DATA) as client: with nng.Socket(nng.Protocols.REP0) as socket: url = 'inproc://%s' % uuid.uuid4() socket.listen(url) client.socket.dial(url) task = tasks.spawn(client.m.greet(name='world')) with self.assertRaises(kernels.KernelTimeout): kernels.run(timeout=0) request = WIRE_DATA.to_upper(Request, socket.recv()) self.assertEqual(request.args, Request.m.greet(name='world')) response = Response(result=Response.Result( greet='hello world')) socket.send(WIRE_DATA.to_lower(response)) kernels.run(timeout=1) self.assertTrue(task.is_completed()) self.assertEqual(task.get_result_nonblocking(), 'hello world')
def make_client(): return clients.Client( interfaces.DatabaseRequest, interfaces.DatabaseResponse, capnps.WIRE_DATA, )
def test_method_names(self): with clients.Client(Request, Response, WIRE_DATA) as client: self.assertEqual(sorted(client.m), ['f', 'greet'])
def make_client(): return clients.Client( CalculatorRequest, CalculatorResponse, jsons.JsonWireData(), )