mirror of
https://github.com/boostorg/redis.git
synced 2026-01-19 04:42:09 +00:00
committed by
GitHub
parent
6791e759f9
commit
682bec618d
@@ -4,55 +4,188 @@
|
||||
* accompanying file LICENSE.txt)
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#define BOOST_TEST_MODULE request
|
||||
#include <boost/redis/request.hpp>
|
||||
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
using boost::redis::request;
|
||||
|
||||
// TODO: Serialization.
|
||||
|
||||
BOOST_AUTO_TEST_CASE(single_arg_allocator)
|
||||
namespace {
|
||||
|
||||
void test_push_no_args()
|
||||
{
|
||||
request req1;
|
||||
req1.push("PING");
|
||||
BOOST_CHECK_EQUAL(req1.payload(), std::string{"*1\r\n$4\r\nPING\r\n"});
|
||||
BOOST_TEST_EQ(req1.payload(), "*1\r\n$4\r\nPING\r\n");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(arg_int)
|
||||
void test_push_int()
|
||||
{
|
||||
request req;
|
||||
req.push("PING", 42);
|
||||
BOOST_CHECK_EQUAL(req.payload(), std::string{"*2\r\n$4\r\nPING\r\n$2\r\n42\r\n"});
|
||||
BOOST_TEST_EQ(req.payload(), "*2\r\n$4\r\nPING\r\n$2\r\n42\r\n");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(multiple_args)
|
||||
void test_push_multiple_args()
|
||||
{
|
||||
char const* res = "*5\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n$2\r\nEX\r\n$1\r\n2\r\n";
|
||||
request req;
|
||||
req.push("SET", "key", "value", "EX", "2");
|
||||
BOOST_CHECK_EQUAL(req.payload(), std::string{res});
|
||||
BOOST_TEST_EQ(req.payload(), res);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(container_and_range)
|
||||
void test_push_range()
|
||||
{
|
||||
std::map<std::string, std::string> in{
|
||||
{"key1", "value1"},
|
||||
{"key2", "value2"}
|
||||
};
|
||||
|
||||
char const* res =
|
||||
constexpr std::string_view expected =
|
||||
"*6\r\n$4\r\nHSET\r\n$3\r\nkey\r\n$4\r\nkey1\r\n$6\r\nvalue1\r\n$4\r\nkey2\r\n$"
|
||||
"6\r\nvalue2\r\n";
|
||||
|
||||
request req1;
|
||||
req1.push_range("HSET", "key", in);
|
||||
BOOST_CHECK_EQUAL(req1.payload(), std::string{res});
|
||||
BOOST_TEST_EQ(req1.payload(), expected);
|
||||
|
||||
request req2;
|
||||
req2.push_range("HSET", "key", std::cbegin(in), std::cend(in));
|
||||
BOOST_CHECK_EQUAL(req2.payload(), std::string{res});
|
||||
BOOST_TEST_EQ(req2.payload(), expected);
|
||||
}
|
||||
|
||||
// Append
|
||||
void test_append()
|
||||
{
|
||||
request req1;
|
||||
req1.push("PING", "req1");
|
||||
|
||||
request req2;
|
||||
req2.push("GET", "mykey");
|
||||
req2.push("GET", "other");
|
||||
|
||||
req1.append(req2);
|
||||
|
||||
constexpr std::string_view expected =
|
||||
"*2\r\n$4\r\nPING\r\n$4\r\nreq1\r\n"
|
||||
"*2\r\n$3\r\nGET\r\n$5\r\nmykey\r\n"
|
||||
"*2\r\n$3\r\nGET\r\n$5\r\nother\r\n";
|
||||
BOOST_TEST_EQ(req1.payload(), expected);
|
||||
BOOST_TEST_EQ(req1.get_commands(), 3u);
|
||||
BOOST_TEST_EQ(req1.get_expected_responses(), 3u);
|
||||
}
|
||||
|
||||
// Commands without responses are handled correctly
|
||||
void test_append_no_response()
|
||||
{
|
||||
request req1;
|
||||
req1.push("PING", "req1");
|
||||
|
||||
request req2;
|
||||
req2.push("SUBSCRIBE", "mychannel");
|
||||
req2.push("GET", "other");
|
||||
|
||||
req1.append(req2);
|
||||
|
||||
constexpr std::string_view expected =
|
||||
"*2\r\n$4\r\nPING\r\n$4\r\nreq1\r\n"
|
||||
"*2\r\n$9\r\nSUBSCRIBE\r\n$9\r\nmychannel\r\n"
|
||||
"*2\r\n$3\r\nGET\r\n$5\r\nother\r\n";
|
||||
BOOST_TEST_EQ(req1.payload(), expected);
|
||||
BOOST_TEST_EQ(req1.get_commands(), 3u);
|
||||
BOOST_TEST_EQ(req1.get_expected_responses(), 2u);
|
||||
}
|
||||
|
||||
// Flags are not modified by append
|
||||
void test_append_flags()
|
||||
{
|
||||
request req1;
|
||||
req1.get_config().cancel_if_not_connected = false;
|
||||
req1.get_config().cancel_if_unresponded = false;
|
||||
req1.get_config().cancel_on_connection_lost = false;
|
||||
req1.push("PING", "req1");
|
||||
|
||||
request req2;
|
||||
req2.get_config().cancel_if_not_connected = true;
|
||||
req2.get_config().cancel_if_unresponded = true;
|
||||
req2.get_config().cancel_on_connection_lost = true;
|
||||
req2.push("GET", "other");
|
||||
|
||||
req1.append(req2);
|
||||
|
||||
constexpr std::string_view expected =
|
||||
"*2\r\n$4\r\nPING\r\n$4\r\nreq1\r\n"
|
||||
"*2\r\n$3\r\nGET\r\n$5\r\nother\r\n";
|
||||
BOOST_TEST_EQ(req1.payload(), expected);
|
||||
BOOST_TEST_NOT(req1.get_config().cancel_if_not_connected);
|
||||
BOOST_TEST_NOT(req1.get_config().cancel_if_unresponded);
|
||||
BOOST_TEST_NOT(req1.get_config().cancel_on_connection_lost);
|
||||
}
|
||||
|
||||
// Empty requests don't cause problems with append
|
||||
void test_append_target_empty()
|
||||
{
|
||||
request req1;
|
||||
|
||||
request req2;
|
||||
req2.push("GET", "other");
|
||||
|
||||
req1.append(req2);
|
||||
|
||||
constexpr std::string_view expected = "*2\r\n$3\r\nGET\r\n$5\r\nother\r\n";
|
||||
BOOST_TEST_EQ(req1.payload(), expected);
|
||||
BOOST_TEST_EQ(req1.get_commands(), 1u);
|
||||
BOOST_TEST_EQ(req1.get_expected_responses(), 1u);
|
||||
}
|
||||
|
||||
void test_append_source_empty()
|
||||
{
|
||||
request req1;
|
||||
req1.push("GET", "other");
|
||||
|
||||
request req2;
|
||||
|
||||
req1.append(req2);
|
||||
|
||||
constexpr std::string_view expected = "*2\r\n$3\r\nGET\r\n$5\r\nother\r\n";
|
||||
BOOST_TEST_EQ(req1.payload(), expected);
|
||||
BOOST_TEST_EQ(req1.get_commands(), 1u);
|
||||
BOOST_TEST_EQ(req1.get_expected_responses(), 1u);
|
||||
}
|
||||
|
||||
void test_append_both_empty()
|
||||
{
|
||||
request req1;
|
||||
request req2;
|
||||
|
||||
req1.append(req2);
|
||||
|
||||
BOOST_TEST_EQ(req1.payload(), "");
|
||||
BOOST_TEST_EQ(req1.get_commands(), 0u);
|
||||
BOOST_TEST_EQ(req1.get_expected_responses(), 0u);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
test_push_no_args();
|
||||
test_push_int();
|
||||
test_push_multiple_args();
|
||||
test_push_range();
|
||||
|
||||
test_append();
|
||||
test_append_no_response();
|
||||
test_append_flags();
|
||||
test_append_target_empty();
|
||||
test_append_source_empty();
|
||||
test_append_both_empty();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user