Skip to content

Commit

Permalink
add duplicate registration test case
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengjian526 committed Jul 25, 2024
1 parent b4daa23 commit 5ef4002
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
30 changes: 10 additions & 20 deletions include/rest_rpc/router.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,23 @@ class router : asio::noncopyable {
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));
}
}

template <bool is_pub = false, typename Function, typename Self>
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);
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/test_rest_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5ef4002

Please sign in to comment.