1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
/* 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>
#define BOOST_TEST_MODULE conn_exec_error
#include <boost/test/included/unit_test.hpp>
#include "common.hpp"
#include <cstddef>
#include <iostream>
namespace net = boost::asio;
namespace redis = boost::redis;
namespace resp3 = redis::resp3;
using error_code = boost::system::error_code;
using boost::redis::connection;
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::error;
using boost::redis::operation;
using namespace std::chrono_literals;
namespace {
BOOST_AUTO_TEST_CASE(no_ignore_error)
{
request req;
// HELLO expects a number, by feeding a string we should get a simple error.
req.push("HELLO", "not-a-number");
net::io_context ioc;
auto conn = std::make_shared<connection>(ioc);
bool exec_finished = false;
conn->async_exec(req, ignore, [&](error_code ec, std::size_t) {
exec_finished = true;
BOOST_TEST(ec == error::resp3_simple_error);
conn->cancel(operation::run);
conn->cancel(operation::reconnection);
});
run(conn);
ioc.run_for(test_timeout);
BOOST_TEST(exec_finished);
}
BOOST_AUTO_TEST_CASE(has_diagnostic)
{
request req;
// HELLO expects a number, by feeding a string we should get a simple error.
req.push("HELLO", "not-a-number");
// The second command should be also executed. Notice PING does not
// require resp3.
req.push("PING", "Barra do Una");
net::io_context ioc;
auto conn = std::make_shared<connection>(ioc);
response<std::string, std::string> resp;
bool exec_finished = false;
conn->async_exec(req, resp, [&](error_code ec, std::size_t) {
exec_finished = true;
BOOST_TEST(ec == error_code());
// HELLO
BOOST_TEST(std::get<0>(resp).has_error());
BOOST_TEST(std::get<0>(resp).error().data_type == resp3::type::simple_error);
auto const diag = std::get<0>(resp).error().diagnostic;
BOOST_TEST(!std::empty(diag));
std::cout << "has_diagnostic: " << diag << std::endl;
// PING
BOOST_TEST(std::get<1>(resp).has_value());
BOOST_TEST(std::get<1>(resp).value() == "Barra do Una");
conn->cancel(operation::run);
conn->cancel(operation::reconnection);
});
run(conn);
ioc.run_for(test_timeout);
BOOST_TEST(exec_finished);
}
BOOST_AUTO_TEST_CASE(resp3_error_in_cmd_pipeline)
{
request req1;
req1.push("HELLO", "3");
req1.push("PING", "req1-msg1");
req1.push("PING", "req1-msg2", "extra arg"); // Error.
req1.push("PING", "req1-msg3"); // Should run ok.
response<ignore_t, std::string, std::string, std::string> resp1;
request req2;
req2.push("PING", "req2-msg1");
response<std::string> resp2;
net::io_context ioc;
auto conn = std::make_shared<connection>(ioc);
bool c2_called = false, c1_called = false;
auto c2 = [&](error_code ec, std::size_t) {
c2_called = true;
BOOST_TEST(ec == error_code());
BOOST_TEST(std::get<0>(resp2).has_value());
BOOST_TEST(std::get<0>(resp2).value() == "req2-msg1");
conn->cancel(operation::run);
conn->cancel(operation::reconnection);
};
auto c1 = [&](error_code ec, std::size_t) {
c1_called = true;
BOOST_TEST(ec == error_code());
BOOST_TEST(std::get<2>(resp1).has_error());
BOOST_TEST(std::get<2>(resp1).error().data_type == resp3::type::simple_error);
auto const diag = std::get<2>(resp1).error().diagnostic;
BOOST_TEST(!std::empty(diag));
std::cout << "resp3_error_in_cmd_pipeline: " << diag << std::endl;
BOOST_TEST(std::get<3>(resp1).has_value());
BOOST_TEST(std::get<3>(resp1).value() == "req1-msg3");
conn->async_exec(req2, resp2, c2);
};
conn->async_exec(req1, resp1, c1);
run(conn);
ioc.run_for(test_timeout);
BOOST_TEST(c1_called);
BOOST_TEST(c2_called);
}
BOOST_AUTO_TEST_CASE(error_in_transaction)
{
request req;
req.push("HELLO", 3);
req.push("MULTI");
req.push("PING");
req.push("PING", "msg2", "error"); // Error.
req.push("PING");
req.push("EXEC");
req.push("PING");
response<
ignore_t, // hello
ignore_t, // multi
ignore_t, // ping
ignore_t, // ping
ignore_t, // ping
response<std::string, std::string, std::string>, // exec
std::string // ping
>
resp;
net::io_context ioc;
auto conn = std::make_shared<connection>(ioc);
bool finished = false;
conn->async_exec(req, resp, [&](error_code ec, std::size_t) {
finished = true;
BOOST_TEST(ec == error_code());
BOOST_TEST(std::get<0>(resp).has_value());
BOOST_TEST(std::get<1>(resp).has_value());
BOOST_TEST(std::get<2>(resp).has_value());
BOOST_TEST(std::get<3>(resp).has_value());
BOOST_TEST(std::get<4>(resp).has_value());
BOOST_TEST(std::get<5>(resp).has_value());
// Test errors in the pipeline commands.
BOOST_TEST(std::get<0>(std::get<5>(resp).value()).has_value());
BOOST_TEST(std::get<0>(std::get<5>(resp).value()).value() == "PONG");
// The ping in the transaction that should be an error.
BOOST_TEST(std::get<1>(std::get<5>(resp).value()).has_error());
BOOST_TEST(
std::get<1>(std::get<5>(resp).value()).error().data_type == resp3::type::simple_error);
auto const diag = std::get<1>(std::get<5>(resp).value()).error().diagnostic;
BOOST_TEST(!std::empty(diag));
// The ping thereafter in the transaction should not be an error.
BOOST_TEST(std::get<2>(std::get<5>(resp).value()).has_value());
BOOST_TEST(std::get<2>(std::get<5>(resp).value()).value() == "PONG");
// The command right after the pipeline should be successful.
BOOST_TEST(std::get<6>(resp).has_value());
BOOST_TEST(std::get<6>(resp).value() == "PONG");
conn->cancel(operation::run);
conn->cancel(operation::reconnection);
});
run(conn);
ioc.run_for(test_timeout);
BOOST_TEST(finished);
}
// This test is important because a SUBSCRIBE command has no response
// on success, but does on error. for example when using a wrong
// syntax, the server will send a simple error response the client is
// not expecting.
//
// Sending the subscribe after the ping command below is just a
// convenience to avoid have it merged in a pipeline making things
// even more complex. For example, without a ping, we might get the
// sequence HELLO + SUBSCRIBE + PING where the hello and ping are
// automatically sent by the implementation. In this case, if the
// subscribe syntax is wrong, redis will send a response, which does
// not exist on success. That response will be interpreted as the
// response to the PING command that comes thereafter and won't be
// forwarded to the receive_op, resulting in a difficult to handle
// error.
BOOST_AUTO_TEST_CASE(subscriber_wrong_syntax)
{
request req1;
req1.push("PING");
request req2;
req2.push("SUBSCRIBE"); // Wrong command syntax.
net::io_context ioc;
auto conn = std::make_shared<connection>(ioc);
bool c1_called = false, c2_called = false, c3_called = false;
auto c2 = [&](error_code ec, std::size_t) {
c2_called = true;
std::cout << "async_exec: subscribe" << std::endl;
BOOST_TEST(ec == error_code());
};
auto c1 = [&](error_code ec, std::size_t) {
c1_called = true;
std::cout << "async_exec: hello" << std::endl;
BOOST_TEST(ec == error_code());
conn->async_exec(req2, ignore, c2);
};
conn->async_exec(req1, ignore, c1);
generic_response gresp;
conn->set_receive_response(gresp);
auto c3 = [&](error_code ec, std::size_t) {
c3_called = true;
std::cout << "async_receive" << std::endl;
BOOST_TEST(!ec);
BOOST_TEST(gresp.has_error());
BOOST_CHECK_EQUAL(gresp.error().data_type, resp3::type::simple_error);
BOOST_TEST(!std::empty(gresp.error().diagnostic));
std::cout << gresp.error().diagnostic << std::endl;
conn->cancel(operation::run);
conn->cancel(operation::reconnection);
};
conn->async_receive(c3);
run(conn);
ioc.run_for(test_timeout);
BOOST_TEST(c1_called);
BOOST_TEST(c2_called);
BOOST_TEST(c3_called);
}
BOOST_AUTO_TEST_CASE(issue_287_generic_response_error_then_success)
{
// Setup
auto cfg = make_test_config();
request req;
req.push("PING", "hello");
req.push("set", "mykey"); // This command has a missing argument and will cause an error
req.push("get", "mykey"); // This one is okay
generic_response resp;
// I/O objects
net::io_context ioc;
connection conn{ioc};
bool run_finished = false, exec_finished = false;
conn.async_run(cfg, [&](error_code ec) {
BOOST_TEST(ec == net::error::operation_aborted);
run_finished = true;
});
conn.async_exec(req, resp, [&](error_code ec, std::size_t) {
BOOST_TEST(ec == error_code());
exec_finished = true;
conn.cancel();
});
ioc.run_for(test_timeout);
BOOST_TEST(run_finished);
BOOST_TEST(exec_finished);
BOOST_TEST(resp.has_error());
BOOST_TEST(resp.error().diagnostic == "ERR wrong number of arguments for 'set' command");
}
} // namespace
|