feat(scheduler-core): implement REPL execution control (task 12.1)
Adds the interactive simulation REPL's execution-control slice (Req 5 AC 22): a single-threaded input loop (readline-based) supporting step [N], continue, quit, and help, pausing before the first replay event and blocking the driver loop while active. Review round 1 raised two findings, both resolved here: - GNU readline (GPL) was linked directly into xce_scheduler, a shipped production daemon. Switched the whole project to libedit (BSD) for line editing -- scheduler and controller (xce_test) both now use editline/readline.h, API-compatible (readline()/add_history()). - Getting a clean regression run after the libedit swap required a full clean rebuild, which exposed a genuine pre-existing SIGSEGV (bug-1504): SnapshotData.internalState was a capnp Data field, and this codebase's capnpc-c backend silently no-ops Data-field encoding without zero-initializing the caller's stack struct, leaving an uninitialized capn_ptr written onto the wire. Fixed by switching internalState to List(UInt8) (with a new internalStateLen wire field), matching the working FetchFileReply.content/bytesRead precedent -- no hand-written accessor code needed. A second, unrelated instance of the same bug class was found in the controller's WAL-catchup path (WalCatchupEntry.payload) and deliberately left unfixed as bug-1505, out of scheduler-core's boundary; needs its own project-wide Data-field audit. Scheduler suite: 34/34 clean. RED-phase reproduction recorded in tasks.md (repl_should_pause() step-budget check disabled -> 2/4 tests fail as expected -> reverted -> 34/34 green). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JPPXeKSUXUqcqvzNXY1rzJ
This commit is contained in:
parent
de00ccff37
commit
02f7f48ff9
17 changed files with 1491 additions and 81 deletions
643
src/scheduler/testsuite/test_repl_execution_control.c
Normal file
643
src/scheduler/testsuite/test_repl_execution_control.c
Normal file
|
|
@ -0,0 +1,643 @@
|
|||
/*
|
||||
* 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/sim.c"
|
||||
#include "../src/sim_loop.c"
|
||||
#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; }
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/* 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. */
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
static xce_sched_sim_driver_t g_t2_sim;
|
||||
static sched_sim_args_t g_t2_args;
|
||||
static int g_t2_rc;
|
||||
|
||||
static void run_driver_for_t2_capture(void)
|
||||
{
|
||||
g_t2_rc = sched_sim_driver_run(&g_t2_sim, &g_t2_args);
|
||||
}
|
||||
|
||||
static void test_pre_replay_pause_blocks_before_any_event_is_processed(void)
|
||||
{
|
||||
char replay_path[PATH_MAX];
|
||||
char script_dir[PATH_MAX];
|
||||
char script_path[PATH_MAX];
|
||||
FILE* f;
|
||||
char* captured;
|
||||
char* ready_pos;
|
||||
char* pend_pos;
|
||||
FILE* saved_stdin_check;
|
||||
|
||||
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(script_dir, sizeof(script_dir), "_prepausescript");
|
||||
snprintf(script_path, sizeof(script_path), "%s/script.txt", script_dir);
|
||||
/* A single "continue" -- 12.1's own scope means continue never
|
||||
* re-pauses, so the run reaches EOF cleanly without reading stdin
|
||||
* again. */
|
||||
write_text_file(script_path, "continue\n");
|
||||
|
||||
saved_stdin_check = freopen(script_path, "r", stdin);
|
||||
TEST_ASSERT_NOT_NULL_MESSAGE(saved_stdin_check,
|
||||
"freopen of scripted stdin must succeed");
|
||||
|
||||
memset(&g_t2_sim, 0, sizeof(g_t2_sim));
|
||||
memset(&g_t2_args, 0, sizeof(g_t2_args));
|
||||
g_t2_args.replay_path = replay_path;
|
||||
g_t2_args.sim_start_ts = SCHED_SIM_START_TS_LATEST;
|
||||
g_t2_args.sim_speed = 0;
|
||||
g_t2_args.interactive = true;
|
||||
|
||||
captured = capture_stdout_around(run_driver_for_t2_capture);
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(0, g_t2_rc);
|
||||
|
||||
ready_pos = strstr(captured, "[sim] ready. type 'help' for commands.");
|
||||
pend_pos = strstr(captured, "PEND jobid=4242");
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
/* 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];
|
||||
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);
|
||||
|
||||
TEST_ASSERT_NOT_NULL_MESSAGE(strstr(captured, "PEND jobid=111"),
|
||||
"session 1's decision must have run");
|
||||
TEST_ASSERT_NULL_MESSAGE(strstr(captured, "PEND jobid=222"),
|
||||
"session 2 must NEVER have run -- step 1 "
|
||||
"must pause strictly after session 1");
|
||||
TEST_ASSERT_NULL_MESSAGE(strstr(captured, "PEND jobid=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);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue