myxce/src/controller/testsuite/test_ctrl_metrics.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

89 lines
3.2 KiB
C

/* test_ctrl_metrics.c — P3-7: Metrics infrastructure unit tests
*
* TDD: these tests are written FIRST to drive the implementation.
* They verify:
* - ctrl_metrics_init() zeroes all 7 counters/gauges
* - CTRL_METRIC_INC increments atomically
* - CTRL_METRIC_SET writes and overwrites a gauge
* - The descriptor table has exactly 7 entries + NULL sentinel
* - ctrl_transient_jobs_stuck counter accumulates correctly
*/
#define _GNU_SOURCE
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <unity/unity.h>
#include "ctrl_metrics.h"
/* ------------------------------------------------------------------ */
/* Tests */
/* ------------------------------------------------------------------ */
static void test_metrics_init_zeros_all_counters(void)
{
ctrl_metrics_init();
TEST_ASSERT_EQUAL_UINT64(0, atomic_load(&ctrl_repl_degraded_count));
TEST_ASSERT_EQUAL_UINT64(0, atomic_load(&ctrl_split_brain_detected));
TEST_ASSERT_EQUAL_UINT64(0, atomic_load(&ctrl_async_events_lost));
TEST_ASSERT_EQUAL_UINT64(0, atomic_load(&ctrl_transient_jobs_stuck));
TEST_ASSERT_EQUAL_UINT64(0, atomic_load(&ctrl_wal_bytes));
TEST_ASSERT_EQUAL_UINT64(0, atomic_load(&ctrl_checkpoint_duration_ms));
TEST_ASSERT_EQUAL_UINT64(0, atomic_load(&ctrl_repl_lag_lsn));
printf("test_metrics_init_zeros_all_counters: PASS\n");
}
static void test_metrics_increment_repl_degraded(void)
{
ctrl_metrics_init();
CTRL_METRIC_INC(ctrl_repl_degraded_count);
CTRL_METRIC_INC(ctrl_repl_degraded_count);
TEST_ASSERT_EQUAL_UINT64(2, atomic_load(&ctrl_repl_degraded_count));
printf("test_metrics_increment_repl_degraded: PASS\n");
}
static void test_metrics_set_gauge(void)
{
ctrl_metrics_init();
CTRL_METRIC_SET(ctrl_wal_bytes, 1024 * 1024);
TEST_ASSERT_EQUAL_UINT64(1024 * 1024, atomic_load(&ctrl_wal_bytes));
CTRL_METRIC_SET(ctrl_wal_bytes, 0);
TEST_ASSERT_EQUAL_UINT64(0, atomic_load(&ctrl_wal_bytes));
printf("test_metrics_set_gauge: PASS\n");
}
static void test_metrics_descriptor_table_has_sentinel(void)
{
/* The descriptor table must end with a NULL-name sentinel
* and contain exactly 7 real entries. */
int i = 0;
while (ctrl_metric_descriptors[i].name != NULL)
i++;
TEST_ASSERT_EQUAL_INT(7, i);
printf("test_metrics_descriptor_table_has_sentinel: PASS\n");
}
static void test_transient_jobs_stuck_counter(void)
{
ctrl_metrics_init();
/* Simulate 3 stuck transient jobs */
for (int j = 0; j < 3; j++)
CTRL_METRIC_INC(ctrl_transient_jobs_stuck);
TEST_ASSERT_EQUAL_UINT64(3, atomic_load(&ctrl_transient_jobs_stuck));
printf("test_transient_jobs_stuck_counter: PASS\n");
}
/* ------------------------------------------------------------------ */
/* Suite entry point (called from controller_test.c) */
/* ------------------------------------------------------------------ */
void test_ctrl_metrics(void)
{
UnitySetTestFile(__FILE__);
RUN_TEST(test_metrics_init_zeros_all_counters);
RUN_TEST(test_metrics_increment_repl_degraded);
RUN_TEST(test_metrics_set_gauge);
RUN_TEST(test_metrics_descriptor_table_has_sentinel);
RUN_TEST(test_transient_jobs_stuck_counter);
}