diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/daemon.py | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/test/daemon.py b/test/daemon.py index c6ff6256..e1be141a 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -38,6 +38,8 @@ private HOME and runtime directory: python3 -B test/daemon.py --far <pardes> the far clients and their blob cache alone, one under `yes` across a slow link, and a file by its hash + python3 -B test/daemon.py --kitty-image <pardes> + a plain client watching a kitty image in its session's terminal python3 -B test/daemon.py --games <pardes> <lib> [seconds] the games in the clients that show them, from zig build addon-test's <prefix>/lib (libsnake, liblife, libliar) @@ -2608,6 +2610,76 @@ def terminals(client): return [int(row.split()[0]) for row in client.read('/index').splitlines() if row.split()[1] == b'term'] +def cpu_ticks(pid): + stat = open(f'/proc/{pid}/stat').read().rsplit(')', 1)[1].split() + return int(stat[11]) + int(stat[12]) + + +def kitty_image(binary, secs=5): + """A plain attach (screen state) watching a terminal in which a program + shows a kitty image (the corpus's scene 10's: 8x8 RGB, 10x4 cells): it + agrees with its session and does not join again (it joined again ~3.3 + times a second: the tag said ` petscii:on` on the session's side only).""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='ri-', 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') + rgb = bytes((x * 30) % 256 for x in range(8 * 8 * 3)) + (root / 'kitty.sh').write_text("printf '\\033_Ga=T,f=24,s=8,v=8,c=10,r=4;%s\\033\\\\'\nprintf '\\nKITTY\\n'\n" + % __import__('base64').b64encode(rgb).decode()) + started, terms = [], [] + try: + term = Term() + terms.append(term) + stub = term.spawn([binary, '--tty', '--9p=img'], env) + started.append(stub.pid) + wait_for('the session\'s 9P socket', (root / 'pardes-9p-img.sock').exists) + nine = Client(root / 'pardes-9p-img.sock') + session = int(dict(line.split(maxsplit=1) for line in nine.read('/status').decode().splitlines())['pid']) + started.append(session) + shell = terminals(nine)[0] + nine.write(f'/pane/{shell}/exec', b'Detach\n') + assert stub.wait(timeout=10) == 0, stub.returncode + t = Term(30, 100) + terms.append(t) + c = t.spawn([binary, 'attach', 'img'], dict(env, PARDES_REPLICA_CHECK=str(root / 'c.check'))) + started.append(c.pid) + wait_for('the client\'s first frame', lambda: b'\x1b[?1049h' in t.text(), timeout=30) + wait_for('the client agrees', lambda: agreed(root / 'c.check', 0), timeout=30) + nine.write(f'/pane/{shell}/pty/data', f'clear; sh {root}/kitty.sh\r'.encode()) + wait_for('KITTY in the session\'s body', lambda: b'KITTY' in nine.read(f'/pane/{shell}/body'), timeout=30) + wait_for('KITTY in the client\'s terminal', lambda: b'KITTY' in t.text(), timeout=30) + mark, seen = len(t.text()), len(checks(root / 'c.check')) + cpu = (cpu_ticks(session), cpu_ticks(c.pid)) + time.sleep(secs) + cpu = [(cpu_ticks(p) - was) * 1000 // os.sysconf('SC_CLK_TCK') for p, was in zip((session, c.pid), cpu)] + again = t.text()[mark:].count(b'joined again') + said = checks(root / 'c.check')[seen:] + tag = nine.read(f'/pane/{shell}/tag') + print(f'kitty image: over {secs} s the client joined again {again} times ({again / secs:.1f}/s), its checks ' + f'{len(said)} ({sum(s.startswith("differ") for s in said)} differ), CPU ms session {cpu[0]} client {cpu[1]}, ' + f'{len(t.text()) - mark} bytes to its terminal; the session\'s tag {tag[:60]!r}') + assert again == 0 and c.poll() is None, (again, said[-3:]) + assert agreed(root / 'c.check', seen), said + exit_session(nine, shell) + nine.socket.close() + wait_for('the client ended with its session', lambda: c.poll() is not None, timeout=20) + print('kitty image: a plain client watching a kitty image agreed and never joined again') + 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 main(binary, cycles): scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' scratch.mkdir(parents=True, exist_ok=True) @@ -2648,6 +2720,7 @@ def main(binary, cycles): manage(binary) relink(binary) answer_late(binary) + kitty_image(binary) shell = terminals(client)[0] shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) started.append(shell_pid) @@ -3662,6 +3735,8 @@ if __name__ == '__main__': far_find(sys.argv[2]) elif sys.argv[1:2] == ['--far-find']: far_find(sys.argv[2]) + elif sys.argv[1:2] == ['--kitty-image']: + kitty_image(sys.argv[2]) elif sys.argv[1:2] == ['--restore']: restore_client(sys.argv[2]) elif sys.argv[1:2] == ['--window-clients']: |
