| 1 | #!/usr/bin/env python3 |
| 2 | """Measure the paused-consumer persistence-channel backlog hermetically.""" |
| 3 | |
| 4 | from __future__ import annotations |
| 5 | |
| 6 | import json |
| 7 | import os |
| 8 | import subprocess |
| 9 | import sys |
| 10 | import tempfile |
| 11 | from pathlib import Path |
| 12 | |
| 13 | |
| 14 | RECEIPT_ENV = "CODEWHALE_TEST_PERSISTENCE_BACKLOG_RECEIPT_PATH" |
| 15 | SOURCE_SHA_ENV = "CODEWHALE_TEST_PERSISTENCE_BACKLOG_SOURCE_SHA" |
| 16 | SOURCE_DIRTY_ENV = "CODEWHALE_TEST_PERSISTENCE_BACKLOG_SOURCE_DIRTY" |
| 17 | RUSTC_VERSION_ENV = "CODEWHALE_TEST_PERSISTENCE_BACKLOG_RUSTC_VERSION" |
| 18 | CARGO_VERSION_ENV = "CODEWHALE_TEST_PERSISTENCE_BACKLOG_CARGO_VERSION" |
| 19 | ROOT = Path(__file__).resolve().parent.parent |
| 20 | TEST_NAME = ( |
| 21 | "tui::persistence_actor::backlog_measurement_tests::" |
| 22 | "write_paused_persistence_backlog_measurement_receipt" |
| 23 | ) |
| 24 | |
| 25 | |
| 26 | def main() -> int: |
| 27 | source_sha = subprocess.run( |
| 28 | ["git", "rev-parse", "HEAD"], |
| 29 | cwd=ROOT, |
| 30 | text=True, |
| 31 | capture_output=True, |
| 32 | check=True, |
| 33 | ).stdout.strip() |
| 34 | source_dirty = bool( |
| 35 | subprocess.run( |
| 36 | ["git", "status", "--porcelain", "--untracked-files=normal"], |
| 37 | cwd=ROOT, |
| 38 | text=True, |
| 39 | capture_output=True, |
| 40 | check=True, |
| 41 | ).stdout |
| 42 | ) |
| 43 | rustc_version = subprocess.run( |
| 44 | ["rustc", "--version"], text=True, capture_output=True, check=True |
| 45 | ).stdout.strip() |
| 46 | cargo_version = subprocess.run( |
| 47 | ["cargo", "--version"], text=True, capture_output=True, check=True |
| 48 | ).stdout.strip() |
| 49 | with tempfile.TemporaryDirectory(prefix="codewhale-persistence-backlog-") as root: |
| 50 | receipt_path = Path(root) / "receipt.json" |
| 51 | env = os.environ.copy() |
| 52 | env["CARGO_NET_OFFLINE"] = "true" |
| 53 | env[RECEIPT_ENV] = str(receipt_path) |
| 54 | env[SOURCE_SHA_ENV] = source_sha |
| 55 | env[SOURCE_DIRTY_ENV] = str(source_dirty).lower() |
| 56 | env[RUSTC_VERSION_ENV] = rustc_version |
| 57 | env[CARGO_VERSION_ENV] = cargo_version |
| 58 | command = [ |
| 59 | "cargo", |
| 60 | "test", |
| 61 | "--locked", |
| 62 | "-p", |
| 63 | "codewhale-tui", |
| 64 | "--bin", |
| 65 | "codewhale-tui", |
| 66 | TEST_NAME, |
| 67 | "--", |
| 68 | "--exact", |
| 69 | "--ignored", |
| 70 | "--test-threads=1", |
| 71 | ] |
| 72 | result = subprocess.run( |
| 73 | command, |
| 74 | env=env, |
| 75 | text=True, |
| 76 | capture_output=True, |
| 77 | check=False, |
| 78 | ) |
| 79 | sys.stderr.write(result.stderr) |
| 80 | if result.returncode != 0: |
| 81 | sys.stdout.write(result.stdout) |
| 82 | return result.returncode |
| 83 | try: |
| 84 | receipt = json.loads(receipt_path.read_text(encoding="utf-8")) |
| 85 | except (OSError, json.JSONDecodeError) as error: |
| 86 | sys.stderr.write(f"invalid persistence backlog receipt: {error}\n") |
| 87 | return 1 |
| 88 | |
| 89 | print(json.dumps(receipt, indent=2, sort_keys=True)) |
| 90 | return 0 |
| 91 | |
| 92 | |
| 93 | if __name__ == "__main__": |
| 94 | raise SystemExit(main()) |
| 95 |