mirror of
https://github.com/boostorg/redis.git
synced 2026-01-19 04:42:09 +00:00
Updates multiplexer to make requests complete with error_code() rather than error_code(0) Integration tests now use io_context::run_for to run with a timeout and avoid deadlocks Tests now use concrete lambdas where generic ones are not required Tests now use BOOST_TEST with operator== to print values on error Tests now use anonymous namespaces to detect dead code Adds run_coroutine_test and removed start from common.hpp Updates test_reconnect to perform relevant checks Refactors how test_issue_50 launches its coroutines Groups tests in CMake as unit or integration Updates Jamfile tests to contain all unit tests and only unit tests
130 lines
3.4 KiB
C++
130 lines
3.4 KiB
C++
/* Copyright (c) 2018-2022 Marcelo Zimbres Silva (mzimbres@gmail.com)
|
|
*
|
|
* Distributed under the Boost Software License, Version 1.0. (See
|
|
* accompanying file LICENSE.txt)
|
|
*/
|
|
|
|
#include <boost/redis/connection.hpp>
|
|
|
|
#include <boost/system/errc.hpp>
|
|
|
|
#include <cstddef>
|
|
#define BOOST_TEST_MODULE conn_exec_cancel
|
|
#include <boost/asio/detached.hpp>
|
|
#include <boost/test/included/unit_test.hpp>
|
|
|
|
#include "common.hpp"
|
|
|
|
#ifdef BOOST_ASIO_HAS_CO_AWAIT
|
|
#include <boost/asio/experimental/awaitable_operators.hpp>
|
|
|
|
// NOTE1: I have observed that if hello and
|
|
// blpop are sent together, Redis will send the response of hello
|
|
// right away, not waiting for blpop.
|
|
|
|
namespace net = boost::asio;
|
|
using error_code = boost::system::error_code;
|
|
using namespace net::experimental::awaitable_operators;
|
|
using boost::redis::operation;
|
|
using boost::redis::error;
|
|
using boost::redis::request;
|
|
using boost::redis::response;
|
|
using boost::redis::generic_response;
|
|
using boost::redis::ignore;
|
|
using boost::redis::ignore_t;
|
|
using boost::redis::logger;
|
|
using boost::redis::connection;
|
|
using namespace std::chrono_literals;
|
|
|
|
namespace {
|
|
|
|
auto implicit_cancel_of_req_written() -> net::awaitable<void>
|
|
{
|
|
auto ex = co_await net::this_coro::executor;
|
|
auto conn = std::make_shared<connection>(ex);
|
|
|
|
auto cfg = make_test_config();
|
|
cfg.health_check_interval = std::chrono::seconds::zero();
|
|
run(conn, cfg);
|
|
|
|
// See NOTE1.
|
|
request req0;
|
|
req0.push("PING");
|
|
co_await conn->async_exec(req0, ignore);
|
|
|
|
// Will be cancelled after it has been written but before the
|
|
// response arrives.
|
|
request req1;
|
|
req1.push("BLPOP", "any", 3);
|
|
|
|
net::steady_timer st{ex};
|
|
st.expires_after(std::chrono::seconds{1});
|
|
|
|
// Achieves implicit cancellation when the timer fires.
|
|
boost::system::error_code ec1, ec2;
|
|
co_await (conn->async_exec(req1, ignore, redir(ec1)) || st.async_wait(redir(ec2)));
|
|
|
|
conn->cancel();
|
|
|
|
// I have observed this produces terminal cancellation so it can't
|
|
// be ignored, an error is expected.
|
|
BOOST_TEST(ec1 == net::error::operation_aborted);
|
|
BOOST_TEST(ec2 == error_code());
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(test_ignore_implicit_cancel_of_req_written)
|
|
{
|
|
run_coroutine_test(implicit_cancel_of_req_written());
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(test_cancel_of_req_written_on_run_canceled)
|
|
{
|
|
net::io_context ioc;
|
|
auto conn = std::make_shared<connection>(ioc);
|
|
|
|
request req0;
|
|
req0.push("PING");
|
|
|
|
// Sends a request that will be blocked forever, so we can test
|
|
// canceling it while waiting for a response.
|
|
request req1;
|
|
req1.get_config().cancel_on_connection_lost = true;
|
|
req1.get_config().cancel_if_unresponded = true;
|
|
req1.push("BLPOP", "any", 0);
|
|
|
|
bool finished = false;
|
|
|
|
auto c1 = [&](error_code ec, std::size_t) {
|
|
BOOST_CHECK_EQUAL(ec, net::error::operation_aborted);
|
|
finished = true;
|
|
};
|
|
|
|
auto c0 = [&](error_code ec, std::size_t) {
|
|
BOOST_TEST(ec == error_code());
|
|
conn->async_exec(req1, ignore, c1);
|
|
};
|
|
|
|
conn->async_exec(req0, ignore, c0);
|
|
|
|
auto cfg = make_test_config();
|
|
cfg.health_check_interval = std::chrono::seconds{5};
|
|
run(conn);
|
|
|
|
net::steady_timer st{ioc};
|
|
st.expires_after(std::chrono::seconds{1});
|
|
st.async_wait([&](error_code ec) {
|
|
BOOST_TEST(ec == error_code());
|
|
conn->cancel(operation::run);
|
|
conn->cancel(operation::reconnection);
|
|
});
|
|
|
|
ioc.run_for(test_timeout);
|
|
BOOST_TEST(finished);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
#else
|
|
BOOST_AUTO_TEST_CASE(dummy) { }
|
|
#endif
|