def test_call_method_ping_pong(self): server = JsonRpcServer(reflect_app) server.listen(('127.0.0.1', 0)) addr = server.addresses[0] client = JsonRpcClient(echo_app) client.connect(addr) result = client.call_method('echo', 'foo') self.assertEqual(result, ['foo']) server.close() client.close()
def test_call_method_tcp(self): server = JsonRpcServer(echo_app) server.listen(('localhost', 0)) addr = server.addresses[0] client = JsonRpcClient() client.connect(addr) result = client.call_method('echo', 'foo') self.assertEqual(result, ['foo']) server.close() client.close()
def test_call_method_multiple_args(self): server = JsonRpcServer(echo_app) server.listen(('127.0.0.1', 0)) addr = server.addresses[0] client = JsonRpcClient() client.connect(addr) result = client.call_method('echo', 'foo', 'bar') self.assertEqual(result, ['foo', 'bar']) server.close() client.close()
def test_call_method_pipe(self): server = JsonRpcServer(echo_app) server.listen(self.pipename(abstract=True)) addr = server.addresses[0] client = JsonRpcClient() client.connect(addr) result = client.call_method('echo', 'foo') self.assertEqual(result, ['foo']) server.close() client.close()
def test_send_notification(self): server = JsonRpcServer(notification_app()) server.listen(('127.0.0.1', 0)) addr = server.addresses[0] client = JsonRpcClient() client.connect(addr) client.send_notification('notify_foo', 'foo') notifications = client.call_method('get_notifications') self.assertEqual(notifications, [['notify_foo', ['foo']]]) server.close() client.close()
def test_call_method_ssl(self): server = JsonRpcServer(echo_app) context = self.get_ssl_context() server.listen(('localhost', 0), ssl=context) addr = server.addresses[0] client = JsonRpcClient() client.connect(addr, ssl=context) result = client.call_method('echo', 'foo') self.assertEqual(result, ['foo']) server.close() client.close()
def test_call_method_error(self): server = JsonRpcServer(echo_app) server.listen(('127.0.0.1', 0)) addr = server.addresses[0] client = JsonRpcClient() client.connect(addr) exc = self.assertRaises(JsonRpcError, client.call_method, 'echo2') self.assertIsInstance(exc, JsonRpcMethodCallError) self.assertIsInstance(exc.error, dict) self.assertEqual(exc.error['code'], jsonrpc.METHOD_NOT_FOUND) server.close() client.close()
def perf_message_throughput(self): server = JsonRpcServer(echo_app) server.listen(('127.0.0.1', 0)) addr = server.addresses[0] client = JsonRpcClient() client.connect(addr) nrequests = 0 t0 = t1 = time.time() while t1 - t0 < 1: result = client.call_method('echo', 'foo') self.assertEqual(result, ['foo']) nrequests += 1 t1 = time.time() throughput = nrequests / (t1 - t0) self.add_result(throughput) server.close() client.close()
def test_send_random(self): server = JsonRpcServer(echo_app) server.listen(('127.0.0.1', 0)) addr = server.addresses[0] client = JsonRpcClient() client.connect(addr) exc = None try: while True: chunk = os.urandom(1024) client.transport.write(chunk) gruvi.sleep(0) except Exception as e: exc = e self.assertIsInstance(exc, TransportError) server.close() client.close()
def test_call_method_trace(self): server = JsonRpcServer(echo_app) server.listen(('localhost', 0)) server._set_tracefile(self.tempname('json.trace')) addr = server.addresses[0] client = JsonRpcClient() client.connect(addr) result = client.call_method('echo', 'foo') self.assertEqual(result, ['foo']) server.close() client.close()
def test_call_method_error(self): server = JsonRpcServer(echo_app) server.listen(('127.0.0.1', 0)) addr = server.addresses[0] client = JsonRpcClient() client.connect(addr) exc = self.assertRaises(JsonRpcError, client.call_method, 'echo2') self.assertIsInstance(exc, JsonRpcError) self.assertIsInstance(exc.error, dict) self.assertEqual(exc.error['code'], jsonrpc.METHOD_NOT_FOUND) server.close() client.close()
def test_connection_limit(self): server = JsonRpcServer(echo_app) server.listen(('127.0.0.1', 0)) addr = server.addresses[0] server.max_connections = 2 clients = [] exc = None try: for i in range(3): client = JsonRpcClient(timeout=2) client.connect(addr) client.call_method('echo') clients.append(client) except Exception as e: exc = e self.assertIsInstance(exc, TransportError) self.assertEqual(len(server.connections), server.max_connections) for client in clients: client.close() server.close()
def test_connection_limit(self): server = JsonRpcServer(echo_app) server.listen(('127.0.0.1', 0)) addr = server.addresses[0] server.max_connections = 10 clients = [] exc = None try: for i in range(15): client = JsonRpcClient(timeout=2) client.connect(addr) client.call_method('echo') clients.append(client) except Exception as e: exc = e self.assertIsInstance(exc, TransportError) self.assertLessEqual(len(server.connections), server.max_connections) for client in clients: client.close() server.close()
# Ping-pong between JSON-RPC client and server. from gruvi.jsonrpc import JsonRpcClient, JsonRpcServer def handler(message, transport, protocol): method = message.get('method') if method == 'ping': protocol.send_response(message, 'pong') server = JsonRpcServer(handler) server.listen(('localhost', 0)) addr = server.addresses[0] client = JsonRpcClient() client.connect(addr) result = client.call_method('ping') print('result = {}'.format(result))