|
1 | 1 | import subprocess |
2 | 2 | import logging |
| 3 | +import time |
| 4 | +import socket |
| 5 | + |
| 6 | + |
| 7 | +def wait_for_port(port, process, timeout=100): |
| 8 | + start = time.time() |
| 9 | + while time.time() - start < timeout: |
| 10 | + # Check if process died |
| 11 | + if process.poll() is not None: |
| 12 | + raise RuntimeError(f"Server process exited with code {process.returncode}") |
| 13 | + try: |
| 14 | + with socket.create_connection(("localhost", port), timeout=1): |
| 15 | + return True |
| 16 | + except (socket.timeout, ConnectionRefusedError, OSError): |
| 17 | + time.sleep(0.2) |
| 18 | + raise RuntimeError(f"Port {port} not ready within {timeout}s") |
3 | 19 |
|
4 | 20 |
|
5 | 21 | def test(codeql, java): |
6 | | - # Each of these serves the "repo" and "repo2" directories on http://localhost:924[89] |
7 | | - repo_server_process = subprocess.Popen(["python3", "-m", "http.server", "9428", "-b", "localhost"], cwd="repo") |
8 | | - repo_server_process2 = subprocess.Popen(["python3", "-m", "http.server", "9429", "-b", "localhost"], cwd="repo2") |
| 22 | + repo_server_process = subprocess.Popen(["python3", "-m", "http.server", "9428", "-b", "localhost"], cwd="repo", stderr=subprocess.PIPE, stdout=subprocess.PIPE) |
| 23 | + repo_server_process2 = subprocess.Popen(["python3", "-m", "http.server", "9429", "-b", "localhost"], cwd="repo2", stderr=subprocess.PIPE, stdout=subprocess.PIPE) |
9 | 24 | try: |
| 25 | + wait_for_port(9428, repo_server_process) |
| 26 | + wait_for_port(9429, repo_server_process2) |
10 | 27 | codeql.database.create( |
11 | 28 | extractor_option="buildless=true", |
12 | 29 | _env={"CODEQL_EXTRACTOR_JAVA_OPTION_BUILDLESS_CLASSPATH_FROM_BUILD_FILES": "true"}, |
|
0 commit comments