Skip to content

Commit

Permalink
Merge pull request #142 from zhengjian526/master
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jul 26, 2024
2 parents aa02a75 + 5ef4002 commit 35761ed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
16 changes: 12 additions & 4 deletions include/rest_rpc/router.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,24 @@ 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());
key2func_name_.emplace(key, name);
return register_nonmember_func<is_pub>(key, std::move(f));
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());
key2func_name_.emplace(key, name);
return register_member_func<is_pub>(key, f, self);
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);
}
}

void remove_handler(std::string const &name) {
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 35761ed

Please sign in to comment.