myxce/src/controller/testsuite/test_gc_retention.c
Rongsong Shen a81f2a249c fix(controller-tests): make Unity test-file attribution and counters consistent suite-wide
Only 21 of 86 test_*.c group functions called their own nested
UNITY_BEGIN()/UNITY_END(). Each nested call reset Unity's global
NumberOfTests/TestFailures counters, so controller_test.c's final
`return UNITY_END();` -- the value meson test actually gates on --
only ever reflected whichever small window of tests ran since the
LAST nested reset, silently discarding pass/fail evidence for
everything before it (measured: 16 of 972 total tests in one run).
The same nested-BEGIN calls also caused PASS/FAIL banners to show
the wrong source file for tests in files that don't reset it
themselves (Unity.TestFile just carries over from whichever group
last called UNITY_BEGIN()).

Fix: replace every nested UNITY_BEGIN() with UnitySetTestFile(__FILE__)
(sets file attribution only, no counter reset) and drop the matching
UNITY_END(); add the same UnitySetTestFile(__FILE__) as the first
statement to the other group functions that had neither. The 2
genuinely standalone binaries (test_job_array_sched_push.c,
test_job_array_apiserver_push.c -- each has its own int main(), not
registered via controller_test.c's TEST() macro) are untouched.

Verified: single consolidated summary now reads
"972 Tests 0 Failures 0 Ignored OK", exactly matching the
ground-truth RUN_TEST call-site count across all 83 registered
group files (976 total minus 4 belonging to the 2 standalone
binaries). Every PASS/FAIL banner now names its own true source
file. Full xce-build.sh all regression pass clean (Ok: 9, Fail: 0).

No production code touched -- test-harness plumbing only.
2026-07-17 14:49:12 +00:00

245 lines
8.4 KiB
C

#define _GNU_SOURCE
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unity/unity.h>
#include "config.h"
#include "persist.h"
#include "xce/types.h"
/* ------------------------------------------------------------------ */
/* Helpers to build minimal job_t / xce_worker_t fixtures */
/* ------------------------------------------------------------------ */
static job_t make_job(unsigned int state, time_t finish_time,
uint64_t transient_since)
{
job_t j;
memset(&j, 0, sizeof(j));
j.job.state = state;
j.job.finish_time = finish_time;
j.job.transient_since = (uint64_t)transient_since;
return j;
}
static xce_worker_t make_worker(uint32_t status, time_t dead_since)
{
xce_worker_t w;
memset(&w, 0, sizeof(w));
w.status = status;
w.dead_since = dead_since;
return w;
}
/* ------------------------------------------------------------------ */
/* Forward declarations for functions under test */
/* ------------------------------------------------------------------ */
extern bool job_is_terminal(const job_t* j);
extern bool job_should_retain(const job_t* j, const persist_config_t* cfg);
extern bool worker_should_retain(const xce_worker_t* w,
const persist_config_t* cfg);
/* ------------------------------------------------------------------ */
/* job_is_terminal tests */
/* ------------------------------------------------------------------ */
static void test_running_job_not_terminal(void)
{
job_t j = make_job(JOB_STAT_RUN, 0, 0);
TEST_ASSERT_FALSE(job_is_terminal(&j));
}
static void test_pending_job_not_terminal(void)
{
job_t j = make_job(JOB_STAT_PEND, 0, 0);
TEST_ASSERT_FALSE(job_is_terminal(&j));
}
static void test_exit_job_is_terminal(void)
{
job_t j = make_job(JOB_STAT_EXIT, time(NULL), 0);
TEST_ASSERT_TRUE(job_is_terminal(&j));
}
static void test_done_job_is_terminal(void)
{
job_t j = make_job(JOB_STAT_DONE, time(NULL), 0);
TEST_ASSERT_TRUE(job_is_terminal(&j));
}
static void test_perr_job_is_terminal(void)
{
job_t j = make_job(JOB_STAT_PERR, time(NULL), 0);
TEST_ASSERT_TRUE(job_is_terminal(&j));
}
static void test_transient_job_not_terminal(void)
{
/* JOB_STAT_TRANSIENT is NOT terminal — it's a special non-terminal state */
job_t j = make_job(JOB_STAT_TRANSIENT, 0, (uint64_t)time(NULL));
TEST_ASSERT_FALSE(job_is_terminal(&j));
}
/* ------------------------------------------------------------------ */
/* job_should_retain: TRANSIENT timeout tests */
/* ------------------------------------------------------------------ */
static void test_transient_job_retained_within_timeout(void)
{
/* transient_since = now - 100s, timeout = 300s → retain */
time_t now = time(NULL);
persist_config_t cfg = persist_config_defaults();
cfg.transient_job_timeout_s = 300;
job_t j = make_job(JOB_STAT_TRANSIENT, 0, now - 100);
TEST_ASSERT_TRUE(job_should_retain(&j, &cfg));
}
static void test_transient_job_expired(void)
{
/* transient_since = now - 400s, timeout = 300s → do NOT retain */
time_t now = time(NULL);
persist_config_t cfg = persist_config_defaults();
cfg.transient_job_timeout_s = 300;
job_t j = make_job(JOB_STAT_TRANSIENT, 0, now - 400);
TEST_ASSERT_FALSE(job_should_retain(&j, &cfg));
}
static void test_transient_job_zero_since_always_retained(void)
{
/* transient_since == 0 means unset → never expire (operator must decide) */
persist_config_t cfg = persist_config_defaults();
cfg.transient_job_timeout_s = 300;
job_t j = make_job(JOB_STAT_TRANSIENT, 0, 0);
TEST_ASSERT_TRUE(job_should_retain(&j, &cfg));
}
/* ------------------------------------------------------------------ */
/* job_should_retain: terminal job TTL tests */
/* ------------------------------------------------------------------ */
static void test_finished_job_retained_within_ttl(void)
{
/* finish_time = now - 1800s, retain = 3600s → retain */
time_t now = time(NULL);
persist_config_t cfg = persist_config_defaults();
cfg.finished_retain_s = 3600;
job_t j = make_job(JOB_STAT_EXIT, now - 1800, 0);
TEST_ASSERT_TRUE(job_should_retain(&j, &cfg));
}
static void test_finished_job_expired(void)
{
/* finish_time = now - 7200s, retain = 3600s → do NOT retain */
time_t now = time(NULL);
persist_config_t cfg = persist_config_defaults();
cfg.finished_retain_s = 3600;
job_t j = make_job(JOB_STAT_EXIT, now - 7200, 0);
TEST_ASSERT_FALSE(job_should_retain(&j, &cfg));
}
static void test_done_job_retained_within_ttl(void)
{
time_t now = time(NULL);
persist_config_t cfg = persist_config_defaults();
cfg.finished_retain_s = 3600;
job_t j = make_job(JOB_STAT_DONE, now - 1800, 0);
TEST_ASSERT_TRUE(job_should_retain(&j, &cfg));
}
static void test_running_job_always_retained(void)
{
persist_config_t cfg = persist_config_defaults();
job_t j = make_job(JOB_STAT_RUN, 0, 0);
TEST_ASSERT_TRUE(job_should_retain(&j, &cfg));
}
static void test_pending_job_always_retained(void)
{
persist_config_t cfg = persist_config_defaults();
job_t j = make_job(JOB_STAT_PEND, 0, 0);
TEST_ASSERT_TRUE(job_should_retain(&j, &cfg));
}
/* ------------------------------------------------------------------ */
/* worker_should_retain tests */
/* ------------------------------------------------------------------ */
static void test_worker_unavail_retained_within_ttl(void)
{
/* dead_since = now - 43200s (12 h), retain = 86400s (24 h) → retain */
time_t now = time(NULL);
persist_config_t cfg = persist_config_defaults();
cfg.worker_retain_s = 86400;
xce_worker_t w = make_worker(XCE_WORKER_UNAVAIL, now - 43200);
TEST_ASSERT_TRUE(worker_should_retain(&w, &cfg));
}
static void test_worker_unavail_expired(void)
{
/* dead_since = now - 100000s, retain = 86400s → do NOT retain */
time_t now = time(NULL);
persist_config_t cfg = persist_config_defaults();
cfg.worker_retain_s = 86400;
xce_worker_t w = make_worker(XCE_WORKER_UNAVAIL, now - 100000);
TEST_ASSERT_FALSE(worker_should_retain(&w, &cfg));
}
static void test_worker_hidden_never_retained(void)
{
/* XCE_WORKER_HIDDEN → cleanup immediately regardless of dead_since */
time_t now = time(NULL);
persist_config_t cfg = persist_config_defaults();
xce_worker_t w = make_worker(XCE_WORKER_HIDDEN, now - 10);
TEST_ASSERT_FALSE(worker_should_retain(&w, &cfg));
}
static void test_worker_healthy_always_retained(void)
{
persist_config_t cfg = persist_config_defaults();
xce_worker_t w = make_worker(0, 0);
TEST_ASSERT_TRUE(worker_should_retain(&w, &cfg));
}
static void test_worker_unavail_zero_dead_since_always_retained(void)
{
/* dead_since == 0 → not yet set, do not GC */
persist_config_t cfg = persist_config_defaults();
cfg.worker_retain_s = 86400;
xce_worker_t w = make_worker(XCE_WORKER_UNAVAIL, 0);
TEST_ASSERT_TRUE(worker_should_retain(&w, &cfg));
}
/* ------------------------------------------------------------------ */
/* Test suite entry point */
/* ------------------------------------------------------------------ */
void test_gc_retention(void)
{
UnitySetTestFile(__FILE__);
RUN_TEST(test_running_job_not_terminal);
RUN_TEST(test_pending_job_not_terminal);
RUN_TEST(test_exit_job_is_terminal);
RUN_TEST(test_done_job_is_terminal);
RUN_TEST(test_perr_job_is_terminal);
RUN_TEST(test_transient_job_not_terminal);
RUN_TEST(test_transient_job_retained_within_timeout);
RUN_TEST(test_transient_job_expired);
RUN_TEST(test_transient_job_zero_since_always_retained);
RUN_TEST(test_finished_job_retained_within_ttl);
RUN_TEST(test_finished_job_expired);
RUN_TEST(test_done_job_retained_within_ttl);
RUN_TEST(test_running_job_always_retained);
RUN_TEST(test_pending_job_always_retained);
RUN_TEST(test_worker_unavail_retained_within_ttl);
RUN_TEST(test_worker_unavail_expired);
RUN_TEST(test_worker_hidden_never_retained);
RUN_TEST(test_worker_healthy_always_retained);
RUN_TEST(test_worker_unavail_zero_dead_since_always_retained);
}