Commit 1a30fff7 authored by Willem Thiart's avatar Willem Thiart
Browse files

Fix: don't start election without voting nodes

parent 6d4bbcc0
......@@ -185,7 +185,8 @@ int raft_periodic(raft_server_t* me_, int msec_since_last_period)
}
else if (me->election_timeout <= me->timeout_elapsed)
{
if (1 < me->num_nodes)
if (1 < raft_get_num_voting_nodes(me_) &&
raft_node_is_voting(raft_get_my_node(me_)))
raft_election_start(me_);
}
......
......@@ -403,6 +403,32 @@ void TestRaft_server_election_timeout_does_not_promote_us_to_leader_if_there_is_
CuAssertTrue(tc, 0 == raft_is_leader(r));
}
void TestRaft_server_election_timeout_does_not_promote_us_to_leader_if_we_are_not_voting_node(CuTest * tc)
{
void *r = raft_new();
raft_add_non_voting_node(r, NULL, 1, 1);
raft_set_election_timeout(r, 1000);
/* clock over (ie. 1000 + 1), causing new election */
raft_periodic(r, 1001);
CuAssertTrue(tc, 0 == raft_is_leader(r));
CuAssertTrue(tc, 0 == raft_get_current_term(r));
}
void TestRaft_server_election_timeout_does_not_start_election_if_there_are_no_voting_nodes(CuTest * tc)
{
void *r = raft_new();
raft_add_non_voting_node(r, NULL, 1, 1);
raft_add_non_voting_node(r, NULL, 2, 0);
raft_set_election_timeout(r, 1000);
/* clock over (ie. 1000 + 1), causing new election */
raft_periodic(r, 1001);
CuAssertTrue(tc, 0 == raft_get_current_term(r));
}
void TestRaft_server_election_timeout_does_promote_us_to_leader_if_there_is_only_1_node(CuTest * tc)
{
void *r = raft_new();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment