feat(raft-library): task 4.1 — sim data structures, raft_sim_init, dump_log; 2 tests
This commit is contained in:
parent
85a7e325a8
commit
d2e2c1ef8a
4 changed files with 349 additions and 2 deletions
|
|
@ -1484,6 +1484,64 @@ static void test_raft_snapshot_returns_current_state(void)
|
|||
TEST_ASSERT_EQUAL_INT(1, snap.election_decided);
|
||||
}
|
||||
|
||||
/* ---------- simulator tests (task 4.1) ---------- */
|
||||
|
||||
#ifdef XCE_TESTING
|
||||
|
||||
/*
|
||||
* test_sim_init_3node
|
||||
*
|
||||
* raft_sim_init(c, 3, 2000) returns 0.
|
||||
* All 3 nodes have: role=FOLLOWER, term=0, voted_for=-1.
|
||||
* leader_count=0, pending_count=0.
|
||||
*/
|
||||
static void test_sim_init_3node(void)
|
||||
{
|
||||
raft_sim_cluster_t c;
|
||||
memset(&c, 0xFF, sizeof(c)); /* canary — force all fields to non-zero */
|
||||
|
||||
int rc = raft_sim_init(&c, 3, 2000);
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(0, rc);
|
||||
TEST_ASSERT_EQUAL_INT(3, c.n);
|
||||
TEST_ASSERT_EQUAL_INT(0, c.leader_count);
|
||||
TEST_ASSERT_EQUAL_INT(0, c.pending_count);
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
TEST_ASSERT_EQUAL_INT(RAFT_FOLLOWER, c.nodes[i].state.role);
|
||||
TEST_ASSERT_EQUAL_UINT64(0, c.nodes[i].state.term);
|
||||
TEST_ASSERT_EQUAL_INT(-1, c.nodes[i].state.voted_for);
|
||||
TEST_ASSERT_EQUAL_INT(i, c.nodes[i].idx);
|
||||
TEST_ASSERT_EQUAL_INT(0, c.nodes[i].partition);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* test_sim_trace_buf
|
||||
*
|
||||
* After raft_sim_init, calling raft_start_election on node 0 (which has
|
||||
* on_trace wired) must append at least one event to node 0's trace_buf.
|
||||
*/
|
||||
static void test_sim_trace_buf(void)
|
||||
{
|
||||
raft_sim_cluster_t c;
|
||||
memset(&c, 0, sizeof(c));
|
||||
|
||||
int rc = raft_sim_init(&c, 3, 2000);
|
||||
TEST_ASSERT_EQUAL_INT(0, rc);
|
||||
|
||||
/* trace_buf must start empty */
|
||||
TEST_ASSERT_EQUAL_INT(0, c.nodes[0].trace_len);
|
||||
|
||||
/* Start election on node 0 — on_trace wired in raft_sim_init will write to trace_buf */
|
||||
raft_start_election(&c.nodes[0].state);
|
||||
|
||||
/* At least one trace event must have been written */
|
||||
TEST_ASSERT_GREATER_THAN_INT(0, c.nodes[0].trace_len);
|
||||
}
|
||||
|
||||
#endif /* XCE_TESTING */
|
||||
|
||||
/* ---------- test_raft entry point ---------- */
|
||||
|
||||
void test_raft(void)
|
||||
|
|
@ -1536,4 +1594,10 @@ void test_raft(void)
|
|||
RUN_TEST(test_check_and_start_voted_for_other);
|
||||
RUN_TEST(test_check_and_start_not_voted);
|
||||
RUN_TEST(test_check_and_start_voted_for_self);
|
||||
|
||||
/* simulator init (task 4.1) */
|
||||
#ifdef XCE_TESTING
|
||||
RUN_TEST(test_sim_init_3node);
|
||||
RUN_TEST(test_sim_trace_buf);
|
||||
#endif
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue