diff options
Diffstat (limited to 'test/daemon.py')
| -rw-r--r-- | test/daemon.py | 110 |
1 files changed, 109 insertions, 1 deletions
diff --git a/test/daemon.py b/test/daemon.py index 41cfbaf7..2be3f355 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -6,16 +6,24 @@ private HOME and runtime directory: from another terminal, N times; - a session killed outright: its shells live on, held by the daemon, and another session adopts them as terminal panes; +- a replica's hello on the daemon's stream socket (phase 5): one of this + build handed to its session, which answers it and its pings; another + build's refused, and a session nobody has; - nothing left behind: no session, shell, daemon or socket. python3 -B test/daemon.py <pardes> [cycles] python3 -B test/daemon.py --gui <pardes-gui> [cycles] (a hidden window; needs a Wayland display, and a pardes beside it for the refusal) + python3 -B test/daemon.py --ping <socket> <session> <pardes> [n] + a hello of <pardes>'s build and n pings on a replica socket (one + `ssh -L` forwards, say), timed """ import fcntl import os import shutil import signal +import socket +import statistics import struct import subprocess import sys @@ -153,6 +161,103 @@ def fds(pid): return -1 +def semantics(binary): + """What a replica's build must share with its session's (--version).""" + out = subprocess.run([binary, '--version'], capture_output=True, check=True).stdout.decode() + return int(next(line.split()[1] for line in out.splitlines() if line.startswith('semantics ')), 16) + + +class Replica: + """A replica's connection (src/detached/wire.zig): length-prefixed + messages, a tag byte and a little-endian u32 length.""" + WIRE = 14 + + def __init__(self, path, timeout=10): + self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self.s.settimeout(timeout) + self.s.connect(os.fspath(path)) + + def send(self, tag, payload): + self.s.sendall(struct.pack('<BI', tag, len(payload)) + payload) + + def take(self, n): + got = b'' + while len(got) < n: + more = self.s.recv(n - len(got)) + if not more: + raise EOFError('the connection ended') + got += more + return got + + def read(self): + tag, n = struct.unpack('<BI', self.take(5)) + return tag, self.take(n) + + def hello(self, session, build): + """('welcome', pid, name) or ('refused', why).""" + name = session.encode() + self.send(0x01, struct.pack('<HQH', self.WIRE, build, len(name)) + name) + tag, body = self.read() + if tag == 0x01: + pid, n = struct.unpack_from('<IH', body) + return 'welcome', pid, body[6:6 + n].decode() + assert tag == 0x02, (tag, body) + n, = struct.unpack_from('<H', body) + return 'refused', body[2:2 + n].decode() + + def ping(self, value): + """One round trip, in ms.""" + t = time.monotonic() + self.send(0x03, struct.pack('<Q', value)) + tag, body = self.read() + assert (tag, body) == (0x03, struct.pack('<Q', value)), (tag, body) + return (time.monotonic() - t) * 1000 + + def close(self): + self.s.close() + + +def replicas(root, binary, session, daemon): + sock = root / 'pardes-replica.sock' + wait_for('the replicas\' socket', sock.exists) + assert len(os.fspath(sock)) < 104, 'FreeBSD\'s sun_path' + ours = semantics(binary) + before = fds(session), fds(daemon) + t = time.monotonic() + r = Replica(sock) + said = r.hello('work', ours) + hello_ms = (time.monotonic() - t) * 1000 + # The session answers, so the connection is its own now: the daemon + # keeps no copy of it. + assert said == ('welcome', session, 'work'), said + rtt = [r.ping(i) for i in range(50)] + assert fds(session) == before[0] + 1 and fds(daemon) == before[1], (before, fds(session), fds(daemon)) + r.close() + wait_for('the replica\'s end, in the session', lambda: fds(session) == before[0]) + said = Replica(sock).hello('work', ours ^ 1) + assert said[0] == 'refused' and 'another build' in said[1] and f'{ours ^ 1:016x}' in said[1], said + said = Replica(sock).hello('nobody', ours) + assert said == ('refused', "no session called 'nobody'"), said + wait_for('the daemon\'s refused connections closed', lambda: fds(daemon) == before[1]) + print(f'daemon: a replica handed to its session (hello {hello_ms:.2f} ms, ping median ' + f'{statistics.median(rtt):.3f} ms); another build, and a session nobody has, refused') + + +def ping(path, session, binary, n): + build = semantics(binary) + t = time.monotonic() + r = Replica(path, timeout=30) + said = r.hello(session, build) + hello_ms = (time.monotonic() - t) * 1000 + if said[0] != 'welcome': + sys.exit(f'refused: {said[1]}') + rtt = sorted(r.ping(i) for i in range(n)) + r.send(0x02, b'') + r.close() + print(f'connect and hello {hello_ms:.2f} ms; {n} pings min/median/p90/max ' + f'{rtt[0]:.2f}/{statistics.median(rtt):.2f}/{rtt[int(n * .9) - 1]:.2f}/{rtt[-1]:.2f} ms') + + def terminals(client): return [int(row.split()[0]) for row in client.read('/index').splitlines() if row.split()[1] == b'term'] @@ -184,6 +289,7 @@ def main(binary, cycles): assert len(daemons) == 1, [cmdline(p) for p in processes(root)] daemon = daemons[0] started.append(daemon) + replicas(root, binary, session, daemon) shell = terminals(client)[0] shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) started.append(shell_pid) @@ -401,7 +507,9 @@ def gui(binary, cycles): if __name__ == '__main__': - if '--gui' in sys.argv: + if sys.argv[1:2] == ['--ping']: + ping(sys.argv[2], sys.argv[3], sys.argv[4], int(sys.argv[5]) if len(sys.argv) > 5 else 50) + elif '--gui' in sys.argv: args = [a for a in sys.argv[1:] if a != '--gui'] gui(args[0], int(args[1]) if len(args) > 1 else 50) else: |
