feat(ha-heartbeat-liveness): add validate_heartbeat_config startup check (task 1.2)

Enforce heartbeat_timeout_ms >= 2 * heartbeat_interval_ms at startup.
validate_heartbeat_config() in startup_validation.c returns -EINVAL with
error message on violation; wired in cluster_init_phase1 before ha_init().
4 unit tests added to test_ha_startup.c.
This commit is contained in:
Rongsong Shen 2026-06-01 12:26:29 +00:00
parent bcb45e59d2
commit 9f9a6b3c0f
4 changed files with 129 additions and 4 deletions

View file

@ -706,6 +706,62 @@ static void test_nonblocking_election_returns_in_under_10ms(void)
elapsed_ms_val);
}
/* ── Req 2.4: heartbeat timing validation at startup ─────────────────────── */
/*
* validate_heartbeat_config: Req 2.4 — startup validation of heartbeat
* timing relationship.
*
* Returns 0 when heartbeat_timeout_ms >= 2 × heartbeat_interval_ms.
* Returns -EINVAL when heartbeat_timeout_ms < 2 × heartbeat_interval_ms;
* writes human-readable error into err_buf.
*/
int validate_heartbeat_config(uint32_t heartbeat_timeout_ms,
uint32_t heartbeat_interval_ms,
char *err_buf, size_t err_len);
static void test_heartbeat_config_valid_exact_double(void)
{
/* timeout == 2 × interval — exactly at the boundary; must return 0. */
char err_buf[256] = {0};
int rc = validate_heartbeat_config(6000, 3000, err_buf, sizeof(err_buf));
TEST_ASSERT_EQUAL_INT(0, rc);
/* No error message on success */
TEST_ASSERT_EQUAL_INT(0, err_buf[0]);
printf("test_heartbeat_config_valid_exact_double: PASS\n");
}
static void test_heartbeat_config_valid_above_double(void)
{
/* timeout > 2 × interval — clearly valid (3× ratio) */
char err_buf[256] = {0};
int rc = validate_heartbeat_config(9000, 3000, err_buf, sizeof(err_buf));
TEST_ASSERT_EQUAL_INT(0, rc);
printf("test_heartbeat_config_valid_above_double: PASS\n");
}
static void test_heartbeat_config_invalid_equal_interval(void)
{
/* timeout == interval — below the 2× floor; must reject with -EINVAL.
* Req 2.4: heartbeat_timeout_ms >= 2 × heartbeat_interval_ms */
char err_buf[256] = {0};
int rc = validate_heartbeat_config(3000, 3000, err_buf, sizeof(err_buf));
TEST_ASSERT_EQUAL_INT(-EINVAL, rc);
/* err_buf must contain a human-readable message */
TEST_ASSERT_GREATER_THAN(0, (int)strlen(err_buf));
printf("test_heartbeat_config_invalid_equal_interval: PASS\n");
}
static void test_heartbeat_config_invalid_less_than_double(void)
{
/* timeout = 1.5 × interval — also below the floor */
char err_buf[256] = {0};
int rc = validate_heartbeat_config(4500, 3000, err_buf, sizeof(err_buf));
TEST_ASSERT_EQUAL_INT(-EINVAL, rc);
TEST_ASSERT_GREATER_THAN(0, (int)strlen(err_buf));
printf("test_heartbeat_config_invalid_less_than_double: PASS\n");
}
/* ── Test suite entry point ───────────────────────────────────────────── */
void test_ha_startup(void)
@ -765,4 +821,10 @@ void test_ha_startup(void)
/* Task 1.1 (ha-heartbeat-liveness) — ha_init zero-initializes last_activity */
RUN_TEST(test_ha_init_last_activity_is_zero);
/* Req 2.4 — heartbeat timing validation at startup */
RUN_TEST(test_heartbeat_config_valid_exact_double);
RUN_TEST(test_heartbeat_config_valid_above_double);
RUN_TEST(test_heartbeat_config_invalid_equal_interval);
RUN_TEST(test_heartbeat_config_invalid_less_than_double);
}