mirror of
https://github.com/boostorg/redis.git
synced 2026-01-19 04:42:09 +00:00
Makes all objects in connection have a stable address (#285)
Moves read_buffer memory reservation to the connection's constructor Makes read_buffer memory reservation size be a power of 2
This commit is contained in:
committed by
GitHub
parent
20ab2c7e70
commit
88d8f3c0ca
@@ -55,6 +55,7 @@ make_test(test_conn_reconnect)
|
||||
make_test(test_conn_exec_cancel)
|
||||
make_test(test_conn_exec_cancel2)
|
||||
make_test(test_conn_echo_stress)
|
||||
make_test(test_conn_move)
|
||||
make_test(test_issue_50)
|
||||
make_test(test_issue_181)
|
||||
make_test(test_conversions)
|
||||
|
||||
@@ -28,10 +28,6 @@ using namespace boost::redis;
|
||||
|
||||
namespace {
|
||||
|
||||
// user tests
|
||||
// logging can be disabled
|
||||
// logging can be changed verbosity
|
||||
|
||||
template <class Conn>
|
||||
void run_with_invalid_config(net::io_context& ioc, Conn& conn)
|
||||
{
|
||||
|
||||
112
test/test_conn_move.cpp
Normal file
112
test/test_conn_move.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
//
|
||||
// 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/request.hpp>
|
||||
#include <boost/redis/response.hpp>
|
||||
|
||||
#include <boost/asio/bind_executor.hpp>
|
||||
#include <boost/asio/error.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/post.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
#include "common.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
|
||||
using boost::system::error_code;
|
||||
namespace net = boost::asio;
|
||||
using namespace boost::redis;
|
||||
|
||||
namespace {
|
||||
|
||||
// Move constructing a connection doesn't leave dangling pointers
|
||||
void test_conn_move_construct()
|
||||
{
|
||||
// Setup
|
||||
net::io_context ioc;
|
||||
connection conn_prev(ioc);
|
||||
connection conn(std::move(conn_prev));
|
||||
request req;
|
||||
req.push("PING", "something");
|
||||
response<std::string> res;
|
||||
|
||||
bool run_finished = false, exec_finished = false;
|
||||
|
||||
// Run the connection
|
||||
conn.async_run(make_test_config(), [&](error_code ec) {
|
||||
run_finished = true;
|
||||
BOOST_TEST_EQ(ec, net::error::operation_aborted);
|
||||
});
|
||||
|
||||
// Launch a PING
|
||||
conn.async_exec(req, res, [&](error_code ec, std::size_t) {
|
||||
exec_finished = true;
|
||||
BOOST_TEST_EQ(ec, error_code());
|
||||
conn.cancel();
|
||||
});
|
||||
|
||||
ioc.run_for(test_timeout);
|
||||
|
||||
// Check
|
||||
BOOST_TEST(run_finished);
|
||||
BOOST_TEST(exec_finished);
|
||||
BOOST_TEST_EQ(std::get<0>(res).value(), "something");
|
||||
}
|
||||
|
||||
// Moving a connection is safe even when it's running,
|
||||
// and it doesn't leave dangling pointers
|
||||
void test_conn_move_assign_while_running()
|
||||
{
|
||||
// Setup
|
||||
net::io_context ioc;
|
||||
connection conn(ioc);
|
||||
connection conn2(ioc); // will be assigned to
|
||||
request req;
|
||||
req.push("PING", "something");
|
||||
response<std::string> res;
|
||||
|
||||
bool run_finished = false, exec_finished = false;
|
||||
|
||||
// Run the connection
|
||||
conn.async_run(make_test_config(), [&](error_code ec) {
|
||||
run_finished = true;
|
||||
BOOST_TEST_EQ(ec, net::error::operation_aborted);
|
||||
});
|
||||
|
||||
// Launch a PING. When it finishes, conn will be moved-from, and conn2 will be valid
|
||||
conn.async_exec(req, res, [&](error_code ec, std::size_t) {
|
||||
exec_finished = true;
|
||||
BOOST_TEST_EQ(ec, error_code());
|
||||
conn2.cancel();
|
||||
});
|
||||
|
||||
// While the operations are running, perform a move
|
||||
net::post(net::bind_executor(ioc.get_executor(), [&] {
|
||||
conn2 = std::move(conn);
|
||||
}));
|
||||
|
||||
ioc.run_for(test_timeout);
|
||||
|
||||
// Check
|
||||
BOOST_TEST(run_finished);
|
||||
BOOST_TEST(exec_finished);
|
||||
BOOST_TEST_EQ(std::get<0>(res).value(), "something");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
test_conn_move_construct();
|
||||
test_conn_move_assign_while_running();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
Reference in New Issue
Block a user