def test_bad_request(): with IPCServer(lambda _: None) as server, SingleRequestHandler(server): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("localhost", server.port)) s.send(b"foo") res = pickle.loads(s.recv(2048)) assert isinstance(res, pickle.UnpicklingError)
def test_stop_single(): requests = [] with IPCServer(requests.append) as server, SingleRequestHandler(server): server.timeout = 1 server.stop() with pytest.raises(socket.error): send_request(server.port, 5) assert not requests
def test_timeout(): with IPCServer(lambda _: None) as server: server.timeout = 1 timeout_called = threading.Event() server.handle_timeout = timeout_called.set with SingleRequestHandler(server): time.sleep(server.timeout + 0.5) assert timeout_called.is_set()
def test_stop_multi(): requests = [] with IPCServer(requests.append) as server, MultiRequestHandler(server): send_request(server.port, 1) assert requests == [1] server.stop() with pytest.raises(socket.error): send_request(server.port, 2) assert requests == [1]
def __init__(self): """Creates and starts a client monitor.""" # This has an internal lock, so it is reused for any operations that # need to be synchronized self.cond = threading.Condition() # A set of clients (psutil.Process objects, which are hashable). # As long as monitor_stdin() hasn't been called yet, we can assume the # parent is still alive, and this can be changed later self.clients = {ClientMonitor._PARENT} # start background tasks self.thread = start_daemon_thread(target=self._background_loop) self.server = IPCServer.run_in_background( on_message=self._handle_message )
import os os.environ["FIFTYONE_DISABLE_SERVICES"] = "1" from fiftyone.service.ipc import IPCServer env = {} def handle_message(message): try: code = compile(message, "", "eval") except SyntaxError: code = compile(message, "", "exec") return eval(code, env) IPCServer(handle_message).serve_forever()
def test_large_request(): with IPCServer(lambda x: x) as server, SingleRequestHandler(server): data = list(range(10000)) assert send_request(server.port, data) == data
def test_multiple_requests(): with IPCServer(lambda x: x * 2) as server, MultiRequestHandler(server): assert send_request(server.port, 5) == 10 assert send_request(server.port, "a") == "aa"
def test_one_request(): with IPCServer(lambda x: x * 2) as server, SingleRequestHandler(server): assert send_request(server.port, 5) == 10
def test_send_ipc_message(): with IPCServer.run_in_background(lambda x: x) as server: assert send_ipc_message(current_process, 6) == 6 with pytest.raises(IOError): send_ipc_message(current_process, 7)
def test_get_listening_tcp_ports(): assert not list_current_ports() with IPCServer(lambda _: None) as server: assert list_current_ports() == [server.port] assert not list_current_ports()
def test_run_in_background(): requests = [] with IPCServer.run_in_background(requests.append) as server: send_request(server.port, 2) send_request(server.port, 3) assert requests == [2, 3]