myxce/src/scheduler/testsuite/test_repl_execution_control.c
Rongsong Shen eb06f16d75
fix(scheduler): wire mem reservation reconciliation into live rusage event path
sched_reconcile_job_resource_usage() was only reachable from
sched_apply_job_rusage() (scheduler.c's sched_update(), which has no live
caller), so real-usage reconciliation never actually ran outside tests.
Wire it into core_events.c's XCE_CORE_EVENT_JOB_RUSAGE handler instead,
the actual live path shared by both normal-mode (driver.c) and sim/replay
mode (sim_loop.c). Adds link-satisfying stubs to the testsuite files that
#include core_events.c directly.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y4M5MJM2zK6nU43da267Ku
2026-09-01 07:08:37 +00:00

936 lines
33 KiB
C

/*
* test_repl_execution_control.c - contract test for the interactive
* simulation REPL's execution-control slice (scheduler-core spec, task
* 12.1, Req 5 AC 22).
*
* Observable under test (task 12.1's own text):
* (a) starting simulation with the REPL enabled pauses before any event
* is processed;
* (b) issuing a step command advances exactly the requested number of
* sessions and returns control to the prompt.
*
* Three tests, at three different granularities (same "unit-level precision
* PLUS integration-level end-to-end proof" split test_sim_event_loop.c's own
* header comment establishes):
*
* Test 1 (unit-level, white-box): drives repl_should_pause() -- the exact
* internal predicate sim_loop.c's post-session hook relies on -- directly,
* asserting it returns false for the first N-1 completed sessions and true
* on exactly the Nth. This is the precise "not N-1, not N+1" proof
* Observable (b) requires; it cannot be obtained by driving the full
* blocking driver (a real REPL prompt loop cannot be sampled mid-flight
* from a single-threaded test without either forking or instrumenting
* production code for tests, neither of which would be as precise as
* calling the exact decision function directly).
*
* Test 2 (integration-level, single-process, stdin freopen()'d from a
* script file): drives a REAL sched_sim_driver_run() with sim->interactive
* set against a real one-session `.replay` fixture whose session produces
* a real PEND decision, scripted with a single "continue\n" line. Proves
* Observable (a) by capturing stdout (dup()/dup2() around the run, same
* technique test_sim_verify.c's own capture_stdout_around() establishes)
* and asserting the pre-replay-pause banner text appears STRICTLY BEFORE
* the decision's own output line -- ordering, not just presence, is the
* proof that nothing was processed before the pause.
*
* Test 3 (integration-level, forked child process): drives a REAL
* sched_sim_driver_run() against a real THREE-session fixture (distinct
* jobid pushed just before each session boundary), scripted with "step
* 1\nquit\n". A child process is required because the CORRECT behavior is
* for `quit` to call exit(0) directly -- this is the genuinely falsifiable
* proof the task's own RED-phase guidance asks for: a broken
* implementation where `step` is a no-op (always runs to EOF, the exact
* probe this task's own text suggests) would run all three sessions to
* completion and return normally instead of ever reading "quit", which
* the parent detects both by exit status (0 only via quit's real exit(0),
* not by falling off the end of the child helper) AND by captured
* content (only the FIRST session's jobid may appear).
*
* Fixture strategy: the SAME real QUEUE_UPDATE+JOBS_PUSH-driving-the-real-
* builtin_fcfs-picker technique test_sim_alt_events.c/test_sim_verify.c's
* own integration-level fixtures establish (build_fixture_scheduler()
* records schedPolicy="builtin_fcfs" so sim_register_default_picker()
* resolves the real plugin; sched_job_is_ready()/sched_queue_is_ready() are
* unconditional `return 1` stubs, task 2.4's own documented, still-live
* scope note, so any queue/job pair immediately becomes a next_job()
* candidate).
*
* White-box #include of the same TU set test_sim_alt_events.c uses (see
* that file's own header comment) -- core.c/core_clock.c/core_events.c/
* core_session.c/decision.c/plugin.c/plugins/builtin_fcfs.c/recording.c/
* recording_session.c/sim.c/sim_loop.c/sim_record.c/sim_verify.c/
* snapshot.c/snapshot_load.c/snapshot_wire.c PLUS the new ../src/repl.c
* under test (task 12.1's own file, pulling readline()/add_history() calls
* into this TU -- see testsuite/meson.build's own comment on this
* executable's readline_dep/history_dep/ncurses_dep link requirement).
*/
#include <errno.h>
#include <limits.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <unity/unity.h>
#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_sim_alt_events.c/test_sim_verify.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_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_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; }
/* 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_scheduler_t* sched, xvec_idval_t* vec,
xlist_t* limits)
{
(void)sched;
(void)vec;
(void)limits;
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;
}
/* 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 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;
}
void setUp(void) { return; }
void tearDown(void) { return; }
/* ---------------------------------------------------------------------- */
/* Fixture helpers -- same techniques test_sim_alt_events.c/test_sim_verify.c */
/* already establish. */
/* ---------------------------------------------------------------------- */
static void make_tmp_dir(char* out, size_t out_len, const char* suffix)
{
char tmpl[PATH_MAX];
snprintf(tmpl, sizeof(tmpl), "/tmp/sched_replexec%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;
}
/* Same dup()/dup2()-around-STDOUT_FILENO capture technique
* test_sim_verify.c's own capture_stdout_around() establishes. */
static char* capture_stdout_around(void (*fn)(void))
{
char tmp[] = "/tmp/xce_replexec_stdoutcap_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");
fn();
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: repl_should_pause() pauses on exactly the Nth completed */
/* session -- the precise unit-level proof of Observable (b). */
/* ---------------------------------------------------------------------- */
static void test_should_pause_fires_on_exactly_the_nth_session(void)
{
xce_sched_sim_driver_t sim;
memset(&sim, 0, sizeof(sim));
sim.interactive = true;
sim.repl_state = repl_state_ensure(&sim);
TEST_ASSERT_NOT_NULL(sim.repl_state);
sim.repl_state->mode = XCE_REPL_MODE_STEP;
sim.repl_state->step_budget = 3;
TEST_ASSERT_FALSE_MESSAGE(repl_should_pause(&sim),
"must not pause after session 1 of 3");
TEST_ASSERT_FALSE_MESSAGE(repl_should_pause(&sim),
"must not pause after session 2 of 3");
TEST_ASSERT_TRUE_MESSAGE(repl_should_pause(&sim),
"must pause after exactly session 3 of 3");
free(sim.repl_state);
printf("test_should_pause_fires_on_exactly_the_nth_session: PASS\n");
}
static void test_should_pause_never_fires_in_continue_mode(void)
{
xce_sched_sim_driver_t sim;
int i;
memset(&sim, 0, sizeof(sim));
sim.interactive = true;
sim.repl_state = repl_state_ensure(&sim);
TEST_ASSERT_NOT_NULL(sim.repl_state);
sim.repl_state->mode = XCE_REPL_MODE_CONTINUE;
/* Req 5 AC 22, task 12.1's own scope: "continue" runs to EOF -- no
* breakpoint mechanism exists yet, so this must never pause no matter
* how many sessions complete. */
for (i = 0; i < 5; i++) {
TEST_ASSERT_FALSE(repl_should_pause(&sim));
}
free(sim.repl_state);
printf("test_should_pause_never_fires_in_continue_mode: PASS\n");
}
/* ---------------------------------------------------------------------- */
/* Test 2: the pre-replay pause happens BEFORE any decision is produced */
/* -- Observable (a), proved by output ORDER, not merely presence. */
/* ---------------------------------------------------------------------- */
/*
* task 12.5 (bug-1594): a completed --interactive run now re-enters the
* REPL (xce_repl_on_replay_complete(), repl.c) instead of returning, so a
* "continue"-to-EOF script that has no further scripted line will hit
* stdin EOF inside that re-entered repl_command_loop() -- which
* repl_command_loop() has always treated as an implicit `quit` (exit(0)).
* Before task 12.5 this test ran the driver IN-PROCESS (capture_stdout_
* around()) because sched_sim_driver_run() genuinely returned once EOF was
* reached; now it must fork, exactly like Test 3 below, since exit(0)
* would otherwise tear down this whole unity binary mid-suite.
*/
static void child_prepause_then_quit(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.sim_speed = 0;
args.interactive = true;
(void)sched_sim_driver_run(&sim, &args);
/* Unreachable in a correct implementation -- the script's trailing
* `quit` (or, absent that, stdin EOF) must already have called
* exit(0) from inside the post-replay-completion pause. */
fflush(stdout);
_exit(111);
}
static void test_pre_replay_pause_blocks_before_any_event_is_processed(void)
{
char replay_path[PATH_MAX];
char aux_dir[PATH_MAX];
char script_path[PATH_MAX];
char capture_path[PATH_MAX];
char pend_marker[80];
char jobid_buf[64];
FILE* f;
pid_t pid;
int status;
char* captured;
char* ready_pos;
char* pend_pos;
f = write_header_and_snapshot(replay_path, sizeof(replay_path),
"_prepause");
TEST_ASSERT_NOT_NULL(f);
append_queue_update_event(f, 1000ULL, "normal");
append_jobs_push_event(f, 1500ULL, 4242ULL, "normal");
append_replay_session(f, 2000ULL); /* the only session boundary */
append_end_of_stream_marker(f);
fclose(f);
make_tmp_dir(aux_dir, sizeof(aux_dir), "_prepauseaux");
snprintf(script_path, sizeof(script_path), "%s/script.txt", aux_dir);
snprintf(capture_path, sizeof(capture_path), "%s/capture.out", aux_dir);
/* "continue" runs to EOF (12.1's own scope: continue never re-pauses
* mid-stream absent a breakpoint); task 12.5 then re-enters the REPL
* at replay completion, so an explicit trailing `quit` is required to
* end the child deterministically. */
write_text_file(script_path, "continue\nquit\n");
pid = fork();
TEST_ASSERT_TRUE_MESSAGE(pid >= 0, "fork() must succeed");
if (pid == 0) {
child_prepause_then_quit(replay_path, script_path, capture_path);
_exit(112); /* unreachable -- child_prepause_then_quit 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), not crash/signal");
TEST_ASSERT_EQUAL_INT_MESSAGE(0, WEXITSTATUS(status),
"quit's exit(0) is the only correct exit "
"path");
captured = read_whole_file(capture_path);
/* bug-1591: sim_print_decision() (sim_loop.c) now renders jobids via
* fmt_jobid(), not raw %llu -- this marker must match that same
* human-readable form. */
fmt_jobid(jobid_buf, sizeof(jobid_buf), 4242ULL);
snprintf(pend_marker, sizeof(pend_marker), "PEND jobid=%s", jobid_buf);
ready_pos = strstr(captured, "[sim] ready. type 'help' for commands.");
pend_pos = strstr(captured, pend_marker);
TEST_ASSERT_NOT_NULL_MESSAGE(ready_pos,
"pre-replay pause banner must be printed");
TEST_ASSERT_NOT_NULL_MESSAGE(pend_pos,
"the fixture's one session must have "
"produced a live PEND decision");
TEST_ASSERT_TRUE_MESSAGE(ready_pos < pend_pos,
"the pre-replay pause banner must appear "
"BEFORE any decision output -- proves no "
"event was processed before the pause");
free(captured);
printf("test_pre_replay_pause_blocks_before_any_event_is_processed: "
"PASS\n");
}
/*
* task 12.5 (bug-1594) regression: `continue` reaching EOF with no
* breakpoints must NOT exit the process -- it must re-enter the REPL so
* the operator can inspect final state, and only `quit` (or stdin EOF,
* pre-existing documented behavior) may end it.
*/
static void child_continue_then_inspect(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.sim_speed = 0;
args.interactive = true;
(void)sched_sim_driver_run(&sim, &args);
fflush(stdout);
_exit(111); /* unreachable -- the script's `quit` must already have
* exited */
}
static void test_continue_with_no_breakpoints_stays_alive_after_replay_complete(
void)
{
char replay_path[PATH_MAX];
char aux_dir[PATH_MAX];
char script_path[PATH_MAX];
char capture_path[PATH_MAX];
FILE* f;
pid_t pid;
int status;
char* captured;
f = write_header_and_snapshot(replay_path, sizeof(replay_path),
"_contnoexit");
TEST_ASSERT_NOT_NULL(f);
append_queue_update_event(f, 1000ULL, "normal");
append_jobs_push_event(f, 1500ULL, 9090ULL, "normal");
append_replay_session(f, 2000ULL);
append_end_of_stream_marker(f);
fclose(f);
make_tmp_dir(aux_dir, sizeof(aux_dir), "_contnoexitaux");
snprintf(script_path, sizeof(script_path), "%s/script.txt", aux_dir);
snprintf(capture_path, sizeof(capture_path), "%s/capture.out", aux_dir);
/* No breakpoints set. After `continue` runs to EOF, the REPL must
* still be alive to process `info summary` before `quit`. */
write_text_file(script_path, "continue\ninfo summary\nquit\n");
pid = fork();
TEST_ASSERT_TRUE_MESSAGE(pid >= 0, "fork() must succeed");
if (pid == 0) {
child_continue_then_inspect(replay_path, script_path, capture_path);
_exit(112);
}
TEST_ASSERT_EQUAL_INT_MESSAGE(pid, waitpid(pid, &status, 0),
"waitpid must reap the REPL child");
TEST_ASSERT_TRUE_MESSAGE(
WIFEXITED(status),
"the child must exit normally via quit's exit(0) -- a hang or "
"crash means the REPL did not survive replay completion");
TEST_ASSERT_EQUAL_INT(0, WEXITSTATUS(status));
captured = read_whole_file(capture_path);
TEST_ASSERT_NOT_NULL_MESSAGE(
strstr(captured, "[sim] replay complete."),
"replay completion must still print its banner");
TEST_ASSERT_NOT_NULL_MESSAGE(
strstr(captured, "1 jobs (1 pending, 0 running)"),
"info summary issued AFTER replay completion must still execute "
"against final state -- proves the REPL did not exit on EOF "
"(bug-1594)");
free(captured);
printf("test_continue_with_no_breakpoints_stays_alive_after_replay_"
"complete: PASS\n");
}
/*
* task 12.5 (bug-1594) regression: `step`/`continue` issued AFTER replay
* is already complete must re-prompt with an informational message, not
* be treated as "hand control back to a driver loop" (there is none left).
*/
static void child_continue_twice_after_complete(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.sim_speed = 0;
args.interactive = true;
(void)sched_sim_driver_run(&sim, &args);
fflush(stdout);
_exit(111);
}
static void test_continue_after_replay_complete_reprompts_instead_of_exiting(
void)
{
char replay_path[PATH_MAX];
char aux_dir[PATH_MAX];
char script_path[PATH_MAX];
char capture_path[PATH_MAX];
FILE* f;
pid_t pid;
int status;
char* captured;
f = write_header_and_snapshot(replay_path, sizeof(replay_path),
"_contafter");
TEST_ASSERT_NOT_NULL(f);
append_queue_update_event(f, 1000ULL, "normal");
append_jobs_push_event(f, 1500ULL, 8080ULL, "normal");
append_replay_session(f, 2000ULL);
append_end_of_stream_marker(f);
fclose(f);
make_tmp_dir(aux_dir, sizeof(aux_dir), "_contafteraux");
snprintf(script_path, sizeof(script_path), "%s/script.txt", aux_dir);
snprintf(capture_path, sizeof(capture_path), "%s/capture.out", aux_dir);
/* Second `continue`/`step` are issued AFTER replay is already
* complete -- both must re-prompt, not attempt to resume anything. */
write_text_file(script_path, "continue\ncontinue\nstep\nquit\n");
pid = fork();
TEST_ASSERT_TRUE_MESSAGE(pid >= 0, "fork() must succeed");
if (pid == 0) {
child_continue_twice_after_complete(replay_path, script_path,
capture_path);
_exit(112);
}
TEST_ASSERT_EQUAL_INT_MESSAGE(pid, waitpid(pid, &status, 0),
"waitpid must reap the REPL child");
TEST_ASSERT_TRUE_MESSAGE(WIFEXITED(status),
"the child must exit normally via quit's "
"exit(0)");
TEST_ASSERT_EQUAL_INT(0, WEXITSTATUS(status));
captured = read_whole_file(capture_path);
{
const char* needle = "[sim] replay already complete -- no more "
"events to process. Use 'quit' to exit.";
char* first = strstr(captured, needle);
char* second = (first != NULL)
? strstr(first + 1, needle)
: NULL;
TEST_ASSERT_NOT_NULL_MESSAGE(
first, "a `continue` issued after replay completion must "
"print the re-prompt message, not silently resume/exit "
"(bug-1594)");
TEST_ASSERT_NOT_NULL_MESSAGE(
second, "a `step` issued after replay completion must ALSO "
"print the re-prompt message");
}
free(captured);
printf("test_continue_after_replay_complete_reprompts_instead_of_"
"exiting: PASS\n");
}
/* ---------------------------------------------------------------------- */
/* Test 3: "step 1" against a three-session fixture processes EXACTLY one */
/* session before re-pausing -- proved end to end via a forked child that */
/* then issues "quit" (real exit(0)), so a broken "step ignores N, always */
/* runs to EOF" implementation is genuinely distinguishable: it would */
/* never read "quit" at all and would exit via a different path/status. */
/* ---------------------------------------------------------------------- */
static void child_step_then_quit(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.sim_speed = 0;
args.interactive = true;
(void)sched_sim_driver_run(&sim, &args);
/* Unreachable in a correct implementation: "quit" (the script's second
* line) must have called exit(0) from inside the post-session pause
* that follows session 1. Falling through here means either the
* pre-replay pause or the post-session pause never blocked on stdin at
* all. */
fflush(stdout);
_exit(111);
}
static void test_step_1_processes_exactly_one_session_then_quit_exits(void)
{
char replay_path[PATH_MAX];
char aux_dir[PATH_MAX];
char script_path[PATH_MAX];
char capture_path[PATH_MAX];
char pend_marker_111[80];
char pend_marker_222[80];
char pend_marker_333[80];
char jobid_buf[64];
FILE* f;
pid_t pid;
int status;
char* captured;
f = write_header_and_snapshot(replay_path, sizeof(replay_path),
"_step1quit");
TEST_ASSERT_NOT_NULL(f);
append_queue_update_event(f, 1000ULL, "normal");
append_jobs_push_event(f, 1500ULL, 111ULL, "normal");
append_replay_session(f, 2000ULL); /* session boundary #1 */
append_jobs_push_event(f, 2500ULL, 222ULL, "normal");
append_replay_session(f, 3000ULL); /* session boundary #2 -- must NEVER
* be reached */
append_jobs_push_event(f, 3500ULL, 333ULL, "normal");
append_replay_session(f, 4000ULL); /* session boundary #3 -- must NEVER
* be reached */
append_end_of_stream_marker(f);
fclose(f);
make_tmp_dir(aux_dir, sizeof(aux_dir), "_step1quitaux");
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, "step 1\nquit\n");
pid = fork();
TEST_ASSERT_TRUE_MESSAGE(pid >= 0, "fork() must succeed");
if (pid == 0) {
child_step_then_quit(replay_path, script_path, capture_path);
_exit(112); /* unreachable -- child_step_then_quit 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), not crash/signal");
TEST_ASSERT_EQUAL_INT_MESSAGE(0, WEXITSTATUS(status),
"quit's exit(0) is the only correct exit "
"path -- a nonzero/111 status means "
"'step 1' never paused (ran to EOF "
"instead), the RED-phase probe this "
"task's own text describes");
captured = read_whole_file(capture_path);
/* bug-1591: sim_print_decision() (sim_loop.c) now renders jobids via
* fmt_jobid(), not raw %llu -- these markers must match that same
* human-readable form. */
fmt_jobid(jobid_buf, sizeof(jobid_buf), 111ULL);
snprintf(pend_marker_111, sizeof(pend_marker_111), "PEND jobid=%s",
jobid_buf);
fmt_jobid(jobid_buf, sizeof(jobid_buf), 222ULL);
snprintf(pend_marker_222, sizeof(pend_marker_222), "PEND jobid=%s",
jobid_buf);
fmt_jobid(jobid_buf, sizeof(jobid_buf), 333ULL);
snprintf(pend_marker_333, sizeof(pend_marker_333), "PEND jobid=%s",
jobid_buf);
TEST_ASSERT_NOT_NULL_MESSAGE(strstr(captured, pend_marker_111),
"session 1's decision must have run");
TEST_ASSERT_NULL_MESSAGE(strstr(captured, pend_marker_222),
"session 2 must NEVER have run -- step 1 "
"must pause strictly after session 1");
TEST_ASSERT_NULL_MESSAGE(strstr(captured, pend_marker_333),
"session 3 must NEVER have run either");
free(captured);
printf("test_step_1_processes_exactly_one_session_then_quit_exits: "
"PASS\n");
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_should_pause_fires_on_exactly_the_nth_session);
RUN_TEST(test_should_pause_never_fires_in_continue_mode);
RUN_TEST(test_pre_replay_pause_blocks_before_any_event_is_processed);
RUN_TEST(test_step_1_processes_exactly_one_session_then_quit_exits);
RUN_TEST(test_continue_with_no_breakpoints_stays_alive_after_replay_complete);
RUN_TEST(test_continue_after_replay_complete_reprompts_instead_of_exiting);
return UNITY_END();
}