diff --git a/include/rest_rpc/router.h b/include/rest_rpc/router.h index 489646e..6d3a01b 100644 --- a/include/rest_rpc/router.h +++ b/include/rest_rpc/router.h @@ -50,16 +50,11 @@ class router : asio::noncopyable { template 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(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(key, std::move(f)); } } @@ -67,16 +62,11 @@ class router : asio::noncopyable { 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(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(key, f, self); } } diff --git a/tests/test_rest_rpc.cpp b/tests/test_rest_rpc.cpp index 7f8271f..ee0f37d 100644 --- a/tests/test_rest_rpc.cpp +++ b/tests/test_rest_rpc.cpp @@ -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("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);