From 4f1ee9ca4eedb54ca4a36012bf449534eb1a6502 Mon Sep 17 00:00:00 2001 From: Dennis Hezel Date: Sun, 8 Dec 2024 17:02:58 +0100 Subject: [PATCH] test: Improve "Awaitable ServerRPC resumable read can be cancelled" --- test/src/test_server_rpc_17.cpp | 11 +---------- test/src/test_server_rpc_20.cpp | 15 ++++----------- test/utils/utils/asio_utils.hpp | 12 ++++++++++++ 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/test/src/test_server_rpc_17.cpp b/test/src/test_server_rpc_17.cpp index 1f821c67..d0b19208 100644 --- a/test/src/test_server_rpc_17.cpp +++ b/test/src/test_server_rpc_17.cpp @@ -556,15 +556,6 @@ TEST_CASE_TEMPLATE("ServerRPC resumable read can be cancelled", RPC, test::Clien { ServerRPCTest test{true}; agrpc::Waiter client_waiter; - const auto complete_client_waiter = [&] - { - client_waiter.initiate( - [&](auto&& e, auto&& token) - { - asio::post(e, token); - }, - test.grpc_context); - }; test.register_and_perform_requests( [&](RPC& rpc, const asio::yield_context& yield) { @@ -588,7 +579,7 @@ TEST_CASE_TEMPLATE("ServerRPC resumable read can be cancelled", RPC, test::Clien CHECK_EQ(asio::error::operation_aborted, ec); CHECK_EQ(1, request.integer()); } - complete_client_waiter(); + test::complete_immediately(test.grpc_context, client_waiter); CHECK_FALSE(waiter.wait(yield)); if constexpr (agrpc::ServerRPCType::BIDIRECTIONAL_STREAMING == RPC::TYPE) diff --git a/test/src/test_server_rpc_20.cpp b/test/src/test_server_rpc_20.cpp index cf574b9e..e3091c3e 100644 --- a/test/src/test_server_rpc_20.cpp +++ b/test/src/test_server_rpc_20.cpp @@ -486,7 +486,8 @@ TEST_CASE_FIXTURE(ServerRPCAwaitableTest, "Awaitable ServerRPC resumable read can be cancelled") { using RPC = test::BidirectionalStreamingServerRPC; - register_and_perform_three_requests( + agrpc::Waiter client_waiter; + register_and_perform_requests( [&](RPC& rpc) -> asio::awaitable { typename RPC::Request request; @@ -498,7 +499,6 @@ TEST_CASE_FIXTURE(ServerRPCAwaitableTest, CHECK_EQ(true, co_await waiter.wait(asio::use_awaitable)); CHECK_EQ(1, request.integer()); - const auto not_to_exceed = test::two_hundred_milliseconds_from_now(); waiter.initiate(agrpc::read, rpc, request); for (int i{}; i != 2; ++i) { @@ -507,10 +507,10 @@ TEST_CASE_FIXTURE(ServerRPCAwaitableTest, waiter.wait(test::ASIO_DEFERRED), asio::post(asio::bind_executor(grpc_context, test::ASIO_DEFERRED))) .async_wait(asio::experimental::wait_for_one(), asio::use_awaitable); - CHECK_LT(test::now(), not_to_exceed); CHECK_EQ(asio::error::operation_aborted, ec); CHECK_EQ(1, request.integer()); } + test::complete_immediately(grpc_context, client_waiter); CHECK_EQ(false, co_await waiter.wait(asio::use_awaitable)); CHECK(co_await rpc.finish(grpc::Status::OK, asio::use_awaitable)); }, @@ -520,15 +520,8 @@ TEST_CASE_FIXTURE(ServerRPCAwaitableTest, start_rpc(rpc, request, response, yield); request.set_integer(1); CHECK(rpc.write(request, yield)); - agrpc::Waiter waiter; - waiter.initiate(agrpc::read, rpc, response); - auto [completion_order, ec, read_ok, wait_ok, alarm] = - asio::experimental::make_parallel_group( - waiter.wait(test::ASIO_DEFERRED), - agrpc::Alarm(grpc_context).wait(test::five_hundred_milliseconds_from_now(), test::ASIO_DEFERRED)) - .async_wait(asio::experimental::wait_for_one(), yield); + client_waiter.wait(yield); CHECK_EQ(grpc::StatusCode::OK, rpc.finish(yield).error_code()); - waiter.wait(yield); }); } #endif diff --git a/test/utils/utils/asio_utils.hpp b/test/utils/utils/asio_utils.hpp index 509e585c..7af85151 100644 --- a/test/utils/utils/asio_utils.hpp +++ b/test/utils/utils/asio_utils.hpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -243,6 +244,17 @@ void post(agrpc::GrpcContext& grpc_context, const std::function& functio void post(const agrpc::GrpcExecutor& executor, const std::function& function); +template +void complete_immediately(agrpc::GrpcContext& grpc_context, agrpc::Waiter& waiter) +{ + waiter.initiate( + [&](auto&& context, auto&& token) + { + asio::post(context, token); + }, + grpc_context); +} + #ifdef AGRPC_TEST_ASIO_HAS_CO_AWAIT void co_spawn(agrpc::GrpcContext& grpc_context, const std::function()>& function);