Skip to content

Commit

Permalink
Cleanup state files after test.
Browse files Browse the repository at this point in the history
  • Loading branch information
szmyd committed Nov 11, 2023
1 parent b7fbe69 commit e0b4e52
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
23 changes: 15 additions & 8 deletions src/tests/MessagingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ class MessagingFixtureBase : public ::testing::Test {
app_3_->map_peers(lookup_map);
}

void TearDown() override {
app_3_->instance_->leave_group(group_id_);
app_2_->instance_->leave_group(group_id_);
app_1_->instance_->leave_group(group_id_);
}

void start(bool data_svc_enabled = false) {
app_1_->start(data_svc_enabled);
app_2_->start(data_svc_enabled);
Expand Down Expand Up @@ -216,10 +222,6 @@ TEST_F(MessagingFixture, ClientRequest) {
{"op_type", 2},
});
EXPECT_TRUE(app_1_->instance_->append_entries(group_id_, {buf}).get());

app_3_->instance_->leave_group(group_id_);
app_2_->instance_->leave_group(group_id_);
app_1_->instance_->leave_group(group_id_);
}

// Basic resiliency test (append_entries)
Expand Down Expand Up @@ -249,10 +251,6 @@ TEST_F(MessagingFixture, MemberCrash) {
EXPECT_FALSE(app_3_->instance_->become_leader(boost::uuids::random_generator()()).get());
EXPECT_TRUE(app_3_->instance_->become_leader(group_id_).get());
EXPECT_TRUE(app_3_->instance_->append_entries(group_id_, {buf}).get());

app_3_->instance_->leave_group(group_id_);
app_2_->instance_->leave_group(group_id_);
app_1_->instance_->leave_group(group_id_);
}

// Test sending a message for a group the messaging service is not aware of.
Expand Down Expand Up @@ -313,6 +311,7 @@ TEST_F(MessagingFixture, SyncAddMember) {
srv_list.clear();
app_1_->instance_->get_srv_config_all(group_id_, srv_list);
EXPECT_EQ(srv_list.size(), 4u);
app_4->instance_->leave_group(group_id_);
}

class DataServiceFixture : public MessagingFixtureBase {
Expand All @@ -324,6 +323,7 @@ class DataServiceFixture : public MessagingFixtureBase {
}

void TearDown() override {
MessagingFixtureBase::TearDown();
for (auto& buf : cli_buf) {
buf.buf_free();
}
Expand Down Expand Up @@ -425,6 +425,13 @@ TEST_F(DataServiceFixture, BasicTest1) {
// test_group: 4 (1 SEND_DATA) + 1 (1 REQUEST_DATA) + 1 (SEND_DATA to a peer) = 6
// data_service_test_group: 1 (1 REQUEST_DATA) + 4 (1 SEND_DATA) = 5
EXPECT_EQ(test_state_mgr::get_server_counter(), 11);
app_5->instance_->leave_group(data_group);
app_5->instance_->leave_group(group_id_);
app_4->instance_->leave_group(data_group);
app_4->instance_->leave_group(group_id_);
app_3_->instance_->leave_group(data_group);
app_2_->instance_->leave_group(data_group);
app_1_->instance_->leave_group(data_group);
}

TEST_F(DataServiceFixture, BasicTest2) {
Expand Down
21 changes: 15 additions & 6 deletions src/tests/test_state_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*********************************************************************************/
#include "test_state_manager.h"

#include <filesystem>
#include <fstream>
#include <memory>
#include <random>
Expand All @@ -29,6 +30,8 @@

#include "test_state_machine.h"

#define STATE_PATH(g, s, f) fmt::format(FMT_STRING("{}_s{}{}"), (g), (s), (f))

using json = nlohmann::json;

std::vector< uint32_t > test_state_mgr::data_vec;
Expand All @@ -50,12 +53,12 @@ std::error_condition jsonObjectFromFile(std::string const& filename, json& json_
}

std::error_condition loadConfigFile(json& config_map, nuraft_mesg::group_id_t const& _group_id, int32_t const _srv_id) {
auto const config_file = fmt::format(FMT_STRING("{}_s{}/config.json"), _group_id, _srv_id);
auto const config_file = STATE_PATH(_group_id, _srv_id, "/config.json");
return jsonObjectFromFile(config_file, config_map);
}

std::error_condition loadStateFile(json& state_map, nuraft_mesg::group_id_t const& _group_id, int32_t const _srv_id) {
auto const state_file = fmt::format(FMT_STRING("{}_s{}/state.json"), _group_id, _srv_id);
auto const state_file = STATE_PATH(_group_id, _srv_id, "/state.json");
return jsonObjectFromFile(state_file, state_map);
}

Expand Down Expand Up @@ -116,7 +119,7 @@ nuraft::ptr< nuraft::cluster_config > test_state_mgr::load_config() {
}

nuraft::ptr< nuraft::log_store > test_state_mgr::load_log_store() {
return nuraft::cs_new< nuraft::jungle_log_store >(fmt::format(FMT_STRING("{}_s{}"), _group_id, _srv_id));
return nuraft::cs_new< nuraft::jungle_log_store >(STATE_PATH(_group_id, _srv_id, ""));
}

nuraft::ptr< nuraft::srv_state > test_state_mgr::read_state() {
Expand All @@ -133,7 +136,7 @@ nuraft::ptr< nuraft::srv_state > test_state_mgr::read_state() {
}

void test_state_mgr::save_config(const nuraft::cluster_config& config) {
auto const config_file = fmt::format(FMT_STRING("{}_s{}/config.json"), _group_id, _srv_id);
auto const config_file = STATE_PATH(_group_id, _srv_id, "/config.json");
auto json_obj = json{{"log_idx", config.get_log_idx()},
{"prev_log_idx", config.get_prev_log_idx()},
{"eventual_consistency", config.is_async_replication()},
Expand All @@ -146,7 +149,7 @@ void test_state_mgr::save_config(const nuraft::cluster_config& config) {
}

void test_state_mgr::save_state(const nuraft::srv_state& state) {
auto const state_file = fmt::format(FMT_STRING("{}_s{}/state.json"), _group_id, _srv_id);
auto const state_file = STATE_PATH(_group_id, _srv_id, "/state.json");
auto json_obj = json{{"term", state.get_term()}, {"voted_for", state.get_voted_for()}};

try {
Expand All @@ -161,7 +164,13 @@ std::shared_ptr< nuraft::state_machine > test_state_mgr::get_state_machine() {
return std::static_pointer_cast< nuraft::state_machine >(_state_machine);
}

void test_state_mgr::permanent_destroy() {}
test_state_mgr::~test_state_mgr() {
if (auto path = std::filesystem::weakly_canonical(STATE_PATH(_group_id, _srv_id, ""));
_will_destroy && std::filesystem::exists(path))
std::filesystem::remove_all(path);
}

void test_state_mgr::permanent_destroy() { _will_destroy = true; }

void test_state_mgr::leave() {}

Expand Down
4 changes: 3 additions & 1 deletion src/tests/test_state_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ class service;
} // namespace nuraft_mesg

class test_state_mgr : public nuraft_mesg::mesg_state_mgr {
bool _will_destroy{false};

public:
test_state_mgr(int32_t srv_id, nuraft_mesg::peer_id_t const& srv_addr, nuraft_mesg::group_id_t const& group_id);
~test_state_mgr() override = default;
~test_state_mgr() override;

nuraft::ptr< nuraft::cluster_config > load_config() override;
void save_config(const nuraft::cluster_config& config) override;
Expand Down

0 comments on commit e0b4e52

Please sign in to comment.