|
3 | 3 |
|
4 | 4 | This product-level guard intentionally exercises only supported CLI/daemon |
5 | 5 | behavior. Detailed request cancellation, session isolation, rendezvous ABI, |
6 | | -version-conflict, and committed-client stop-refusal semantics are covered by |
7 | | -``tests/test_daemon_runtime.c`` and are not duplicated here through a retired |
8 | | -frontend protocol. |
| 6 | +and version-conflict semantics are covered by ``tests/test_daemon_runtime.c``; |
| 7 | +the committed-client stop refusal is also checked here through a foreground CLI |
| 8 | +index operation. |
9 | 9 |
|
10 | 10 | Covered here: |
11 | 11 | * cold one-shot operation and startup-tax guidance; |
12 | 12 | * permanent daemon start/status and stable PID across concurrent CLI clients; |
13 | 13 | * daemon-backed indexing and persisted project visibility; |
14 | 14 | * daemon crash recovery and fresh generation startup; |
15 | | -* clean/idempotent daemon stop. |
| 15 | +* committed-client stop refusal during a foreground index and clean/idempotent daemon stop. |
16 | 16 | """ |
17 | 17 |
|
18 | 18 | import json |
@@ -49,9 +49,13 @@ def output_text(result): |
49 | 49 | ) |
50 | 50 |
|
51 | 51 |
|
52 | | -def run_cli(binary, cache, args, timeout=OP_TIMEOUT): |
| 52 | +def run_cli(binary, cache, args, timeout=OP_TIMEOUT, runtime=None): |
53 | 53 | env = dict(os.environ) |
54 | 54 | env["CBM_CACHE_DIR"] = str(cache) |
| 55 | + if runtime is not None: |
| 56 | + runtime = Path(runtime) |
| 57 | + runtime.mkdir(parents=True, exist_ok=True) |
| 58 | + env["CBM_RUNTIME_DIR"] = str(runtime) |
55 | 59 | return subprocess.run( |
56 | 60 | [str(binary)] + list(args), |
57 | 61 | stdout=subprocess.PIPE, |
@@ -128,6 +132,86 @@ def create_repo(root): |
128 | 132 | ) |
129 | 133 |
|
130 | 134 |
|
| 135 | +def create_busy_repo(root): |
| 136 | + root.mkdir() |
| 137 | + for index in range(2000): |
| 138 | + (root / ("module_{:04d}.py".format(index))).write_text( |
| 139 | + "def value_{}():\n return {}\n".format(index, index), encoding="utf-8" |
| 140 | + ) |
| 141 | + |
| 142 | + |
| 143 | +def section_stop_refuses_busy(binary, work): |
| 144 | + """Exercise stop refusal with a real index client on an isolated daemon.""" |
| 145 | + cache = work / "busy-cache" |
| 146 | + runtime = work / "busy-runtime" |
| 147 | + cache.mkdir() |
| 148 | + repo = work / "busy-repo" |
| 149 | + create_busy_repo(repo) |
| 150 | + daemon_pid = 0 |
| 151 | + index_process = None |
| 152 | + try: |
| 153 | + start = run_cli(binary, cache, ["daemon", "start"], timeout=START_TIMEOUT, |
| 154 | + runtime=runtime) |
| 155 | + daemon_pid = daemon_pid_from(output_text(start)) |
| 156 | + check(start.returncode == 0 and daemon_pid > 1, |
| 157 | + "isolated daemon did not start: " + output_text(start)[:800]) |
| 158 | + index_process = subprocess.Popen( |
| 159 | + [str(binary), "index", str(repo), "--mode", "fast", "--json"], |
| 160 | + stdout=subprocess.PIPE, |
| 161 | + stderr=subprocess.PIPE, |
| 162 | + env={**os.environ, "CBM_CACHE_DIR": str(cache), "CBM_RUNTIME_DIR": str(runtime)}, |
| 163 | + ) |
| 164 | + deadline = time.monotonic() + 60 |
| 165 | + attached = False |
| 166 | + while time.monotonic() < deadline: |
| 167 | + status = require_json( |
| 168 | + run_cli(binary, cache, ["daemon", "status", "--json"], runtime=runtime), |
| 169 | + "daemon status during foreground index", |
| 170 | + ) |
| 171 | + if status.get("committed_clients", 0) > 0: |
| 172 | + attached = True |
| 173 | + break |
| 174 | + if index_process.poll() is not None: |
| 175 | + break |
| 176 | + time.sleep(0.2) |
| 177 | + check(attached, "foreground index never held a committed daemon client") |
| 178 | + stop = run_cli(binary, cache, ["daemon", "stop"], runtime=runtime) |
| 179 | + check(stop.returncode != 0 and "daemon: NOT stopped" in output_text(stop), |
| 180 | + "daemon stop did not refuse while index client was attached: " |
| 181 | + + output_text(stop)[:800]) |
| 182 | + status = require_json( |
| 183 | + run_cli(binary, cache, ["daemon", "status", "--json"], runtime=runtime), |
| 184 | + "daemon status after refused stop", |
| 185 | + ) |
| 186 | + check(status.get("active") and status.get("pid") == daemon_pid, |
| 187 | + "daemon was not left active after refusing stop") |
| 188 | + index_out, index_err = index_process.communicate(timeout=180) |
| 189 | + check(index_process.returncode == 0, |
| 190 | + "foreground index failed: " |
| 191 | + + (index_out + index_err).decode("utf-8", "replace")[-800:]) |
| 192 | + stop = run_cli(binary, cache, ["daemon", "stop"], runtime=runtime) |
| 193 | + check(stop.returncode == 0, "daemon stop failed after index exited: " |
| 194 | + + output_text(stop)[:800]) |
| 195 | + check(wait_runtime_not_running(binary, cache, runtime), |
| 196 | + "daemon did not stop after index client detached") |
| 197 | + finally: |
| 198 | + if index_process is not None and index_process.poll() is None: |
| 199 | + index_process.kill() |
| 200 | + index_process.communicate() |
| 201 | + if daemon_pid: |
| 202 | + force_kill(daemon_pid) |
| 203 | + |
| 204 | + |
| 205 | +def wait_runtime_not_running(binary, cache, runtime, timeout=STOP_TIMEOUT): |
| 206 | + deadline = time.monotonic() + timeout |
| 207 | + while time.monotonic() < deadline: |
| 208 | + status = run_cli(binary, cache, ["daemon", "status"], runtime=runtime) |
| 209 | + if status.returncode != 0 and "not running" in output_text(status): |
| 210 | + return True |
| 211 | + time.sleep(0.2) |
| 212 | + return False |
| 213 | + |
| 214 | + |
131 | 215 | def require_json(result, label): |
132 | 216 | check(result.returncode == 0, label + " failed: " + output_text(result)[:800]) |
133 | 217 | try: |
@@ -218,6 +302,8 @@ def projects_call(index): |
218 | 302 | "parallel CLI clients restarted the permanent daemon", |
219 | 303 | ) |
220 | 304 |
|
| 305 | + section_stop_refuses_busy(binary, tmpdir) |
| 306 | + |
221 | 307 | indexed = run_cli( |
222 | 308 | binary, |
223 | 309 | cache, |
|
0 commit comments