From 41dd94afd2c818a070c0347ebed81f50e538b436 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Sun, 5 Oct 2025 18:41:13 +0800 Subject: [PATCH] add test --- tests/test_rest_rpc1.cpp | 41 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/tests/test_rest_rpc1.cpp b/tests/test_rest_rpc1.cpp index 7a90814..15d990d 100644 --- a/tests/test_rest_rpc1.cpp +++ b/tests/test_rest_rpc1.cpp @@ -40,8 +40,7 @@ int round1(int i) { return i; } std::string_view echo_sv(std::string_view str) { return str; } -template -asio::awaitable response(auto ctx) { +template asio::awaitable response(auto ctx) { auto ec = co_await ctx.template response_s("test"); if (ec) { REST_LOG_ERROR << "response error: " << ec.message(); @@ -68,12 +67,14 @@ std::string_view delay_response(std::string_view str) { // // } // auto executor = ctx.get_executor(); // // async_start(executor, response(std::move(ctx))); - // asio::co_spawn(executor, response(std::move(ctx)), asio::detached); + // asio::co_spawn(executor, response(std::move(ctx)), + // asio::detached); // }); // thd.detach(); auto executor = ctx.get_executor(); - asio::co_spawn(executor, response(std::move(ctx)), asio::detached); + asio::co_spawn(executor, response(std::move(ctx)), + asio::detached); // this return value is meaningless, because it will response later, the // return type is important for client, so just return an empty value here. @@ -287,6 +288,38 @@ TEST_CASE("test server start") { std::cout << ec.message() << "\n"; } +TEST_CASE("test pub sub") { + rpc_server server("127.0.0.1:9004"); + server.async_start(); + + rpc_client client{}; + sync_wait(get_global_executor(), client.connect("127.0.0.1:9004")); + + std::promise promise; + auto sub = [&]() -> asio::awaitable { + for (int i = 0; i < 5; i++) { + auto [ec, result] = co_await client.subscribe("topic1"); + REST_LOG_INFO << result; + CHECK(ec == rpc_errc::ok); + CHECK(result == "publish message"); + } + promise.set_value(); + }; + + asio::co_spawn(client.get_executor(), sub(), asio::detached); + std::this_thread::sleep_for(std::chrono::seconds(2)); + + auto pub = [&]() -> asio::awaitable { + for (int i = 0; i < 5; i++) { + co_await server.publish("topic1", "publish message"); + } + }; + + asio::co_spawn(client.get_executor(), pub(), asio::detached); + + promise.get_future().wait(); +} + // doctest comments // 'function' : must be 'attribute' - see issue #182 DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4007)