|
| 1 | +import argparse |
| 2 | +import ast |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | + |
| 9 | + |
| 10 | +JEROMQ_URL = "https://repo1.maven.org/maven2/org/zeromq/jeromq/0.6.0/jeromq-0.6.0.jar" |
| 11 | + |
| 12 | + |
| 13 | +def parse_args(): |
| 14 | + parser = argparse.ArgumentParser(description="Run Java e2e example smoke check") |
| 15 | + parser.add_argument("--jar", type=Path, help="Path to jeromq jar") |
| 16 | + parser.add_argument("--keep-study", action="store_true", help="Keep generated study files") |
| 17 | + return parser.parse_args() |
| 18 | + |
| 19 | +def main(): |
| 20 | + args = parse_args() |
| 21 | + here = Path(__file__).resolve().parent |
| 22 | + repo_root = here.parents[1] |
| 23 | + study_dir = here / "study" |
| 24 | + jar_path = args.jar or (repo_root / ".ci-cache" / "java" / "jeromq-0.6.0.jar") |
| 25 | + |
| 26 | + if not jar_path.exists(): |
| 27 | + raise FileNotFoundError( |
| 28 | + f"Missing jeromq jar at {jar_path}. Download from {JEROMQ_URL} or pass --jar." |
| 29 | + ) |
| 30 | + |
| 31 | + if study_dir.exists(): |
| 32 | + shutil.rmtree(study_dir) |
| 33 | + (study_dir / "1").mkdir(parents=True, exist_ok=True) |
| 34 | + (study_dir / "1" / "concore.maxtime").write_text("20", encoding="utf-8") |
| 35 | + |
| 36 | + subprocess.run( |
| 37 | + [ |
| 38 | + "javac", |
| 39 | + "-cp", |
| 40 | + str(jar_path), |
| 41 | + str(repo_root / "concoredocker.java"), |
| 42 | + str(here / "pm_java.java"), |
| 43 | + ], |
| 44 | + check=True, |
| 45 | + cwd=repo_root, |
| 46 | + ) |
| 47 | + |
| 48 | + env = os.environ.copy() |
| 49 | + env["CONCORE_STUDY_DIR"] = str(study_dir) |
| 50 | + classpath = os.pathsep.join([str(repo_root), str(here), str(jar_path)]) |
| 51 | + |
| 52 | + py_log = open(study_dir / "controller.log", "w", encoding="utf-8") |
| 53 | + java_log = open(study_dir / "pm_java.log", "w", encoding="utf-8") |
| 54 | + |
| 55 | + py_proc = subprocess.Popen( |
| 56 | + [sys.executable, str(here / "controller.py")], |
| 57 | + cwd=repo_root, |
| 58 | + env=env, |
| 59 | + stdout=py_log, |
| 60 | + stderr=subprocess.STDOUT, |
| 61 | + ) |
| 62 | + java_proc = subprocess.Popen( |
| 63 | + ["java", "-cp", classpath, "pm_java"], |
| 64 | + cwd=repo_root, |
| 65 | + env=env, |
| 66 | + stdout=java_log, |
| 67 | + stderr=subprocess.STDOUT, |
| 68 | + ) |
| 69 | + |
| 70 | + try: |
| 71 | + py_rc = py_proc.wait(timeout=45) |
| 72 | + java_rc = java_proc.wait(timeout=45) |
| 73 | + except subprocess.TimeoutExpired: |
| 74 | + py_proc.kill() |
| 75 | + java_proc.kill() |
| 76 | + py_log.close() |
| 77 | + java_log.close() |
| 78 | + raise RuntimeError("Timed out waiting for node processes") |
| 79 | + |
| 80 | + py_log.close() |
| 81 | + java_log.close() |
| 82 | + |
| 83 | + if py_rc != 0 or java_rc != 0: |
| 84 | + raise RuntimeError( |
| 85 | + f"Node process failed (controller={py_rc}, pm_java={java_rc}). " |
| 86 | + f"See {study_dir / 'controller.log'} and {study_dir / 'pm_java.log'}." |
| 87 | + ) |
| 88 | + |
| 89 | + u_path = study_dir / "1" / "u" |
| 90 | + ym_path = study_dir / "1" / "ym" |
| 91 | + if not u_path.exists() or not ym_path.exists(): |
| 92 | + raise RuntimeError("Expected output files were not produced") |
| 93 | + |
| 94 | + u_val = ast.literal_eval(u_path.read_text(encoding="utf-8")) |
| 95 | + ym_val = ast.literal_eval(ym_path.read_text(encoding="utf-8")) |
| 96 | + if not isinstance(u_val, list) or len(u_val) < 2: |
| 97 | + raise RuntimeError("u output did not match expected wire format") |
| 98 | + if not isinstance(ym_val, list) or len(ym_val) < 2: |
| 99 | + raise RuntimeError("ym output did not match expected wire format") |
| 100 | + |
| 101 | + print("smoke_check passed") |
| 102 | + print(f"u: {u_val}") |
| 103 | + print(f"ym: {ym_val}") |
| 104 | + |
| 105 | + if not args.keep_study and study_dir.exists(): |
| 106 | + shutil.rmtree(study_dir) |
| 107 | + |
| 108 | + class_file = here / "pm_java.class" |
| 109 | + if class_file.exists(): |
| 110 | + class_file.unlink() |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + main() |
0 commit comments