def test_server_restart(socket_dir): """ Test whether server works properly on restart. """ number_of_restarts = 5 def run_server_n_times(n): async def coro(): class MockServer(Server): @rpc_method async def mock_method(self): return {'success': True} server = MockServer(socket_dir=socket_dir) for i in range(n): with server: await server.run_once() asyncio.run(coro()) p = multiprocessing.Process(target=run_server_n_times, args=[number_of_restarts], daemon=True) p.start() for i in range(number_of_restarts): client = Client(socket_dir=socket_dir) response = client.call('mock_server', 'mock_method', timeout=3) assert response['success'] p.terminate() p.join()
def test_sigterm(socket_dir): """ Run a basic mock tests. """ signal_event = multiprocessing.Event() def run_server(): signal_handler = SignalHandler(signal.SIGTERM) signal_socket = signal_handler.get_socket() class MockServer(Server): @rpc_method def mock_method(self): return {'success': True} def signal_handler(self): signum = signal_socket.recv().decode() if signum == 'SIGTERM': signal_event.set() self.stop() server = MockServer(socket_dir=socket_dir) server.register(fd=signal_socket, callback=server.signal_handler) with signal_handler, server: server.run() p = multiprocessing.Process(target=run_server) p.start() mock_client = Client(socket_dir=socket_dir) mock_client.call('mock_server', 'mock_method', timeout=3) p.terminate() assert signal_event.wait(1)
def test_server_cache(socket_dir): counter = multiprocessing.Value('i') counter.value = 0 def run_server(): async def coro(): class MockServer(Server): @rpc_method async def mock_method(self): await asyncio.sleep(0.1) counter.value += 1 return {'success': True} await MockServer(socket_dir=socket_dir).run() asyncio.run(coro()) p = multiprocessing.Process(target=run_server, daemon=True) p.start() # Retry 10 times client = Client(socket_dir=socket_dir) response = client.call(server='mock_server', method='mock_method', timeout=1) assert response['success'] # Assert method executed only once assert counter.value == 1 p.terminate() p.join()
def test_client_returns_result_on_success(): context = mock_context( response={"id": "abc", "result": 7, "error": None}) client = Client('inproc://zrpc', context=context) assert client.add(3, 4) == 7 context.validate()
def run_client(): client = Client(socket_dir=socket_dir) barrier.wait(timeout=1) response = client.call(server='mock_server', method='mock_method', timeout=0.1) assert response['success'] barrier.wait(timeout=1)
def main(): client = Client() while True: timestamp = datetime.datetime.now().strftime('%F_%H-%M-%S-%f') response = client.call(server='test_server', method='func', args=('haha', ), kwargs={'kwarg': 'brt'}) print('RPC response [%s]: %s' % (timestamp, response))
def run_client(): client = Client(socket_dir=socket_dir) response = client.call(server='mock_server', method='mock_method', args=('1', '2'), kwargs={ 'kwarg1': 3, 'kwarg2': 4 }) if response['success']: rpc_event.set()
def test_server_cache(socket_dir): counter = multiprocessing.Value('i') counter.value = 0 class MockServer(Server): @rpc_method def mock_method(self): time.sleep(0.1) counter.value += 1 return {'success': True} def run_server(): MockServer(socket_dir=socket_dir).run() multiprocessing.Process(target=run_server, daemon=True).start() # Retry 10 times client = Client(socket_dir=socket_dir, retry_timeout=0.01) client.call(server='mock_server', method='mock_method', timeout=1) # Assert method executed only once assert counter.value == 1
def test_client_raises_exception_on_failure(): context = mock_context( response={ "id": "abc", "result": None, "error": { "type": "exceptions.TypeError", "message": "TypeError: add expected 2 arguments, got 1", "args": ["add expected 2 arguments, got 1"] } }) client = Client('inproc://zrpc', context=context) assert_raises(Error, client.add, 3) context.validate()
def run_client(): client = Client(socket_dir=socket_dir, retry_timeout=0.1) response = client.call('mock_server', 'mock_method') if response['success']: rpc_event.set()
def run_client(): mock_client = Client(socket_dir=socket_dir) response = mock_client.call('mock_server', 'mock_method') if response['success']: rpc_event.set()
def test_dotted_names_resolve_to_dotted_methods(): context = mock_context(response={"id": "abc", "result": 7, "error": None}) client = Client('inproc://zrpc', context=context) assert client.math.add(3, 4) == 7 context.validate()
def test_client_returns_result_on_success(): context = mock_context(response={"id": "abc", "result": 7, "error": None}) client = Client('inproc://zrpc', context=context) assert client.add(3, 4) == 7 context.validate()
def run_client(): client = Client(socket_dir=socket_dir) try: client.call('non_existant', 'non_existant', timeout=0.1) except RPCTimeoutError: client_event.set()
from zrpc.client import Client client = Client() client.open("tcp://127.0.0.1:5000") response = client.call_raw('echo', b'ping') print(response) client.close()