Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
empiredan committed Dec 5, 2024
1 parent 8aa3449 commit 840f68b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 16 deletions.
2 changes: 1 addition & 1 deletion idl/duplication.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,5 @@ struct duplication_list_response
2:string hint_message;

// app name => duplications owned by an app
3:map<string, duplication_app_state> app_state;
3:map<string, duplication_app_state> app_states;
}
3 changes: 3 additions & 0 deletions idl/utils.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ enum pattern_match_type
{
PMT_INVALID = 0,

// The string always matches no matter what the given pattern is.
PMT_MATCH_ALL,

// The string must exactly equal to the given pattern.
PMT_MATCH_EXACT,

Expand Down
2 changes: 1 addition & 1 deletion src/meta/duplication/meta_duplication_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void meta_duplication_service::list_duplication_info(const duplication_list_requ
dup_app.duplications.emplace(dup_id, dup->to_partition_level_entry_for_list());
}

response.app_state.emplace(app_name, dup_app);
response.app_states.emplace(app_name, dup_app);
}
}

Expand Down
92 changes: 84 additions & 8 deletions src/meta/test/meta_duplication_service_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@
#include "utils/time_utils.h"
#include "utils_types.h"

namespace dsn {
namespace replication {
namespace dsn::replication {

class meta_duplication_service_test : public meta_test_base
{
Expand Down Expand Up @@ -533,6 +532,39 @@ class meta_duplication_service_test : public meta_test_base
ASSERT_TRUE(app->duplicating);
}
}

void test_list_dup_app_state(const std::string &app_name, const duplication_app_state &state)
{
const auto &app = find_app(app_name);
ASSERT_EQ(app->app_id, state.appid);

ASSERT_EQ(app->duplications.size(), state.duplications.size());
for (const auto &[dup_id, dup] : app->duplications) {
ASSERT_TRUE(gutil::ContainsKey(state.duplications, dup_id));
ASSERT_EQ(dup_id, gutil::FindOrDie(state.duplications, dup_id).dupid);
ASSERT_EQ(app->partition_count,
gutil::FindOrDie(state.duplications, dup_id).partition_states.size());
}
}

void test_list_dup_info(const std::vector<std::string> &app_names,
const std::string &app_name_pattern,
utils::pattern_match_type::type match_type)
{
const auto &resp = list_dup_info(app_name_pattern, match_type);

for (const auto &[app_name, _] : resp.app_states) {
std::cout << app_name << std::endl;
}

ASSERT_EQ(ERR_OK, resp.err);
ASSERT_EQ(app_names.size(), resp.app_states.size());

for (const auto &app_name : app_names) {
ASSERT_TRUE(gutil::ContainsKey(resp.app_states, app_name));
test_list_dup_app_state(app_name, gutil::FindOrDie(resp.app_states, app_name));
}
}
};

const std::string meta_duplication_service_test::kTestAppName = "test_app";
Expand Down Expand Up @@ -810,7 +842,7 @@ TEST_F(meta_duplication_service_test, query_duplication_info)
change_dup_status(kTestAppName, test_dup, duplication_status::DS_PAUSE);

auto resp = query_dup_info(kTestAppName);
ASSERT_EQ(resp.err, ERR_OK);
ASSERT_EQ(ERR_OK, resp.err);
ASSERT_EQ(1, resp.entry_list.size());
ASSERT_EQ(duplication_status::DS_PREPARE, resp.entry_list.back().status);
ASSERT_EQ(test_dup, resp.entry_list.back().dupid);
Expand All @@ -822,6 +854,43 @@ TEST_F(meta_duplication_service_test, query_duplication_info)
ASSERT_TRUE(resp.entry_list.empty());
}

TEST_F(meta_duplication_service_test, list_duplication_info)
{
clear_apps();

create_app(kTestAppName, 8);
create_dup(kTestAppName);
create_dup(kTestAppName);

std::string app_name_1("test_list_dup");
create_app(app_name_1, 4);
create_dup(app_name_1);

std::string app_name_2("list_apps_test");
create_app(app_name_2, 8);

std::string app_name_3("test_dup_list");
create_app(app_name_3, 8);
create_dup(app_name_3);
create_dup(app_name_3);
create_dup(app_name_3);

test_list_dup_info({kTestAppName, app_name_1, app_name_2, app_name_3},
{},
utils::pattern_match_type::PMT_MATCH_ALL);

test_list_dup_info({app_name_2}, app_name_2, utils::pattern_match_type::PMT_MATCH_EXACT);

test_list_dup_info(
{kTestAppName, app_name_2}, "app", utils::pattern_match_type::PMT_MATCH_ANYWHERE);

test_list_dup_info({kTestAppName, app_name_1, app_name_3},
"test",
utils::pattern_match_type::PMT_MATCH_PREFIX);

test_list_dup_info({app_name_2}, "test", utils::pattern_match_type::PMT_MATCH_POSTFIX);
}

TEST_F(meta_duplication_service_test, re_add_duplication)
{
create_app(kTestAppName);
Expand Down Expand Up @@ -887,9 +956,17 @@ TEST_F(meta_duplication_service_test, query_duplication_handler)
static_cast<uint64_t>(dup->create_timestamp_ms), ts_buf, sizeof(ts_buf));
ASSERT_EQ(std::string() + R"({"1":{"create_ts":")" + ts_buf + R"(","dupid":)" +
std::to_string(dup->id) +
R"(,"fail_mode":"FAIL_SLOW","remote":"slave-cluster")"
R"(,"remote_app_name":"remote_test_app","remote_replica_count":3)"
R"(,"status":"DS_PREPARE"},"appid":2})",
R"(,"fail_mode":"FAIL_SLOW","partition_states":{)"
R"("0":{"confirmed_decree":-1,"last_committed_decree":-1},)"
R"("1":{"confirmed_decree":-1,"last_committed_decree":-1},)"
R"("2":{"confirmed_decree":-1,"last_committed_decree":-1},)"
R"("3":{"confirmed_decree":-1,"last_committed_decree":-1},)"
R"("4":{"confirmed_decree":-1,"last_committed_decree":-1},)"
R"("5":{"confirmed_decree":-1,"last_committed_decree":-1},)"
R"("6":{"confirmed_decree":-1,"last_committed_decree":-1},)"
R"("7":{"confirmed_decree":-1,"last_committed_decree":-1})"
R"(},"remote":"slave-cluster","remote_app_name":"remote_test_app",)"
R"("remote_replica_count":3,"status":"DS_PREPARE"},"appid":2})",
fake_resp.body);
}

Expand Down Expand Up @@ -1112,5 +1189,4 @@ TEST_F(meta_duplication_service_test, mark_follower_app_created_for_duplication)
}
}

} // namespace replication
} // namespace dsn
} // namespace dsn::replication
7 changes: 7 additions & 0 deletions src/meta/test/meta_test_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ void meta_test_base::drop_app(const std::string &name)
ASSERT_TRUE(_ss->spin_wait_staging(30));
}

void meta_test_base::clear_apps()
{
zauto_write_lock l(_ss->_lock);
_ss->_exist_apps.clear();
_ss->_all_apps.clear();
}

std::shared_ptr<app_state> meta_test_base::find_app(const std::string &name)
{
return _ss->get_app(name);
Expand Down
2 changes: 2 additions & 0 deletions src/meta/test/meta_test_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class meta_test_base : public testing::Test
// drop an app for test.
void drop_app(const std::string &name);

void clear_apps();

configuration_update_app_env_response update_app_envs(const std::string &app_name,
const std::vector<std::string> &env_keys,
const std::vector<std::string> &env_vals);
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/service_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

namespace dsn {

service_node::service_node(service_app_spec &app_spec) { _app_spec = app_spec; }
service_node::service_node(service_app_spec &app_spec) : _app_spec(app_spec) {}

bool service_node::rpc_register_handler(task_code code,
const char *extra_name,
Expand Down
10 changes: 5 additions & 5 deletions src/utils/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ error_code pattern_match(const std::string &str,
const std::string &pattern,
pattern_match_type::type match_type)
{
if (pattern.empty()) {
// Empty pattern means everything is matched.
return ERR_OK;
}

bool matched = false;
switch (match_type) {
case pattern_match_type::PMT_MATCH_ALL:
// Everything is matched.
matched = true;
break;

case pattern_match_type::PMT_MATCH_EXACT:
matched = str == pattern;
break;
Expand Down

0 comments on commit 840f68b

Please sign in to comment.