def test_client(): """test of Server class """ logger = PythonLogger(LogLevel.TRACE) name = 'add' logger = PythonLogger() executor = FunctionMethodExecutor(logger, name, add) method_server = SimpleMethodServer(logger, [executor]) server_config = ServerConfig() server_config.tcp_acceptors = [TCPAcceptorConfig()] server = Server(logger, server_config, method_server) server.start() client_config = ClientConfig() client = Client(logger, client_config) client.start() assert client.request(name, 2, 3) == 5 assert ADD_CALLED.is_set() ADD_CALLED.clear() client.notify(name, 2, 3) ADD_CALLED.wait(timeout=10) client.stop() server.stop()
def test_python_logger(caplog): """test of PythonLogger class """ logger = PythonLogger() logger.write("mprpc.test_python_logger", "python_logger_test.py", 1, "test_python_logger", LogLevel.WARN, "warning message") assert caplog.record_tuples == [("mprpc.test_python_logger", logging.WARN, "warning message")] caplog.clear()
def test_simple_method_server_request_invalid_method(): """test of requests for non-existing methods to SimpleMethodServer class """ name = 'test_method' executor = FunctionMethodExecutor(PythonLogger(), name, add) server = SimpleMethodServer(PythonLogger(), [executor]) session = MockSession() msgid = 37 name = 'invalid_method' params = [2, 3] request = pack_request(msgid, name, params) err = None has_response = None response = None processed = Event() def handler(err_in: ErrorInfo, has_response_in: bool, response_in: MessageData): """handler """ nonlocal err nonlocal has_response nonlocal response nonlocal processed err = err_in has_response = has_response_in response = response_in processed.set() server.async_process_message(session, request, handler) timeout = 10.0 processed.wait(timeout=timeout) assert processed.is_set() assert err is not None assert not err assert has_response assert response is not None response = validate_message(unpack_object(response)) assert response.msgtype == MsgType.RESPONSE assert response.msgid == msgid assert response.error is not None
def test_function_method_executor_request(): """test of requests in FunctionMethodExecutor class """ name = 'test_method' executor = FunctionMethodExecutor(PythonLogger(), name, add) assert executor.name == name assert executor.function == add # pylint: disable=comparison-with-callable session = MockSession() msgid = 37 params = [2, 3] request = Request(MsgType.REQUEST, msgid, name, params) response_data = executor.process_request(session, request) response = validate_message(unpack_object(response_data)) assert response.msgtype == MsgType.RESPONSE assert response.msgid == msgid assert response.error is None assert response.result == 5 msgid = 37 params = [2] request = Request(MsgType.REQUEST, msgid, name, params) response_data = executor.process_request(session, request) response = validate_message(unpack_object(response_data)) assert response.msgtype == MsgType.RESPONSE assert response.msgid == msgid assert response.error is not None
def test_simple_method_server_notification(): """test of notifications to SimpleMethodServer class """ name = 'test_method' executor = FunctionMethodExecutor(PythonLogger(), name, add) server = SimpleMethodServer(PythonLogger(), [executor]) session = MockSession() params = [2, 3] notification = pack_notification(name, params) err = None has_response = None response = None processed = Event() def handler(err_in: ErrorInfo, has_response_in: bool, response_in: MessageData): """handler """ nonlocal err nonlocal has_response nonlocal response nonlocal processed err = err_in has_response = has_response_in response = response_in processed.set() server.async_process_message(session, notification, handler) timeout = 10.0 processed.wait(timeout=timeout) assert processed.is_set() assert err is not None assert not err assert has_response is not None assert not has_response assert response == MessageData()
def test_function_method_executor_notification(): """test of notifications in FunctionMethodExecutor class """ name = 'test_method' executor = FunctionMethodExecutor(PythonLogger(), name, add) assert executor.name == name assert executor.function == add # pylint: disable=comparison-with-callable session = MockSession() params = [2, 3] notification = Notification(MsgType.NOTIFICATION, name, params) session = MockSession() executor.process_notification(session, notification) params = [2] notification = Notification(MsgType.NOTIFICATION, name, params) executor.process_notification(session, notification)
def test_server(): """test of Server class """ logger = PythonLogger(LogLevel.TRACE) name = 'add' logger = PythonLogger() executor = FunctionMethodExecutor(logger, name, add) method_server = SimpleMethodServer(logger, [executor]) server_config = ServerConfig() server_config.tcp_acceptors = [TCPAcceptorConfig()] server = Server(logger, server_config, method_server) server.start() client_config = ClientConfig() response_error = None response_data = None response_received_event = Event() def client_process_message(error: ErrorInfo, data: MessageData): nonlocal response_error nonlocal response_data nonlocal response_received_event response_error = error response_data = data response_received_event.set() client = PythonClientHelper(logger, client_config, client_process_message) client.start() request_error = None request_sent_event = Event() def on_request_sent(error: ErrorInfo): nonlocal request_error nonlocal request_sent_event request_error = error request_sent_event.set() msgid = 37 data = pack_request(msgid, name, [2, 3]) client.async_send(data, on_request_sent) timeout = 10.0 request_sent_event.wait(timeout=timeout) assert request_sent_event.is_set() assert not request_error response_received_event.wait(timeout=timeout) assert response_received_event.is_set() assert not response_error response = validate_message(unpack_object(response_data)) assert response.msgtype == MsgType.RESPONSE assert response.msgid == msgid assert response.error is None assert response.result == 5 client.stop() server.stop()
def test_python_server_helper(): """test of PythonServerHelper class """ logger = PythonLogger(LogLevel.TRACE) request_received_event = Event() def server_process_message( _, data: MessageData, handler: Callable[[ErrorInfo, bool, MessageData], Any]): nonlocal request_received_event request_received_event.set() handler(ErrorInfo(), True, data) server_config = ServerConfig() server_config.tcp_acceptors = [TCPAcceptorConfig()] server = PythonServerHelper(logger, server_config, server_process_message) server.start() client_config = ClientConfig() response_error = None response_data = None response_received_event = Event() def client_process_message(error: ErrorInfo, data: MessageData): nonlocal response_error nonlocal response_data nonlocal response_received_event response_error = error response_data = data response_received_event.set() client = PythonClientHelper(logger, client_config, client_process_message) client.start() request_error = None request_sent_event = Event() def on_request_sent(error: ErrorInfo): nonlocal request_error nonlocal request_sent_event request_error = error request_sent_event.set() data = pack_object([1, 2, 3]) client.async_send(data, on_request_sent) timeout = 10.0 request_sent_event.wait(timeout=timeout) assert request_sent_event.is_set() assert not request_error request_received_event.wait(timeout=timeout) assert request_received_event.is_set() response_received_event.wait(timeout=timeout) assert response_received_event.is_set() assert not response_error assert response_data == data client.stop() server.stop()