Commit 9ebc6d71 authored by Willem Thiart's avatar Willem Thiart
Browse files

Become leader if num_nodes == 1

parent fed506d1
...@@ -144,7 +144,12 @@ int raft_periodic(raft_server_t* me_, int msec_since_last_period) ...@@ -144,7 +144,12 @@ int raft_periodic(raft_server_t* me_, int msec_since_last_period)
} }
} }
else if (me->election_timeout <= me->timeout_elapsed) else if (me->election_timeout <= me->timeout_elapsed)
raft_election_start(me_); {
if (1 == me->num_nodes)
raft_become_leader(me_);
else
raft_election_start(me_);
}
if (me->last_applied_idx < me->commit_idx) if (me->last_applied_idx < me->commit_idx)
if (-1 == raft_apply_entry(me_)) if (-1 == raft_apply_entry(me_))
......
...@@ -312,17 +312,16 @@ void TestRaft_server_periodic_elapses_election_timeout(CuTest * tc) ...@@ -312,17 +312,16 @@ void TestRaft_server_periodic_elapses_election_timeout(CuTest * tc)
CuAssertTrue(tc, 100 == raft_get_timeout_elapsed(r)); CuAssertTrue(tc, 100 == raft_get_timeout_elapsed(r));
} }
void void TestRaft_server_election_timeout_promotes_us_to_leader_if_there_is_only_1_node(CuTest * tc)
TestRaft_server_election_timeout_sets_to_zero_when_elapsed_time_greater_than_timeout(
CuTest * tc)
{ {
void *r = raft_new(); void *r = raft_new();
raft_add_node(r, (void*)1, 1);
raft_set_election_timeout(r, 1000); raft_set_election_timeout(r, 1000);
/* greater than 1000 */ /* clock over (ie. 1000 + 1), causing new election */
raft_periodic(r, 2000); raft_periodic(r, 1001);
/* less than 1000 as the timeout would be randomised */
CuAssertTrue(tc, raft_get_timeout_elapsed(r) < 1000); CuAssertTrue(tc, 1 == raft_is_leader(r));
} }
void TestRaft_server_cfg_sets_num_nodes(CuTest * tc) void TestRaft_server_cfg_sets_num_nodes(CuTest * tc)
......
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