mirror of
https://github.com/qicosmos/rest_rpc.git
synced 2026-08-29 08:34:47 +08:00
add ut
This commit is contained in:
@@ -85,12 +85,10 @@ public:
|
||||
route_result.ec = rpc_errc::ok;
|
||||
}
|
||||
} catch (const std::exception &ex) {
|
||||
rpc_service::msgpack_codec codec;
|
||||
route_result.result =
|
||||
std::string("exception occur when call").append(ex.what());
|
||||
route_result.ec = rpc_errc::function_exception;
|
||||
} catch (...) {
|
||||
rpc_service::msgpack_codec codec;
|
||||
route_result.result = std::string("unknown exception occur when call ")
|
||||
.append(get_name_by_key(key));
|
||||
route_result.ec = rpc_errc::function_unknown_exception;
|
||||
@@ -120,24 +118,16 @@ private:
|
||||
using args_tuple =
|
||||
typename util::function_traits<Function>::parameters_type;
|
||||
using R = typename util::function_traits<Function>::return_type;
|
||||
try {
|
||||
if constexpr (std::tuple_size_v<args_tuple> == 0) {
|
||||
co_await handle_zero_arg<R>(f, ret, self);
|
||||
if constexpr (std::tuple_size_v<args_tuple> == 0) {
|
||||
co_await handle_zero_arg<R>(f, ret, self);
|
||||
} else {
|
||||
using first_t = std::tuple_element_t<0, args_tuple>;
|
||||
if constexpr (std::tuple_size_v<args_tuple> == 1 &&
|
||||
util::is_basic_v<first_t>) {
|
||||
co_await handle_one_arg<R, first_t>(str, f, ret, self);
|
||||
} else {
|
||||
using first_t = std::tuple_element_t<0, args_tuple>;
|
||||
if constexpr (std::tuple_size_v<args_tuple> == 1 &&
|
||||
util::is_basic_v<first_t>) {
|
||||
co_await handle_one_arg<R, first_t>(str, f, ret, self);
|
||||
} else {
|
||||
co_await handle_more_args<R, args_tuple>(str, f, ret, self);
|
||||
}
|
||||
co_await handle_more_args<R, args_tuple>(str, f, ret, self);
|
||||
}
|
||||
} catch (std::invalid_argument &e) {
|
||||
ret.ec = rpc_errc::invalid_argument;
|
||||
ret.result = e.what();
|
||||
} catch (const std::exception &e) {
|
||||
ret.ec = rpc_errc::function_exception;
|
||||
ret.result = e.what();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ include_directories(
|
||||
"../thirdparty/asio"
|
||||
)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
|
||||
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
|
||||
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
|
||||
|
||||
add_executable(test_rest_rpc test_rest_rpc.cpp)
|
||||
|
||||
@@ -87,6 +87,10 @@ asio::awaitable<std::string> echo_coro(std::string str) { co_return str; }
|
||||
|
||||
void no_arg() { std::cout << "no args\n"; }
|
||||
|
||||
void exception_func() { throw std::invalid_argument("invalid"); }
|
||||
|
||||
void unknown_exception_func() { throw 1; }
|
||||
|
||||
asio::awaitable<void> no_arg_coro() {
|
||||
std::cout << "no args\n";
|
||||
co_return;
|
||||
@@ -109,8 +113,25 @@ asio::awaitable<void> test_router() {
|
||||
router.register_handler<no_arg_coro>();
|
||||
router.register_handler<no_arg_coro1>();
|
||||
router.register_handler<add_coro>();
|
||||
router.register_handler<no_arg>();
|
||||
|
||||
auto name = router.get_name_by_key(get_key<add>());
|
||||
CHECK(name == "add");
|
||||
router.remove_handler<add>();
|
||||
name = router.get_name_by_key(get_key<add>());
|
||||
CHECK(name != "add");
|
||||
router.register_handler<add>();
|
||||
name = router.get_name_by_key(get_key<add>());
|
||||
CHECK(name == "add");
|
||||
CHECK_THROWS_AS(router.register_handler<add>(), std::invalid_argument);
|
||||
|
||||
name = router.get_name_by_key(111);
|
||||
CHECK(name == "111");
|
||||
|
||||
{
|
||||
auto ret0 = co_await router.route(get_key<no_arg>(), "");
|
||||
CHECK(ret0.ec == rpc_errc::ok);
|
||||
|
||||
auto ret = co_await router.route(get_key<no_arg_coro>(), "");
|
||||
CHECK(ret.ec == rpc_errc::ok);
|
||||
|
||||
@@ -119,6 +140,15 @@ asio::awaitable<void> test_router() {
|
||||
|
||||
auto ret2 = co_await router.route(get_key<echo_coro>(), "test");
|
||||
CHECK(ret2.ec == rpc_errc::ok);
|
||||
|
||||
router.register_handler<exception_func>();
|
||||
router.register_handler<unknown_exception_func>();
|
||||
auto ret3 = co_await router.route(get_key<exception_func>(), "");
|
||||
CHECK(ret3.ec == rpc_errc::function_exception);
|
||||
auto ret4 = co_await router.route(get_key<unknown_exception_func>(), "");
|
||||
CHECK(ret4.ec == rpc_errc::function_unknown_exception);
|
||||
auto ret5 = co_await router.route(111, "");
|
||||
CHECK(ret5.ec == rpc_errc::no_such_function);
|
||||
}
|
||||
|
||||
dummy d{};
|
||||
|
||||
Reference in New Issue
Block a user