mirror of
https://github.com/boostorg/redis.git
synced 2026-01-19 04:42:09 +00:00
Added API for generic_flat_response, replaced generic_response with new response type in tests
This commit is contained in:
committed by
Marcelo Zimbres
parent
019a080b65
commit
84fa39918f
@@ -20,6 +20,7 @@
|
|||||||
#include <charconv>
|
#include <charconv>
|
||||||
#include <deque>
|
#include <deque>
|
||||||
#include <forward_list>
|
#include <forward_list>
|
||||||
|
#include <iostream>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
@@ -140,17 +141,17 @@ void boost_redis_from_bulk(T& t, resp3::basic_node<String> const& node, system::
|
|||||||
//================================================
|
//================================================
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
auto prepare_done(T&) noexcept
|
inline auto prepare_done(T*) noexcept
|
||||||
{
|
{
|
||||||
return [] { };
|
return [] { };
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <>
|
||||||
auto prepare_done(generic_flat_response& resp) noexcept
|
inline auto prepare_done(generic_flat_response* resp) noexcept
|
||||||
{
|
{
|
||||||
return [resp]() mutable {
|
return [resp] {
|
||||||
if (resp.has_value()) {
|
if (resp->has_value()) {
|
||||||
resp.value().set_view();
|
resp->value().set_view();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,8 +63,12 @@ struct offset_string {
|
|||||||
std::string_view data;
|
std::string_view data;
|
||||||
std::size_t offset{};
|
std::size_t offset{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
|
|
||||||
|
operator std::string() const { return std::string{data}; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline std::ostream& operator<<(std::ostream& os, offset_string const& s) { return os << s.data; }
|
||||||
|
|
||||||
using offset_node = basic_node<offset_string>;
|
using offset_node = basic_node<offset_string>;
|
||||||
|
|
||||||
} // namespace boost::redis::resp3
|
} // namespace boost::redis::resp3
|
||||||
|
|||||||
@@ -49,6 +49,18 @@ public:
|
|||||||
view_.reserve(num_nodes);
|
view_.reserve(num_nodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void clear()
|
||||||
|
{
|
||||||
|
data_.clear();
|
||||||
|
view_.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t size() const noexcept { return view_.size(); }
|
||||||
|
bool empty() noexcept { return view_.empty(); }
|
||||||
|
|
||||||
|
resp3::offset_node& at(std::size_t index) { return view_.at(index); }
|
||||||
|
resp3::offset_node const& at(std::size_t index) const { return view_.at(index); }
|
||||||
|
|
||||||
std::vector<resp3::offset_node> const& view() const { return view_; }
|
std::vector<resp3::offset_node> const& view() const { return view_; }
|
||||||
std::vector<resp3::offset_node>& view() { return view_; }
|
std::vector<resp3::offset_node>& view() { return view_; }
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
#include <boost/test/included/unit_test.hpp>
|
#include <boost/test/included/unit_test.hpp>
|
||||||
|
|
||||||
using boost::redis::generic_response;
|
using boost::redis::generic_response;
|
||||||
|
using boost::redis::generic_flat_response;
|
||||||
using boost::redis::response;
|
using boost::redis::response;
|
||||||
using boost::redis::ignore;
|
using boost::redis::ignore;
|
||||||
using boost::redis::any_adapter;
|
using boost::redis::any_adapter;
|
||||||
@@ -24,10 +25,12 @@ BOOST_AUTO_TEST_CASE(any_adapter_response_types)
|
|||||||
response<int> r1;
|
response<int> r1;
|
||||||
response<int, std::string> r2;
|
response<int, std::string> r2;
|
||||||
generic_response r3;
|
generic_response r3;
|
||||||
|
generic_flat_response r4;
|
||||||
|
|
||||||
BOOST_CHECK_NO_THROW(any_adapter{r1});
|
BOOST_CHECK_NO_THROW(any_adapter{r1});
|
||||||
BOOST_CHECK_NO_THROW(any_adapter{r2});
|
BOOST_CHECK_NO_THROW(any_adapter{r2});
|
||||||
BOOST_CHECK_NO_THROW(any_adapter{r3});
|
BOOST_CHECK_NO_THROW(any_adapter{r3});
|
||||||
|
BOOST_CHECK_NO_THROW(any_adapter{r4});
|
||||||
BOOST_CHECK_NO_THROW(any_adapter{ignore});
|
BOOST_CHECK_NO_THROW(any_adapter{ignore});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -262,4 +262,4 @@ int main()
|
|||||||
test_flexible().run();
|
test_flexible().run();
|
||||||
|
|
||||||
return boost::report_errors();
|
return boost::report_errors();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
namespace net = boost::asio;
|
namespace net = boost::asio;
|
||||||
using boost::redis::config;
|
using boost::redis::config;
|
||||||
using boost::redis::connection;
|
using boost::redis::connection;
|
||||||
using boost::redis::generic_response;
|
using boost::redis::generic_flat_response;
|
||||||
using boost::redis::ignore;
|
using boost::redis::ignore;
|
||||||
using boost::redis::operation;
|
using boost::redis::operation;
|
||||||
using boost::redis::request;
|
using boost::redis::request;
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ using boost::redis::operation;
|
|||||||
using boost::redis::error;
|
using boost::redis::error;
|
||||||
using boost::redis::request;
|
using boost::redis::request;
|
||||||
using boost::redis::response;
|
using boost::redis::response;
|
||||||
using boost::redis::generic_response;
|
using boost::redis::generic_flat_response;
|
||||||
using boost::redis::ignore;
|
using boost::redis::ignore;
|
||||||
using boost::redis::ignore_t;
|
using boost::redis::ignore_t;
|
||||||
using boost::redis::logger;
|
using boost::redis::logger;
|
||||||
|
|||||||
95
test/test_conn_exec_cancel2.cpp
Normal file
95
test/test_conn_exec_cancel2.cpp
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
/* 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 <cstddef>
|
||||||
|
#define BOOST_TEST_MODULE conn_exec_cancel
|
||||||
|
#include <boost/test/included/unit_test.hpp>
|
||||||
|
|
||||||
|
#include "common.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#ifdef BOOST_ASIO_HAS_CO_AWAIT
|
||||||
|
|
||||||
|
// NOTE1: Sends hello separately. I have observed that if hello and
|
||||||
|
// blpop are sent toguether, Redis will send the response of hello
|
||||||
|
// right away, not waiting for blpop. That is why we have to send it
|
||||||
|
// separately.
|
||||||
|
|
||||||
|
namespace net = boost::asio;
|
||||||
|
using error_code = boost::system::error_code;
|
||||||
|
using boost::redis::operation;
|
||||||
|
using boost::redis::request;
|
||||||
|
using boost::redis::response;
|
||||||
|
using boost::redis::generic_flat_response;
|
||||||
|
using boost::redis::ignore;
|
||||||
|
using boost::redis::ignore_t;
|
||||||
|
using boost::redis::config;
|
||||||
|
using boost::redis::logger;
|
||||||
|
using boost::redis::connection;
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
auto async_ignore_explicit_cancel_of_req_written() -> net::awaitable<void>
|
||||||
|
{
|
||||||
|
auto ex = co_await net::this_coro::executor;
|
||||||
|
|
||||||
|
generic_flat_response gresp;
|
||||||
|
auto conn = std::make_shared<connection>(ex);
|
||||||
|
|
||||||
|
run(conn);
|
||||||
|
|
||||||
|
net::steady_timer st{ex};
|
||||||
|
st.expires_after(std::chrono::seconds{1});
|
||||||
|
|
||||||
|
// See NOTE1.
|
||||||
|
request req0;
|
||||||
|
req0.push("PING", "async_ignore_explicit_cancel_of_req_written");
|
||||||
|
co_await conn->async_exec(req0, gresp);
|
||||||
|
|
||||||
|
request req1;
|
||||||
|
req1.push("BLPOP", "any", 3);
|
||||||
|
|
||||||
|
bool seen = false;
|
||||||
|
conn->async_exec(req1, gresp, [&](error_code ec, std::size_t) {
|
||||||
|
// No error should occur since the cancellation should be ignored
|
||||||
|
std::cout << "async_exec (1): " << ec.message() << std::endl;
|
||||||
|
BOOST_TEST(ec == error_code());
|
||||||
|
seen = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Will complete while BLPOP is pending.
|
||||||
|
error_code ec;
|
||||||
|
co_await st.async_wait(net::redirect_error(ec));
|
||||||
|
conn->cancel(operation::exec);
|
||||||
|
|
||||||
|
BOOST_TEST(ec == error_code());
|
||||||
|
|
||||||
|
request req2;
|
||||||
|
req2.push("PING");
|
||||||
|
|
||||||
|
// Test whether the connection remains usable after a call to
|
||||||
|
// cancel(exec).
|
||||||
|
co_await conn->async_exec(req2, gresp, net::redirect_error(ec));
|
||||||
|
conn->cancel();
|
||||||
|
|
||||||
|
BOOST_TEST(ec == error_code());
|
||||||
|
BOOST_TEST(seen);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(test_ignore_explicit_cancel_of_req_written)
|
||||||
|
{
|
||||||
|
run_coroutine_test(async_ignore_explicit_cancel_of_req_written());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
#else
|
||||||
|
BOOST_AUTO_TEST_CASE(dummy) { }
|
||||||
|
#endif
|
||||||
@@ -21,7 +21,7 @@ using error_code = boost::system::error_code;
|
|||||||
using boost::redis::connection;
|
using boost::redis::connection;
|
||||||
using boost::redis::request;
|
using boost::redis::request;
|
||||||
using boost::redis::response;
|
using boost::redis::response;
|
||||||
using boost::redis::generic_response;
|
using boost::redis::generic_flat_response;
|
||||||
using boost::redis::ignore;
|
using boost::redis::ignore;
|
||||||
using boost::redis::ignore_t;
|
using boost::redis::ignore_t;
|
||||||
using boost::redis::error;
|
using boost::redis::error;
|
||||||
@@ -266,7 +266,7 @@ BOOST_AUTO_TEST_CASE(subscriber_wrong_syntax)
|
|||||||
|
|
||||||
conn->async_exec(req1, ignore, c1);
|
conn->async_exec(req1, ignore, c1);
|
||||||
|
|
||||||
generic_response gresp;
|
generic_flat_response gresp;
|
||||||
conn->set_receive_response(gresp);
|
conn->set_receive_response(gresp);
|
||||||
|
|
||||||
auto c3 = [&](error_code ec, std::size_t) {
|
auto c3 = [&](error_code ec, std::size_t) {
|
||||||
|
|||||||
Reference in New Issue
Block a user