Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
resetius committed Nov 24, 2023
1 parent df4a96d commit 569650e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/raft.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,25 @@ class TRaft {
void Process(TMessageHolder<TMessage> message, INode* replyTo = nullptr);
void ApplyResult(ITimeSource::Time now, std::unique_ptr<TResult> result, INode* replyTo = nullptr);

// ut
EState CurrentStateName() const {
return StateName;
}

void Become(EState newStateName);

const TState* GetState() const {
return State.get();
}

const TVolatileState* GetVolatileState() const {
return VolatileState.get();
}

const auto GetLastTime() const {
return LastTime;
}

private:
std::unique_ptr<TResult> Follower(ITimeSource::Time now, TMessageHolder<TMessage> message);
std::unique_ptr<TResult> Candidate(ITimeSource::Time now, TMessageHolder<TMessage> message);
Expand Down
39 changes: 39 additions & 0 deletions test/test_raft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,42 @@ void test_become_same_func(void**) {
assert_true(raft->CurrentStateName() == EState::FOLLOWER);
}

void test_apply_empty_result(void**) {
auto ts = std::make_shared<TFakeTimeSource>();
auto raft = MakeRaft({}, 3, ts);
auto state = raft->GetState();
auto volatileState = raft->GetVolatileState();
assert_true(raft->CurrentStateName() == EState::FOLLOWER);
raft->ApplyResult(ts->Now(), {});
assert_true(raft->CurrentStateName() == EState::FOLLOWER);
assert_true(state == raft->GetState());
assert_true(volatileState == raft->GetVolatileState());
}

void test_apply_state_func_change_result(void**) {
auto ts = std::make_shared<TFakeTimeSource>();
auto raft = MakeRaft({}, 3, ts);
auto state = raft->GetState();
auto volatileState = raft->GetVolatileState();
assert_true(raft->CurrentStateName() == EState::FOLLOWER);
raft->ApplyResult(ts->Now(), std::make_unique<TResult>(TResult {
.NextStateName = EState::CANDIDATE
}));
assert_true(raft->CurrentStateName() == EState::CANDIDATE);
assert_true(state == raft->GetState());
assert_true(volatileState == raft->GetVolatileState());
}

void test_apply_time_change_result(void**) {
auto ts = std::make_shared<TFakeTimeSource>();
auto raft = MakeRaft({}, 3, ts);
auto n = ts->Now();
raft->ApplyResult(n, std::make_unique<TResult>(TResult {
.UpdateLastTime = true
}));
assert_true(raft->GetLastTime() == n);
}

int main() {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_empty),
Expand All @@ -173,6 +209,9 @@ int main() {
cmocka_unit_test(test_initial),
cmocka_unit_test(test_become),
cmocka_unit_test(test_become_same_func),
cmocka_unit_test(test_apply_empty_result),
cmocka_unit_test(test_apply_state_func_change_result),
cmocka_unit_test(test_apply_time_change_result),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

0 comments on commit 569650e

Please sign in to comment.