summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/daemon.py153
-rw-r--r--test/rollback_bench.zig18
2 files changed, 165 insertions, 6 deletions
diff --git a/test/daemon.py b/test/daemon.py
index ce19b502..b54b2b54 100644
--- a/test/daemon.py
+++ b/test/daemon.py
@@ -7,8 +7,12 @@ private HOME and runtime directory:
- 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;
+ build handed to its session, which welcomes it, sends its dump and
+ answers its pings; another build's refused, and a session nobody has;
+- replica clients (`pardes --attach=<socket>:<name>`, phase 5 part B) of a
+ detached session, one through the delay relay: each one's keys in the
+ session's text, every client's digest the session's once quiet, one
+ killed outright while the other types on;
- nothing left behind: no session, shell, daemon or socket.
python3 -B test/daemon.py <pardes> [cycles]
@@ -17,6 +21,8 @@ private HOME and runtime directory:
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
+ python3 -B test/daemon.py --clients <pardes>
+ the replica clients alone
"""
import fcntl
import os
@@ -170,7 +176,7 @@ def semantics(binary):
class Replica:
"""A replica's connection (src/detached/wire.zig): length-prefixed
messages, a tag byte and a little-endian u32 length."""
- WIRE = 14
+ WIRE = 15
def __init__(self, path, timeout=10):
self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
@@ -194,22 +200,34 @@ class Replica:
return tag, self.take(n)
def hello(self, session, build):
- """('welcome', pid, name) or ('refused', why)."""
+ """('welcome', pid, name) or ('refused', why); a welcome's dump
+ is read, whole."""
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)
+ client, view, dump_len, gmtoff, digest = struct.unpack_from("<HBQiQ", body, 6 + n)
+ got = 0
+ while got < dump_len:
+ tag, piece = self.read()
+ assert tag == 0x04, tag
+ got += len(piece)
+ self.dump_len = dump_len
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."""
+ """One round trip, in ms (the session's units on the way are
+ passed over)."""
t = time.monotonic()
self.send(0x03, struct.pack('<Q', value))
- tag, body = self.read()
+ while True:
+ tag, body = self.read()
+ if tag != 0x05:
+ break
assert (tag, body) == (0x03, struct.pack('<Q', value)), (tag, body)
return (time.monotonic() - t) * 1000
@@ -258,6 +276,126 @@ def ping(path, session, binary, n):
f'{rtt[0]:.2f}/{statistics.median(rtt):.2f}/{rtt[int(n * .9) - 1]:.2f}/{rtt[-1]:.2f} ms')
+def checks(path):
+ """A replica client's checks against its session (PARDES_REPLICA_CHECK)."""
+ try:
+ return path.read_text().splitlines()
+ except FileNotFoundError:
+ return []
+
+
+def agreed(path, after):
+ """A check after the first `after` agreed; one that differed fails."""
+ lines = checks(path)
+ bad = [line for line in lines if line.startswith('differ')]
+ assert not bad, (path.name, bad)
+ return len(lines) > after
+
+
+def clients(binary):
+ """Replica clients of a detached session: one on the replica socket,
+ one through the delay relay; each one's keys in the session's text and
+ every client's digest the session's once quiet; one killed outright
+ while the other types on; 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='rc-', 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')
+ (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(300)))
+ started = []
+ terms = []
+ try:
+ sess = subprocess.Popen([binary, '--detach=files', str(notes)], env=env, cwd=root, stdin=subprocess.DEVNULL,
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True)
+ started.append(sess.pid)
+ sock = root / 'pardes-replica.sock'
+ wait_for('the replica socket', sock.exists)
+ wait_for('the session\'s 9P socket', (root / 'pardes-9p-files.sock').exists)
+ nine = Client(root / 'pardes-9p-files.sock')
+ pane = next(int(row.split()[0]) for row in nine.read('/index').splitlines() if row.split()[1] == b'text')
+ alone = fds(sess.pid)
+ relay = subprocess.Popen([sys.executable, '-B', str(Path(__file__).parent / 'delay_relay.py'),
+ str(root / 'relay.sock'), str(sock), '--delay', '40'],
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+ started.append(relay.pid)
+ wait_for('the relay', (root / 'relay.sock').exists)
+ ta, tb = Term(30, 100), Term(24, 80)
+ terms += [ta, tb]
+ a = ta.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'a.check')))
+ b = tb.spawn([binary, f'--attach={root / "relay.sock"}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'b.check')))
+ started += [a.pid, b.pid]
+ for t in (ta, tb):
+ wait_for('a client\'s first frame', lambda: b'line 3 of the notes' in t.text(), timeout=20)
+ os.write(ta.master, b'Ofrom-A\x1b')
+ os.write(tb.master, b'jjofrom-B\x1b')
+ wait_for('both clients\' keys in the session\'s text', lambda: b'from-A' in nine.read(f'/pane/{pane}/body')
+ and b'from-B' in nine.read(f'/pane/{pane}/body'))
+ wait_for('client A agrees once quiet', lambda: agreed(root / 'a.check', 0))
+ wait_for('client B agrees once quiet', lambda: agreed(root / 'b.check', 0))
+ # Killed outright: the session and the other client carry on.
+ os.kill(b.pid, signal.SIGKILL)
+ b.wait(timeout=10)
+ seen = len(checks(root / 'a.check'))
+ os.write(ta.master, b'jjjAagain-A\x1b')
+ wait_for('client A\'s keys after B went', lambda: b'again-A' in nine.read(f'/pane/{pane}/body'))
+ wait_for('client A agrees again', lambda: agreed(root / 'a.check', seen))
+ assert sess.poll() is None
+ # Detach typed in it: it alone goes.
+ os.write(ta.master, b' sD')
+ wait_for('client A let go', lambda: a.poll() is not None, timeout=10)
+ assert b'let this client go' in ta.text(), ta.text()[-300:]
+ # A third client, a shell: its keys wait for the session (a pty
+ # write), its output comes back in units, the same on both sides.
+ nine.write(f'/pane/{pane}/exec', b'Tty\n')
+ shell = wait_for('a shell pane', lambda: next((int(row.split()[0]) for row in nine.read('/index').splitlines()
+ if row.split()[1] == b'term'), None))
+ wait_for('the shell\'s prompt', lambda: b'$' in nine.read(f'/pane/{shell}/body') or b'#' in nine.read(f'/pane/{shell}/body'))
+ alone = fds(sess.pid)
+ tc = Term(30, 100)
+ terms.append(tc)
+ c = tc.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'c.check')))
+ started.append(c.pid)
+ wait_for('client C\'s first frame', lambda: b'$' in tc.text(), timeout=20)
+ run(nine, shell, b'echo from-the-session\n')
+ os.write(tc.master, b'echo from-C\r')
+ wait_for('client C\'s keys in the shell', lambda: b'from-C' in nine.read(f'/pane/{shell}/body'))
+ wait_for('client C agrees with a shell\'s output', lambda: agreed(root / 'c.check', 0))
+ # Its terminal hung up: it dies, and the session carries on.
+ c.send_signal(signal.SIGHUP)
+ c.wait(timeout=10)
+ relay.kill()
+ relay.wait()
+ wait_for('the session\'s fds level', lambda: fds(sess.pid) == alone, timeout=10)
+ print(f'daemon: two replica clients (one through a 40 ms relay) typed into a detached session; '
+ f'digests agreed {len(checks(root / "a.check"))} and {len(checks(root / "b.check"))} times; '
+ f'one killed, the other typed on and detached; a third typed into a shell, agreed '
+ f'{len(checks(root / "c.check"))} times, and was hung up')
+ try:
+ nine.write(f'/pane/{pane}/exec', b'Exit\n')
+ except OSError as e:
+ # An edited file asks before it goes.
+ assert 'Exit again' in str(e), e
+ nine.write(f'/pane/{pane}/exec', b'Exit\n')
+ sess.wait(timeout=10)
+ nine.socket.close()
+ wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=10)
+ print('daemon: replica clients\' session ended, nothing left behind')
+ finally:
+ for t in terms:
+ t.close()
+ for pid in started + processes(root):
+ try:
+ os.kill(pid, signal.SIGKILL)
+ except OSError:
+ pass
+ shutil.rmtree(root, ignore_errors=True)
+
+
def terminals(client):
return [int(row.split()[0]) for row in client.read('/index').splitlines() if row.split()[1] == b'term']
@@ -290,6 +428,7 @@ def main(binary, cycles):
daemon = daemons[0]
started.append(daemon)
replicas(root, binary, session, daemon)
+ clients(binary)
shell = terminals(client)[0]
shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1])
started.append(shell_pid)
@@ -856,6 +995,8 @@ if __name__ == '__main__':
gui_upgrades(Path(sys.argv[3]).resolve(), Path(sys.argv[4]).resolve())
elif sys.argv[1:2] == ['--upgrade']:
upgrades(Path(sys.argv[2]).resolve(), Path(sys.argv[3]).resolve())
+ elif sys.argv[1:2] == ['--clients']:
+ clients(sys.argv[2])
elif 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:
diff --git a/test/rollback_bench.zig b/test/rollback_bench.zig
index 38fe011d..0f0c77a4 100644
--- a/test/rollback_bench.zig
+++ b/test/rollback_bench.zig
@@ -158,6 +158,24 @@ pub fn main(init: std.process.Init) !void {
x.* = t1 - t0;
y.* = nowNs() - t1;
}
+ // A join (docs/next-daemon.md, Phase 5 part B notes): the session
+ // dumped at a step boundary, which is what the authority pays, and
+ // the joiner's load of it.
+ var dump: [5]u64 = undefined;
+ var load: [5]u64 = undefined;
+ var dump_len: usize = 0;
+ for (&dump, &load) |*x, *y| {
+ const t0 = nowNs();
+ const bytes = try pardes.dump.dumpBytes(core);
+ const t1 = nowNs();
+ dump_len = bytes.len;
+ const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }, bytes, null);
+ y.* = nowNs() - t1;
+ x.* = t1 - t0;
+ loaded.deinit();
+ core.gpa.free(bytes);
+ }
+ std.debug.print("{s:<8} join: dump {d:.1} MB in {d:.2} ms (the session's step), load {d:.2} ms (the joiner's)\n", .{ s.name, @as(f64, @floatFromInt(dump_len)) / 1e6, @as(f64, @floatFromInt(median(&dump))) * 1e-6, @as(f64, @floatFromInt(median(&load))) * 1e-6 });
var panes: usize = 0;
for (core.panes) |slot| panes += @intFromBool(slot != null);
const ms = 1e-6;