// // 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 #include #include #include #include #include #include #include #include #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 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 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 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); } template void test_receive2() { // 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_receive2(asio::cancel_after(1ms, [&](error_code ec) { BOOST_TEST_EQ(ec, asio::error::operation_aborted); receive_finished = true; })); ioc.run_for(test_timeout); BOOST_TEST(receive_finished); } } // namespace int main() { test_run>(); test_run(); test_exec>(); test_exec(); test_receive>(); test_receive(); test_receive2>(); test_receive2(); return boost::report_errors(); }