add duplicate registration test case

This commit is contained in:
zhengjian
2024-07-25 22:00:18 +08:00
parent b4daa23f5e
commit 5ef4002228
2 changed files with 30 additions and 20 deletions
+10 -20
View File
@@ -50,16 +50,11 @@ public:
template <bool is_pub = false, typename Function>
void register_handler(std::string const &name, Function f, bool pub = false) {
uint32_t key = MD5::MD5Hash32(name.data());
try {
if (key2func_name_.find(key) != key2func_name_.end()) {
throw std::invalid_argument("duplicate registration key !");
} else {
key2func_name_.emplace(key, name);
return register_nonmember_func<is_pub>(key, std::move(f));
}
} catch (const std::exception &e) {
std::cerr << e.what() << '\n';
throw;
if (key2func_name_.find(key) != key2func_name_.end()) {
throw std::invalid_argument("duplicate registration key !");
} else {
key2func_name_.emplace(key, name);
return register_nonmember_func<is_pub>(key, std::move(f));
}
}
@@ -67,16 +62,11 @@ public:
void register_handler(std::string const &name, const Function &f,
Self *self) {
uint32_t key = MD5::MD5Hash32(name.data());
try {
if (key2func_name_.find(key) != key2func_name_.end()) {
throw std::invalid_argument("duplicate registration key !");
} else {
key2func_name_.emplace(key, name);
return register_member_func<is_pub>(key, f, self);
}
} catch (const std::exception &e) {
std::cerr << e.what() << '\n';
throw;
if (key2func_name_.find(key) != key2func_name_.end()) {
throw std::invalid_argument("duplicate registration key !");
} else {
key2func_name_.emplace(key, name);
return register_member_func<is_pub>(key, f, self);
}
}
+20
View File
@@ -487,6 +487,26 @@ TEST_CASE("test_server_delay_response") {
server.async_run();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
CHECK(r);
auto result = client.call<std::string>("delay_echo", "test_delay_echo");
CHECK_EQ(result, "test_delay_echo");
}
TEST_CASE("test_server_duplicate_registration_key") {
rpc_server server(9000, std::thread::hardware_concurrency());
server.register_handler("delay_echo", delay_echo);
try {
server.register_handler("delay_echo", delay_echo);
} catch (const std::exception &e) {
string_view ew{e.what()};
std::cerr << ew << '\n';
CHECK_EQ(ew, "duplicate registration key !");
}
server.async_run();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
CHECK(r);