mirror of
https://github.com/qicosmos/rest_rpc.git
synced 2026-08-29 08:34:47 +08:00
add duplicate registration test case
This commit is contained in:
@@ -50,34 +50,24 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void remove_handler(std::string const &name) {
|
||||
|
||||
@@ -493,3 +493,23 @@ TEST_CASE("test_server_delay_response") {
|
||||
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);
|
||||
auto result = client.call<std::string>("delay_echo", "test_delay_echo");
|
||||
CHECK_EQ(result, "test_delay_echo");
|
||||
}
|
||||
Reference in New Issue
Block a user