| 1 | #!/usr/bin/env python3 |
| 2 | """Run one end-to-end realtime probe: the real Docs server (seeded with a |
| 3 | generated document) and the production DocsModel in an XCTest process. |
| 4 | |
| 5 | run-e2e.py <seed.bin> <TestName> [KEY=VALUE ...] |
| 6 | |
| 7 | TestName is testEndToEndLocalTyping, testEndToEndRemoteUpdates or |
| 8 | testEndToEndIdle. KEY=VALUE pairs become DOCS_RT_AUDIT_* environment |
| 9 | variables (KEYS=40 CADENCE_MS=125 BURST=10 IDLE_S=40). Expects the Go test |
| 10 | binary at $DOCS_RT_AUDIT_GOTEST (built with `go test -c ./internal/docs`) |
| 11 | and a release test build of native/apple. Never touches user data. |
| 12 | """ |
| 13 | import os |
| 14 | from pathlib import Path |
| 15 | import signal |
| 16 | import subprocess |
| 17 | import sys |
| 18 | import tempfile |
| 19 | import time |
| 20 | |
| 21 | root = Path(__file__).resolve().parents[3] |
| 22 | seed, test = sys.argv[1], sys.argv[2] |
| 23 | extra = dict(arg.split("=", 1) for arg in sys.argv[3:]) |
| 24 | gotest = os.environ.get("DOCS_RT_AUDIT_GOTEST", "/tmp/rt-audit/docs.test") |
| 25 | |
| 26 | with tempfile.TemporaryDirectory(prefix="docs-rt-e2e-", dir="/tmp") as tmp: |
| 27 | scratch = Path(tmp) |
| 28 | env = dict(os.environ, DOCS_RT_AUDIT_DIR=tmp, DOCS_RT_AUDIT_SEED=seed, TMPDIR="/tmp") |
| 29 | for key, value in extra.items(): |
| 30 | env["DOCS_RT_AUDIT_" + key] = value |
| 31 | log = (scratch / "server.log").open("w") |
| 32 | server = subprocess.Popen([gotest, "-test.run", "^TestNativeRealtimeAuditServer$", "-test.count=1", "-test.timeout=25m"], |
| 33 | cwd=root / "internal/docs", env=env, stdout=log, stderr=subprocess.STDOUT, start_new_session=True) |
| 34 | try: |
| 35 | deadline = time.monotonic() + 120 |
| 36 | while not (scratch / "connection.json").exists(): |
| 37 | if server.poll() is not None: |
| 38 | print((scratch / "server.log").read_text()[-4000:]) |
| 39 | raise SystemExit("server exited") |
| 40 | if time.monotonic() > deadline: |
| 41 | raise SystemExit("server did not start") |
| 42 | time.sleep(0.05) |
| 43 | result = subprocess.run(["swift", "test", "-c", "release", "-Xswiftc", "-enable-testing", "--skip-build", |
| 44 | "--package-path", str(root / "native/apple"), "--filter", "RealtimeAuditE2ETests/" + test], |
| 45 | env=env, capture_output=True, text=True, timeout=1500) |
| 46 | out = result.stdout + result.stderr |
| 47 | for line in out.splitlines(): |
| 48 | if line.startswith(("RTE2E", "RTAUDIT")) or ": error:" in line: |
| 49 | print(line) |
| 50 | if os.environ.get("DOCS_RT_AUDIT_KEEP"): |
| 51 | keep = Path(os.environ["DOCS_RT_AUDIT_KEEP"]) |
| 52 | keep.mkdir(parents=True, exist_ok=True) |
| 53 | (keep / f"{Path(seed).stem}-{test}.requests.jsonl").write_text((scratch / "requests.jsonl").read_text()) |
| 54 | (scratch / "done").touch() |
| 55 | server.wait(timeout=20) |
| 56 | raise SystemExit(result.returncode) |
| 57 | finally: |
| 58 | if server.poll() is None: |
| 59 | os.killpg(server.pid, signal.SIGTERM) |
| 60 | server.wait(timeout=10) |
| 61 | log.close() |
| 62 | |