void TestRaft_scenario_leader_appears(CuTest * tc) { unsigned long i, j; raft_server_t *r[3]; void* sender[3]; senders_new(); for (j = 0; j < 3; j++) sender[j] = sender_new((void*)j); for (j = 0; j < 3; j++) { r[j] = raft_new(); sender_set_raft(sender[j], r[j]); raft_set_election_timeout(r[j], 500); raft_add_node(r[j], sender[0], 1, j==0); raft_add_node(r[j], sender[1], 2, j==1); raft_add_node(r[j], sender[2], 3, j==2); raft_set_callbacks(r[j], &((raft_cbs_t) { .send_requestvote = sender_requestvote, .send_appendentries = sender_appendentries, .persist_term = __raft_persist_term, .persist_vote = __raft_persist_vote, .log = NULL }), sender[j]); }
void TestRaft_server_election_timeout_sets_to_zero_when_elapsed_time_greater_than_timeout( CuTest * tc) { void *r = raft_new(); raft_set_election_timeout(r, 1000); /* greater than 1000 */ raft_periodic(r, 2000); /* less than 1000 as the timeout would be randomised */ CuAssertTrue(tc, raft_get_timeout_elapsed(r) < 1000); }
/* Candidate 5.2 */ void TestRaft_follower_becoming_candidate_resets_election_timeout(CuTest * tc) { void *r = raft_new(); raft_set_election_timeout(r, 1000); CuAssertTrue(tc, 0 == raft_get_timeout_elapsed(r)); raft_periodic(r, 900); CuAssertTrue(tc, 900 == raft_get_timeout_elapsed(r)); raft_become_candidate(r); /* time is selected randomly */ CuAssertTrue(tc, raft_get_timeout_elapsed(r) < 900); }
void TestRaft_server_periodic_elapses_election_timeout(CuTest * tc) { void *r = raft_new(); /* we don't want to set the timeout to zero */ raft_set_election_timeout(r, 1000); CuAssertTrue(tc, 0 == raft_get_timeout_elapsed(r)); raft_periodic(r, 0); CuAssertTrue(tc, 0 == raft_get_timeout_elapsed(r)); raft_periodic(r, 100); CuAssertTrue(tc, 100 == raft_get_timeout_elapsed(r)); }
bool RaftConsensus::initialize(const Options& options) { if (_raft) { return true; } _raft = raft_new(); if (!_raft) { return false; } // // Seed the random number generator // rc_seed_random(); // // Initialize callbacks // rc_init_func(); raft_set_callbacks(_raft, &rc_funcs, this); // // add self // _opt = options; if (!addNode(_opt.node_id)) { return false; } // // Start as master // if (_opt.is_master) { becomeMaster(); } // // Set the election timeout // raft_set_election_timeout(_raft, _opt.election_timeout_ms); return true; }
void TestRaft_follower_recv_appendentries_resets_election_timeout( CuTest * tc) { void *r = raft_new(); raft_set_election_timeout(r, 1000); raft_periodic(r, 900); msg_appendentries_t ae; msg_appendentries_response_t aer; memset(&ae, 0, sizeof(msg_appendentries_t)); ae.term = 1; raft_recv_appendentries(r, 1, &ae, &aer); CuAssertTrue(tc, 0 == raft_get_timeout_elapsed(r)); }
void TestRaft_follower_becomes_candidate_when_election_timeout_occurs( CuTest * tc) { void *r = raft_new(); /* 1 second election timeout */ raft_set_election_timeout(r, 1000); raft_add_node(r, (void*)1, 1); raft_add_node(r, (void*)2, 0); /* 1.001 seconds have passed */ raft_periodic(r, 1001); /* is a candidate now */ CuAssertTrue(tc, 1 == raft_is_candidate(r)); }