/* * test_sim_print_decision_gate.c - contract test for the interactive * simulation REPL's `sim_print_decision` Gate component * (scheduler-sim-repl-verbosity spec, task 4.1, Requirements 2.3, 5.1, 5.4): * retiring sim_print_decision()'s (sim_loop.c) unconditional bare * per-decision echo under `continue` mode now that the Round Rollup (task * 2.2, already implemented/committed) prints its own per-round summary -- * while leaving `step` mode and non-interactive (headless) batch output * completely untouched. * * Why a new file rather than extending an existing one: per this task's own * dispatch instructions, line counts of the two most recent sibling REPL * test files were checked first -- * - test_repl_round_rollup.c is 1022 lines, already 222 lines OVER this * project's 800-line cap (src/scheduler/CLAUDE.md) on its own; landing * three more tests there would only grow that pre-existing violation. * - test_repl_event_echo.c is 704 lines (96 lines of headroom before the * cap), but its own header comment documents a deliberate, load-bearing * "every fixture in this file is job-free" discipline (bug-1595 jobid- * reuse safety) -- this task's fixtures are decision-producing rounds, * which are inherently job-having, so extending that file would break * its own stated invariant AND still risk pushing it over cap once the * three tests' fixture/assertion surface (including a THIRD harness -- * the non-fork, non-interactive batch-capture technique neither sibling * file has any use for) is accounted for. * Neither candidate has real headroom, so (following this spec's now- * established proactive-split convention -- see test_repl_round_rollup.c's/ * test_repl_event_echo.c's own "Correction (post-review, ...)" header notes * citing design.md) this task creates a new file instead of reacting to a * cap violation after the fact. * * bug-1610 (buglog.json) is the original user report of exactly this * duplication: under `continue`, an unchanged PEND decision prints an * identical bare "[sim] PEND jobid=..." line every round it remains * pending, with zero new information each time now that the Round Rollup * (task 2.2) already reports the same information once per collapsed * range. This task's tests are the RED-phase proof that the duplication * bug-1610 reported must go away. * * bug-1595 jobid-reuse safety: each test below uses its own dedicated * replay fixture and a jobid unique to that fixture (6101/6102/6103) -- * no fixture mixes a "real" job under test with an unrelated padding/filler * job, so the specific hazard bug-1595 logged (a filler event's jobid * accidentally mutating the real job under test) has no surface to occur * on here. * * THIS TASK (4.1) IS RED-PHASE ONLY: sim_loop.c's decision-drain loop * still calls sim_print_decision(d) unconditionally today, with no * reference to sim->repl_state->mode or sim->interactive at that call site * (task 4.2, a separate later task, adds the gate). Every symbol this file * references already exists today, so all three tests build and link * cleanly -- but test_continue_mode... below FAILS ITS OWN RUNTIME * ASSERTION, because today's sim_print_decision() still unconditionally * prints the bare per-decision line even in continue mode, duplicating the * Round Rollup's own summary. The other two tests (step mode, headless * batch) pin ALREADY-CORRECT, unaffected behavior and PASS today -- a * legitimate regression-safety net for task 4.2 to keep green, the same * shape task 3.1's own step-mode regression test took. Verified concretely * against a real incremental build before this task was reported done -- * see this task's own Status Report. * * Assumed output contract this file pins (matches design.md's * `sim_print_decision` Gate component and repl_rollup.c's own Round Rollup * print format, which test_repl_round_rollup.c's header comment documents * in full): * - sim_print_decision() (sim_loop.c) prints one bare * "[sim] DISPATCH jobid=" / "[sim] PEND jobid=" / * "[sim] PREEMPT jobid=" line per drained decision, in EVERY mode, * TODAY. After task 4.2, this bare line must no longer appear while * sim->interactive && rs->mode == XCE_REPL_MODE_CONTINUE -- the Round * Rollup's own "[sim] round - (...): dispatch,

* pend, preempt -- jobids=..." summary line becomes the sole * source of decision-related output in that mode. * - Under `step` mode and non-interactive (headless, no -i/--interactive) * batch runs, sim_print_decision()'s bare per-decision line must * continue to print exactly as it does today -- this gate applies * ONLY when sim->interactive && rs->mode == XCE_REPL_MODE_CONTINUE. * - Tests use strstr() substring containment on captured real stdout, * the same technique test_repl_round_rollup.c's/test_repl_event_echo.c's * own tests establish. The Round Rollup's own summary line format * ("... : dispatch,

pend, preempt -- jobids=...") never * contains the bare line's literal substring ("PEND jobid=" / * "DISPATCH jobid=" / "PREEMPT jobid="), so a strstr() check for the * bare line's absence cannot be confused with the round summary's own, * differently-shaped text. */ #include #include #include #include #include #include #include #include #include #include #include #include "controller.capnp.h" #include "kvmgr.h" #include "scheduler.h" #include "xstring.h" /* Pull in the real functions under test into this TU (white-box #include, * same convention as test_repl_round_rollup.c/test_repl_event_echo.c/ * test_repl_execution_control.c). */ #include "../src/core.c" #include "../src/core_clock.c" #include "../src/core_events.c" #include "../src/core_session.c" #include "../src/decision.c" #include "../src/plugin.c" #include "../src/plugins/builtin_fcfs.c" #include "../src/recording.c" #include "../src/recording_session.c" #include "../src/repl.c" #include "../src/repl_fmt.c" #include "../src/repl_rollup.c" #include "../src/repl_break.c" #include "../src/repl_info.c" #include "../src/repl_params.c" #include "../src/sim.c" #include "../src/sim_loop.c" #include "../src/sim_picker.c" /* bug-1567: sched_sim_driver_run() now calls sim_bootstrap_picker() (sim_loop.c-cap sibling) as an extern -- undefined reference at link time otherwise */ #include "../src/sim_limit.c" /* bug-1825: sched_sim_driver_run() now also calls sim_bootstrap_limit_plugin() (sim_loop.c-cap sibling, sim_picker.c's own bug-1567 precedent) as an extern -- undefined reference at link time otherwise */ #include "../src/sim_record.c" #include "../src/sim_verify.c" #include "../src/snapshot.c" #include "../src/snapshot_load.c" #include "../src/snapshot_wire.c" void kill_sched_job(void* arg) { (void)arg; } void kill_sched_queue(void* arg) { (void)arg; } void kill_sched_worker(void* arg) { (void)arg; } void kill_sched_user(void* arg) { (void)arg; } void kill_sched_project(void* arg) { (void)arg; } void kill_sched_resval(xce_val_t* val) { (void)val; } /* repl_info.c (scheduler-sim-diagnostics spec, task 2.1) now calls * limit_get_configured_limits() (plugins/limit.c, not part of this TU) to * resolve `info limit`'s name/all argument -- link-satisfying stub only, * same rationale as kill_sched_job()/kill_sched_resval() above; this * file's own fixtures never register the limit plugin, so NULL * (limit_get_configured_limits()'s own documented "plugin absent" return) * is also the behaviorally correct answer here, not just a link * placeholder. repl_info_limit() itself is not yet wired into * repl_cmd_info() (task 4), so no test in this file can reach it anyway. */ var limit_get_configured_limits(xce_sched_core_t* core) { (void)core; return NULL; } /* scheduler-sim-diagnostics spec, task 3.1: repl_info.c now also calls * limit_get_usage_instances() (plugins/limit.c, not part of this TU) to * read a limit's live usage instances -- identical link-satisfying stub, * same rationale as limit_get_configured_limits() immediately above; this * file's own fixtures never register the limit plugin, so NULL * (limit_get_usage_instances()'s own documented "plugin absent" return) is * also the behaviorally correct answer here, not just a link placeholder. */ var limit_get_usage_instances(xce_sched_core_t* core) { (void)core; return NULL; } /* scheduler-sim-diagnostics spec, task 6 (bug-1825): sim_loop.c's * sched_sim_driver_run() now also calls sim_bootstrap_limit_plugin() * (sim_limit.c, pulled into this TU via the #include above), which in * turn calls limit_new_plugin() (plugins/limit.c, not part of this TU) -- * identical link-satisfying stub rationale as limit_get_configured_ * limits()/limit_get_usage_instances() immediately above. Returning NULL * here is also the behaviorally correct answer, not just a link * placeholder: sim_bootstrap_limit_plugin() already handles a NULL * limit_new_plugin() return gracefully (logs an ERROR, registers nothing, * returns) -- the exact same "out of memory" path sim_bootstrap_picker() * (sim_picker.c) takes for a NULL builtin_fcfs_new_plugin() -- so this * file's fixtures simply end up with no limit plugin registered, same * observable state as before this task. */ xce_sched_plugin_t* limit_new_plugin(void) { return NULL; } /* limit_seed_config() (plugins/limit.c, not part of this TU) -- bug-1825 * remediation round 2's new honestly-typed config-seed entry point. * sim_bootstrap_limit_plugin() (sim_limit.c) only calls it after * plugin_register_core() has already succeeded against a real, non-NULL * limit_new_plugin() plugin -- unreachable in this file since the * limit_new_plugin() stub immediately above always returns NULL, but the * symbol must still resolve at link time. Same link-satisfying-stub * rationale as limit_new_plugin() immediately above. */ int limit_seed_config(xce_sched_plugin_t* plugin, xce_sched_core_t* core, xce_conf4sched_t* config) { (void)plugin; (void)core; (void)config; return 0; } /* resource-accounting.md's reconciliation (resource.c, not linked here -- * this file only #includes core_events.c). core_events.c's * XCE_CORE_EVENT_JOB_RUSAGE handler calls this whenever job->accounts != * NULL; link-satisfying stub only, same rationale as every other stub in * this block (see test_job_update.c's identical stub). */ int sched_reconcile_job_resource_usage(xce_sched_core_t* c, xce_job_account_t* a, xce_job_rusages_t* r) { (void)c; (void)a; (void)r; return 0; } void sched_worker_release_host_ctx(xce_sched_worker_t* w) { (void)w; } int sched_fill_limit_requests(xce_sched_alloc_req_t* alloc_req, xlist_t* limits, xlist_t* rusages, int nhosts, int nslots) { (void)alloc_req; (void)limits; (void)rusages; (void)nhosts; (void)nslots; return 0; } /* sched_sim_driver_run()'s (sim_loop.c, scheduler-sim-decision-parity task * 2.1) new round-boundary call to the relocated sched_account_jobs() * (core.h) -- a no-op stand-in here since this file does not link * scheduler.c/resource.c, same rationale test_sim_equivalence.c's own copy * documents. */ int sched_account_jobs(xce_sched_core_t* core) { (void)core; return 0; } /* bug-1836 (scheduler-driver-equivalence fix, Recorded-Decision * Reconciliation capacity gate): link-satisfying stubs, mirroring this * file's existing sched_account_jobs() no-op precedent immediately above -- * sim_loop.c's sim_reconcile_unaccounted_jobs() now also calls * sched_check_host_satisfy()/sched_check_shared_satisfy() (resource.c) to * re-validate a recorded decision against live capacity, and * sched_charge_account() (resource.c) to keep that check's own view of * capacity up to date across multiple candidates in the same round. This * file never exercises real resource contention (same rationale as the * existing sched_account_jobs()/sched_ctx_get_uncomit_host() no-ops), so * the satisfy checks trivially always succeed and the charge call is a * no-op. */ int sched_check_host_satisfy(xce_sched_core_t* core, xce_sched_worker_t* w, xvec_t* uncomitted, xvec_idval_t* wanted, xvec_idval_t* unmeet) { (void)core; (void)w; (void)uncomitted; (void)wanted; (void)unmeet; return 1; } int sched_check_shared_satisfy(xce_sched_core_t* core, xvec_t* uncomitted, xvec_idval_t* wanted, xvec_idval_t* unmeet) { (void)core; (void)uncomitted; (void)wanted; (void)unmeet; return 1; } int sched_charge_account(xce_sched_core_t* core, xce_job_account_t* acct) { (void)core; (void)acct; return 0; } int sched_uncharge_account(xce_sched_core_t* core, xce_job_account_t* acct) { (void)core; (void)acct; return 0; } /* Real reimplementation of schedobj.c's sched_resolve_job_picker -- same * copy test_sim_alt_events.c/test_sim_verify.c/test_sim_event_loop.c/ * test_repl_execution_control.c/test_repl_round_rollup.c/ * test_repl_event_echo.c each carry (schedobj.c is not linked here: it * would need plugin_register(), which requires a genuine * xce_scheduler_t* neither this file's fixtures nor sim_loop.c's own * core-scoped registration path have, Req 5 AC 17). */ int sched_resolve_job_picker(xce_sched_core_t* core, xce_sched_plugin_t** out) { xce_sched_plugin_t* plugin = NULL; int rc = 0; if ((core == NULL) || (out == NULL)) { return -EINVAL; } *out = NULL; if (core->job_pick_plugin == NULL) { return -EINVAL; } lock(core->plugins, RTTI_LOCK_READ); rc = key_find(core->plugins, core->job_pick_plugin, (void**)&plugin); unlock(core->plugins); if (rc <= 0) { return (rc < 0) ? rc : -ENOENT; } if ((plugin == NULL) || (plugin->picker_iface == NULL) || (plugin->picker_iface->next_job == NULL) || (plugin->picker_iface->begin_session == NULL) || (plugin->picker_iface->end_session == NULL) || (plugin->picker_iface->on_dispatch == NULL) || (plugin->picker_iface->on_pend == NULL)) { return -EINVAL; } *out = plugin; return 0; } /* scheduler-sim-live-allocation spec, task 4.1: sim_loop.c's real * sched_sim_driver_run() (white-box #include'd into this TU above) now * spawns sched_job_picker()/sched_allocator() (sched_election.c) directly * instead of its own, now-removed sim_picker_thread_fn() -- an undefined * reference at link time in this white-box TU otherwise, since * sched_election.c's own sched_allocator() calls the REAL * sched_make_allocation()/sched_commit_decision() (scheduler.c), neither * part of this file's own hand-picked white-box #include set (this file's * own header comment lists that set). This file's own test(s) do not * exercise genuine resource-matching allocation, so this link-satisfying * pair faithfully reproduces the exact PEND-only contract the REMOVED * sim_picker_thread_fn() itself used to provide -- split across the SAME * two-thread jbuf hand-off the real production pair now uses (one * producer, one consumer, matching the project's jbuf capacity-1 * rendezvous invariant) instead of the old single fused thread -- so this * file's own existing assertions are unaffected by this task's production * change. Genuine resource-based dispatch through the real, unmodified * sched_job_picker()/sched_allocator() pair is proved separately by * test_sim_live_allocation.c (task 4.1's own dedicated fixture, which * links the real scheduler_srcs instead of white-boxing individual files, * precisely so it can reach the real allocation chain this stub * deliberately does not). */ void* sched_job_picker(void* arg) { xce_sched_core_t* core = (xce_sched_core_t*)arg; xce_sched_plugin_t* picker = NULL; for (;;) { int go = 0; uint64_t jobid = 0; cbuf_get(&(core->wbuf), &go); if (sched_resolve_job_picker(core, &picker) < 0) { jobid = 0; cbuf_put(&(core->jbuf), &jobid); continue; } picker->picker_iface->begin_session(picker); for (;;) { xce_sched_job_t* job = picker->picker_iface->next_job(picker); if (job == NULL) { break; } jobid = job->jobid; cbuf_put(&(core->jbuf), &jobid); } jobid = 0; cbuf_put(&(core->jbuf), &jobid); picker->picker_iface->end_session(picker); } return NULL; } void* sched_allocator(void* arg) { xce_sched_core_t* core = (xce_sched_core_t*)arg; xce_sched_plugin_t* picker = NULL; for (;;) { uint64_t jobid = 0; xce_decision_t* decision = NULL; xce_sched_job_t* job; cbuf_get(&(core->jbuf), &jobid); if (jobid == 0) { decision = NULL; cbuf_put(&(core->decisions), &decision); continue; } job = sched_get_job(core, jobid); if (job == NULL) { continue; } (void)sched_resolve_job_picker(core, &picker); if (picker != NULL) { picker->picker_iface->on_pend(picker, job->jobid); } decision = make_pend_decision(make_pendreason(job->jobid)); cbuf_put(&(core->decisions), &decision); } return NULL; } /* scheduler-sim-live-allocation spec, task 4.1 (bug-1829 fix): sim_loop.c's * real sched_sim_driver_run() (white-box #include'd into this TU above) now * also calls sim_bootstrap_seqjob_allocator()/sim_bootstrap_parjob_ * allocator() (sim_alloc.c) -- undefined references at link time in this * white-box TU otherwise, since neither symbol is part of this file's own * hand-picked white-box #include set. This file's own test(s) exercise the * link-satisfying sched_job_picker()/sched_allocator() stub pair above * (which never reaches real primary allocation), so a true no-op is the * correct, behavior-preserving stand-in here -- the real bootstrap logic is * exercised separately by test_sim_live_allocation.c (task 4.1's own * dedicated fixture, which links the real scheduler_srcs). */ void sim_bootstrap_seqjob_allocator(xce_sched_sim_driver_t* sim) { (void)sim; } void sim_bootstrap_parjob_allocator(xce_sched_sim_driver_t* sim) { (void)sim; } void setUp(void) { return; } void tearDown(void) { return; } /* ---------------------------------------------------------------------- */ /* Fixture helpers -- duplicated from test_repl_round_rollup.c/ */ /* test_repl_event_echo.c/test_sim_event_loop.c (same techniques those */ /* files already establish), per this file family's established split */ /* convention (see header comment above): copied, not shared via a new */ /* header. */ /* ---------------------------------------------------------------------- */ static void make_tmp_dir(char* out, size_t out_len, const char* suffix) { char tmpl[PATH_MAX]; snprintf(tmpl, sizeof(tmpl), "/tmp/sched_pdgate%s_XXXXXX", suffix); TEST_ASSERT_NOT_NULL(mkdtemp(tmpl)); TEST_ASSERT_TRUE(strlen(tmpl) < out_len); strcpy(out, tmpl); } static void build_fixture_scheduler(xce_scheduler_t* sched, char* record_dir) { int rc; memset(sched, 0, sizeof(*sched)); mylock_init(&(sched->record_lock)); rc = xce_sched_core_init(&(sched->core)); TEST_ASSERT_EQUAL_INT(0, rc); sched->core.job_pick_plugin = (char*)"builtin_fcfs"; sched->record_dir = record_dir; } static FILE* write_header_and_snapshot(char* out_path, size_t out_path_len, const char* suffix) { char record_dir[PATH_MAX]; xce_scheduler_t sched; int rc; make_tmp_dir(record_dir, sizeof(record_dir), suffix); build_fixture_scheduler(&sched, record_dir); rc = sched_record_start(&sched, "fixture.replay"); TEST_ASSERT_EQUAL_INT(0, rc); TEST_ASSERT_TRUE(strlen(sched.record_file_path) < out_path_len); strcpy(out_path, sched.record_file_path); return sched.record_file; } static void append_queue_update_event(FILE* f, uint64_t ts, const char* queue_name) { xce_queue_t queue; replay_event_t src; struct capn c; struct capn_segment* cs; ReplayEvent_ptr p; memset(&queue, 0, sizeof(queue)); queue.queue = (char*)queue_name; queue.status = QUEUE_STAT_OPEN; queue.priority = 1; memset(&src, 0, sizeof(src)); src.ts = ts; src.body_which = ReplayEvent_body_queueUpdate; src.body.queue_update = &queue; capn_init_malloc(&c); cs = capn_root(&c).seg; encode_ReplayEvent_ptr(cs, &p, &src); capn_setp(capn_root(&c), 0, p.p); TEST_ASSERT_EQUAL_INT(0, write_capnp_message(f, &c)); capn_free(&c); } static void append_jobs_push_event(FILE* f, uint64_t ts, uint64_t jobid, const char* queue_name) { job_submit_data_t submit; xce_job_t job; xce_job_t* jobs[1]; xce_jobs_data_t jobs_data; replay_event_t src; struct capn c; struct capn_segment* cs; ReplayEvent_ptr p; memset(&submit, 0, sizeof(submit)); submit.queue = (char*)queue_name; submit.user = (char*)"alice"; submit.mintasks = 1; submit.maxtasks = 1; memset(&job, 0, sizeof(job)); job.jobid = jobid; job.state = JOB_STAT_PEND; job.submit = &submit; jobs[0] = &job; memset(&jobs_data, 0, sizeof(jobs_data)); jobs_data.n_total = 1; jobs_data.count = 1; jobs_data.jobs = jobs; memset(&src, 0, sizeof(src)); src.ts = ts; src.body_which = ReplayEvent_body_jobsPush; src.body.jobs_push = &jobs_data; capn_init_malloc(&c); cs = capn_root(&c).seg; encode_ReplayEvent_ptr(cs, &p, &src); capn_setp(capn_root(&c), 0, p.p); TEST_ASSERT_EQUAL_INT(0, write_capnp_message(f, &c)); capn_free(&c); } static void append_replay_session(FILE* f, uint64_t begin_round) { struct capn c; struct capn_segment* cs; ReplaySession_ptr sp; capn_init_malloc(&c); cs = capn_root(&c).seg; sp = new_ReplaySession(cs); ReplaySession_set_beginRound(sp, begin_round); capn_setp(capn_root(&c), 0, sp.p); TEST_ASSERT_EQUAL_INT(0, write_capnp_message(f, &c)); capn_free(&c); } static void append_end_of_stream_marker(FILE* f) { replay_event_t src; struct capn c; struct capn_segment* cs; ReplayEvent_ptr p; memset(&src, 0, sizeof(src)); src.body_which = ReplayEvent_body_endSession; capn_init_malloc(&c); cs = capn_root(&c).seg; encode_ReplayEvent_ptr(cs, &p, &src); capn_setp(capn_root(&c), 0, p.p); TEST_ASSERT_EQUAL_INT(0, write_capnp_message(f, &c)); capn_free(&c); } static void write_text_file(const char* path, const char* content) { FILE* f = fopen(path, "w"); TEST_ASSERT_NOT_NULL(f); TEST_ASSERT_TRUE(fputs(content, f) >= 0); fclose(f); } static char* read_whole_file(const char* path) { FILE* f = fopen(path, "rb"); long len; char* buf; TEST_ASSERT_NOT_NULL(f); TEST_ASSERT_EQUAL_INT(0, fseek(f, 0, SEEK_END)); len = ftell(f); TEST_ASSERT_TRUE(len >= 0); rewind(f); buf = (char*)malloc((size_t)len + 1); TEST_ASSERT_NOT_NULL(buf); if (len > 0) { TEST_ASSERT_EQUAL_INT((int)len, (int)fread(buf, 1, (size_t)len, f)); } buf[len] = '\0'; fclose(f); return buf; } /* child_run_repl_and_exit / run_repl_fixture_and_capture - the same fork/ * freopen/waitpid/capture shape test_repl_round_rollup.c's/ * test_repl_event_echo.c's own pair establishes: run a scripted command * sequence to completion against a real sched_sim_driver_run() with * args.interactive = true, capturing real stdout. Used by the `continue`- * mode and `step`-mode tests below, which both need a real REPL command * loop reading a scripted stdin. */ static void child_run_repl_and_exit(const char* replay_path, const char* script_path, const char* capture_path) { xce_sched_sim_driver_t sim; sched_sim_args_t args; if (freopen(script_path, "r", stdin) == NULL) { _exit(120); } if (freopen(capture_path, "w", stdout) == NULL) { _exit(121); } memset(&sim, 0, sizeof(sim)); memset(&args, 0, sizeof(args)); args.replay_path = replay_path; args.sim_start_ts = SCHED_SIM_START_TS_LATEST; args.interactive = true; (void)sched_sim_driver_run(&sim, &args); fflush(stdout); _exit(111); /* unreachable -- the script's `quit` must already have * exited */ } static char* run_repl_fixture_and_capture(const char* replay_path, const char* script, const char* aux_suffix) { char aux_dir[PATH_MAX]; char script_path[PATH_MAX]; char capture_path[PATH_MAX]; pid_t pid; int status; make_tmp_dir(aux_dir, sizeof(aux_dir), aux_suffix); snprintf(script_path, sizeof(script_path), "%s/script.txt", aux_dir); snprintf(capture_path, sizeof(capture_path), "%s/capture.out", aux_dir); write_text_file(script_path, script); pid = fork(); TEST_ASSERT_TRUE_MESSAGE(pid >= 0, "fork() must succeed"); if (pid == 0) { child_run_repl_and_exit(replay_path, script_path, capture_path); _exit(112); /* unreachable -- child_run_repl_and_exit never returns */ } TEST_ASSERT_EQUAL_INT_MESSAGE(pid, waitpid(pid, &status, 0), "waitpid must reap the REPL child"); TEST_ASSERT_TRUE_MESSAGE(WIFEXITED(status), "the REPL child must exit normally via quit's " "exit(0)"); TEST_ASSERT_EQUAL_INT(0, WEXITSTATUS(status)); return read_whole_file(capture_path); } /* run_sim_and_capture_stdout - the same dup()/dup2()-around-STDOUT_FILENO, * NO-FORK capture technique test_sim_event_loop.c's own * run_sim_and_capture_stdout() establishes: args.interactive is left at its * memset(0) default (false), so sched_sim_driver_run() takes the batch * (non-interactive, headless) path -- no REPL command loop, no stdin/ * readline involved at all, matching Requirement 5.4's "non-interactive * (headless) replay's decision output is unaffected" scope exactly, and the * `test_sim_*.c` pattern this task's dispatch instructions point at. `sim` * is `static` for the same never-joined-detached-picker-thread reason * test_sim_event_loop.c's own copy documents. */ static char* run_sim_and_capture_stdout(const char* replay_path, int* out_rc) { sched_sim_args_t args; static xce_sched_sim_driver_t sim; char tmp[] = "/tmp/xce_pdgate_run_XXXXXX"; int fd = mkstemp(tmp); int saved_stdout; off_t len; char* buf; TEST_ASSERT_TRUE_MESSAGE(fd >= 0, "mkstemp for stdout capture must " "succeed"); fflush(stdout); saved_stdout = dup(STDOUT_FILENO); TEST_ASSERT_TRUE_MESSAGE(saved_stdout >= 0, "dup(STDOUT_FILENO) must " "succeed"); TEST_ASSERT_TRUE_MESSAGE(dup2(fd, STDOUT_FILENO) >= 0, "dup2 redirect of STDOUT_FILENO must succeed"); memset(&args, 0, sizeof(args)); args.replay_path = replay_path; args.sim_start_ts = SCHED_SIM_START_TS_LATEST; /* args.interactive left false -- this is the headless/batch path. */ *out_rc = sched_sim_driver_run(&sim, &args); fflush(stdout); dup2(saved_stdout, STDOUT_FILENO); close(saved_stdout); len = lseek(fd, 0, SEEK_CUR); TEST_ASSERT_TRUE(len >= 0); buf = (char*)calloc(1, (size_t)len + 1); TEST_ASSERT_NOT_NULL(buf); TEST_ASSERT_TRUE(lseek(fd, 0, SEEK_SET) == 0); if (len > 0) { ssize_t nread = read(fd, buf, (size_t)len); TEST_ASSERT_TRUE(nread == len); } close(fd); unlink(tmp); return buf; } /* ---------------------------------------------------------------------- */ /* Test 1: under `continue` mode, a round with >=1 decision must NOT print */ /* a bare per-decision line -- the Round Rollup's own summary line is the */ /* sole source of decision output in this mode (Requirement 2.3). THIS IS */ /* THE TEST EXPECTED TO FAIL TODAY (see header comment): sim_loop.c's */ /* decision-drain loop still calls sim_print_decision(d) unconditionally, */ /* so today's real captured output contains BOTH the bare line AND the */ /* Round Rollup's summary -- exactly the duplication bug-1610 reported. */ /* ---------------------------------------------------------------------- */ static void test_continue_mode_produces_no_bare_decision_line_outside_round_summary( void) { char replay_path[PATH_MAX]; FILE* f; char* captured; char jobid_x[64]; char bare_pend_marker[96]; f = write_header_and_snapshot(replay_path, sizeof(replay_path), "_pdgatecontinue"); TEST_ASSERT_NOT_NULL(f); append_queue_update_event(f, 1000ULL, "normal"); append_jobs_push_event(f, 1500ULL, 6101ULL, "normal"); append_replay_session(f, 2000000000ULL); /* round 1: 1 pend (X) */ append_end_of_stream_marker(f); fclose(f); captured = run_repl_fixture_and_capture(replay_path, "continue\nquit\n", "_pdgatecontinueaux"); fmt_jobid(jobid_x, sizeof(jobid_x), 6101ULL); snprintf(bare_pend_marker, sizeof(bare_pend_marker), "[sim] PEND jobid=%s", jobid_x); /* Sanity check (already true today, per task 2.2): the Round Rollup's * own summary line for this round IS present -- proves the fixture * actually produced a decision and that the round summary path is the * one under test here, not a fixture-setup mistake. */ TEST_ASSERT_NOT_NULL_MESSAGE( strstr(captured, "1 pend"), "sanity check: the Round Rollup's own per-round summary must " "report this round's decision (already implemented by task 2.2)"); /* THE ASSERTION EXPECTED TO FAIL TODAY: sim_print_decision()'s bare * per-decision line must no longer appear outside the Round Rollup's * own summary while executing in continue mode (Requirement 2.3) -- * today it still does, duplicating the information bug-1610 * originally reported. */ TEST_ASSERT_NULL_MESSAGE( strstr(captured, bare_pend_marker), "under continue mode, sim_print_decision()'s bare per-decision " "line must NOT appear outside the Round Rollup's own summary -- " "the round summary is the sole source of decision output in this " "mode (Requirement 2.3); today's unconditional echo still prints " "it, duplicating bug-1610's originally-reported information"); free(captured); printf( "test_continue_mode_produces_no_bare_decision_line_outside_round_" "summary: PASS\n"); } /* ---------------------------------------------------------------------- */ /* Test 2: while single-stepping, the bare per-decision line still prints */ /* exactly as before -- full detail, unaffected by this feature's eventual */ /* continue-mode change (Requirement 5.1). Regression-safety net: this */ /* test already passes today and must stay green through task 4.2. */ /* ---------------------------------------------------------------------- */ static void test_step_mode_still_prints_full_bare_decision_line(void) { char replay_path[PATH_MAX]; FILE* f; char* captured; char jobid_x[64]; char bare_pend_marker[96]; f = write_header_and_snapshot(replay_path, sizeof(replay_path), "_pdgatestep"); TEST_ASSERT_NOT_NULL(f); append_queue_update_event(f, 1000ULL, "normal"); append_jobs_push_event(f, 1500ULL, 6102ULL, "normal"); append_replay_session(f, 2000000000ULL); /* round 1: 1 pend (X) */ append_end_of_stream_marker(f); fclose(f); captured = run_repl_fixture_and_capture(replay_path, "step\nquit\n", "_pdgatestepaux"); fmt_jobid(jobid_x, sizeof(jobid_x), 6102ULL); snprintf(bare_pend_marker, sizeof(bare_pend_marker), "[sim] PEND jobid=%s", jobid_x); TEST_ASSERT_NOT_NULL_MESSAGE( strstr(captured, bare_pend_marker), "while single-stepping, sim_print_decision()'s bare per-decision " "line must still print exactly as before -- full detail, " "completely unaffected by this feature's continue-mode change " "(Requirement 5.1)"); free(captured); printf("test_step_mode_still_prints_full_bare_decision_line: PASS\n"); } /* ---------------------------------------------------------------------- */ /* Test 3: a non-interactive (headless, no -i/--interactive) batch replay */ /* run's decision output is completely unaffected -- this whole feature */ /* (including this specific gate) only ever applies when sim->interactive */ /* is true (Requirement 5.4). No REPL/readline/prompt involved at all -- */ /* same direct-call, no-fork pattern test_sim_event_loop.c's own */ /* run_sim_and_capture_stdout() establishes for headless `--sim` runs. */ /* ---------------------------------------------------------------------- */ static void test_headless_noninteractive_batch_run_decision_output_unaffected(void) { char replay_path[PATH_MAX]; FILE* f; char* captured; char jobid_x[64]; char bare_pend_marker[96]; int rc; f = write_header_and_snapshot(replay_path, sizeof(replay_path), "_pdgateheadless"); TEST_ASSERT_NOT_NULL(f); append_queue_update_event(f, 1000ULL, "normal"); append_jobs_push_event(f, 1500ULL, 6103ULL, "normal"); append_replay_session(f, 2000000000ULL); /* round 1: 1 pend (X) */ append_end_of_stream_marker(f); fclose(f); captured = run_sim_and_capture_stdout(replay_path, &rc); fmt_jobid(jobid_x, sizeof(jobid_x), 6103ULL); snprintf(bare_pend_marker, sizeof(bare_pend_marker), "[sim] PEND jobid=%s", jobid_x); TEST_ASSERT_EQUAL_INT(0, rc); TEST_ASSERT_NOT_NULL_MESSAGE( strstr(captured, bare_pend_marker), "a non-interactive (headless) batch replay run's bare per-decision " "line must print exactly as today -- this feature's gate only " "ever applies when sim->interactive is true (Requirement 5.4)"); free(captured); printf( "test_headless_noninteractive_batch_run_decision_output_" "unaffected: PASS\n"); } int main(void) { UNITY_BEGIN(); RUN_TEST( test_continue_mode_produces_no_bare_decision_line_outside_round_summary); RUN_TEST(test_step_mode_still_prints_full_bare_decision_line); RUN_TEST( test_headless_noninteractive_batch_run_decision_output_unaffected); return UNITY_END(); }