diff --git a/src/tests/MessagingTest.cpp b/src/tests/MessagingTest.cpp index 36c2b19..6ef2b6c 100644 --- a/src/tests/MessagingTest.cpp +++ b/src/tests/MessagingTest.cpp @@ -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); @@ -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) @@ -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. @@ -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 { @@ -324,6 +323,7 @@ class DataServiceFixture : public MessagingFixtureBase { } void TearDown() override { + MessagingFixtureBase::TearDown(); for (auto& buf : cli_buf) { buf.buf_free(); } @@ -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) { diff --git a/src/tests/test_state_manager.cpp b/src/tests/test_state_manager.cpp index 5365034..0cc84ee 100644 --- a/src/tests/test_state_manager.cpp +++ b/src/tests/test_state_manager.cpp @@ -14,6 +14,7 @@ *********************************************************************************/ #include "test_state_manager.h" +#include #include #include #include @@ -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; @@ -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); } @@ -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() { @@ -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()}, @@ -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 { @@ -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() {} diff --git a/src/tests/test_state_manager.h b/src/tests/test_state_manager.h index 01808e8..449fe46 100644 --- a/src/tests/test_state_manager.h +++ b/src/tests/test_state_manager.h @@ -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;