Skip to content

Commit de1ae5c

Browse files
committed
Merge fu/win-stop-busy-test into fu/integration
Keep foreground-index committed-client stop-refusal coverage in the Windows stability script and daemon smoke test. No textual conflicts or both-side changed files; reviewed added functions and registrations for duplicate bodies and calls.
2 parents 3daac78 + c8c01c2 commit de1ae5c

2 files changed

Lines changed: 176 additions & 5 deletions

File tree

‎tests/test_daemon_smoke.py‎

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
44
This product-level guard intentionally exercises only supported CLI/daemon
55
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.
99
1010
Covered here:
1111
* cold one-shot operation and startup-tax guidance;
1212
* permanent daemon start/status and stable PID across concurrent CLI clients;
1313
* daemon-backed indexing and persisted project visibility;
1414
* 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.
1616
"""
1717

1818
import json
@@ -49,9 +49,13 @@ def output_text(result):
4949
)
5050

5151

52-
def run_cli(binary, cache, args, timeout=OP_TIMEOUT):
52+
def run_cli(binary, cache, args, timeout=OP_TIMEOUT, runtime=None):
5353
env = dict(os.environ)
5454
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)
5559
return subprocess.run(
5660
[str(binary)] + list(args),
5761
stdout=subprocess.PIPE,
@@ -128,6 +132,86 @@ def create_repo(root):
128132
)
129133

130134

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+
131215
def require_json(result, label):
132216
check(result.returncode == 0, label + " failed: " + output_text(result)[:800])
133217
try:
@@ -218,6 +302,8 @@ def projects_call(index):
218302
"parallel CLI clients restarted the permanent daemon",
219303
)
220304

305+
section_stop_refuses_busy(binary, tmpdir)
306+
221307
indexed = run_cli(
222308
binary,
223309
cache,

‎tests/windows/test_daemon_stability.py‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,90 @@ def make_probe_repo(work):
241241
return repo
242242

243243

244+
def make_busy_repo(work):
245+
"""Many tiny source files keep a real foreground index client attached."""
246+
repo = os.path.join(work, "busy-repo")
247+
os.makedirs(repo, exist_ok=True)
248+
for index in range(2000):
249+
with open(os.path.join(repo, "module_%04d.py" % index), "w",
250+
encoding="utf-8") as handle:
251+
handle.write("def value_%d():\n return %d\n" % (index, index))
252+
return repo
253+
254+
255+
def section_stop_refuses_busy(binary, work):
256+
cache = section_dirs(work, "busy-stop")
257+
daemon_pid = 0
258+
index = None
259+
try:
260+
start = run_cli(binary, cache, ["daemon", "start"], timeout=60)
261+
daemon_pid = pid_from(out_text(start))
262+
if start.returncode != 0 or not daemon_pid:
263+
raise SetupFailure("permanent daemon did not start for the busy-stop check:\n%s"
264+
% out_text(start))
265+
266+
index = subprocess.Popen([binary, "index", make_busy_repo(work), "--mode", "fast", "--json"],
267+
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
268+
env=cli_env(cache))
269+
deadline = time.monotonic() + 60
270+
busy_status = None
271+
while time.monotonic() < deadline:
272+
status = run_cli(binary, cache, ["daemon", "status", "--json"], timeout=30)
273+
if status.returncode == 0:
274+
try:
275+
payload = json.loads((status.stdout or b"").decode("utf-8"))
276+
except ValueError:
277+
payload = {}
278+
if payload.get("committed_clients", 0) > 0:
279+
busy_status = payload
280+
break
281+
if index.poll() is not None:
282+
break
283+
time.sleep(STATUS_POLL_S)
284+
if not busy_status:
285+
stdout, stderr = index.communicate(timeout=30)
286+
print("RED: foreground index never held a committed daemon client (rc=%s):\n%s"
287+
% (index.returncode, excerpt(stdout + stderr)))
288+
index = None
289+
return False
290+
291+
stop = run_cli(binary, cache, ["daemon", "stop"])
292+
stop_text = out_text(stop)
293+
if stop.returncode == 0 or "daemon: NOT stopped" not in stop_text:
294+
print("RED: daemon stop must refuse while index is attached:\n%s"
295+
% excerpt(stop_text))
296+
return False
297+
status = run_cli(binary, cache, ["daemon", "status", "--json"])
298+
try:
299+
after = json.loads((status.stdout or b"").decode("utf-8"))
300+
except ValueError:
301+
after = {}
302+
if status.returncode != 0 or not after.get("active") or after.get("pid") != daemon_pid:
303+
print("RED: daemon was not left active after refusing stop:\n%s"
304+
% excerpt(out_text(status)))
305+
return False
306+
307+
stdout, stderr = index.communicate(timeout=180)
308+
index_rc = index.returncode
309+
index = None
310+
if index_rc != 0:
311+
print("RED: foreground index failed:\n%s" % excerpt(stdout + stderr))
312+
return False
313+
stop = run_cli(binary, cache, ["daemon", "stop"])
314+
if stop.returncode != 0 or not wait_status_not_running(binary, cache, 45):
315+
print("RED: daemon did not stop after the index client detached:\n%s"
316+
% excerpt(out_text(stop)))
317+
return False
318+
daemon_pid = 0
319+
print("PASS: daemon stop refused with an attached index client and succeeded after it exited")
320+
return True
321+
finally:
322+
if index is not None and index.poll() is None:
323+
index.kill()
324+
index.communicate()
325+
kill_pid(daemon_pid)
326+
327+
244328
def section_crash_recovery(binary, work):
245329
cache = section_dirs(work, "crash")
246330
daemon_pid = 0
@@ -381,6 +465,7 @@ def main():
381465
section_hook_fail_open,
382466
section_start_status_port,
383467
section_crash_recovery,
468+
section_stop_refuses_busy,
384469
section_churn_stability,
385470
section_cold_storm,
386471
]

0 commit comments

Comments
 (0)