tools/client/test_portable_semaphore.py (2901B)
1 """Compare Linux semaphore adapters with Windows, including cross-process lifetime.""" 2 import os 3 import pathlib 4 import subprocess 5 6 7 root = pathlib.Path(__file__).resolve().parents[2] 8 decompiled = root / "client" 9 work = root / "local/client/portable-semaphore" 10 work.mkdir(parents=True, exist_ok=True) 11 reference = work / "reference.exe" 12 subprocess.run([ 13 str(root / "toolchains/llvm-mingw-20261006-ucrt-x86_64/bin/i686-w64-mingw32-clang.exe"), 14 "-O2", "tests/semaphore.c", "-o", str(reference), 15 ], cwd=decompiled, check=True) 16 linux_path = "/mnt/" + decompiled.drive[0].lower() + decompiled.as_posix()[2:] 17 linux_output = "/mnt/" + work.drive[0].lower() + work.as_posix()[2:] + "/translated" 18 wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] 19 subprocess.run(wsl + [ 20 "gcc", "-O2", "-DRECOMP_PORTABLE", "-Iruntime", "-fno-strict-aliasing", 21 "tests/semaphore.c", "runtime/linux_sync.c", "runtime/guest_memory.c", 22 "runtime/recomp_state.c", "-lm", "-o", linux_output, 23 ], check=True) 24 expected = subprocess.check_output([str(reference)], text=True, timeout=30) 25 actual = subprocess.check_output(wsl + [linux_output], text=True, timeout=30) 26 assert actual == expected, (expected, actual) 27 print("Windows == Linux: " + actual.strip(), flush=True) 28 29 # Independent processes open the same object, wait, release and survive another close. 30 for command in [[str(reference)], wsl + [linux_output]]: 31 name = f"AO_Semaphore_Process_Test_{os.getpid()}" 32 holder = subprocess.Popen(command + ["hold", name], stdin=subprocess.PIPE, 33 stdout=subprocess.PIPE, text=True) 34 try: 35 assert holder.stdout.readline().strip() == "ready" 36 assert subprocess.check_output(command + ["probe", name], text=True, timeout=10).strip() == "exists" 37 for mode in ["wait", "wait-infinite"]: 38 waiter = subprocess.Popen(command + [mode, name], stdout=subprocess.PIPE, text=True) 39 try: 40 assert waiter.stdout.readline().strip() == "waiting" 41 assert subprocess.check_output(command + ["release", name], text=True, timeout=10).strip() == "released" 42 assert waiter.communicate(timeout=10)[0].strip() == "awakened" 43 assert waiter.returncode == 0 44 finally: 45 if waiter.poll() is None: 46 waiter.terminate() 47 waiter.wait(timeout=10) 48 assert subprocess.check_output(command + ["probe", name], text=True, timeout=10).strip() == "exists" 49 finally: 50 # The child deliberately dies without CloseHandle, on either OS. 51 holder.communicate(input="x\n", timeout=10) 52 assert holder.returncode != 0 53 assert subprocess.check_output(command + ["probe", name], text=True, timeout=10).strip() == "missing" 54 print("Windows == Linux: cross-process wait/release and last-handle cleanup after process death passed")