2
0
mirror of https://github.com/boostorg/redis.git synced 2026-01-19 04:42:09 +00:00

Adds support for asio::cancel_after (#324)

* Adds support for asio::cancel_after in connection::{async_exec, async_run}
* Adds cancel_after tests
* Adds an example on using asio::cancel_after
* Adds a discussion page on timeouts and the `cancel_if_unresponded` flag

close #226
This commit is contained in:
Anarthal (Rubén Pérez)
2025-10-06 18:11:25 +02:00
committed by GitHub
parent 0c159280ba
commit d3e335942f
9 changed files with 309 additions and 35 deletions

View File

@@ -64,6 +64,7 @@ make_test(test_issue_50)
make_test(test_conversions)
make_test(test_conn_tls)
make_test(test_unix_sockets)
make_test(test_conn_cancel_after)
# Coverage
set(

View File

@@ -0,0 +1,110 @@
//
// Copyright (c) 2025 Marcelo Zimbres Silva (mzimbres@gmail.com),
// Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/redis/connection.hpp>
#include <boost/redis/ignore.hpp>
#include <boost/redis/request.hpp>
#include <boost/redis/response.hpp>
#include <boost/asio/cancel_after.hpp>
#include <boost/asio/error.hpp>
#include <boost/asio/experimental/channel_error.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/core/lightweight_test.hpp>
#include "common.hpp"
using namespace std::chrono_literals;
namespace asio = boost::asio;
using boost::system::error_code;
using boost::redis::request;
using boost::redis::basic_connection;
using boost::redis::connection;
using boost::redis::ignore;
using boost::redis::generic_response;
namespace {
template <class Connection>
void test_run()
{
// Setup
asio::io_context ioc;
Connection conn{ioc};
bool run_finished = false;
// Call the function with a very short timeout
conn.async_run(make_test_config(), asio::cancel_after(1ms, [&](error_code ec) {
BOOST_TEST_EQ(ec, asio::error::operation_aborted);
run_finished = true;
}));
ioc.run_for(test_timeout);
BOOST_TEST(run_finished);
}
template <class Connection>
void test_exec()
{
// Setup
asio::io_context ioc;
Connection conn{ioc};
bool exec_finished = false;
request req;
req.push("PING", "cancel_after");
// Call the function with a very short timeout.
// The connection is not being run, so these can't succeed
conn.async_exec(req, ignore, asio::cancel_after(1ms, [&](error_code ec, std::size_t) {
BOOST_TEST_EQ(ec, asio::error::operation_aborted);
exec_finished = true;
}));
ioc.run_for(test_timeout);
BOOST_TEST(exec_finished);
}
template <class Connection>
void test_receive()
{
// Setup
asio::io_context ioc;
Connection conn{ioc};
bool receive_finished = false;
generic_response resp;
conn.set_receive_response(resp);
// Call the function with a very short timeout.
conn.async_receive(asio::cancel_after(1ms, [&](error_code ec, std::size_t) {
BOOST_TEST_EQ(ec, asio::experimental::channel_errc::channel_cancelled);
receive_finished = true;
}));
ioc.run_for(test_timeout);
BOOST_TEST(receive_finished);
}
} // namespace
int main()
{
test_run<basic_connection<asio::io_context::executor_type>>();
test_run<connection>();
test_exec<basic_connection<asio::io_context::executor_type>>();
test_exec<connection>();
test_receive<basic_connection<asio::io_context::executor_type>>();
test_receive<connection>();
return boost::report_errors();
}

View File

@@ -12,6 +12,7 @@
#include <boost/asio/bind_cancellation_slot.hpp>
#include <boost/asio/cancellation_signal.hpp>
#include <boost/asio/cancellation_type.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/core/lightweight_test.hpp>
#include "common.hpp"
@@ -27,13 +28,14 @@ using namespace boost::redis;
namespace {
// Terminal and partial cancellation work for async_run
template <class Connection>
void test_per_operation_cancellation(std::string_view name, net::cancellation_type_t cancel_type)
{
std::cerr << "Running test case: " << name << std::endl;
// Setup
net::io_context ioc;
connection conn{ioc};
Connection conn{ioc};
net::cancellation_signal sig;
request req;
@@ -66,8 +68,21 @@ void test_per_operation_cancellation(std::string_view name, net::cancellation_ty
int main()
{
test_per_operation_cancellation("terminal", net::cancellation_type_t::terminal);
test_per_operation_cancellation("partial", net::cancellation_type_t::partial);
using basic_connection_t = basic_connection<net::io_context::executor_type>;
test_per_operation_cancellation<basic_connection_t>(
"basic_connection, terminal",
net::cancellation_type_t::terminal);
test_per_operation_cancellation<basic_connection_t>(
"basic_connection, partial",
net::cancellation_type_t::partial);
test_per_operation_cancellation<connection>(
"connection, terminal",
net::cancellation_type_t::terminal);
test_per_operation_cancellation<connection>(
"connection, partial",
net::cancellation_type_t::partial);
return boost::report_errors();
}