diff options
Diffstat (limited to 'test/daemon.py')
| -rw-r--r-- | test/daemon.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/test/daemon.py b/test/daemon.py index 5214bb00..5a53f149 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -27,6 +27,8 @@ private HOME and runtime directory: the replica clients alone python3 -B test/daemon.py --launch <pardes> `pardes new --detach` returns once its session runs + python3 -B test/daemon.py --pair <pardes> + PARDES_PAIR: a terminal's session and its first client python3 -B test/daemon.py --relink <pardes> the daemon killed: its sessions register again with the next python3 -B test/daemon.py --far <pardes> @@ -595,6 +597,75 @@ def launch(binary): shutil.rmtree(root, ignore_errors=True) +def pair(binary): + """PARDES_PAIR: `pardes FILE` in a terminal is a session forked off for + it with this terminal its first client (the last step's design, 1.1): + the job the client, the terminal its own; the session its child, in a + session of its own, registered (`ps`: detached, the client listed), + the file drawn, keys in, the digests agreed once quiet; the session's + Exit ends both. With no daemon the client killed outright ends the + session (its last link). Nothing left behind.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='rp-', dir=scratch)) + env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and 'PAGER' not in k} + env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), + TERM='xterm-256color', SHELL=shutil.which('bash') or '/bin/bash', PARDES_NOTIME='1', PARDES_PAIR='1') + (root / 'config' / 'pardes').mkdir(parents=True) + (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n') + notes = root / 'notes.txt' + notes.write_text(''.join(f'line {i} of the notes\n' for i in range(30))) + terms = [] + + def session_of(name): + sock = root / f'pardes-9p-{name}.sock' + wait_for(f'session {name}\'s 9P socket', sock.exists) + nine = Client(sock) + return nine, int(dict(line.split(maxsplit=1) for line in nine.read('/status').decode().splitlines())['pid']) + + try: + t = Term(30, 100) + terms.append(t) + c = t.spawn([binary, '--9p=pair', str(notes)], dict(env, PARDES_REPLICA_CHECK=str(root / 'pair.check'))) + nine, session = session_of('pair') + assert session != c.pid and ppid(session) == c.pid and os.getsid(session) == session, (session, c.pid) + wait_for('the file drawn on the terminal', lambda: b'line 3 of the notes' in t.text(), timeout=30) + listed = lambda: subprocess.run([binary, 'ps'], env=env, capture_output=True).stdout.decode() + wait_for('the client listed', lambda: f'session pair {session} (tty, detached)' in listed() + and f'client {c.pid} ' in listed()) + pane = next(int(row.split()[0]) for row in nine.read('/index').splitlines() if row.split()[1] == b'text') + os.write(t.master, b'Ofrom-the-pair\x1b') + wait_for('its keys in the session\'s text', lambda: b'from-the-pair' in nine.read(f'/pane/{pane}/body')) + wait_for('out of insert mode', lambda: normal_again(t)) + wait_for('the client agrees once quiet', lambda: agreed(root / 'pair.check', 0)) + exit_session(nine, pane) + code = c.wait(timeout=20) + wait_for('the session gone', lambda: not alive(session), timeout=20) + print(f'pair: client {c.pid} (the job) and session {session} (its child, setsid, listed detached with the client); ' + f'drawn, typed, agreed; Exit ended both (the client exit {code})') + nine.socket.close() + lone = Term(24, 80) + terms.append(lone) + c2 = lone.spawn([binary, '--9p=lone', str(notes)], dict(env, PARDES_DAEMON='off')) + nine2, session2 = session_of('lone') + wait_for('the lone pair drawn', lambda: b'line 3 of the notes' in lone.text(), timeout=30) + nine2.socket.close() + os.kill(c2.pid, signal.SIGKILL) + c2.wait(timeout=10) + wait_for('the lone session ended with its last link', lambda: not alive(session2), timeout=20) + wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=20) + print(f'pair: with no daemon, client {c2.pid} killed outright, its session {session2} ended; nothing left behind') + finally: + for p in processes(root): + try: + os.kill(p, signal.SIGKILL) + except OSError: + pass + for t in terms: + t.close() + shutil.rmtree(root, ignore_errors=True) + + def manage(binary): """`pardes --ps`, `--kill=NAME` and `--kill-all`: two detached sessions and their shells listed, one session killed outright and its shell @@ -1591,6 +1662,7 @@ def main(binary, cycles): far(binary) by_hash(binary) launch(binary) + pair(binary) manage(binary) relink(binary) shell = terminals(client)[0] @@ -2303,6 +2375,8 @@ if __name__ == '__main__': manage(sys.argv[2]) elif sys.argv[1:2] == ['--launch']: launch(sys.argv[2]) + elif sys.argv[1:2] == ['--pair']: + pair(sys.argv[2]) elif sys.argv[1:2] == ['--relink']: relink(sys.argv[2]) elif sys.argv[1:2] == ['--games']: |
