Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
willemt committed Jan 8, 2018
1 parent 396f7b7 commit 65351cb
Showing 1 changed file with 69 additions and 4 deletions.
73 changes: 69 additions & 4 deletions tests/test_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,24 @@ int __raft_send_requestvote(raft_server_t* raft,
return 0;
}

int __raft_send_appendentries(raft_server_t* raft,
static int __raft_send_appendentries(raft_server_t* raft,
void* udata,
raft_node_t* node,
msg_appendentries_t* msg)
{
return 0;
}

static int __raft_node_has_sufficient_logs(
raft_server_t* raft,
void *user_data,
raft_node_t* node)
{
int *flag = (int*)user_data;
*flag += 1;
return 0;
}

raft_cbs_t generic_funcs = {
.persist_term = __raft_persist_term,
.persist_vote = __raft_persist_vote,
Expand Down Expand Up @@ -427,8 +437,7 @@ void TestRaft_server_increment_lastApplied_when_lastApplied_lt_commitidx(

/* let time lapse */
raft_periodic(r, 1);
CuAssertTrue(tc, 0 != raft_get_last_applied_idx(r));
CuAssertTrue(tc, 1 == raft_get_last_applied_idx(r));
CuAssertIntEquals(tc, 1, raft_get_last_applied_idx(r));
}

void TestRaft_server_apply_entry_increments_last_applied_idx(CuTest* tc)
Expand Down Expand Up @@ -1279,6 +1288,7 @@ void TestRaft_follower_recv_appendentries_delete_entries_if_conflict_with_new_en
ae.entries = &mety;
ae.n_entries = 1;

/* str4 has overwritten the last 2 entries */
raft_recv_appendentries(r, raft_get_node(r, 2), &ae, &aer);
CuAssertTrue(tc, 1 == aer.success);
CuAssertTrue(tc, 2 == raft_get_log_count(r));
Expand Down Expand Up @@ -2799,6 +2809,62 @@ void TestRaft_leader_recv_appendentries_response_increase_commit_idx_when_majori
CuAssertIntEquals(tc, 2, raft_get_last_applied_idx(r));
}

void TestRaft_leader_recv_appendentries_response_set_has_sufficient_logs_for_node(
CuTest * tc)
{
raft_cbs_t funcs = {
.applylog = __raft_applylog,
.persist_term = __raft_persist_term,
.node_has_sufficient_logs = __raft_node_has_sufficient_logs,
.log = NULL
};
msg_appendentries_response_t aer;

void *r = raft_new();
raft_add_node(r, NULL, 1, 1);
raft_add_node(r, NULL, 2, 0);
raft_add_node(r, NULL, 3, 0);
raft_add_node(r, NULL, 4, 0);
raft_node_t* node = raft_add_node(r, NULL, 5, 0);

int has_sufficient_logs_flag = 0;
raft_set_callbacks(r, &funcs, &has_sufficient_logs_flag);

/* I'm the leader */
raft_set_state(r, RAFT_STATE_LEADER);
raft_set_current_term(r, 1);
raft_set_commit_idx(r, 0);
/* the last applied idx will became 1, and then 2 */
raft_set_last_applied_idx(r, 0);

/* append entries - we need two */
raft_entry_t ety = {};
ety.term = 1;
ety.id = 1;
ety.data.buf = "aaaa";
ety.data.len = 4;
raft_append_entry(r, &ety);
ety.id = 2;
raft_append_entry(r, &ety);
ety.id = 3;
raft_append_entry(r, &ety);

memset(&aer, 0, sizeof(msg_appendentries_response_t));

raft_send_appendentries(r, raft_get_node(r, 5));
aer.term = 1;
aer.success = 1;
aer.current_idx = 2;
aer.first_idx = 1;

raft_node_set_voting(node, 0);
raft_recv_appendentries_response(r, node, &aer);
CuAssertIntEquals(tc, 1, has_sufficient_logs_flag);

raft_recv_appendentries_response(r, node, &aer);
CuAssertIntEquals(tc, 1, has_sufficient_logs_flag);
}

void TestRaft_leader_recv_appendentries_response_increase_commit_idx_using_voting_nodes_majority(
CuTest * tc)
{
Expand Down Expand Up @@ -3397,7 +3463,6 @@ void TestRaft_leader_recv_entry_fails_if_prevlogidx_less_than_commit(CuTest * tc
CuAssertIntEquals(tc, 0, aer.success);
}


void TestRaft_leader_recv_entry_does_not_send_new_appendentries_to_slow_nodes(CuTest * tc)
{
void *r = raft_new();
Expand Down

0 comments on commit 65351cb

Please sign in to comment.