summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/daemon.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/test/daemon.py b/test/daemon.py
index 5c620ba4..c497bb19 100644
--- a/test/daemon.py
+++ b/test/daemon.py
@@ -1096,6 +1096,88 @@ def far(binary):
shutil.rmtree(root, ignore_errors=True)
+def by_hash(binary):
+ """A file over the wire's 16 MiB bound opened while clients are
+ attached (its read was one unit, dropped: the client joined again, a
+ Debug session panicked): it goes by its hash, a client on this machine
+ reading it itself and a far one sent it once, kept on disk; a far client
+ that joins after is sent nothing of it."""
+ scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn'
+ scratch.mkdir(parents=True, exist_ok=True)
+ root = Path(tempfile.mkdtemp(prefix='rh-', dir=scratch))
+ cache = root / 'cache'
+ 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'),
+ XDG_CACHE_HOME=str(cache), 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)))
+ big = root / 'big.txt'
+ with open(big, 'wb') as f:
+ for i in range(17 << 14):
+ f.write(b'%08d %s\n' % (i, os.urandom(27).hex().encode()[:54]))
+ started, terms = [], []
+
+ def client(name, far):
+ t = Term(24, 90)
+ terms.append(t)
+ c = t.spawn([binary, f'--attach={root / "pardes-replica.sock"}:files'],
+ dict(env, PARDES_REPLICA_CHECK=str(root / f'{name}.check'), PARDES_REPLICA_STATS=str(root / f'{name}.stats'),
+ **({'PARDES_REPLICA_FAR': '1'} if far else {})))
+ started.append(c.pid)
+ wait_for(f'client {name}\'s first frame', lambda: b'line 3 of the notes' in t.text(), timeout=30)
+ time.sleep(.3)
+ return t, c
+
+ def read(pid):
+ return next(int(line.split()[1]) for line in open(f'/proc/{pid}/io') if line.startswith('rchar'))
+ 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)
+ wait_for('the replica socket', (root / 'pardes-replica.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')
+ (ta, a), (tf, f) = client('a', False), client('f', True)
+ seen = {n: len(checks(root / f'{n}.check')) for n in 'af'}
+ was = {c.pid: read(c.pid) for c in (a, f)}
+ nine.write(f'/pane/{pane}/exec', f'Look {big}\n'.encode())
+ for n in 'af':
+ wait_for(f'client {n} agrees with the big file open', lambda: agreed(root / f'{n}.check', seen[n] + 1), timeout=60)
+ assert sess.poll() is None and a.poll() is None and f.poll() is None
+ assert not any(b'joined again' in t.text() for t in (ta, tf))
+ size = big.stat().st_size
+ # This machine's read it from disk; the far one was sent it, zstd.
+ got = {c.pid: read(c.pid) - was[c.pid] for c in (a, f)}
+ assert size <= got[a.pid] < size + (512 << 10), (size, got)
+ assert got[f.pid] < size, (size, got)
+ tg, g = client('g', True)
+ os.write(tg.master, b' sD')
+ wait_for('client G let go', lambda: g.poll() is not None, timeout=20)
+ g_join = joins(root / 'g.stats')[0]
+ assert g_join['from_disk'] == g_join['blobs'] and g_join['sent_kb'] == 0, g_join
+ print(f'daemon: a {size >> 20} MiB file opened under two clients went by its hash: the one on this machine read '
+ f'it itself, the far one was sent {got[f.pid] >> 10} KB and kept it; a far client that joined after was '
+ f'sent none of its {g_join["blobs"]} blobs')
+ for t, c in ((ta, a), (tf, f)):
+ os.write(t.master, b' sD')
+ wait_for('a client let go', lambda: c.poll() is not None, timeout=20)
+ exit_session(nine, pane)
+ sess.wait(timeout=10)
+ nine.socket.close()
+ 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 far_joins(sock, session, binary, n):
"""`n` far joins of one session in a row, each read whole (its blobs
asked for by none, its zstd frames unpacked) and answering a ping, then
@@ -1191,6 +1273,7 @@ def main(binary, cycles):
replicas(root, binary, session, daemon)
clients(binary)
far(binary)
+ by_hash(binary)
manage(binary)
shell = terminals(client)[0]
shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1])
@@ -1863,6 +1946,7 @@ if __name__ == '__main__':
manage(sys.argv[2])
elif sys.argv[1:2] == ['--far']:
far(sys.argv[2])
+ by_hash(sys.argv[2])
elif sys.argv[1:2] == ['--window-clients']:
window_clients(sys.argv[2], sys.argv[3])
pdf_window(sys.argv[2], sys.argv[3])