From c2b1cefeb2680adf670a5d2e30601eeb331c0fbf Mon Sep 17 00:00:00 2001 From: qicosmos Date: Fri, 10 Oct 2025 10:04:21 +0800 Subject: [PATCH] test struct --- include/rest_rpc/codec.h | 4 +-- include/rest_rpc/rpc_client.hpp | 10 ++++++-- include/rest_rpc/rpc_connection.hpp | 2 +- tests/test_rest_rpc.cpp | 38 +++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/include/rest_rpc/codec.h b/include/rest_rpc/codec.h index 020fd76..983eddc 100644 --- a/include/rest_rpc/codec.h +++ b/include/rest_rpc/codec.h @@ -29,11 +29,11 @@ struct msgpack_codec { } else { buffer_type buffer(init_size); msgpack::pack(buffer, std::forward_as_tuple(std::forward(args)...)); - return buffer; + return std::string(buffer.data(), buffer.size()); } } - template static std::string pack_to_string(Arg &arg) { + template static std::string pack_to_string(Arg &&arg) { buffer_type buffer(init_size); msgpack::pack(buffer, arg); return std::string(buffer.data(), buffer.size()); diff --git a/include/rest_rpc/rpc_client.hpp b/include/rest_rpc/rpc_client.hpp index 15b8373..e3c1a08 100644 --- a/include/rest_rpc/rpc_client.hpp +++ b/include/rest_rpc/rpc_client.hpp @@ -226,8 +226,14 @@ private: result.ec = (rpc_errc)socket_->body_[0]; if constexpr (!std::is_void_v) { rpc_service::msgpack_codec codec; - result.value = codec.unpack(std::string_view( - socket_->body_.data() + 1, resp_header.body_len - 1)); + if constexpr (util::is_basic_v) { + result.value = codec.unpack(std::string_view( + socket_->body_.data() + 1, resp_header.body_len - 1)); + } else { + auto tp = codec.unpack>(std::string_view( + socket_->body_.data() + 1, resp_header.body_len - 1)); + result.value = std::move(std::get<0>(tp)); + } } if (resp_header.msg_type == 1) { // pubsub diff --git a/include/rest_rpc/rpc_connection.hpp b/include/rest_rpc/rpc_connection.hpp index e91bc7c..00f411c 100644 --- a/include/rest_rpc/rpc_connection.hpp +++ b/include/rest_rpc/rpc_connection.hpp @@ -63,7 +63,7 @@ public: asio::as_tuple(asio::use_awaitable)); if (ec) { if (ec != asio::error::eof) { - REST_LOG_INFO << "read http head error: " << ec.message(); + REST_LOG_INFO << "read head error: " << ec.message(); } else { REST_LOG_INFO << "read head error: " << ec.message(); } diff --git a/tests/test_rest_rpc.cpp b/tests/test_rest_rpc.cpp index 557c0ca..54b339d 100644 --- a/tests/test_rest_rpc.cpp +++ b/tests/test_rest_rpc.cpp @@ -8,6 +8,20 @@ using namespace rest_rpc; +struct person { + size_t id; + std::string name; + size_t age; + MSGPACK_DEFINE(id, name, age); +}; + +person get_person(person p) { return p; } + +person modify_person(person p, std::string name) { + p.name = name; + return p; +} + struct dummy { int add(int a, int b) { return a + b; } @@ -217,6 +231,15 @@ TEST_CASE("test rpc_connection") { tcp_socket socket(io_ctx); bool cross_ending_ = false; rpc_router router; + router.register_handler(); + rpc_service::msgpack_codec codec; + person p{1, "tom", 20}; + + auto buf = codec.pack_to_string(std::tuple(p)); + auto ret = sync_wait(get_global_executor(), + router.route(get_key(), buf)); + auto tp = + codec.unpack>(ret.data().data(), ret.data().size()); dummy d{}; router.register_handler<&dummy::add>(&d); auto conn = std::make_shared(std::move(socket), conn_id, @@ -244,6 +267,8 @@ TEST_CASE("test server start") { server.register_handler(); server.register_handler(); server.register_handler(); + server.register_handler(); + server.register_handler(); server.set_conn_max_age(std::chrono::seconds(5)); server.set_check_conn_interval(std::chrono::seconds(1)); auto ec = server.async_start(); @@ -278,6 +303,19 @@ TEST_CASE("test server start") { auto result = sync_wait(cl.get_executor(), cl.call_for(std::chrono::minutes(2), 1, 2)); CHECK(result.ec == rpc_errc::ok); + CHECK(result.value == 3); + } + { + person p{1, "tom", 20}; + auto result = sync_wait( + cl.get_executor(), cl.call_for(std::chrono::minutes(2), p)); + CHECK(result.ec == rpc_errc::ok); + + auto result1 = sync_wait( + cl.get_executor(), + cl.call_for(std::chrono::minutes(2), p, "jack")); + CHECK(result1.ec == rpc_errc::ok); + CHECK(result1.value.name == "jack"); } { // auto result = sync_wait(cl.get_executor(),