def request(status_file: str, command: str, *, timeout: Optional[int] = None, **kwds: object) -> Dict[str, Any]: """Send a request to the daemon. Return the JSON dict with the response. Raise BadStatus if there is something wrong with the status file or if the process whose pid is in the status file has died. Return {'error': <message>} if an IPC operation or receive() raised OSError. This covers cases such as connection refused or closed prematurely as well as invalid JSON received. """ response = {} # type: Dict[str, str] args = dict(kwds) args['command'] = command # Tell the server whether this request was initiated from a human-facing terminal, # so that it can format the type checking output accordingly. args['is_tty'] = sys.stdout.isatty() args['terminal_width'] = get_terminal_width() bdata = json.dumps(args).encode('utf8') _, name = get_status(status_file) try: with IPCClient(name, timeout) as client: client.write(bdata) response = receive(client) except (OSError, IPCException) as err: return {'error': str(err)} # TODO: Other errors, e.g. ValueError, UnicodeError else: return response
def request(status_file: str, command: str, *, timeout: Optional[int] = None, **kwds: object) -> Dict[str, Any]: """Send a request to the daemon. Return the JSON dict with the response. Raise BadStatus if there is something wrong with the status file or if the process whose pid is in the status file has died. Return {'error': <message>} if an IPC operation or receive() raised OSError. This covers cases such as connection refused or closed prematurely as well as invalid JSON received. """ response = {} # type: Dict[str, str] args = dict(kwds) args.update(command=command) bdata = json.dumps(args).encode('utf8') _, name = get_status(status_file) try: with IPCClient(name, timeout) as client: client.write(bdata) response = receive(client) except (OSError, IPCException) as err: return {'error': str(err)} # TODO: Other errors, e.g. ValueError, UnicodeError else: return response
def test_connect_twice(self) -> None: queue = Queue() # type: Queue[str] msg = 'this is a test message' p = Process(target=server, args=(msg, queue), daemon=True) p.start() connection_name = queue.get() with IPCClient(connection_name, timeout=1) as client: assert client.read() == msg.encode() client.write( b'' ) # don't let the server hang up yet, we want to connect again. with IPCClient(connection_name, timeout=1) as client: client.write(b'test') queue.close() queue.join_thread() p.join()
def test_transaction_large(self) -> None: queue = Queue() # type: Queue[str] msg = 't' * 100001 # longer than the max read size of 100_000 p = Process(target=server, args=(msg, queue), daemon=True) p.start() connection_name = queue.get() with IPCClient(connection_name, timeout=1) as client: assert client.read() == msg.encode() client.write(b'test') queue.close() queue.join_thread() p.join()