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

106 lines
3.4 KiB
C

#include <stdatomic.h>
#include <stdio.h>
#include <string.h>
#include <unity/unity.h>
#include "ha.h"
/* P3-6: Manual failover shim tests.
*
* Validates the ha_trigger_manual_failover_for_test() shim:
* - Role transitions from PRIMARY → STANDBY → PRIMARY
* - Term is monotonically increasing across multiple cycles
* - hooks_registered is reset after demotion
*/
static void test_manual_failover_transitions_role_and_increments_term(void)
{
ha_state_t ha;
ha_config_t cfg = {
.heartbeat_interval_ms = 3000,
.heartbeat_timeout_ms = 10000,
.election_timeout_ms = 2000,
.probe_connect_timeout_ms = 500,
.max_promotion_retries = 5,
};
ha_init(&ha, &cfg, 0);
/* Start as primary */
ha_do_promote(&ha);
uint64_t term_before = atomic_load_explicit(&ha.term, memory_order_acquire);
TEST_ASSERT_EQUAL_INT(CTRL_PRIMARY,
(int)atomic_load_explicit(&ha.role, memory_order_acquire));
/* Manual failover: demote then re-promote via shim */
uint64_t new_term = ha_trigger_manual_failover_for_test(&ha);
/* Term must have incremented (new election) */
TEST_ASSERT_TRUE(new_term > term_before);
/* Role must end as PRIMARY (re-promoted) */
TEST_ASSERT_EQUAL_INT(CTRL_PRIMARY,
(int)atomic_load_explicit(&ha.role, memory_order_acquire));
ha_fini(&ha);
printf("test_manual_failover_transitions_role_and_increments_term: PASS\n");
}
static void test_manual_failover_resets_hooks_registered(void)
{
ha_state_t ha;
ha_config_t cfg = {
.heartbeat_interval_ms = 3000,
.heartbeat_timeout_ms = 10000,
.election_timeout_ms = 2000,
.probe_connect_timeout_ms = 500,
.max_promotion_retries = 5,
};
ha_init(&ha, &cfg, 0);
ha_do_promote(&ha);
ha.hooks_registered = 1; /* Simulate hooks registered */
ha_trigger_manual_failover_for_test(&ha);
/* After demote+re-promote: hooks_registered was cleared by demote.
* Demotion ALWAYS resets hooks_registered to 0. */
TEST_ASSERT_EQUAL_INT(CTRL_PRIMARY,
(int)atomic_load_explicit(&ha.role, memory_order_acquire));
/* Verify that demote reset the flag (no real cluster to re-register with) */
TEST_ASSERT_EQUAL_INT(0, ha.hooks_registered);
ha_fini(&ha);
printf("test_manual_failover_resets_hooks_registered: PASS\n");
}
static void test_manual_failover_monotonic_term_across_multiple_cycles(void)
{
ha_state_t ha;
ha_config_t cfg = {
.heartbeat_interval_ms = 3000,
.heartbeat_timeout_ms = 10000,
.election_timeout_ms = 2000,
.probe_connect_timeout_ms = 500,
.max_promotion_retries = 5,
};
ha_init(&ha, &cfg, 0);
uint64_t prev_term = 0;
for (int i = 0; i < 5; i++) {
uint64_t new_term = ha_trigger_manual_failover_for_test(&ha);
TEST_ASSERT_TRUE(new_term > prev_term);
prev_term = new_term;
}
TEST_ASSERT_EQUAL_INT(CTRL_PRIMARY,
(int)atomic_load_explicit(&ha.role, memory_order_acquire));
ha_fini(&ha);
printf("test_manual_failover_monotonic_term_across_multiple_cycles: PASS\n");
}
void test_p3_manual_failover(void)
{
UnitySetTestFile(__FILE__);
RUN_TEST(test_manual_failover_transitions_role_and_increments_term);
RUN_TEST(test_manual_failover_resets_hooks_registered);
RUN_TEST(test_manual_failover_monotonic_term_across_multiple_cycles);
}