diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..f4b44b08 --- /dev/null +++ b/.clang-format @@ -0,0 +1,176 @@ +--- +Language: Cpp +ColumnLimit: 100 +IndentWidth: 3 +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: true + AfterClass: false + AfterControlStatement: false + AfterEnum: true + AfterFunction: true + AfterNamespace: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: true +AccessModifierOffset: -3 +BinPackArguments: false +BinPackParameters: false +AlignAfterOpenBracket: AlwaysBreak +PointerAlignment: Left +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^|' + Priority: -10 + SortPriority: 1 + - Regex: '^' + Priority: -8 + SortPriority: 3 + - Regex: "^<.*" + Priority: -6 + SortPriority: 5 + - Regex: ".*" + Priority: -5 + SortPriority: 4 +IndentCaseLabels: true +AllowShortCaseLabelsOnASingleLine: true +AllowAllArgumentsOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AlignArrayOfStructures: Left +DerivePointerAlignment: false +PenaltyBreakAssignment: 2000000 +PenaltyBreakBeforeFirstCallParameter: 0 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyBreakScopeResolution: 1000000 +PenaltyReturnTypeOnItsOwnLine: 200000000 + +# Defaults (based on Google) +AlignConsecutiveMacros: false +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Left +AlignOperands: true +AlignTrailingComments: true +AllowAllConstructorInitializersOnNextLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortFunctionsOnASingleLine: All +AllowShortLambdasOnASingleLine: Empty +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: ExceptShortType +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: Yes +BreakTemplateDeclarations: Leave +BreakBeforeBinaryOperators: None +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: true +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +CommentPragmas: '(^ IWYU pragma:)|(^\\copydoc )|(^ ?\\n)' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 0 +ContinuationIndentWidth: 3 +Cpp11BracedListStyle: true +DeriveLineEnding: true +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IncludeIsMainRegex: null +IncludeIsMainSourceRegex: "" +IndentGotoLabels: false +IndentPPDirectives: None +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: "" +MacroBlockEnd: "" +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Never +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +RawStringFormats: + - Language: Cpp + Delimiters: + - cc + - CC + - cpp + - Cpp + - CPP + - "c++" + - "C++" + CanonicalDelimiter: "" + BasedOnStyle: google + - Language: TextProto + Delimiters: + - pb + - PB + - proto + - PROTO + EnclosingFunctions: + - EqualsProto + - EquivToProto + - PARSE_PARTIAL_TEXT_PROTO + - PARSE_TEST_PROTO + - PARSE_TEXT_PROTO + - ParseTextOrDie + - ParseTextProtoOrDie + CanonicalDelimiter: "" + BasedOnStyle: google +ReflowComments: false +SortIncludes: true +SortUsingDeclarations: false +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyBlock: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: false +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpaceBeforeSquareBrackets: false +Standard: Auto +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseCRLF: false +UseTab: Never +AlignConsecutiveShortCaseStatements: + Enabled: true + AcrossEmptyLines: false + AcrossComments: true + AlignCaseColons: false +--- + diff --git a/benchmarks/cpp/asio/echo_server_client.cpp b/benchmarks/cpp/asio/echo_server_client.cpp index 7a5eca4b..5eb3796f 100644 --- a/benchmarks/cpp/asio/echo_server_client.cpp +++ b/benchmarks/cpp/asio/echo_server_client.cpp @@ -4,8 +4,9 @@ * accompanying file LICENSE.txt) */ -#include #include + +#include #if defined(BOOST_ASIO_HAS_CO_AWAIT) namespace net = boost::asio; @@ -62,6 +63,10 @@ int main(int argc, char* argv[]) std::cerr << e.what() << std::endl; } } -#else // defined(BOOST_ASIO_HAS_CO_AWAIT) -auto main() -> int {std::cout << "Requires coroutine support." << std::endl; return 1;} -#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) +#else // defined(BOOST_ASIO_HAS_CO_AWAIT) +auto main() -> int +{ + std::cout << "Requires coroutine support." << std::endl; + return 1; +} +#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) diff --git a/benchmarks/cpp/asio/echo_server_direct.cpp b/benchmarks/cpp/asio/echo_server_direct.cpp index e8289862..b06bffe3 100644 --- a/benchmarks/cpp/asio/echo_server_direct.cpp +++ b/benchmarks/cpp/asio/echo_server_direct.cpp @@ -8,9 +8,10 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // +#include + #include #include -#include #if defined(BOOST_ASIO_HAS_CO_AWAIT) namespace net = boost::asio; @@ -27,37 +28,41 @@ constexpr net::use_awaitable_t use_awaitable; awaitable_type echo(tcp_socket socket) { - try { - char data[1024]; - for (;;) { - std::size_t n = co_await socket.async_read_some(net::buffer(data), use_awaitable); - co_await async_write(socket, net::buffer(data, n), use_awaitable); - } - } catch (std::exception const&) { - //std::printf("echo Exception: %s\n", e.what()); - } + try { + char data[1024]; + for (;;) { + std::size_t n = co_await socket.async_read_some(net::buffer(data), use_awaitable); + co_await async_write(socket, net::buffer(data, n), use_awaitable); + } + } catch (std::exception const&) { + //std::printf("echo Exception: %s\n", e.what()); + } } awaitable_type listener() { - auto ex = co_await this_coro::executor; - tcp_acceptor acceptor(ex, {tcp::v4(), 55555}); - for (;;) { - tcp_socket socket = co_await acceptor.async_accept(use_awaitable); - co_spawn(ex, echo(std::move(socket)), detached); - } + auto ex = co_await this_coro::executor; + tcp_acceptor acceptor(ex, {tcp::v4(), 55555}); + for (;;) { + tcp_socket socket = co_await acceptor.async_accept(use_awaitable); + co_spawn(ex, echo(std::move(socket)), detached); + } } int main() { - try { - net::io_context io_context{BOOST_ASIO_CONCURRENCY_HINT_UNSAFE_IO}; - co_spawn(io_context, listener(), detached); - io_context.run(); - } catch (std::exception const& e) { - std::printf("Exception: %s\n", e.what()); - } + try { + net::io_context io_context{BOOST_ASIO_CONCURRENCY_HINT_UNSAFE_IO}; + co_spawn(io_context, listener(), detached); + io_context.run(); + } catch (std::exception const& e) { + std::printf("Exception: %s\n", e.what()); + } } -#else // defined(BOOST_ASIO_HAS_CO_AWAIT) -auto main() -> int {std::cout << "Requires coroutine support." << std::endl; return 1;} -#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) +#else // defined(BOOST_ASIO_HAS_CO_AWAIT) +auto main() -> int +{ + std::cout << "Requires coroutine support." << std::endl; + return 1; +} +#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) diff --git a/example/cpp17_intro.cpp b/example/cpp17_intro.cpp index d0a98b4e..dbfaff46 100644 --- a/example/cpp17_intro.cpp +++ b/example/cpp17_intro.cpp @@ -5,7 +5,9 @@ */ #include + #include + #include namespace asio = boost::asio; @@ -14,7 +16,7 @@ using boost::redis::request; using boost::redis::response; using boost::redis::config; -auto main(int argc, char * argv[]) -> int +auto main(int argc, char* argv[]) -> int { try { config cfg; @@ -47,4 +49,3 @@ auto main(int argc, char * argv[]) -> int return 1; } } - diff --git a/example/cpp17_intro_sync.cpp b/example/cpp17_intro_sync.cpp index 1ed9a7d6..88c3e618 100644 --- a/example/cpp17_intro_sync.cpp +++ b/example/cpp17_intro_sync.cpp @@ -6,15 +6,15 @@ #include "sync_connection.hpp" -#include #include +#include using boost::redis::sync_connection; using boost::redis::request; using boost::redis::response; using boost::redis::config; -auto main(int argc, char * argv[]) -> int +auto main(int argc, char* argv[]) -> int { try { config cfg; diff --git a/example/cpp20_chat_room.cpp b/example/cpp20_chat_room.cpp index bfb02e7f..d41a69d0 100644 --- a/example/cpp20_chat_room.cpp +++ b/example/cpp20_chat_room.cpp @@ -5,13 +5,15 @@ */ #include -#include + #include #include -#include #include -#include +#include +#include + #include +#include #if defined(BOOST_ASIO_HAS_CO_AWAIT) #if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) @@ -38,8 +40,7 @@ using namespace std::chrono_literals; // Chat over Redis pubsub. To test, run this program from multiple // terminals and type messages to stdin. -auto -receiver(std::shared_ptr conn) -> awaitable +auto receiver(std::shared_ptr conn) -> awaitable { request req; req.push("SUBSCRIBE", "channel"); @@ -48,7 +49,6 @@ receiver(std::shared_ptr conn) -> awaitable conn->set_receive_response(resp); while (conn->will_reconnect()) { - // Subscribe to channels. co_await conn->async_exec(req, ignore); @@ -56,19 +56,17 @@ receiver(std::shared_ptr conn) -> awaitable for (error_code ec;;) { co_await conn->async_receive(redirect_error(use_awaitable, ec)); if (ec) - break; // Connection lost, break so we can reconnect to channels. - std::cout - << resp.value().at(1).value - << " " << resp.value().at(2).value - << " " << resp.value().at(3).value - << std::endl; + break; // Connection lost, break so we can reconnect to channels. + std::cout << resp.value().at(1).value << " " << resp.value().at(2).value << " " + << resp.value().at(3).value << std::endl; resp.value().clear(); } } } // Publishes stdin messages to a Redis channel. -auto publisher(std::shared_ptr in, std::shared_ptr conn) -> awaitable +auto publisher(std::shared_ptr in, std::shared_ptr conn) + -> awaitable { for (std::string msg;;) { auto n = co_await async_read_until(*in, dynamic_buffer(msg, 1024), "\n"); @@ -96,11 +94,11 @@ auto co_main(config cfg) -> awaitable stream->cancel(); } -#else // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) +#else // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) auto co_main(config const&) -> awaitable { std::cout << "Requires support for posix streams." << std::endl; co_return; } -#endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) -#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) +#endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) +#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) diff --git a/example/cpp20_containers.cpp b/example/cpp20_containers.cpp index f68441f8..98aedce7 100644 --- a/example/cpp20_containers.cpp +++ b/example/cpp20_containers.cpp @@ -5,11 +5,13 @@ */ #include -#include + #include +#include + +#include #include #include -#include #if defined(BOOST_ASIO_HAS_CO_AWAIT) @@ -24,38 +26,41 @@ using boost::asio::awaitable; using boost::asio::detached; using boost::asio::consign; -template +template std::ostream& operator<<(std::ostream& os, std::optional const& opt) { - if (opt.has_value()) - std::cout << opt.value(); - else - std::cout << "null"; + if (opt.has_value()) + std::cout << opt.value(); + else + std::cout << "null"; - return os; + return os; } void print(std::map const& cont) { - for (auto const& e: cont) + for (auto const& e : cont) std::cout << e.first << ": " << e.second << "\n"; } template void print(std::vector const& cont) { - for (auto const& e: cont) std::cout << e << " "; + for (auto const& e : cont) + std::cout << e << " "; std::cout << "\n"; } // Stores the content of some STL containers in Redis. auto store(std::shared_ptr conn) -> awaitable { - std::vector vec - {1, 2, 3, 4, 5, 6}; + std::vector vec{1, 2, 3, 4, 5, 6}; - std::map map - {{"key1", "value1"}, {"key2", "value2"}, {"key3", "value3"}}; + std::map map{ + {"key1", "value1"}, + {"key2", "value2"}, + {"key3", "value3"} + }; request req; req.push_range("RPUSH", "rpush-key", vec); @@ -100,22 +105,22 @@ auto transaction(std::shared_ptr conn) -> awaitable { request req; req.push("MULTI"); - req.push("LRANGE", "rpush-key", 0, -1); // Retrieves - req.push("HGETALL", "hset-key"); // Retrieves + req.push("LRANGE", "rpush-key", 0, -1); // Retrieves + req.push("HGETALL", "hset-key"); // Retrieves req.push("MGET", "key", "non-existing-key"); req.push("EXEC"); response< - ignore_t, // multi - ignore_t, // lrange - ignore_t, // hgetall - ignore_t, // mget + ignore_t, // multi + ignore_t, // lrange + ignore_t, // hgetall + ignore_t, // mget response< std::optional>, std::optional>, - std::optional>> - > // exec - > resp; + std::optional>>> // exec + > + resp; co_await conn->async_exec(req, resp); @@ -137,4 +142,4 @@ awaitable co_main(config cfg) conn->cancel(); } -#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) +#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) diff --git a/example/cpp20_echo_server.cpp b/example/cpp20_echo_server.cpp index 698d4c84..ac3e0c80 100644 --- a/example/cpp20_echo_server.cpp +++ b/example/cpp20_echo_server.cpp @@ -5,10 +5,12 @@ */ #include -#include + +#include #include #include -#include +#include + #include #if defined(BOOST_ASIO_HAS_CO_AWAIT) @@ -22,10 +24,8 @@ using boost::system::error_code; using boost::redis::connection; using namespace std::chrono_literals; -auto -echo_server_session( - asio::ip::tcp::socket socket, - std::shared_ptr conn) -> asio::awaitable +auto echo_server_session(asio::ip::tcp::socket socket, std::shared_ptr conn) + -> asio::awaitable { request req; response resp; @@ -67,4 +67,4 @@ auto co_main(config cfg) -> asio::awaitable conn->cancel(); } -#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) +#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) diff --git a/example/cpp20_intro.cpp b/example/cpp20_intro.cpp index 0c0b7422..7389da2a 100644 --- a/example/cpp20_intro.cpp +++ b/example/cpp20_intro.cpp @@ -5,9 +5,11 @@ */ #include + #include -#include #include +#include + #include #if defined(BOOST_ASIO_HAS_CO_AWAIT) @@ -38,4 +40,4 @@ auto co_main(config cfg) -> asio::awaitable std::cout << "PING: " << std::get<0>(resp).value() << std::endl; } -#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) +#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) diff --git a/example/cpp20_intro_tls.cpp b/example/cpp20_intro_tls.cpp index 31e19fe3..4403d68d 100644 --- a/example/cpp20_intro_tls.cpp +++ b/example/cpp20_intro_tls.cpp @@ -5,9 +5,11 @@ */ #include -#include -#include + #include +#include +#include + #include #if defined(BOOST_ASIO_HAS_CO_AWAIT) @@ -50,4 +52,4 @@ auto co_main(config cfg) -> asio::awaitable std::cout << "Response: " << std::get<0>(resp).value() << std::endl; } -#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) +#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) diff --git a/example/cpp20_json.cpp b/example/cpp20_json.cpp index 7295d5db..c44b64a7 100644 --- a/example/cpp20_json.cpp +++ b/example/cpp20_json.cpp @@ -5,20 +5,23 @@ */ #include -#include -#include + #include +#include #include -#include +#include + #include +#include #if defined(BOOST_ASIO_HAS_CO_AWAIT) -#include +#include + #include +#include #include #include -#include namespace asio = boost::asio; namespace resp3 = boost::redis::resp3; @@ -30,7 +33,7 @@ using boost::redis::config; using boost::redis::connection; using boost::redis::resp3::node_view; -// Struct that will be stored in Redis using json serialization. +// Struct that will be stored in Redis using json serialization. struct user { std::string name; std::string age; @@ -46,11 +49,7 @@ void boost_redis_to_bulk(std::string& to, user const& u) resp3::boost_redis_to_bulk(to, boost::json::serialize(boost::json::value_from(u))); } -void -boost_redis_from_bulk( - user& u, - node_view const& node, - boost::system::error_code&) +void boost_redis_from_bulk(user& u, node_view const& node, boost::system::error_code&) { u = boost::json::value_to(boost::json::parse(node.value)); } @@ -66,8 +65,8 @@ auto co_main(config cfg) -> asio::awaitable // Stores and retrieves in the same request. request req; - req.push("SET", "json-key", u); // Stores in Redis. - req.push("GET", "json-key"); // Retrieves from Redis. + req.push("SET", "json-key", u); // Stores in Redis. + req.push("GET", "json-key"); // Retrieves from Redis. response resp; @@ -75,10 +74,9 @@ auto co_main(config cfg) -> asio::awaitable conn->cancel(); // Prints the first ping - std::cout - << "Name: " << std::get<1>(resp).value().name << "\n" - << "Age: " << std::get<1>(resp).value().age << "\n" - << "Country: " << std::get<1>(resp).value().country << "\n"; + std::cout << "Name: " << std::get<1>(resp).value().name << "\n" + << "Age: " << std::get<1>(resp).value().age << "\n" + << "Country: " << std::get<1>(resp).value().country << "\n"; } -#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) +#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) diff --git a/example/cpp20_protobuf.cpp b/example/cpp20_protobuf.cpp index 3d1f40eb..cdf4eb3b 100644 --- a/example/cpp20_protobuf.cpp +++ b/example/cpp20_protobuf.cpp @@ -6,10 +6,12 @@ #include #include + #include -#include #include +#include #include + #include // See the definition in person.proto. This header is automatically @@ -32,8 +34,7 @@ using boost::redis::resp3::node_view; using tutorial::person; // Boost.Redis customization points (example/protobuf.hpp) -namespace tutorial -{ +namespace tutorial { // Below I am using a Boost.Redis to indicate a protobuf error, this // is ok for an example, users however might want to define their own @@ -47,18 +48,14 @@ void boost_redis_to_bulk(std::string& to, person const& u) resp3::boost_redis_to_bulk(to, tmp); } -void -boost_redis_from_bulk( - person& u, - node_view const& node, - boost::system::error_code& ec) +void boost_redis_from_bulk(person& u, node_view const& node, boost::system::error_code& ec) { - std::string const tmp {node.value}; + std::string const tmp{node.value}; if (!u.ParseFromString(tmp)) ec = boost::redis::error::invalid_data_type; } -} // tutorial +} // namespace tutorial using tutorial::boost_redis_to_bulk; using tutorial::boost_redis_from_bulk; @@ -84,10 +81,9 @@ asio::awaitable co_main(config cfg) co_await conn->async_exec(req, resp); conn->cancel(); - std::cout - << "Name: " << std::get<1>(resp).value().name() << "\n" - << "Age: " << std::get<1>(resp).value().id() << "\n" - << "Email: " << std::get<1>(resp).value().email() << "\n"; + std::cout << "Name: " << std::get<1>(resp).value().name() << "\n" + << "Age: " << std::get<1>(resp).value().id() << "\n" + << "Email: " << std::get<1>(resp).value().email() << "\n"; } -#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) +#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) diff --git a/example/cpp20_resolve_with_sentinel.cpp b/example/cpp20_resolve_with_sentinel.cpp index cca77ded..d3073ab6 100644 --- a/example/cpp20_resolve_with_sentinel.cpp +++ b/example/cpp20_resolve_with_sentinel.cpp @@ -5,9 +5,11 @@ */ #include -#include -#include + #include +#include +#include + #include #if defined(BOOST_ASIO_HAS_CO_AWAIT) @@ -21,8 +23,7 @@ using boost::redis::config; using boost::redis::address; using boost::redis::connection; -auto redir(boost::system::error_code& ec) - { return asio::redirect_error(asio::use_awaitable, ec); } +auto redir(boost::system::error_code& ec) { return asio::redirect_error(asio::use_awaitable, ec); } // For more info see // - https://redis.io/docs/manual/sentinel. @@ -48,7 +49,9 @@ auto resolve_master_address(std::vector
const& addresses) -> asio::awai conn->cancel(); conn->reset_stream(); if (!ec && std::get<0>(resp)) - co_return address{std::get<0>(resp).value().value().at(0), std::get<0>(resp).value().value().at(1)}; + co_return address{ + std::get<0>(resp).value().value().at(0), + std::get<0>(resp).value().value().at(1)}; } co_return address{}; @@ -58,18 +61,17 @@ auto co_main(config cfg) -> asio::awaitable { // A list of sentinel addresses from which only one is responsive. // This simulates sentinels that are down. - std::vector
const addresses - { address{"foo", "26379"} - , address{"bar", "26379"} - , cfg.addr + std::vector
const addresses{ + address{"foo", "26379"}, + address{"bar", "26379"}, + cfg.addr }; auto const ep = co_await resolve_master_address(addresses); - std::clog - << "Host: " << ep.host << "\n" - << "Port: " << ep.port << "\n" - << std::flush; + std::clog << "Host: " << ep.host << "\n" + << "Port: " << ep.port << "\n" + << std::flush; } -#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) +#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) diff --git a/example/cpp20_streams.cpp b/example/cpp20_streams.cpp index 738f2ff8..785503fd 100644 --- a/example/cpp20_streams.cpp +++ b/example/cpp20_streams.cpp @@ -5,11 +5,13 @@ */ #include -#include -#include -#include -#include + #include +#include +#include +#include +#include + #include #if defined(BOOST_ASIO_HAS_CO_AWAIT) @@ -29,53 +31,51 @@ using net::signal_set; auto stream_reader(std::shared_ptr conn) -> net::awaitable { - std::string redisStreamKey_; - request req; - generic_response resp; + std::string redisStreamKey_; + request req; + generic_response resp; - std::string stream_id{"$"}; - std::string const field = "myfield"; + std::string stream_id{"$"}; + std::string const field = "myfield"; - for (;;) { - req.push("XREAD", "BLOCK", "0", "STREAMS", "test-topic", stream_id); - co_await conn->async_exec(req, resp); + for (;;) { + req.push("XREAD", "BLOCK", "0", "STREAMS", "test-topic", stream_id); + co_await conn->async_exec(req, resp); - //std::cout << "Response: "; - //for (auto i = 0UL; i < resp->size(); ++i) { - // std::cout << resp->at(i).value << ", "; - //} - //std::cout << std::endl; + //std::cout << "Response: "; + //for (auto i = 0UL; i < resp->size(); ++i) { + // std::cout << resp->at(i).value << ", "; + //} + //std::cout << std::endl; - // The following approach was taken in order to be able to - // deal with the responses, as generated by redis in the case - // that there are multiple stream 'records' within a single - // generic_response. The nesting and number of values in - // resp.value() are different, depending on the contents - // of the stream in redis. Uncomment the above commented-out - // code for examples while running the XADD command. + // The following approach was taken in order to be able to + // deal with the responses, as generated by redis in the case + // that there are multiple stream 'records' within a single + // generic_response. The nesting and number of values in + // resp.value() are different, depending on the contents + // of the stream in redis. Uncomment the above commented-out + // code for examples while running the XADD command. - std::size_t item_index = 0; - while (item_index < std::size(resp.value())) { - auto const& val = resp.value().at(item_index).value; + std::size_t item_index = 0; + while (item_index < std::size(resp.value())) { + auto const& val = resp.value().at(item_index).value; - if (field.compare(val) == 0) { - // We've hit a myfield field. - // The streamId is located at item_index - 2 - // The payload is located at item_index + 1 - stream_id = resp.value().at(item_index - 2).value; - std::cout - << "StreamId: " << stream_id << ", " - << "MyField: " << resp.value().at(item_index + 1).value - << std::endl; - ++item_index; // We can increase so we don't read this again - } + if (field.compare(val) == 0) { + // We've hit a myfield field. + // The streamId is located at item_index - 2 + // The payload is located at item_index + 1 + stream_id = resp.value().at(item_index - 2).value; + std::cout << "StreamId: " << stream_id << ", " + << "MyField: " << resp.value().at(item_index + 1).value << std::endl; + ++item_index; // We can increase so we don't read this again + } - ++item_index; - } + ++item_index; + } - req.clear(); - resp.value().clear(); - } + req.clear(); + resp.value().clear(); + } } // Run this in another terminal: @@ -94,4 +94,4 @@ auto co_main(config cfg) -> net::awaitable co_await sig_set.async_wait(); conn->cancel(); } -#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) +#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) diff --git a/example/cpp20_subscriber.cpp b/example/cpp20_subscriber.cpp index 6d9a7948..ab372e46 100644 --- a/example/cpp20_subscriber.cpp +++ b/example/cpp20_subscriber.cpp @@ -6,13 +6,15 @@ #include #include + #include -#include #include -#include #include +#include #include #include +#include + #include #if defined(BOOST_ASIO_HAS_CO_AWAIT) @@ -47,8 +49,7 @@ using asio::signal_set; */ // Receives server pushes. -auto -receiver(std::shared_ptr conn) -> asio::awaitable +auto receiver(std::shared_ptr conn) -> asio::awaitable { request req; req.push("SUBSCRIBE", "channel"); @@ -58,7 +59,6 @@ receiver(std::shared_ptr conn) -> asio::awaitable // Loop while reconnection is enabled while (conn->will_reconnect()) { - // Reconnect to the channels. co_await conn->async_exec(req, ignore); @@ -72,13 +72,10 @@ receiver(std::shared_ptr conn) -> asio::awaitable } if (ec) - break; // Connection lost, break so we can reconnect to channels. + break; // Connection lost, break so we can reconnect to channels. - std::cout - << resp.value().at(1).value - << " " << resp.value().at(2).value - << " " << resp.value().at(3).value - << std::endl; + std::cout << resp.value().at(1).value << " " << resp.value().at(2).value << " " + << resp.value().at(3).value << std::endl; consume_one(resp); } @@ -98,4 +95,4 @@ auto co_main(config cfg) -> asio::awaitable conn->cancel(); } -#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) +#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) diff --git a/example/main.cpp b/example/main.cpp index 0bef3ccc..43bcbc6b 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -4,11 +4,13 @@ * accompanying file LICENSE.txt) */ -#include #include +#include + #include -#include #include +#include + #include namespace asio = boost::asio; @@ -19,7 +21,7 @@ using boost::redis::logger; extern asio::awaitable co_main(config); -auto main(int argc, char * argv[]) -> int +auto main(int argc, char* argv[]) -> int { try { config cfg; @@ -42,7 +44,7 @@ auto main(int argc, char * argv[]) -> int } } -#else // defined(BOOST_ASIO_HAS_CO_AWAIT) +#else // defined(BOOST_ASIO_HAS_CO_AWAIT) auto main() -> int { @@ -50,4 +52,4 @@ auto main() -> int return 0; } -#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) +#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) diff --git a/example/sync_connection.hpp b/example/sync_connection.hpp index c568ca98..c2a832ed 100644 --- a/example/sync_connection.hpp +++ b/example/sync_connection.hpp @@ -7,16 +7,17 @@ #include #include + #include #include #include -#include + #include +#include using namespace std::chrono_literals; -namespace boost::redis -{ +namespace boost::redis { class sync_connection { public: @@ -25,10 +26,7 @@ public: , conn_{std::make_shared(ioc_)} { } - ~sync_connection() - { - thread_.join(); - } + ~sync_connection() { thread_.join(); } void run(config cfg) { @@ -42,16 +40,18 @@ public: void stop() { - asio::dispatch(ioc_, [this]() { conn_->cancel(); }); + asio::dispatch(ioc_, [this]() { + conn_->cancel(); + }); } template auto exec(request const& req, Response& resp) { - asio::dispatch( - conn_->get_executor(), - asio::deferred([this, &req, &resp]() { return conn_->async_exec(req, resp, asio::deferred); })) - (asio::use_future).get(); + asio::dispatch(conn_->get_executor(), asio::deferred([this, &req, &resp]() { + return conn_->async_exec(req, resp, asio::deferred); + }))(asio::use_future) + .get(); } private: @@ -60,4 +60,4 @@ private: std::thread thread_; }; -} +} // namespace boost::redis diff --git a/include/boost/redis.hpp b/include/boost/redis.hpp index 4017bf52..346b6f34 100644 --- a/include/boost/redis.hpp +++ b/include/boost/redis.hpp @@ -8,12 +8,12 @@ #define BOOST_REDIS_HPP #include -#include #include -#include -#include +#include #include #include +#include +#include /** @defgroup high-level-api Reference * @@ -25,4 +25,4 @@ * This page contains the documentation of the Aedis low-level API. */ -#endif // BOOST_REDIS_HPP +#endif // BOOST_REDIS_HPP diff --git a/include/boost/redis/adapter/adapt.hpp b/include/boost/redis/adapter/adapt.hpp index 7cd625f0..5d954513 100644 --- a/include/boost/redis/adapter/adapt.hpp +++ b/include/boost/redis/adapter/adapt.hpp @@ -7,20 +7,20 @@ #ifndef BOOST_REDIS_ADAPTER_ADAPT_HPP #define BOOST_REDIS_ADAPTER_ADAPT_HPP +#include +#include #include #include -#include -#include + #include #include -#include #include #include +#include #include -namespace boost::redis::adapter -{ +namespace boost::redis::adapter { /** @brief Adapts a type to be used as a response. * @@ -34,7 +34,7 @@ namespace boost::redis::adapter * * @param t Tuple containing the responses. */ -template +template auto boost_redis_adapt(T& t) noexcept { return detail::response_traits::adapt(t); @@ -71,10 +71,12 @@ auto boost_redis_adapt(T& t) noexcept * co_await resp3::async_read(socket, dynamic_buffer(buffer), adapt(execs)); * @endcode */ -template +template auto adapt2(T& t = redis::ignore) noexcept - { return detail::result_traits::adapt(t); } +{ + return detail::result_traits::adapt(t); +} -} // boost::redis::adapter +} // namespace boost::redis::adapter -#endif // BOOST_REDIS_ADAPTER_ADAPT_HPP +#endif // BOOST_REDIS_ADAPTER_ADAPT_HPP diff --git a/include/boost/redis/adapter/any_adapter.hpp b/include/boost/redis/adapter/any_adapter.hpp index 82b7e70f..b35b4895 100644 --- a/include/boost/redis/adapter/any_adapter.hpp +++ b/include/boost/redis/adapter/any_adapter.hpp @@ -7,10 +7,11 @@ #ifndef BOOST_REDIS_ANY_ADAPTER_HPP #define BOOST_REDIS_ANY_ADAPTER_HPP - -#include #include +#include + #include + #include #include #include @@ -33,26 +34,26 @@ namespace boost::redis { */ class any_adapter { public: - using fn_type = std::function; + using fn_type = std::function; - struct impl_t { - fn_type adapt_fn; - std::size_t supported_response_size; - } impl_; + struct impl_t { + fn_type adapt_fn; + std::size_t supported_response_size; + } impl_; - template - static auto create_impl(T& resp) -> impl_t - { - using namespace boost::redis::adapter; - auto adapter = boost_redis_adapt(resp); - std::size_t size = adapter.get_supported_response_size(); - return { std::move(adapter), size }; - } + template + static auto create_impl(T& resp) -> impl_t + { + using namespace boost::redis::adapter; + auto adapter = boost_redis_adapt(resp); + std::size_t size = adapter.get_supported_response_size(); + return {std::move(adapter), size}; + } - template - friend class basic_connection; + template + friend class basic_connection; - /** + /** * @brief Constructor. * * Creates a type-erased response adapter from `resp` by calling @@ -62,10 +63,12 @@ public: * This object stores a reference to `resp`, which must be kept alive * while `*this` is being used. */ - template >> - explicit any_adapter(T& resp) : impl_(create_impl(resp)) {} + template >> + explicit any_adapter(T& resp) + : impl_(create_impl(resp)) + { } }; -} +} // namespace boost::redis #endif diff --git a/include/boost/redis/adapter/detail/adapters.hpp b/include/boost/redis/adapter/detail/adapters.hpp index f4760ecb..c67191c6 100644 --- a/include/boost/redis/adapter/detail/adapters.hpp +++ b/include/boost/redis/adapter/detail/adapters.hpp @@ -7,27 +7,28 @@ #ifndef BOOST_REDIS_ADAPTER_ADAPTERS_HPP #define BOOST_REDIS_ADAPTER_ADAPTERS_HPP -#include -#include -#include -#include #include +#include +#include +#include +#include + #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include -#include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // See https://stackoverflow.com/a/31658120/1077832 #ifdef _LIBCPP_VERSION @@ -35,60 +36,46 @@ #include #endif -namespace boost::redis::adapter::detail -{ +namespace boost::redis::adapter::detail { // Exclude bools, char and charXY_t types -template struct is_integral_number : std::is_integral {}; -template <> struct is_integral_number : std::false_type {}; -template <> struct is_integral_number : std::false_type {}; -template <> struct is_integral_number : std::false_type {}; -template <> struct is_integral_number : std::false_type {}; -template <> struct is_integral_number : std::false_type {}; +template struct is_integral_number : std::is_integral { }; +template <> struct is_integral_number : std::false_type { }; +template <> struct is_integral_number : std::false_type { }; +template <> struct is_integral_number : std::false_type { }; +template <> struct is_integral_number : std::false_type { }; +template <> struct is_integral_number : std::false_type { }; #ifdef __cpp_char8_t -template <> struct is_integral_number : std::false_type {}; +template <> struct is_integral_number : std::false_type { }; #endif -template::value> +template ::value> struct converter; -template +template struct converter { template - static void - apply( - T& i, - resp3::basic_node const& node, - system::error_code& ec) + static void apply(T& i, resp3::basic_node const& node, system::error_code& ec) { - auto const res = - std::from_chars(node.value.data(), node.value.data() + node.value.size(), i); + auto const res = std::from_chars(node.value.data(), node.value.data() + node.value.size(), i); if (res.ec != std::errc()) ec = redis::error::not_a_number; } }; -template<> +template <> struct converter { template - static void - apply( - bool& t, - resp3::basic_node const& node, - system::error_code& ec) + static void apply(bool& t, resp3::basic_node const& node, system::error_code& ec) { t = *node.value.data() == 't'; } }; -template<> +template <> struct converter { template - static void - apply( - double& d, - resp3::basic_node const& node, - system::error_code& ec) + static void apply(double& d, resp3::basic_node const& node, system::error_code& ec) { #ifdef _LIBCPP_VERSION // The string in node.value is not null terminated and we also @@ -103,15 +90,14 @@ struct converter { auto const res = std::from_chars(node.value.data(), node.value.data() + node.value.size(), d); if (res.ec != std::errc()) ec = redis::error::not_a_double; -#endif // _LIBCPP_VERSION +#endif // _LIBCPP_VERSION } }; template struct converter, false> { template - static void - apply( + static void apply( std::basic_string& s, resp3::basic_node const& node, system::error_code&) @@ -123,11 +109,7 @@ struct converter, false> { template struct from_bulk_impl { template - static void - apply( - T& t, - resp3::basic_node const& node, - system::error_code& ec) + static void apply(T& t, resp3::basic_node const& node, system::error_code& ec) { converter::apply(t, node, ec); } @@ -136,8 +118,7 @@ struct from_bulk_impl { template struct from_bulk_impl> { template - static void - apply( + static void apply( std::optional& op, resp3::basic_node const& node, system::error_code& ec) @@ -150,11 +131,7 @@ struct from_bulk_impl> { }; template -void -boost_redis_from_bulk( - T& t, - resp3::basic_node const& node, - system::error_code& ec) +void boost_redis_from_bulk(T& t, resp3::basic_node const& node, system::error_code& ec) { from_bulk_impl::apply(t, node, ec); } @@ -167,7 +144,9 @@ private: Result* result_; public: - explicit general_aggregate(Result* c = nullptr): result_(c) {} + explicit general_aggregate(Result* c = nullptr) + : result_(c) + { } template void operator()(resp3::basic_node const& nd, system::error_code&) { @@ -175,10 +154,18 @@ public: switch (nd.data_type) { case resp3::type::blob_error: case resp3::type::simple_error: - *result_ = error{nd.data_type, std::string{std::cbegin(nd.value), std::cend(nd.value)}}; + *result_ = error{ + nd.data_type, + std::string{std::cbegin(nd.value), std::cend(nd.value)} + }; break; default: - result_->value().push_back({nd.data_type, nd.aggregate_size, nd.depth, std::string{std::cbegin(nd.value), std::cend(nd.value)}}); + result_->value().push_back({ + nd.data_type, + nd.aggregate_size, + nd.depth, + std::string{std::cbegin(nd.value), std::cend(nd.value)} + }); } } }; @@ -189,7 +176,9 @@ private: Node* result_; public: - explicit general_simple(Node* t = nullptr) : result_(t) {} + explicit general_simple(Node* t = nullptr) + : result_(t) + { } template void operator()(resp3::basic_node const& nd, system::error_code&) @@ -198,7 +187,10 @@ public: switch (nd.data_type) { case resp3::type::blob_error: case resp3::type::simple_error: - *result_ = error{nd.data_type, std::string{std::cbegin(nd.value), std::cend(nd.value)}}; + *result_ = error{ + nd.data_type, + std::string{std::cbegin(nd.value), std::cend(nd.value)} + }; break; default: result_->value().data_type = nd.data_type; @@ -212,7 +204,7 @@ public: template class simple_impl { public: - void on_value_available(Result&) {} + void on_value_available(Result&) { } template void operator()(Result& result, resp3::basic_node const& node, system::error_code& ec) @@ -232,8 +224,7 @@ private: typename Result::iterator hint_; public: - void on_value_available(Result& result) - { hint_ = std::end(result); } + void on_value_available(Result& result) { hint_ = std::end(result); } template void operator()(Result& result, resp3::basic_node const& nd, system::error_code& ec) @@ -247,8 +238,8 @@ public: BOOST_ASSERT(nd.aggregate_size == 1); if (nd.depth < 1) { - ec = redis::error::expects_resp3_set; - return; + ec = redis::error::expects_resp3_set; + return; } typename Result::key_type obj; @@ -264,23 +255,22 @@ private: bool on_key_ = true; public: - void on_value_available(Result& result) - { current_ = std::end(result); } + void on_value_available(Result& result) { current_ = std::end(result); } template void operator()(Result& result, resp3::basic_node const& nd, system::error_code& ec) { if (is_aggregate(nd.data_type)) { if (element_multiplicity(nd.data_type) != 2) - ec = redis::error::expects_resp3_map; + ec = redis::error::expects_resp3_map; return; } BOOST_ASSERT(nd.aggregate_size == 1); if (nd.depth < 1) { - ec = redis::error::expects_resp3_map; - return; + ec = redis::error::expects_resp3_map; + return; } if (on_key_) { @@ -300,7 +290,7 @@ public: template class vector_impl { public: - void on_value_available(Result& ) { } + void on_value_available(Result&) { } template void operator()(Result& result, resp3::basic_node const& nd, system::error_code& ec) @@ -321,13 +311,13 @@ private: int i_ = -1; public: - void on_value_available(Result& ) { } + void on_value_available(Result&) { } template void operator()(Result& result, resp3::basic_node const& nd, system::error_code& ec) { if (is_aggregate(nd.data_type)) { - if (i_ != -1) { + if (i_ != -1) { ec = redis::error::nested_aggregate_not_supported; return; } @@ -352,21 +342,20 @@ public: template struct list_impl { - - void on_value_available(Result& ) { } + void on_value_available(Result&) { } template void operator()(Result& result, resp3::basic_node const& nd, system::error_code& ec) { if (!is_aggregate(nd.data_type)) { - BOOST_ASSERT(nd.aggregate_size == 1); - if (nd.depth < 1) { - ec = redis::error::expects_resp3_aggregate; - return; - } + BOOST_ASSERT(nd.aggregate_size == 1); + if (nd.depth < 1) { + ec = redis::error::expects_resp3_aggregate; + return; + } - result.push_back({}); - boost_redis_from_bulk(result.back(), nd, ec); + result.push_back({}); + boost_redis_from_bulk(result.back(), nd, ec); } } }; @@ -374,43 +363,69 @@ struct list_impl { //--------------------------------------------------- template -struct impl_map { using type = simple_impl; }; +struct impl_map { + using type = simple_impl; +}; template -struct impl_map> { using type = set_impl>; }; +struct impl_map> { + using type = set_impl>; +}; template -struct impl_map> { using type = set_impl>; }; +struct impl_map> { + using type = set_impl>; +}; template -struct impl_map> { using type = set_impl>; }; +struct impl_map> { + using type = set_impl>; +}; template -struct impl_map> { using type = set_impl>; }; +struct impl_map> { + using type = set_impl>; +}; template -struct impl_map> { using type = map_impl>; }; +struct impl_map> { + using type = map_impl>; +}; template -struct impl_map> { using type = map_impl>; }; +struct impl_map> { + using type = map_impl>; +}; template -struct impl_map> { using type = map_impl>; }; +struct impl_map> { + using type = map_impl>; +}; template -struct impl_map> { using type = map_impl>; }; +struct impl_map> { + using type = map_impl>; +}; template -struct impl_map> { using type = vector_impl>; }; +struct impl_map> { + using type = vector_impl>; +}; template -struct impl_map> { using type = array_impl>; }; +struct impl_map> { + using type = array_impl>; +}; template -struct impl_map> { using type = list_impl>; }; +struct impl_map> { + using type = list_impl>; +}; template -struct impl_map> { using type = list_impl>; }; +struct impl_map> { + using type = list_impl>; +}; //--------------------------------------------------- @@ -421,6 +436,7 @@ template class wrapper> { public: using response_type = result; + private: response_type* result_; typename impl_map::type impl_; @@ -433,15 +449,18 @@ private: case resp3::type::null: case resp3::type::simple_error: case resp3::type::blob_error: - *result_ = error{nd.data_type, {std::cbegin(nd.value), std::cend(nd.value)}}; + *result_ = error{ + nd.data_type, + {std::cbegin(nd.value), std::cend(nd.value)} + }; return true; - default: - return false; + default: return false; } } public: - explicit wrapper(response_type* t = nullptr) : result_(t) + explicit wrapper(response_type* t = nullptr) + : result_(t) { if (result_) { result_->value() = T{}; @@ -481,21 +500,22 @@ private: switch (nd.data_type) { case resp3::type::blob_error: case resp3::type::simple_error: - *result_ = error{nd.data_type, {std::cbegin(nd.value), std::cend(nd.value)}}; + *result_ = error{ + nd.data_type, + {std::cbegin(nd.value), std::cend(nd.value)} + }; return true; - default: - return false; + default: return false; } } public: - explicit wrapper(response_type* o = nullptr) : result_(o) {} + explicit wrapper(response_type* o = nullptr) + : result_(o) + { } template - void - operator()( - resp3::basic_node const& nd, - system::error_code& ec) + void operator()(resp3::basic_node const& nd, system::error_code& ec) { BOOST_ASSERT_MSG(!!result_, "Unexpected null pointer"); @@ -509,14 +529,14 @@ public: return; if (!result_->value().has_value()) { - result_->value() = T{}; - impl_.on_value_available(result_->value().value()); + result_->value() = T{}; + impl_.on_value_available(result_->value().value()); } impl_(result_->value().value(), nd, ec); } }; -} // boost::redis::adapter::detail +} // namespace boost::redis::adapter::detail -#endif // BOOST_REDIS_ADAPTER_ADAPTERS_HPP +#endif // BOOST_REDIS_ADAPTER_ADAPTERS_HPP diff --git a/include/boost/redis/adapter/detail/response_traits.hpp b/include/boost/redis/adapter/detail/response_traits.hpp index 67b0a5ca..66684a70 100644 --- a/include/boost/redis/adapter/detail/response_traits.hpp +++ b/include/boost/redis/adapter/detail/response_traits.hpp @@ -7,19 +7,19 @@ #ifndef BOOST_REDIS_ADAPTER_DETAIL_RESPONSE_TRAITS_HPP #define BOOST_REDIS_ADAPTER_DETAIL_RESPONSE_TRAITS_HPP +#include #include #include -#include + #include #include -#include #include #include +#include #include -namespace boost::redis::adapter::detail -{ +namespace boost::redis::adapter::detail { class ignore_adapter { public: @@ -28,15 +28,17 @@ public: { switch (nd.data_type) { case resp3::type::simple_error: ec = redis::error::resp3_simple_error; break; - case resp3::type::blob_error: ec = redis::error::resp3_blob_error; break; - case resp3::type::null: ec = redis::error::resp3_null; break; - default:; + case resp3::type::blob_error: ec = redis::error::resp3_blob_error; break; + case resp3::type::null: ec = redis::error::resp3_null; break; + default: ; } } [[nodiscard]] auto get_supported_response_size() const noexcept - { return static_cast(-1);} + { + return static_cast(-1); + } }; template @@ -50,14 +52,13 @@ private: adapters_array_type adapters_; public: - explicit static_adapter(Response& r) - { - assigner::assign(adapters_, r); - } + explicit static_adapter(Response& r) { assigner::assign(adapters_, r); } [[nodiscard]] auto get_supported_response_size() const noexcept - { return size;} + { + return size; + } template void operator()(std::size_t i, resp3::basic_node const& nd, system::error_code& ec) @@ -65,7 +66,11 @@ public: using std::visit; // I am usure whether this should be an error or an assertion. BOOST_ASSERT(i < adapters_.size()); - visit([&](auto& arg){arg(nd, ec);}, adapters_.at(i)); + visit( + [&](auto& arg) { + arg(nd, ec); + }, + adapters_.at(i)); } }; @@ -81,9 +86,10 @@ public: { } [[nodiscard]] - auto - get_supported_response_size() const noexcept - { return static_cast(-1);} + auto get_supported_response_size() const noexcept + { + return static_cast(-1); + } template void operator()(std::size_t, resp3::basic_node const& nd, system::error_code& ec) @@ -100,8 +106,7 @@ struct response_traits { using response_type = ignore_t; using adapter_type = detail::ignore_adapter; - static auto adapt(response_type&) noexcept - { return detail::ignore_adapter{}; } + static auto adapt(response_type&) noexcept { return detail::ignore_adapter{}; } }; template <> @@ -109,8 +114,7 @@ struct response_traits> { using response_type = result; using adapter_type = detail::ignore_adapter; - static auto adapt(response_type&) noexcept - { return detail::ignore_adapter{}; } + static auto adapt(response_type&) noexcept { return detail::ignore_adapter{}; } }; template @@ -118,31 +122,35 @@ struct response_traits, Allocator>> using response_type = result, Allocator>>; using adapter_type = vector_adapter; - static auto adapt(response_type& v) noexcept - { return adapter_type{v}; } + static auto adapt(response_type& v) noexcept { return adapter_type{v}; } }; -template +template struct response_traits> { using response_type = response; using adapter_type = static_adapter; - static auto adapt(response_type& r) noexcept - { return adapter_type{r}; } + static auto adapt(response_type& r) noexcept { return adapter_type{r}; } }; template class wrapper { public: - explicit wrapper(Adapter adapter) : adapter_{adapter} {} + explicit wrapper(Adapter adapter) + : adapter_{adapter} + { } template void operator()(resp3::basic_node const& nd, system::error_code& ec) - { return adapter_(0, nd, ec); } + { + return adapter_(0, nd, ec); + } [[nodiscard]] auto get_supported_response_size() const noexcept - { return adapter_.get_supported_response_size();} + { + return adapter_.get_supported_response_size(); + } private: Adapter adapter_; @@ -154,6 +162,6 @@ auto make_adapter_wrapper(Adapter adapter) return wrapper{adapter}; } -} // boost::redis::adapter::detail +} // namespace boost::redis::adapter::detail -#endif // BOOST_REDIS_ADAPTER_DETAIL_RESPONSE_TRAITS_HPP +#endif // BOOST_REDIS_ADAPTER_DETAIL_RESPONSE_TRAITS_HPP diff --git a/include/boost/redis/adapter/detail/result_traits.hpp b/include/boost/redis/adapter/detail/result_traits.hpp index 4c920781..723ca2aa 100644 --- a/include/boost/redis/adapter/detail/result_traits.hpp +++ b/include/boost/redis/adapter/detail/result_traits.hpp @@ -7,21 +7,21 @@ #ifndef BOOST_REDIS_ADAPTER_RESPONSE_TRAITS_HPP #define BOOST_REDIS_ADAPTER_RESPONSE_TRAITS_HPP -#include -#include -#include #include -#include #include +#include +#include +#include +#include + #include -#include -#include #include +#include #include +#include -namespace boost::redis::adapter::detail -{ +namespace boost::redis::adapter::detail { /* Traits class for response objects. * @@ -65,27 +65,29 @@ struct result_traits, Allocator>>> template using adapter_t = typename result_traits>::adapter_type; -template +template auto internal_adapt(T& t) noexcept - { return result_traits>::adapt(t); } +{ + return result_traits>::adapt(t); +} template struct assigner { - template - static void assign(T1& dest, T2& from) - { - dest[N].template emplace(internal_adapt(std::get(from))); - assigner::assign(dest, from); - } + template + static void assign(T1& dest, T2& from) + { + dest[N].template emplace(internal_adapt(std::get(from))); + assigner::assign(dest, from); + } }; template <> struct assigner<0> { - template - static void assign(T1& dest, T2& from) - { - dest[0].template emplace<0>(internal_adapt(std::get<0>(from))); - } + template + static void assign(T1& dest, T2& from) + { + dest[0].template emplace<0>(internal_adapt(std::get<0>(from))); + } }; template @@ -94,13 +96,9 @@ class static_aggregate_adapter; template class static_aggregate_adapter> { private: - using adapters_array_type = - std::array< - mp11::mp_rename< - mp11::mp_transform< - adapter_t, Tuple>, - std::variant>, - std::tuple_size::value>; + using adapters_array_type = std::array< + mp11::mp_rename, std::variant>, + std::tuple_size::value>; // Tuple element we are currently on. std::size_t i_ = 0; @@ -148,19 +146,22 @@ public: return; } - visit([&](auto& arg){arg(elem, ec);}, adapters_[i_]); + visit( + [&](auto& arg) { + arg(elem, ec); + }, + adapters_[i_]); count(elem); } }; template -struct result_traits>> -{ +struct result_traits>> { using response_type = result>; using adapter_type = static_aggregate_adapter; static auto adapt(response_type& r) noexcept { return adapter_type{&r}; } }; -} // boost::redis::adapter::detail +} // namespace boost::redis::adapter::detail -#endif // BOOST_REDIS_ADAPTER_RESPONSE_TRAITS_HPP +#endif // BOOST_REDIS_ADAPTER_RESPONSE_TRAITS_HPP diff --git a/include/boost/redis/adapter/ignore.hpp b/include/boost/redis/adapter/ignore.hpp index 94938d64..bdf82a99 100644 --- a/include/boost/redis/adapter/ignore.hpp +++ b/include/boost/redis/adapter/ignore.hpp @@ -7,13 +7,14 @@ #ifndef BOOST_REDIS_ADAPTER_IGNORE_HPP #define BOOST_REDIS_ADAPTER_IGNORE_HPP -#include #include +#include + #include + #include -namespace boost::redis::adapter -{ +namespace boost::redis::adapter { /** @brief An adapter that ignores responses * @ingroup high-level-api @@ -25,13 +26,13 @@ struct ignore { { switch (nd.data_type) { case resp3::type::simple_error: ec = redis::error::resp3_simple_error; break; - case resp3::type::blob_error: ec = redis::error::resp3_blob_error; break; - case resp3::type::null: ec = redis::error::resp3_null; break; - default:; + case resp3::type::blob_error: ec = redis::error::resp3_blob_error; break; + case resp3::type::null: ec = redis::error::resp3_null; break; + default: ; } } }; -} // boost::redis::adapter +} // namespace boost::redis::adapter -#endif // BOOST_REDIS_ADAPTER_IGNORE_HPP +#endif // BOOST_REDIS_ADAPTER_IGNORE_HPP diff --git a/include/boost/redis/adapter/result.hpp b/include/boost/redis/adapter/result.hpp index b1af7b65..b909e227 100644 --- a/include/boost/redis/adapter/result.hpp +++ b/include/boost/redis/adapter/result.hpp @@ -8,13 +8,14 @@ #ifndef BOOST_REDIS_ADAPTER_RESULT_HPP #define BOOST_REDIS_ADAPTER_RESULT_HPP -#include #include +#include + #include + #include -namespace boost::redis::adapter -{ +namespace boost::redis::adapter { /** @brief Stores any resp3 error * @ingroup high-level-api @@ -44,10 +45,7 @@ inline bool operator==(error const& a, error const& b) * @param a Left hand side error object. * @param b Right hand side error object. */ -inline bool operator!=(error const& a, error const& b) -{ - return !(a == b); -} +inline bool operator!=(error const& a, error const& b) { return !(a == b); } /** @brief Stores response to individual Redis commands * @ingroup high-level-api @@ -55,27 +53,19 @@ inline bool operator!=(error const& a, error const& b) template using result = system::result; -BOOST_NORETURN inline void -throw_exception_from_error(error const & e, boost::source_location const &) +BOOST_NORETURN inline void throw_exception_from_error(error const& e, boost::source_location const&) { system::error_code ec; switch (e.data_type) { - case resp3::type::simple_error: - ec = redis::error::resp3_simple_error; - break; - case resp3::type::blob_error: - ec = redis::error::resp3_blob_error; - break; - case resp3::type::null: - ec = redis::error::resp3_null; - break; - default: - BOOST_ASSERT_MSG(false, "Unexpected data type."); + case resp3::type::simple_error: ec = redis::error::resp3_simple_error; break; + case resp3::type::blob_error: ec = redis::error::resp3_blob_error; break; + case resp3::type::null: ec = redis::error::resp3_null; break; + default: BOOST_ASSERT_MSG(false, "Unexpected data type."); } throw system::system_error(ec, e.diagnostic); } -} // boost::redis::adapter +} // namespace boost::redis::adapter -#endif // BOOST_REDIS_ADAPTER_RESULT_HPP +#endif // BOOST_REDIS_ADAPTER_RESULT_HPP diff --git a/include/boost/redis/config.hpp b/include/boost/redis/config.hpp index 24b21ce3..7fff87ad 100644 --- a/include/boost/redis/config.hpp +++ b/include/boost/redis/config.hpp @@ -7,13 +7,12 @@ #ifndef BOOST_REDIS_CONFIG_HPP #define BOOST_REDIS_CONFIG_HPP -#include #include -#include #include +#include +#include -namespace boost::redis -{ +namespace boost::redis { /** @brief Address of a Redis server * @ingroup high-level-api @@ -88,6 +87,6 @@ struct config { std::size_t max_read_size = (std::numeric_limits::max)(); }; -} // boost::redis +} // namespace boost::redis -#endif // BOOST_REDIS_CONFIG_HPP +#endif // BOOST_REDIS_CONFIG_HPP diff --git a/include/boost/redis/connection.hpp b/include/boost/redis/connection.hpp index ec7def78..ea364c91 100644 --- a/include/boost/redis/connection.hpp +++ b/include/boost/redis/connection.hpp @@ -42,7 +42,6 @@ #include #include #include - #include #include @@ -53,8 +52,7 @@ #include namespace boost::redis { -namespace detail -{ +namespace detail { template class append_some_op { @@ -67,17 +65,15 @@ private: public: append_some_op(AsyncReadStream& stream, DynamicBuffer buf, std::size_t size) - : stream_ {stream} - , buf_ {std::move(buf)} + : stream_{stream} + , buf_{std::move(buf)} , size_{size} { } template - void operator()( Self& self - , system::error_code ec = {} - , std::size_t n = 0) + void operator()(Self& self, system::error_code ec = {}, std::size_t n = 0) { - BOOST_ASIO_CORO_REENTER (coro_) + BOOST_ASIO_CORO_REENTER(coro_) { tmp_ = buf_.size(); buf_.grow(size_); @@ -96,22 +92,22 @@ public: }; template -auto -async_append_some( +auto async_append_some( AsyncReadStream& stream, DynamicBuffer buffer, std::size_t size, CompletionToken&& token) { - return asio::async_compose - < CompletionToken - , void(system::error_code, std::size_t) - >(append_some_op {stream, buffer, size}, token, stream); + return asio::async_compose( + append_some_op{stream, buffer, size}, + token, + stream); } template -using exec_notifier_type = - asio::experimental::channel; +using exec_notifier_type = asio::experimental::channel< + Executor, + void(system::error_code, std::size_t)>; template struct exec_op { @@ -124,9 +120,9 @@ struct exec_op { asio::coroutine coro_{}; template - void operator()(Self& self , system::error_code = {}, std::size_t = 0) + void operator()(Self& self, system::error_code = {}, std::size_t = 0) { - BOOST_ASIO_CORO_REENTER (coro_) + BOOST_ASIO_CORO_REENTER(coro_) { // Check whether the user wants to wait for the connection to // be stablished. @@ -188,21 +184,19 @@ struct writer_op { asio::coroutine coro{}; template - void operator()( Self& self - , system::error_code ec = {} - , std::size_t n = 0) + void operator()(Self& self, system::error_code ec = {}, std::size_t n = 0) { ignore_unused(n); - BOOST_ASIO_CORO_REENTER (coro) for (;;) + BOOST_ASIO_CORO_REENTER(coro) for (;;) { while (conn_->mpx_.prepare_write() != 0) { if (conn_->use_ssl()) { BOOST_ASIO_CORO_YIELD asio::async_write( - conn_->next_layer(), - asio::buffer(conn_->mpx_.get_write_buffer()), - std::move(self)); + conn_->next_layer(), + asio::buffer(conn_->mpx_.get_write_buffer()), + std::move(self)); } else { BOOST_ASIO_CORO_YIELD asio::async_write( @@ -248,7 +242,10 @@ struct writer_op { template struct reader_op { - using dyn_buffer_type = asio::dynamic_string_buffer, std::allocator>; + using dyn_buffer_type = asio::dynamic_string_buffer< + char, + std::char_traits, + std::allocator>; // TODO: Move this to config so the user can fine tune? static constexpr std::size_t buffer_growth_hint = 4096; @@ -259,13 +256,11 @@ struct reader_op { asio::coroutine coro{}; template - void operator()( Self& self - , system::error_code ec = {} - , std::size_t n = 0) + void operator()(Self& self, system::error_code ec = {}, std::size_t n = 0) { ignore_unused(n); - BOOST_ASIO_CORO_REENTER (coro) for (;;) + BOOST_ASIO_CORO_REENTER(coro) for (;;) { // Appends some data to the buffer if necessary. if (!res_.first.has_value() || conn_->mpx_.is_data_needed()) { @@ -331,7 +326,6 @@ struct reader_op { self.complete(asio::error::operation_aborted); return; } - } } } @@ -350,21 +344,22 @@ public: run_op(Conn* conn, Logger l) : conn_{conn} , logger_{l} - {} + { } template - void operator()( Self& self - , order_t order = {} - , system::error_code ec0 = {} - , system::error_code ec1 = {} - , system::error_code ec2 = {} - , system::error_code ec3 = {} - , system::error_code ec4 = {}) + void operator()( + Self& self, + order_t order = {}, + system::error_code ec0 = {}, + system::error_code ec1 = {}, + system::error_code ec2 = {}, + system::error_code ec3 = {}, + system::error_code ec4 = {}) { - BOOST_ASIO_CORO_REENTER (coro_) for (;;) + BOOST_ASIO_CORO_REENTER(coro_) for (;;) { BOOST_ASIO_CORO_YIELD - conn_->resv_.async_resolve(asio::prepend(std::move(self), order_t {})); + conn_->resv_.async_resolve(asio::prepend(std::move(self), order_t{})); logger_.on_resolve(ec0, conn_->resv_.results()); @@ -377,7 +372,7 @@ public: conn_->ctor_.async_connect( conn_->next_layer().next_layer(), conn_->resv_.results(), - asio::prepend(std::move(self), order_t {})); + asio::prepend(std::move(self), order_t{})); logger_.on_connect(ec0, conn_->ctor_.endpoint()); @@ -391,13 +386,8 @@ public: conn_->next_layer().async_handshake( asio::ssl::stream_base::client, asio::prepend( - asio::cancel_after( - conn_->cfg_.ssl_handshake_timeout, - std::move(self) - ), - order_t {} - ) - ); + asio::cancel_after(conn_->cfg_.ssl_handshake_timeout, std::move(self)), + order_t{})); logger_.on_ssl_handshake(ec0); @@ -414,14 +404,22 @@ public: // causing an authentication problem. BOOST_ASIO_CORO_YIELD asio::experimental::make_parallel_group( - [this](auto token) { return conn_->handshaker_.async_hello(*conn_, logger_, token); }, - [this](auto token) { return conn_->health_checker_.async_ping(*conn_, logger_, token); }, - [this](auto token) { return conn_->health_checker_.async_check_timeout(*conn_, logger_, token);}, - [this](auto token) { return conn_->reader(logger_, token);}, - [this](auto token) { return conn_->writer(logger_, token);} - ).async_wait( - asio::experimental::wait_for_one_error(), - std::move(self)); + [this](auto token) { + return conn_->handshaker_.async_hello(*conn_, logger_, token); + }, + [this](auto token) { + return conn_->health_checker_.async_ping(*conn_, logger_, token); + }, + [this](auto token) { + return conn_->health_checker_.async_check_timeout(*conn_, logger_, token); + }, + [this](auto token) { + return conn_->reader(logger_, token); + }, + [this](auto token) { + return conn_->writer(logger_, token); + }) + .async_wait(asio::experimental::wait_for_one_error(), std::move(self)); if (order[0] == 0 && !!ec0) { self.complete(ec0); @@ -449,7 +447,7 @@ public: conn_->writer_timer_.expires_after(conn_->cfg_.reconnect_wait_interval); BOOST_ASIO_CORO_YIELD - conn_->writer_timer_.async_wait(asio::prepend(std::move(self), order_t {})); + conn_->writer_timer_.async_wait(asio::prepend(std::move(self), order_t{})); if (ec0) { self.complete(ec0); return; @@ -465,7 +463,7 @@ public: } }; -} // boost::redis::detail +} // namespace detail /** @brief A SSL connection to the Redis server. * @ingroup high-level-api @@ -489,24 +487,21 @@ public: using executor_type = Executor; /// Returns the associated executor. - executor_type get_executor() noexcept - {return writer_timer_.get_executor();} + executor_type get_executor() noexcept { return writer_timer_.get_executor(); } /// Rebinds the socket type to another executor. template - struct rebind_executor - { + struct rebind_executor { /// The connection type when rebound to the specified executor. using other = basic_connection; }; /** @brief Constructor - * - * @param ex Executor on which connection operation will run. - * @param ctx SSL context. - */ - explicit - basic_connection( + * + * @param ex Executor on which connection operation will run. + * @param ctx SSL context. + */ + explicit basic_connection( executor_type ex, asio::ssl::context ctx = asio::ssl::context{asio::ssl::context::tlsv12_client}) : ctx_{std::move(ctx)} @@ -521,8 +516,7 @@ public: } /// Constructs from a context. - explicit - basic_connection( + explicit basic_connection( asio::io_context& ioc, asio::ssl::context ctx = asio::ssl::context{asio::ssl::context::tlsv12_client}) : basic_connection(ioc.get_executor(), std::move(ctx)) @@ -568,11 +562,7 @@ public: template < class Logger = logger, class CompletionToken = asio::default_completion_token_t> - auto - async_run( - config const& cfg = {}, - Logger l = Logger{}, - CompletionToken&& token = {}) + auto async_run(config const& cfg = {}, Logger l = Logger{}, CompletionToken&& token = {}) { cfg_ = cfg; resv_.set_config(cfg); @@ -581,10 +571,10 @@ public: handshaker_.set_config(cfg); l.set_prefix(cfg.log_prefix); - return asio::async_compose - < CompletionToken - , void(system::error_code) - >(detail::run_op{this, l}, token, writer_timer_); + return asio::async_compose( + detail::run_op{this, l}, + token, + writer_timer_); } /** @brief Receives server side pushes asynchronously. @@ -611,7 +601,9 @@ public: */ template > auto async_receive(CompletionToken&& token = {}) - { return receive_channel_.async_receive(std::forward(token)); } + { + return receive_channel_.async_receive(std::forward(token)); + } /** @brief Receives server pushes synchronously without blocking. * @@ -628,8 +620,7 @@ public: { std::size_t size = 0; - auto f = [&](system::error_code const& ec2, std::size_t n) - { + auto f = [&](system::error_code const& ec2, std::size_t n) { ec = ec2; size = n; }; @@ -669,13 +660,8 @@ public: */ template < class Response = ignore_t, - class CompletionToken = asio::default_completion_token_t - > - auto - async_exec( - request const& req, - Response& resp = ignore, - CompletionToken&& token = {}) + class CompletionToken = asio::default_completion_token_t> + auto async_exec(request const& req, Response& resp = ignore, CompletionToken&& token = {}) { return this->async_exec(req, any_adapter(resp), std::forward(token)); } @@ -686,26 +672,26 @@ public: * encapsulates a reference to a response object. */ template > - auto - async_exec( - request const& req, - any_adapter adapter, - CompletionToken&& token = {}) + auto async_exec(request const& req, any_adapter adapter, CompletionToken&& token = {}) { auto& adapter_impl = adapter.impl_; - BOOST_ASSERT_MSG(req.get_expected_responses() <= adapter_impl.supported_response_size, "Request and response have incompatible sizes."); + BOOST_ASSERT_MSG( + req.get_expected_responses() <= adapter_impl.supported_response_size, + "Request and response have incompatible sizes."); - auto notifier = std::make_shared>(get_executor(), 1); + auto notifier = std::make_shared>( + get_executor(), + 1); auto info = detail::make_elem(req, std::move(adapter_impl.adapt_fn)); info->set_done_callback([notifier]() { notifier->try_send(std::error_code{}, 0); }); - return asio::async_compose - < CompletionToken - , void(system::error_code, std::size_t) - >(detail::exec_op{this, notifier, info}, token, writer_timer_); + return asio::async_compose( + detail::exec_op{this, notifier, info}, + token, + writer_timer_); } /** @brief Cancel operations. @@ -722,80 +708,72 @@ public: void cancel(operation op = operation::all) { switch (op) { - case operation::resolve: - resv_.cancel(); - break; - case operation::exec: - mpx_.cancel_waiting(); - break; + case operation::resolve: resv_.cancel(); break; + case operation::exec: mpx_.cancel_waiting(); break; case operation::reconnection: cfg_.reconnect_wait_interval = std::chrono::seconds::zero(); break; - case operation::run: - cancel_run(); - break; - case operation::receive: - receive_channel_.cancel(); - break; - case operation::health_check: - health_checker_.cancel(); - break; + case operation::run: cancel_run(); break; + case operation::receive: receive_channel_.cancel(); break; + case operation::health_check: health_checker_.cancel(); break; case operation::all: resv_.cancel(); cfg_.reconnect_wait_interval = std::chrono::seconds::zero(); health_checker_.cancel(); - cancel_run(); // run - receive_channel_.cancel(); // receive - mpx_.cancel_waiting(); // exec + cancel_run(); // run + receive_channel_.cancel(); // receive + mpx_.cancel_waiting(); // exec break; default: /* ignore */; } } - auto run_is_canceled() const noexcept - { return mpx_.get_cancel_run_state(); } + auto run_is_canceled() const noexcept { return mpx_.get_cancel_run_state(); } /// Returns true if the connection was canceled. bool will_reconnect() const noexcept - { return cfg_.reconnect_wait_interval != std::chrono::seconds::zero();} + { + return cfg_.reconnect_wait_interval != std::chrono::seconds::zero(); + } /// Returns the ssl context. - auto const& get_ssl_context() const noexcept - { return ctx_;} + auto const& get_ssl_context() const noexcept { return ctx_; } /// Resets the underlying stream. void reset_stream() - { stream_ = std::make_unique(writer_timer_.get_executor(), ctx_); } + { + stream_ = std::make_unique(writer_timer_.get_executor(), ctx_); + } /// Returns a reference to the next layer. - auto& next_layer() noexcept - { return *stream_; } + auto& next_layer() noexcept { return *stream_; } /// Returns a const reference to the next layer. - auto const& next_layer() const noexcept - { return *stream_; } + auto const& next_layer() const noexcept { return *stream_; } /// Sets the response object of `async_receive` operations. template void set_receive_response(Response& response) - { mpx_.set_receive_response(response); } + { + mpx_.set_receive_response(response); + } /// Returns connection usage information. - usage get_usage() const noexcept - { return mpx_.get_usage(); } + usage get_usage() const noexcept { return mpx_.get_usage(); } private: using clock_type = std::chrono::steady_clock; using clock_traits_type = asio::wait_traits; using timer_type = asio::basic_waitable_timer; - using receive_channel_type = asio::experimental::channel; + using receive_channel_type = asio::experimental::channel< + executor_type, + void(system::error_code, std::size_t)>; using resolver_type = detail::resolver; using health_checker_type = detail::health_checker; using resp3_handshaker_type = detail::resp3_handshaker; - auto use_ssl() const noexcept - { return cfg_.use_ssl;} + auto use_ssl() const noexcept { return cfg_.use_ssl; } void cancel_run() { @@ -813,20 +791,19 @@ private: template auto reader(Logger l, CompletionToken&& token) { - return asio::async_compose - < CompletionToken - , void(system::error_code) - >(detail::reader_op{this, l}, - std::forward(token), writer_timer_); + return asio::async_compose( + detail::reader_op{this, l}, + std::forward(token), + writer_timer_); } template auto writer(Logger l, CompletionToken&& token) { - return asio::async_compose - < CompletionToken - , void(system::error_code) - >(detail::writer_op{this, l}, std::forward(token), writer_timer_); + return asio::async_compose( + detail::writer_op{this, l}, + std::forward(token), + writer_timer_); } void close() @@ -840,10 +817,7 @@ private: auto is_open() const noexcept { return stream_->next_layer().is_open(); } auto& lowest_layer() noexcept { return stream_->lowest_layer(); } - [[nodiscard]] bool trigger_write() const noexcept - { - return is_open() && !mpx_.is_writing(); - } + [[nodiscard]] bool trigger_write() const noexcept { return is_open() && !mpx_.is_writing(); } asio::ssl::context ctx_; std::unique_ptr stream_; @@ -877,43 +851,41 @@ public: using executor_type = asio::any_io_executor; /// Contructs from an executor. - explicit - connection( + explicit connection( executor_type ex, asio::ssl::context ctx = asio::ssl::context{asio::ssl::context::tlsv12_client}); /// Contructs from a context. - explicit - connection( + explicit connection( asio::io_context& ioc, asio::ssl::context ctx = asio::ssl::context{asio::ssl::context::tlsv12_client}); /// Returns the underlying executor. - executor_type get_executor() noexcept - { return impl_.get_executor(); } + executor_type get_executor() noexcept { return impl_.get_executor(); } /// Calls `boost::redis::basic_connection::async_run`. template auto async_run(config const& cfg, logger l, CompletionToken&& token = {}) { - return asio::async_initiate< - CompletionToken, void(boost::system::error_code)>( - [](auto handler, connection* self, config const* cfg, logger l) - { - self->async_run_impl(*cfg, l, std::move(handler)); - }, token, this, &cfg, l); + return asio::async_initiate( + [](auto handler, connection* self, config const* cfg, logger l) { + self->async_run_impl(*cfg, l, std::move(handler)); + }, + token, + this, + &cfg, + l); } /// Calls `boost::redis::basic_connection::async_receive`. template auto async_receive(CompletionToken&& token = {}) - { return impl_.async_receive(std::forward(token)); } + { + return impl_.async_receive(std::forward(token)); + } /// Calls `boost::redis::basic_connection::receive`. - std::size_t receive(system::error_code& ec) - { - return impl_.receive(ec); - } + std::size_t receive(system::error_code& ec) { return impl_.receive(ec); } /// Calls `boost::redis::basic_connection::async_exec`. template @@ -924,61 +896,53 @@ public: /// Calls `boost::redis::basic_connection::async_exec`. template - auto - async_exec( - request const& req, - any_adapter adapter, - CompletionToken&& token = {}) + auto async_exec(request const& req, any_adapter adapter, CompletionToken&& token = {}) { - return asio::async_initiate< - CompletionToken, void(boost::system::error_code, std::size_t)>( - [](auto handler, connection* self, request const* req, any_adapter&& adapter) - { - self->async_exec_impl(*req, std::move(adapter), std::move(handler)); - }, token, this, &req, std::move(adapter)); + return asio::async_initiate( + [](auto handler, connection* self, request const* req, any_adapter&& adapter) { + self->async_exec_impl(*req, std::move(adapter), std::move(handler)); + }, + token, + this, + &req, + std::move(adapter)); } /// Calls `boost::redis::basic_connection::cancel`. void cancel(operation op = operation::all); /// Calls `boost::redis::basic_connection::will_reconnect`. - bool will_reconnect() const noexcept - { return impl_.will_reconnect();} + bool will_reconnect() const noexcept { return impl_.will_reconnect(); } /// Calls `boost::redis::basic_connection::next_layer`. - auto& next_layer() noexcept - { return impl_.next_layer(); } + auto& next_layer() noexcept { return impl_.next_layer(); } /// Calls `boost::redis::basic_connection::next_layer`. - auto const& next_layer() const noexcept - { return impl_.next_layer(); } + auto const& next_layer() const noexcept { return impl_.next_layer(); } /// Calls `boost::redis::basic_connection::reset_stream`. - void reset_stream() - { impl_.reset_stream();} + void reset_stream() { impl_.reset_stream(); } /// Sets the response object of `async_receive` operations. template void set_receive_response(Response& response) - { impl_.set_receive_response(response); } + { + impl_.set_receive_response(response); + } /// Returns connection usage information. - usage get_usage() const noexcept - { return impl_.get_usage(); } + usage get_usage() const noexcept { return impl_.get_usage(); } /// Returns the ssl context. - auto const& get_ssl_context() const noexcept - { return impl_.get_ssl_context();} + auto const& get_ssl_context() const noexcept { return impl_.get_ssl_context(); } private: - void - async_run_impl( + void async_run_impl( config const& cfg, logger l, asio::any_completion_handler token); - - void - async_exec_impl( + + void async_exec_impl( request const& req, any_adapter&& adapter, asio::any_completion_handler token); @@ -986,6 +950,6 @@ private: basic_connection impl_; }; -} // boost::redis +} // namespace boost::redis -#endif // BOOST_REDIS_CONNECTION_HPP +#endif // BOOST_REDIS_CONNECTION_HPP diff --git a/include/boost/redis/detail/connector.hpp b/include/boost/redis/detail/connector.hpp index c9a31b08..193dab5d 100644 --- a/include/boost/redis/detail/connector.hpp +++ b/include/boost/redis/detail/connector.hpp @@ -9,16 +9,17 @@ #include #include + +#include #include #include #include #include -#include -#include -#include -namespace boost::redis::detail -{ +#include +#include + +namespace boost::redis::detail { template struct connect_op { @@ -28,15 +29,20 @@ struct connect_op { asio::coroutine coro{}; template - void operator()( Self& self - , system::error_code const& ec = {} - , asio::ip::tcp::endpoint const& ep= {}) + void operator()( + Self& self, + system::error_code const& ec = {}, + asio::ip::tcp::endpoint const& ep = {}) { - BOOST_ASIO_CORO_REENTER (coro) + BOOST_ASIO_CORO_REENTER(coro) { BOOST_ASIO_CORO_YIELD - asio::async_connect(*stream, *res_, - [](system::error_code const&, auto const&) { return true; }, + asio::async_connect( + *stream, + *res_, + [](system::error_code const&, auto const&) { + return true; + }, asio::cancel_after(ctor_->timeout_, std::move(self))); ctor_->endpoint_ = ep; @@ -52,23 +58,20 @@ struct connect_op { class connector { public: - void set_config(config const& cfg) - { timeout_ = cfg.connect_timeout; } + void set_config(config const& cfg) { timeout_ = cfg.connect_timeout; } template - auto - async_connect( - Stream& stream, - asio::ip::tcp::resolver::results_type const& res, - CompletionToken&& token) + auto async_connect( + Stream& stream, + asio::ip::tcp::resolver::results_type const& res, + CompletionToken&& token) { - return asio::async_compose - < CompletionToken - , void(system::error_code) - >(connect_op{this, &stream, &res}, token); + return asio::async_compose( + connect_op{this, &stream, &res}, + token); } - auto const& endpoint() const noexcept { return endpoint_;} + auto const& endpoint() const noexcept { return endpoint_; } private: template friend struct connect_op; @@ -77,6 +80,6 @@ private: asio::ip::tcp::endpoint endpoint_; }; -} // boost::redis::detail +} // namespace boost::redis::detail -#endif // BOOST_REDIS_CONNECTOR_HPP +#endif // BOOST_REDIS_CONNECTOR_HPP diff --git a/include/boost/redis/detail/health_checker.hpp b/include/boost/redis/detail/health_checker.hpp index 3ac6c948..ef90b875 100644 --- a/include/boost/redis/detail/health_checker.hpp +++ b/include/boost/redis/detail/health_checker.hpp @@ -7,19 +7,20 @@ #ifndef BOOST_REDIS_HEALTH_CHECKER_HPP #define BOOST_REDIS_HEALTH_CHECKER_HPP -#include -#include -#include #include #include #include -#include +#include +#include + #include #include #include #include -#include +#include + #include +#include namespace boost::redis::detail { @@ -34,7 +35,7 @@ public: template void operator()(Self& self, system::error_code ec = {}, std::size_t = 0) { - BOOST_ASIO_CORO_REENTER (coro_) for (;;) + BOOST_ASIO_CORO_REENTER(coro_) for (;;) { if (checker_->ping_interval_ == std::chrono::seconds::zero()) { logger_.trace("ping_op (1): timeout disabled."); @@ -84,7 +85,7 @@ public: template void operator()(Self& self, system::error_code ec = {}) { - BOOST_ASIO_CORO_REENTER (coro_) for (;;) + BOOST_ASIO_CORO_REENTER(coro_) for (;;) { if (checker_->ping_interval_ == std::chrono::seconds::zero()) { logger_.trace("check_timeout_op (1): timeout disabled."); @@ -130,11 +131,10 @@ public: template class health_checker { private: - using timer_type = - asio::basic_waitable_timer< - std::chrono::steady_clock, - asio::wait_traits, - Executor>; + using timer_type = asio::basic_waitable_timer< + std::chrono::steady_clock, + asio::wait_traits, + Executor>; public: health_checker(Executor ex) @@ -160,20 +160,22 @@ public: template auto async_ping(Connection& conn, Logger l, CompletionToken token) { - return asio::async_compose - < CompletionToken - , void(system::error_code) - >(ping_op{this, &conn, l}, token, conn, ping_timer_); + return asio::async_compose( + ping_op{this, &conn, l}, + token, + conn, + ping_timer_); } template auto async_check_timeout(Connection& conn, Logger l, CompletionToken token) { checker_has_exited_ = false; - return asio::async_compose - < CompletionToken - , void(system::error_code) - >(check_timeout_op{this, &conn, l}, token, conn, wait_timer_); + return asio::async_compose( + check_timeout_op{this, &conn, l}, + token, + conn, + wait_timer_); } private: @@ -188,6 +190,6 @@ private: bool checker_has_exited_ = false; }; -} // boost::redis::detail +} // namespace boost::redis::detail -#endif // BOOST_REDIS_HEALTH_CHECKER_HPP +#endif // BOOST_REDIS_HEALTH_CHECKER_HPP diff --git a/include/boost/redis/detail/helper.hpp b/include/boost/redis/detail/helper.hpp index 2a58bdc5..d6c2481a 100644 --- a/include/boost/redis/detail/helper.hpp +++ b/include/boost/redis/detail/helper.hpp @@ -9,8 +9,7 @@ #include -namespace boost::redis::detail -{ +namespace boost::redis::detail { template auto is_cancelled(T const& self) @@ -18,20 +17,18 @@ auto is_cancelled(T const& self) return self.get_cancellation_state().cancelled() != asio::cancellation_type_t::none; } -#define BOOST_REDIS_CHECK_OP0(X)\ - if (ec || redis::detail::is_cancelled(self)) {\ - X\ - self.complete(!!ec ? ec : asio::error::operation_aborted);\ - return;\ +#define BOOST_REDIS_CHECK_OP0(X) \ + if (ec || redis::detail::is_cancelled(self)) { \ + X self.complete(!!ec ? ec : asio::error::operation_aborted); \ + return; \ } -#define BOOST_REDIS_CHECK_OP1(X)\ - if (ec || redis::detail::is_cancelled(self)) {\ - X\ - self.complete(!!ec ? ec : asio::error::operation_aborted, {});\ - return;\ +#define BOOST_REDIS_CHECK_OP1(X) \ + if (ec || redis::detail::is_cancelled(self)) { \ + X self.complete(!!ec ? ec : asio::error::operation_aborted, {}); \ + return; \ } -} // boost::redis::detail +} // namespace boost::redis::detail -#endif // BOOST_REDIS_HELPER_HPP +#endif // BOOST_REDIS_HELPER_HPP diff --git a/include/boost/redis/detail/multiplexer.hpp b/include/boost/redis/detail/multiplexer.hpp index 1c799ed3..96ebe4fd 100644 --- a/include/boost/redis/detail/multiplexer.hpp +++ b/include/boost/redis/detail/multiplexer.hpp @@ -20,12 +20,11 @@ #include #include #include +#include #include #include -#include -namespace boost::redis -{ +namespace boost::redis { class request; @@ -35,63 +34,61 @@ using tribool = std::optional; struct multiplexer { using adapter_type = std::function; - using pipeline_adapter_type = std::function; + using pipeline_adapter_type = std::function< + void(std::size_t, resp3::node_view const&, system::error_code&)>; struct elem { public: explicit elem(request const& req, pipeline_adapter_type adapter); - void set_done_callback(std::function f) noexcept - { done_ = std::move(f); }; + void set_done_callback(std::function f) noexcept { done_ = std::move(f); }; - auto notify_done() noexcept - { done_(); } + auto notify_done() noexcept { done_(); } auto notify_error(system::error_code ec) noexcept -> void; [[nodiscard]] auto is_waiting() const noexcept - { return status_ == status::waiting; } + { + return status_ == status::waiting; + } [[nodiscard]] auto is_written() const noexcept - { return status_ == status::written; } + { + return status_ == status::written; + } [[nodiscard]] auto is_staged() const noexcept - { return status_ == status::staged; } + { + return status_ == status::staged; + } - void mark_written() noexcept - { status_ = status::written; } + void mark_written() noexcept { status_ = status::written; } - void mark_staged() noexcept - { status_ = status::staged; } + void mark_staged() noexcept { status_ = status::staged; } - void mark_waiting() noexcept - { status_ = status::waiting; } + void mark_waiting() noexcept { status_ = status::waiting; } - auto get_error() const -> system::error_code const& - { return ec_; } + auto get_error() const -> system::error_code const& { return ec_; } - auto get_request() const -> request const& - { return *req_; } + auto get_request() const -> request const& { return *req_; } - auto get_read_size() const -> std::size_t - { return read_size_; } + auto get_read_size() const -> std::size_t { return read_size_; } - auto get_remaining_responses() const -> std::size_t - { return remaining_responses_; } + auto get_remaining_responses() const -> std::size_t { return remaining_responses_; } auto commit_response(std::size_t read_size) -> void; - auto get_adapter() -> adapter_type& - { return adapter_; } + auto get_adapter() -> adapter_type& { return adapter_; } private: enum class status - { waiting - , staged - , written + { + waiting, + staged, + written }; request const* req_; @@ -124,7 +121,9 @@ struct multiplexer { [[nodiscard]] auto const& get_parser() const noexcept - { return parser_; } + { + return parser_; + } //[[nodiscard]] auto cancel_waiting() -> std::size_t; @@ -134,19 +133,27 @@ struct multiplexer { [[nodiscard]] auto get_cancel_run_state() const noexcept -> bool - { return cancel_run_called_; } + { + return cancel_run_called_; + } [[nodiscard]] auto get_write_buffer() noexcept -> std::string_view - { return std::string_view{write_buffer_}; } + { + return std::string_view{write_buffer_}; + } [[nodiscard]] auto get_read_buffer() noexcept -> std::string& - { return read_buffer_; } + { + return read_buffer_; + } [[nodiscard]] auto is_data_needed() const noexcept -> bool - { return std::empty(read_buffer_); } + { + return std::empty(read_buffer_); + } // TODO: Change signature to receive an adapter instead of a // response. @@ -160,7 +167,9 @@ struct multiplexer { [[nodiscard]] auto get_usage() const noexcept -> usage - { return usage_;} + { + return usage_; + } [[nodiscard]] auto is_writing() const noexcept -> bool; @@ -189,12 +198,10 @@ private: adapter_type receive_adapter_; }; -auto -make_elem( - request const& req, - multiplexer::pipeline_adapter_type adapter) -> std::shared_ptr; +auto make_elem(request const& req, multiplexer::pipeline_adapter_type adapter) + -> std::shared_ptr; -} // detail -} // boost::redis +} // namespace detail +} // namespace boost::redis -#endif // BOOST_REDIS_MULTIPLEXER_HPP +#endif // BOOST_REDIS_MULTIPLEXER_HPP diff --git a/include/boost/redis/detail/resolver.hpp b/include/boost/redis/detail/resolver.hpp index c55def30..6f67fccd 100644 --- a/include/boost/redis/detail/resolver.hpp +++ b/include/boost/redis/detail/resolver.hpp @@ -9,15 +9,16 @@ #include #include + +#include #include #include #include -#include -#include -#include -namespace boost::redis::detail -{ +#include +#include + +namespace boost::redis::detail { template struct resolve_op { @@ -25,11 +26,12 @@ struct resolve_op { asio::coroutine coro{}; template - void operator()( Self& self - , system::error_code ec = {} - , asio::ip::tcp::resolver::results_type res = {}) + void operator()( + Self& self, + system::error_code ec = {}, + asio::ip::tcp::resolver::results_type res = {}) { - BOOST_ASIO_CORO_REENTER (coro) + BOOST_ASIO_CORO_REENTER(coro) { BOOST_ASIO_CORO_YIELD resv_->resv_.async_resolve( @@ -51,22 +53,22 @@ struct resolve_op { template class resolver { public: - resolver(Executor ex) : resv_{ex} {} + resolver(Executor ex) + : resv_{ex} + { } template auto async_resolve(CompletionToken&& token) { - return asio::async_compose - < CompletionToken - , void(system::error_code) - >(resolve_op{this}, token, resv_); + return asio::async_compose( + resolve_op{this}, + token, + resv_); } - void cancel() - { resv_.cancel(); } + void cancel() { resv_.cancel(); } - auto const& results() const noexcept - { return results_;} + auto const& results() const noexcept { return results_; } void set_config(config const& cfg) { @@ -84,6 +86,6 @@ private: asio::ip::tcp::resolver::results_type results_; }; -} // boost::redis::detail +} // namespace boost::redis::detail -#endif // BOOST_REDIS_RESOLVER_HPP +#endif // BOOST_REDIS_RESOLVER_HPP diff --git a/include/boost/redis/detail/resp3_handshaker.hpp b/include/boost/redis/detail/resp3_handshaker.hpp index 2bc83996..6ce9a857 100644 --- a/include/boost/redis/detail/resp3_handshaker.hpp +++ b/include/boost/redis/detail/resp3_handshaker.hpp @@ -8,20 +8,20 @@ #define BOOST_REDIS_RUNNER_HPP #include -#include -#include #include #include #include +#include +#include + #include #include //#include -#include -#include #include +#include +#include -namespace boost::redis::detail -{ +namespace boost::redis::detail { void push_hello(config const& cfg, request& req); @@ -38,12 +38,15 @@ struct hello_op { template void operator()(Self& self, system::error_code ec = {}, std::size_t = 0) { - BOOST_ASIO_CORO_REENTER (coro_) + BOOST_ASIO_CORO_REENTER(coro_) { handshaker_->add_hello(); BOOST_ASIO_CORO_YIELD - conn_->async_exec(handshaker_->hello_req_, any_adapter(handshaker_->hello_resp_), std::move(self)); + conn_->async_exec( + handshaker_->hello_req_, + any_adapter(handshaker_->hello_resp_), + std::move(self)); logger_.on_hello(ec, handshaker_->hello_resp_); if (ec) { @@ -66,16 +69,15 @@ struct hello_op { template class resp3_handshaker { public: - void set_config(config const& cfg) - { cfg_ = cfg; } + void set_config(config const& cfg) { cfg_ = cfg; } template auto async_hello(Connection& conn, Logger l, CompletionToken token) { - return asio::async_compose - < CompletionToken - , void(system::error_code) - >(hello_op{this, &conn, l}, token, conn); + return asio::async_compose( + hello_op{this, &conn, l}, + token, + conn); } private: @@ -94,12 +96,11 @@ private: if (!hello_resp_.has_value()) return true; - auto f = [](auto const& e) - { + auto f = [](auto const& e) { switch (e.data_type) { case resp3::type::simple_error: - case resp3::type::blob_error: return true; - default: return false; + case resp3::type::blob_error: return true; + default: return false; } }; @@ -111,6 +112,6 @@ private: config cfg_; }; -} // boost::redis::detail +} // namespace boost::redis::detail -#endif // BOOST_REDIS_RUNNER_HPP +#endif // BOOST_REDIS_RUNNER_HPP diff --git a/include/boost/redis/detail/runner.hpp b/include/boost/redis/detail/runner.hpp index 2319a3de..2c541f59 100644 --- a/include/boost/redis/detail/runner.hpp +++ b/include/boost/redis/detail/runner.hpp @@ -9,26 +9,27 @@ #include #include -#include -#include #include #include #include #include +#include +#include + +#include #include #include #include #include -#include #include #include -#include -#include -#include -#include +#include -namespace boost::redis::detail -{ +#include +#include +#include + +namespace boost::redis::detail { void push_hello(config const& cfg, request& req); @@ -45,7 +46,7 @@ struct hello_op { template void operator()(Self& self, system::error_code ec = {}, std::size_t = 0) { - BOOST_ASIO_CORO_REENTER (coro_) + BOOST_ASIO_CORO_REENTER(coro_) { runner_->add_hello(); @@ -85,21 +86,22 @@ public: : runner_{runner} , conn_{conn} , logger_{l} - {} + { } template - void operator()( Self& self - , order_t order = {} - , system::error_code ec0 = {} - , system::error_code ec1 = {} - , system::error_code ec2 = {} - , system::error_code ec3 = {} - , system::error_code ec4 = {}) + void operator()( + Self& self, + order_t order = {}, + system::error_code ec0 = {}, + system::error_code ec1 = {}, + system::error_code ec2 = {}, + system::error_code ec3 = {}, + system::error_code ec4 = {}) { - BOOST_ASIO_CORO_REENTER (coro_) for (;;) + BOOST_ASIO_CORO_REENTER(coro_) for (;;) { BOOST_ASIO_CORO_YIELD - conn_->resv_.async_resolve(asio::prepend(std::move(self), order_t {})); + conn_->resv_.async_resolve(asio::prepend(std::move(self), order_t{})); logger_.on_resolve(ec0, conn_->resv_.results()); @@ -112,7 +114,7 @@ public: conn_->ctor_.async_connect( conn_->next_layer().next_layer(), conn_->resv_.results(), - asio::prepend(std::move(self), order_t {})); + asio::prepend(std::move(self), order_t{})); logger_.on_connect(ec0, conn_->ctor_.endpoint()); @@ -126,13 +128,8 @@ public: conn_->next_layer().async_handshake( asio::ssl::stream_base::client, asio::prepend( - asio::cancel_after( - runner_->cfg_.ssl_handshake_timeout, - std::move(self) - ), - order_t {} - ) - ); + asio::cancel_after(runner_->cfg_.ssl_handshake_timeout, std::move(self)), + order_t{})); logger_.on_ssl_handshake(ec0); @@ -149,14 +146,22 @@ public: // causing an authentication problem. BOOST_ASIO_CORO_YIELD asio::experimental::make_parallel_group( - [this](auto token) { return runner_->async_hello(*conn_, logger_, token); }, - [this](auto token) { return conn_->health_checker_.async_ping(*conn_, logger_, token); }, - [this](auto token) { return conn_->health_checker_.async_check_timeout(*conn_, logger_, token);}, - [this](auto token) { return conn_->reader(logger_, token);}, - [this](auto token) { return conn_->writer(logger_, token);} - ).async_wait( - asio::experimental::wait_for_one_error(), - std::move(self)); + [this](auto token) { + return runner_->async_hello(*conn_, logger_, token); + }, + [this](auto token) { + return conn_->health_checker_.async_ping(*conn_, logger_, token); + }, + [this](auto token) { + return conn_->health_checker_.async_check_timeout(*conn_, logger_, token); + }, + [this](auto token) { + return conn_->reader(logger_, token); + }, + [this](auto token) { + return conn_->writer(logger_, token); + }) + .async_wait(asio::experimental::wait_for_one_error(), std::move(self)); if (order[0] == 0 && !!ec0) { self.complete(ec0); @@ -184,7 +189,7 @@ public: conn_->writer_timer_.expires_after(conn_->cfg_.reconnect_wait_interval); BOOST_ASIO_CORO_YIELD - conn_->writer_timer_.async_wait(asio::prepend(std::move(self), order_t {})); + conn_->writer_timer_.async_wait(asio::prepend(std::move(self), order_t{})); if (ec0) { self.complete(ec0); return; @@ -207,32 +212,28 @@ public: : cfg_{cfg} { } - void set_config(config const& cfg) - { - cfg_ = cfg; - } + void set_config(config const& cfg) { cfg_ = cfg; } template auto async_run(Connection& conn, Logger l, CompletionToken token) { - return asio::async_compose - < CompletionToken - , void(system::error_code) - >(runner_op{this, &conn, l}, token, conn); + return asio::async_compose( + runner_op{this, &conn, l}, + token, + conn); } private: - template friend class runner_op; template friend struct hello_op; template auto async_hello(Connection& conn, Logger l, CompletionToken token) { - return asio::async_compose - < CompletionToken - , void(system::error_code) - >(hello_op{this, &conn, l}, token, conn); + return asio::async_compose( + hello_op{this, &conn, l}, + token, + conn); } void add_hello() @@ -248,12 +249,11 @@ private: if (!hello_resp_.has_value()) return true; - auto f = [](auto const& e) - { + auto f = [](auto const& e) { switch (e.data_type) { case resp3::type::simple_error: - case resp3::type::blob_error: return true; - default: return false; + case resp3::type::blob_error: return true; + default: return false; } }; @@ -265,6 +265,6 @@ private: config cfg_; }; -} // boost::redis::detail +} // namespace boost::redis::detail -#endif // BOOST_REDIS_RUNNER_HPP +#endif // BOOST_REDIS_RUNNER_HPP diff --git a/include/boost/redis/detail/write.hpp b/include/boost/redis/detail/write.hpp index a6cb0639..47c11f2a 100644 --- a/include/boost/redis/detail/write.hpp +++ b/include/boost/redis/detail/write.hpp @@ -7,9 +7,10 @@ #ifndef BOOST_REDIS_WRITE_HPP #define BOOST_REDIS_WRITE_HPP -#include #include +#include + namespace boost::redis::detail { /** \brief Writes a request synchronously. @@ -18,13 +19,13 @@ namespace boost::redis::detail { * \param stream Stream to write the request to. * \param req Request to write. */ -template +template auto write(SyncWriteStream& stream, request const& req) { return asio::write(stream, asio::buffer(req.payload())); } -template +template auto write(SyncWriteStream& stream, request const& req, system::error_code& ec) { return asio::write(stream, asio::buffer(req.payload()), ec); @@ -37,10 +38,10 @@ auto write(SyncWriteStream& stream, request const& req, system::error_code& ec) * \param req Request to write. * \param token Asio completion token. */ -template< +template < class AsyncWriteStream, - class CompletionToken = asio::default_completion_token_t - > + class CompletionToken = + asio::default_completion_token_t > auto async_write( AsyncWriteStream& stream, request const& req, @@ -50,6 +51,6 @@ auto async_write( return asio::async_write(stream, asio::buffer(req.payload()), token); } -} // boost::redis::detail +} // namespace boost::redis::detail -#endif // BOOST_REDIS_WRITE_HPP +#endif // BOOST_REDIS_WRITE_HPP diff --git a/include/boost/redis/error.hpp b/include/boost/redis/error.hpp index 3ab56fce..6839a1eb 100644 --- a/include/boost/redis/error.hpp +++ b/include/boost/redis/error.hpp @@ -93,13 +93,13 @@ enum class error */ auto make_error_code(error e) -> system::error_code; -} // boost::redis +} // namespace boost::redis namespace std { -template<> -struct is_error_code_enum<::boost::redis::error> : std::true_type {}; +template <> +struct is_error_code_enum<::boost::redis::error> : std::true_type { }; -} // std +} // namespace std -#endif // BOOST_REDIS_ERROR_HPP +#endif // BOOST_REDIS_ERROR_HPP diff --git a/include/boost/redis/ignore.hpp b/include/boost/redis/ignore.hpp index bdaae05c..55527b62 100644 --- a/include/boost/redis/ignore.hpp +++ b/include/boost/redis/ignore.hpp @@ -13,8 +13,7 @@ #include #include -namespace boost::redis -{ +namespace boost::redis { /** @brief Type used to ignore responses. * @ingroup high-level-api @@ -44,6 +43,6 @@ using ignore_t = std::decay_t; */ extern ignore_t ignore; -} // boost::redis +} // namespace boost::redis -#endif // BOOST_REDIS_IGNORE_HPP +#endif // BOOST_REDIS_IGNORE_HPP diff --git a/include/boost/redis/impl/connection.ipp b/include/boost/redis/impl/connection.ipp index a798223d..fc4008ad 100644 --- a/include/boost/redis/impl/connection.ipp +++ b/include/boost/redis/impl/connection.ipp @@ -5,6 +5,7 @@ */ #include + #include namespace boost::redis { @@ -17,8 +18,7 @@ connection::connection(asio::io_context& ioc, asio::ssl::context ctx) : impl_{ioc.get_executor(), std::move(ctx)} { } -void -connection::async_run_impl( +void connection::async_run_impl( config const& cfg, logger l, asio::any_completion_handler token) @@ -26,8 +26,7 @@ connection::async_run_impl( impl_.async_run(cfg, l, std::move(token)); } -void -connection::async_exec_impl( +void connection::async_exec_impl( request const& req, any_adapter&& adapter, asio::any_completion_handler token) @@ -35,9 +34,6 @@ connection::async_exec_impl( impl_.async_exec(req, std::move(adapter), std::move(token)); } -void connection::cancel(operation op) -{ - impl_.cancel(op); -} +void connection::cancel(operation op) { impl_.cancel(op); } -} // namespace boost::redis +} // namespace boost::redis diff --git a/include/boost/redis/impl/error.ipp b/include/boost/redis/impl/error.ipp index a5ca7081..662e7f81 100644 --- a/include/boost/redis/impl/error.ipp +++ b/include/boost/redis/impl/error.ipp @@ -5,62 +5,62 @@ */ #include + #include namespace boost::redis { namespace detail { struct error_category_impl : system::error_category { - virtual ~error_category_impl() = default; - auto name() const noexcept -> char const* override - { - return "boost.redis"; - } + auto name() const noexcept -> char const* override { return "boost.redis"; } auto message(int ev) const -> std::string override { - switch(static_cast(ev)) { - case error::invalid_data_type: return "Invalid resp3 type."; - case error::not_a_number: return "Can't convert string to number (maybe forgot to upgrade to RESP3?)."; - case error::exceeeds_max_nested_depth: return "Exceeds the maximum number of nested responses."; - case error::unexpected_bool_value: return "Unexpected bool value."; - case error::empty_field: return "Expected field value is empty."; - case error::expects_resp3_simple_type: return "Expects a resp3 simple type."; - case error::expects_resp3_aggregate: return "Expects resp3 aggregate."; - case error::expects_resp3_map: return "Expects resp3 map."; - case error::expects_resp3_set: return "Expects resp3 set."; - case error::nested_aggregate_not_supported: return "Nested aggregate not_supported."; - case error::resp3_simple_error: return "Got RESP3 simple-error."; - case error::resp3_blob_error: return "Got RESP3 blob-error."; - case error::incompatible_size: return "Aggregate container has incompatible size."; - case error::not_a_double: return "Not a double."; - case error::resp3_null: return "Got RESP3 null."; - case error::not_connected: return "Not connected."; - case error::resolve_timeout: return "Resolve timeout."; - case error::connect_timeout: return "Connect timeout."; - case error::pong_timeout: return "Pong timeout."; - case error::ssl_handshake_timeout: return "SSL handshake timeout."; - case error::sync_receive_push_failed: return "Can't receive server push synchronously without blocking."; - case error::incompatible_node_depth: return "Incompatible node depth."; - case error::resp3_hello: return "RESP3 handshake error (hello command)."; - default: BOOST_ASSERT(false); return "Boost.Redis error."; + switch (static_cast(ev)) { + case error::invalid_data_type: return "Invalid resp3 type."; + case error::not_a_number: + return "Can't convert string to number (maybe forgot to upgrade to RESP3?)."; + case error::exceeeds_max_nested_depth: + return "Exceeds the maximum number of nested responses."; + case error::unexpected_bool_value: return "Unexpected bool value."; + case error::empty_field: return "Expected field value is empty."; + case error::expects_resp3_simple_type: return "Expects a resp3 simple type."; + case error::expects_resp3_aggregate: return "Expects resp3 aggregate."; + case error::expects_resp3_map: return "Expects resp3 map."; + case error::expects_resp3_set: return "Expects resp3 set."; + case error::nested_aggregate_not_supported: return "Nested aggregate not_supported."; + case error::resp3_simple_error: return "Got RESP3 simple-error."; + case error::resp3_blob_error: return "Got RESP3 blob-error."; + case error::incompatible_size: return "Aggregate container has incompatible size."; + case error::not_a_double: return "Not a double."; + case error::resp3_null: return "Got RESP3 null."; + case error::not_connected: return "Not connected."; + case error::resolve_timeout: return "Resolve timeout."; + case error::connect_timeout: return "Connect timeout."; + case error::pong_timeout: return "Pong timeout."; + case error::ssl_handshake_timeout: return "SSL handshake timeout."; + case error::sync_receive_push_failed: + return "Can't receive server push synchronously without blocking."; + case error::incompatible_node_depth: return "Incompatible node depth."; + case error::resp3_hello: return "RESP3 handshake error (hello command)."; + default: BOOST_ASSERT(false); return "Boost.Redis error."; } } }; auto category() -> system::error_category const& { - static error_category_impl instance; - return instance; + static error_category_impl instance; + return instance; } -} // detail +} // namespace detail auto make_error_code(error e) -> system::error_code { - return system::error_code{static_cast(e), detail::category()}; + return system::error_code{static_cast(e), detail::category()}; } -} // boost::redis::detail +} // namespace boost::redis diff --git a/include/boost/redis/impl/ignore.ipp b/include/boost/redis/impl/ignore.ipp index 05c46214..b790f39e 100644 --- a/include/boost/redis/impl/ignore.ipp +++ b/include/boost/redis/impl/ignore.ipp @@ -6,7 +6,6 @@ #include -namespace boost::redis -{ +namespace boost::redis { ignore_t ignore; } diff --git a/include/boost/redis/impl/logger.ipp b/include/boost/redis/impl/logger.ipp index b6931cfa..7ed4c54e 100644 --- a/include/boost/redis/impl/logger.ipp +++ b/include/boost/redis/impl/logger.ipp @@ -5,12 +5,13 @@ */ #include + #include + #include #include -namespace boost::redis -{ +namespace boost::redis { void logger::write_prefix() { @@ -18,7 +19,9 @@ void logger::write_prefix() std::clog << prefix_; } -void logger::on_resolve(system::error_code const& ec, asio::ip::tcp::resolver::results_type const& res) +void logger::on_resolve( + system::error_code const& ec, + asio::ip::tcp::resolver::results_type const& res) { if (level_ < level::info) return; @@ -71,10 +74,7 @@ void logger::on_ssl_handshake(system::error_code const& ec) std::clog << "SSL handshake: " << ec.message() << std::endl; } -void -logger::on_write( - system::error_code const& ec, - std::string_view payload) +void logger::on_write(system::error_code const& ec, std::string_view payload) { if (level_ < level::info) return; @@ -104,10 +104,7 @@ void logger::on_read(system::error_code const& ec, std::size_t n) std::clog << std::endl; } -void -logger::on_hello( - system::error_code const& ec, - generic_response const& resp) +void logger::on_hello(system::error_code const& ec, generic_response const& resp) { if (level_ < level::info) return; @@ -145,4 +142,4 @@ void logger::trace(std::string_view op, system::error_code const& ec) std::clog << op << ": " << ec.message() << std::endl; } -} // boost::redis +} // namespace boost::redis diff --git a/include/boost/redis/impl/multiplexer.ipp b/include/boost/redis/impl/multiplexer.ipp index 2c510ec8..945222dd 100644 --- a/include/boost/redis/impl/multiplexer.ipp +++ b/include/boost/redis/impl/multiplexer.ipp @@ -9,8 +9,7 @@ #include -namespace boost::redis::detail -{ +namespace boost::redis::detail { multiplexer::elem::elem(request const& req, pipeline_adapter_type adapter) : req_{&req} @@ -20,8 +19,7 @@ multiplexer::elem::elem(request const& req, pipeline_adapter_type adapter) , ec_{{}} , read_size_{0} { - adapter_ = [this, adapter](resp3::node_view const& nd, system::error_code& ec) - { + adapter_ = [this, adapter](resp3::node_view const& nd, system::error_code& ec) { auto const i = req_->get_expected_responses() - remaining_responses_; adapter(i, nd, ec); }; @@ -75,17 +73,15 @@ void multiplexer::add(std::shared_ptr const& info) reqs_.push_back(info); if (info->get_request().has_hello_priority()) { - auto rend = - std::partition_point(std::rbegin(reqs_), std::rend(reqs_), [](auto const& e) { - return e->is_waiting(); + auto rend = std::partition_point(std::rbegin(reqs_), std::rend(reqs_), [](auto const& e) { + return e->is_waiting(); }); std::rotate(std::rbegin(reqs_), std::rbegin(reqs_) + 1, rend); } } -std::pair -multiplexer::commit_read(system::error_code& ec) +std::pair multiplexer::commit_read(system::error_code& ec) { // We arrive here in two states: // @@ -97,7 +93,7 @@ multiplexer::commit_read(system::error_code& ec) // 2. On a new message, in which case we have to determine // whether the next messag is a push or a response. // - if (!on_push_) // Prepare for new message. + if (!on_push_) // Prepare for new message. on_push_ = is_next_push(); if (on_push_) { @@ -111,7 +107,9 @@ multiplexer::commit_read(system::error_code& ec) return std::make_pair(std::make_optional(true), size); } - BOOST_ASSERT_MSG(is_waiting_response(), "Not waiting for a response (using MONITOR command perhaps?)"); + BOOST_ASSERT_MSG( + is_waiting_response(), + "Not waiting for a response (using MONITOR command perhaps?)"); BOOST_ASSERT(!reqs_.empty()); BOOST_ASSERT(reqs_.front() != nullptr); BOOST_ASSERT(reqs_.front()->get_remaining_responses() != 0); @@ -148,10 +146,12 @@ std::size_t multiplexer::prepare_write() { // Coalesces the requests and marks them staged. After a // successful write staged requests will be marked as written. - auto const point = - std::partition_point(std::cbegin(reqs_), std::cend(reqs_), [](auto const& ri) { + auto const point = std::partition_point( + std::cbegin(reqs_), + std::cend(reqs_), + [](auto const& ri) { return !ri->is_waiting(); - }); + }); std::for_each(point, std::cend(reqs_), [this](auto const& ri) { // Stage the request. @@ -166,11 +166,9 @@ std::size_t multiplexer::prepare_write() return static_cast(d); } - std::size_t multiplexer::cancel_waiting() { - auto f = [](auto const& ptr) - { + auto f = [](auto const& ptr) { BOOST_ASSERT(ptr != nullptr); return !ptr->is_waiting(); }; @@ -196,8 +194,7 @@ auto multiplexer::cancel_on_conn_lost() -> std::size_t } // Must return false if the request should be removed. - auto cond = [](auto const& ptr) - { + auto cond = [](auto const& ptr) { BOOST_ASSERT(ptr != nullptr); if (ptr->is_waiting()) { @@ -281,7 +278,7 @@ bool multiplexer::is_next_push() const noexcept std::size_t multiplexer::release_push_requests() { auto point = std::stable_partition(std::begin(reqs_), std::end(reqs_), [](auto const& ptr) { - return !(ptr->is_written() && ptr->get_request().get_expected_responses() == 0); + return !(ptr->is_written() && ptr->get_request().get_expected_responses() == 0); }); std::for_each(point, std::end(reqs_), [](auto const& ptr) { @@ -305,17 +302,12 @@ bool multiplexer::is_waiting_response() const noexcept return !reqs_.front()->is_waiting(); } -bool multiplexer::is_writing() const noexcept -{ - return !write_buffer_.empty(); -} +bool multiplexer::is_writing() const noexcept { return !write_buffer_.empty(); } -auto -make_elem( - request const& req, - multiplexer::pipeline_adapter_type adapter) -> std::shared_ptr +auto make_elem(request const& req, multiplexer::pipeline_adapter_type adapter) + -> std::shared_ptr { return std::make_shared(req, std::move(adapter)); } -} +} // namespace boost::redis::detail diff --git a/include/boost/redis/impl/request.ipp b/include/boost/redis/impl/request.ipp index d2c29f08..ada4adb8 100644 --- a/include/boost/redis/impl/request.ipp +++ b/include/boost/redis/impl/request.ipp @@ -12,10 +12,13 @@ namespace boost::redis::detail { auto has_response(std::string_view cmd) -> bool { - if (cmd == "SUBSCRIBE") return true; - if (cmd == "PSUBSCRIBE") return true; - if (cmd == "UNSUBSCRIBE") return true; + if (cmd == "SUBSCRIBE") + return true; + if (cmd == "PSUBSCRIBE") + return true; + if (cmd == "UNSUBSCRIBE") + return true; return false; } -} // boost:redis::detail +} // namespace boost::redis::detail diff --git a/include/boost/redis/impl/resp3_handshaker.ipp b/include/boost/redis/impl/resp3_handshaker.ipp index e18bf928..db516419 100644 --- a/include/boost/redis/impl/resp3_handshaker.ipp +++ b/include/boost/redis/impl/resp3_handshaker.ipp @@ -6,8 +6,7 @@ #include -namespace boost::redis::detail -{ +namespace boost::redis::detail { void push_hello(config const& cfg, request& req) { @@ -24,4 +23,4 @@ void push_hello(config const& cfg, request& req) req.push("SELECT", cfg.database_index.value()); } -} // boost::redis::detail +} // namespace boost::redis::detail diff --git a/include/boost/redis/impl/response.ipp b/include/boost/redis/impl/response.ipp index e2d687d8..a4b09a6e 100644 --- a/include/boost/redis/impl/response.ipp +++ b/include/boost/redis/impl/response.ipp @@ -4,20 +4,20 @@ * accompanying file LICENSE.txt) */ -#include #include +#include + #include -namespace boost::redis -{ +namespace boost::redis { void consume_one(generic_response& r, system::error_code& ec) { if (r.has_error()) - return; // Nothing to consume. + return; // Nothing to consume. if (std::empty(r.value())) - return; // Nothing to consume. + return; // Nothing to consume. auto const depth = r.value().front().depth; @@ -29,8 +29,9 @@ void consume_one(generic_response& r, system::error_code& ec) return; } - auto f = [depth](auto const& e) - { return e.depth == depth; }; + auto f = [depth](auto const& e) { + return e.depth == depth; + }; auto match = std::find_if(std::next(std::cbegin(r.value())), std::cend(r.value()), f); @@ -45,4 +46,4 @@ void consume_one(generic_response& r) throw system::system_error(ec); } -} // boost::redis::resp3 +} // namespace boost::redis diff --git a/include/boost/redis/logger.hpp b/include/boost/redis/logger.hpp index 79e55b50..a5503f1a 100644 --- a/include/boost/redis/logger.hpp +++ b/include/boost/redis/logger.hpp @@ -8,10 +8,14 @@ #define BOOST_REDIS_LOGGER_HPP #include + #include + #include -namespace boost::system {class error_code;} +namespace boost::system { +class error_code; +} namespace boost::redis { @@ -66,7 +70,7 @@ public: */ logger(level l = level::debug) : level_{l} - {} + { } /** @brief Called when the resolve operation completes. * @ingroup high-level-api @@ -120,10 +124,7 @@ public: * * @param prefix The prefix. */ - void set_prefix(std::string_view prefix) - { - prefix_ = prefix; - } + void set_prefix(std::string_view prefix) { prefix_ = prefix; } void trace(std::string_view message); void trace(std::string_view op, system::error_code const& ec); @@ -134,6 +135,6 @@ private: std::string_view prefix_; }; -} // boost::redis +} // namespace boost::redis -#endif // BOOST_REDIS_LOGGER_HPP +#endif // BOOST_REDIS_LOGGER_HPP diff --git a/include/boost/redis/operation.hpp b/include/boost/redis/operation.hpp index 233d1239..71b89162 100644 --- a/include/boost/redis/operation.hpp +++ b/include/boost/redis/operation.hpp @@ -15,7 +15,8 @@ namespace boost::redis { * The operations listed below can be passed to the * `boost::redis::connection::cancel` member function. */ -enum class operation { +enum class operation +{ /// Resolve operation. resolve, /// Connect operation. @@ -36,6 +37,6 @@ enum class operation { all, }; -} // boost::redis +} // namespace boost::redis -#endif // BOOST_REDIS_OPERATION_HPP +#endif // BOOST_REDIS_OPERATION_HPP diff --git a/include/boost/redis/request.hpp b/include/boost/redis/request.hpp index 2dcbf529..142467a5 100644 --- a/include/boost/redis/request.hpp +++ b/include/boost/redis/request.hpp @@ -7,19 +7,19 @@ #ifndef BOOST_REDIS_REQUEST_HPP #define BOOST_REDIS_REQUEST_HPP -#include #include +#include +#include #include #include -#include // NOTE: For some commands like hset it would be a good idea to assert // the value type is a pair. namespace boost::redis { -namespace detail{ +namespace detail { auto has_response(std::string_view cmd) -> bool; } @@ -80,23 +80,25 @@ public: * * \param cfg Configuration options. */ - explicit - request(config cfg = config{true, false, true, true}) - : cfg_{cfg} {} + explicit request(config cfg = config{true, false, true, true}) + : cfg_{cfg} + { } - //// Returns the number of responses expected for this request. + //// Returns the number of responses expected for this request. [[nodiscard]] auto get_expected_responses() const noexcept -> std::size_t - { return expected_responses_;}; + { + return expected_responses_; + }; - //// Returns the number of commands contained in this request. - [[nodiscard]] auto get_commands() const noexcept -> std::size_t - { return commands_;}; + //// Returns the number of commands contained in this request. + [[nodiscard]] auto get_commands() const noexcept -> std::size_t { return commands_; }; - [[nodiscard]] auto payload() const noexcept -> std::string_view - { return payload_;} + [[nodiscard]] auto payload() const noexcept -> std::string_view { return payload_; } [[nodiscard]] auto has_hello_priority() const noexcept -> auto const& - { return has_hello_priority_;} + { + return has_hello_priority_; + } /// Clears the request preserving allocated memory. void clear() @@ -108,14 +110,13 @@ public: } /// Calls std::string::reserve on the internal storage. - void reserve(std::size_t new_cap = 0) - { payload_.reserve(new_cap); } + void reserve(std::size_t new_cap = 0) { payload_.reserve(new_cap); } /// Returns a const reference to the config object. - [[nodiscard]] auto get_config() const noexcept -> auto const& {return cfg_; } + [[nodiscard]] auto get_config() const noexcept -> auto const& { return cfg_; } /// Returns a reference to the config object. - [[nodiscard]] auto get_config() noexcept -> auto& {return cfg_; } + [[nodiscard]] auto get_config() noexcept -> auto& { return cfg_; } /** @brief Appends a new command to the end of the request. * @@ -185,13 +186,12 @@ public: * See cpp20_serialization.cpp */ template - void - push_range( + void push_range( std::string_view const& cmd, std::string_view const& key, ForwardIterator begin, ForwardIterator end, - typename std::iterator_traits::value_type * = nullptr) + typename std::iterator_traits::value_type* = nullptr) { using value_type = typename std::iterator_traits::value_type; @@ -205,7 +205,7 @@ public: resp3::add_bulk(payload_, key); for (; begin != end; ++begin) - resp3::add_bulk(payload_, *begin); + resp3::add_bulk(payload_, *begin); check_cmd(cmd); } @@ -238,12 +238,11 @@ public: * See cpp20_serialization.cpp */ template - void - push_range( + void push_range( std::string_view const& cmd, ForwardIterator begin, ForwardIterator end, - typename std::iterator_traits::value_type * = nullptr) + typename std::iterator_traits::value_type* = nullptr) { using value_type = typename std::iterator_traits::value_type; @@ -256,7 +255,7 @@ public: resp3::add_bulk(payload_, cmd); for (; begin != end; ++begin) - resp3::add_bulk(payload_, *begin); + resp3::add_bulk(payload_, *begin); check_cmd(cmd); } @@ -272,12 +271,11 @@ public: * \tparam A type that can be passed to `std::cbegin()` and `std::cend()`. */ template - void - push_range( + void push_range( std::string_view const& cmd, std::string_view const& key, Range const& range, - decltype(std::begin(range)) * = nullptr) + decltype(std::begin(range))* = nullptr) { using std::begin; using std::end; @@ -294,11 +292,10 @@ public: * \tparam A type that can be passed to `std::cbegin()` and `std::cend()`. */ template - void - push_range( + void push_range( std::string_view cmd, Range const& range, - decltype(std::cbegin(range)) * = nullptr) + decltype(std::cbegin(range))* = nullptr) { using std::cbegin; using std::cend; @@ -324,6 +321,6 @@ private: bool has_hello_priority_ = false; }; -} // boost::redis::resp3 +} // namespace boost::redis -#endif // BOOST_REDIS_REQUEST_HPP +#endif // BOOST_REDIS_REQUEST_HPP diff --git a/include/boost/redis/resp3/impl/parser.ipp b/include/boost/redis/resp3/impl/parser.ipp index eda9991d..e5964bb9 100644 --- a/include/boost/redis/resp3/impl/parser.ipp +++ b/include/boost/redis/resp3/impl/parser.ipp @@ -4,8 +4,9 @@ * accompanying file LICENSE.txt) */ -#include #include +#include + #include #include @@ -20,10 +21,7 @@ void to_int(std::size_t& i, std::string_view sv, system::error_code& ec) ec = error::not_a_number; } -parser::parser() -{ - reset(); -} +parser::parser() { reset(); } void parser::reset() { @@ -32,11 +30,10 @@ void parser::reset() bulk_length_ = (std::numeric_limits::max)(); bulk_ = type::invalid; consumed_ = 0; - sizes_[0] = 2; // The sentinel must be more than 1. + sizes_[0] = 2; // The sentinel must be more than 1. } -std::size_t -parser::get_suggested_buffer_growth(std::size_t hint) const noexcept +std::size_t parser::get_suggested_buffer_growth(std::size_t hint) const noexcept { if (!bulk_expected()) return hint; @@ -47,20 +44,14 @@ parser::get_suggested_buffer_growth(std::size_t hint) const noexcept return hint; } -std::size_t -parser::get_consumed() const noexcept -{ - return consumed_; -} +std::size_t parser::get_consumed() const noexcept { return consumed_; } -bool -parser::done() const noexcept +bool parser::done() const noexcept { return depth_ == 0 && bulk_ == type::invalid && consumed_ != 0; } -void -parser::commit_elem() noexcept +void parser::commit_elem() noexcept { --sizes_[depth_]; while (sizes_[depth_] == 0) { @@ -69,15 +60,14 @@ parser::commit_elem() noexcept } } -auto -parser::consume(std::string_view view, system::error_code& ec) noexcept -> parser::result +auto parser::consume(std::string_view view, system::error_code& ec) noexcept -> parser::result { switch (bulk_) { case type::invalid: { auto const pos = view.find(sep, consumed_); if (pos == std::string::npos) - return {}; // Needs more data to proceeed. + return {}; // Needs more data to proceeed. auto const t = to_type(view.at(consumed_)); auto const content = view.substr(consumed_ + 1, pos - 1 - consumed_); @@ -88,14 +78,14 @@ parser::consume(std::string_view view, system::error_code& ec) noexcept -> parse consumed_ = pos + 2; if (!bulk_expected()) return ret; + } + [[fallthrough]]; - } [[fallthrough]]; - - default: // Handles bulk. + default: // Handles bulk. { auto const span = bulk_length_ + 2; if ((std::size(view) - consumed_) < span) - return {}; // Needs more data to proceeed. + return {}; // Needs more data to proceeed. auto const bulk_view = view.substr(consumed_, bulk_length_); node_type const ret = {bulk_, 1, depth_, bulk_view}; @@ -108,11 +98,8 @@ parser::consume(std::string_view view, system::error_code& ec) noexcept -> parse } } -auto -parser::consume_impl( - type t, - std::string_view elem, - system::error_code& ec) -> parser::node_type +auto parser::consume_impl(type t, std::string_view elem, system::error_code& ec) + -> parser::node_type { BOOST_ASSERT(!bulk_expected()); @@ -120,13 +107,13 @@ parser::consume_impl( switch (t) { case type::streamed_string_part: { - to_int(bulk_length_ , elem, ec); + to_int(bulk_length_, elem, ec); if (ec) return {}; if (bulk_length_ == 0) { ret = {type::streamed_string_part, 1, depth_, {}}; - sizes_[depth_] = 1; // We are done. + sizes_[depth_] = 1; // We are done. bulk_ = type::invalid; commit_elem(); } else { @@ -145,7 +132,7 @@ parser::consume_impl( sizes_[++depth_] = (std::numeric_limits::max)(); ret = {type::streamed_string, 0, depth_, {}}; } else { - to_int(bulk_length_ , elem , ec); + to_int(bulk_length_, elem, ec); if (ec) return {}; @@ -155,13 +142,13 @@ parser::consume_impl( case type::boolean: { if (std::empty(elem)) { - ec = error::empty_field; - return {}; + ec = error::empty_field; + return {}; } if (elem.at(0) != 'f' && elem.at(0) != 't') { - ec = error::unexpected_bool_value; - return {}; + ec = error::unexpected_bool_value; + return {}; } ret = {t, 1, depth_, elem}; @@ -172,10 +159,11 @@ parser::consume_impl( case type::number: { if (std::empty(elem)) { - ec = error::empty_field; - return {}; + ec = error::empty_field; + return {}; } - } [[fallthrough]]; + } + [[fallthrough]]; case type::simple_error: case type::simple_string: case type::null: @@ -217,4 +205,4 @@ parser::consume_impl( return ret; } -} // boost::redis::resp3 +} // namespace boost::redis::resp3 diff --git a/include/boost/redis/resp3/impl/serialization.ipp b/include/boost/redis/resp3/impl/serialization.ipp index 16ad124a..74e8c918 100644 --- a/include/boost/redis/resp3/impl/serialization.ipp +++ b/include/boost/redis/resp3/impl/serialization.ipp @@ -4,8 +4,8 @@ * accompanying file LICENSE.txt) */ -#include #include +#include namespace boost::redis::resp3 { @@ -35,8 +35,5 @@ void add_blob(std::string& payload, std::string_view blob) payload += parser::sep; } -void add_separator(std::string& payload) -{ - payload += parser::sep; -} -} // boost::redis::resp3 +void add_separator(std::string& payload) { payload += parser::sep; } +} // namespace boost::redis::resp3 diff --git a/include/boost/redis/resp3/impl/type.ipp b/include/boost/redis/resp3/impl/type.ipp index bfe33abc..983f69fe 100644 --- a/include/boost/redis/resp3/impl/type.ipp +++ b/include/boost/redis/resp3/impl/type.ipp @@ -5,6 +5,7 @@ */ #include + #include namespace boost::redis::resp3 { @@ -12,24 +13,24 @@ namespace boost::redis::resp3 { auto to_string(type t) noexcept -> char const* { switch (t) { - case type::array: return "array"; - case type::push: return "push"; - case type::set: return "set"; - case type::map: return "map"; - case type::attribute: return "attribute"; - case type::simple_string: return "simple_string"; - case type::simple_error: return "simple_error"; - case type::number: return "number"; - case type::doublean: return "doublean"; - case type::boolean: return "boolean"; - case type::big_number: return "big_number"; - case type::null: return "null"; - case type::blob_error: return "blob_error"; - case type::verbatim_string: return "verbatim_string"; - case type::blob_string: return "blob_string"; - case type::streamed_string: return "streamed_string"; + case type::array: return "array"; + case type::push: return "push"; + case type::set: return "set"; + case type::map: return "map"; + case type::attribute: return "attribute"; + case type::simple_string: return "simple_string"; + case type::simple_error: return "simple_error"; + case type::number: return "number"; + case type::doublean: return "doublean"; + case type::boolean: return "boolean"; + case type::big_number: return "big_number"; + case type::null: return "null"; + case type::blob_error: return "blob_error"; + case type::verbatim_string: return "verbatim_string"; + case type::blob_string: return "blob_string"; + case type::streamed_string: return "streamed_string"; case type::streamed_string_part: return "streamed_string_part"; - default: return "invalid"; + default: return "invalid"; } } @@ -39,4 +40,4 @@ auto operator<<(std::ostream& os, type t) -> std::ostream& return os; } -} // boost::redis::resp3 +} // namespace boost::redis::resp3 diff --git a/include/boost/redis/resp3/node.hpp b/include/boost/redis/resp3/node.hpp index 1275081d..f11083c2 100644 --- a/include/boost/redis/resp3/node.hpp +++ b/include/boost/redis/resp3/node.hpp @@ -48,10 +48,12 @@ struct basic_node { template auto operator==(basic_node const& a, basic_node const& b) { + // clang-format off return a.aggregate_size == b.aggregate_size && a.depth == b.depth && a.data_type == b.data_type && a.value == b.value; + // clang-format on }; /** @brief A node in the response tree that owns its data @@ -64,6 +66,6 @@ using node = basic_node; */ using node_view = basic_node; -} // boost::redis::resp3 +} // namespace boost::redis::resp3 -#endif // BOOST_REDIS_RESP3_NODE_HPP +#endif // BOOST_REDIS_RESP3_NODE_HPP diff --git a/include/boost/redis/resp3/parser.hpp b/include/boost/redis/resp3/parser.hpp index d40e239a..4a95a984 100644 --- a/include/boost/redis/resp3/parser.hpp +++ b/include/boost/redis/resp3/parser.hpp @@ -8,11 +8,13 @@ #define BOOST_REDIS_RESP3_PARSER_HPP #include + #include + #include -#include #include #include +#include namespace boost::redis::resp3 { @@ -54,7 +56,9 @@ private: // returns type::invalid. [[nodiscard]] auto bulk_expected() const noexcept -> bool - { return bulk_ != type::invalid; } + { + return bulk_ != type::invalid; + } public: parser(); @@ -76,12 +80,7 @@ public: // parser is either done or an error occured, that can be checked on // ec. template -bool -parse( - resp3::parser& p, - std::string_view const& msg, - Adapter& adapter, - system::error_code& ec) +bool parse(resp3::parser& p, std::string_view const& msg, Adapter& adapter, system::error_code& ec) { while (!p.done()) { auto const res = p.consume(msg, ec); @@ -99,6 +98,6 @@ parse( return true; } -} // boost::redis::resp3 +} // namespace boost::redis::resp3 -#endif // BOOST_REDIS_RESP3_PARSER_HPP +#endif // BOOST_REDIS_RESP3_PARSER_HPP diff --git a/include/boost/redis/resp3/serialization.hpp b/include/boost/redis/resp3/serialization.hpp index 0a0dc47f..de9733ee 100644 --- a/include/boost/redis/resp3/serialization.hpp +++ b/include/boost/redis/resp3/serialization.hpp @@ -7,10 +7,11 @@ #ifndef BOOST_REDIS_RESP3_SERIALIZATION_HPP #define BOOST_REDIS_RESP3_SERIALIZATION_HPP +#include #include + #include #include -#include #include #include @@ -57,12 +58,11 @@ struct add_bulk_impl { } }; -template +template struct add_bulk_impl> { static void add(std::string& payload, std::tuple const& t) { - auto f = [&](auto const&... vs) - { + auto f = [&](auto const&... vs) { using namespace boost::redis::resp3; (boost_redis_to_bulk(payload, vs), ...); }; @@ -94,19 +94,18 @@ struct bulk_counter; template struct bulk_counter { - static constexpr auto size = 1U; + static constexpr auto size = 1U; }; template struct bulk_counter> { - static constexpr auto size = 2U; + static constexpr auto size = 2U; }; void add_blob(std::string& payload, std::string_view blob); void add_separator(std::string& payload); -namespace detail -{ +namespace detail { template void deserialize(std::string_view const& data, Adapter adapter, system::error_code& ec) @@ -134,11 +133,11 @@ void deserialize(std::string_view const& data, Adapter adapter) deserialize(data, adapter, ec); if (ec) - BOOST_THROW_EXCEPTION(system::system_error{ec}); + BOOST_THROW_EXCEPTION(system::system_error{ec}); } -} +} // namespace detail -} // boost::redis::resp3 +} // namespace boost::redis::resp3 -#endif // BOOST_REDIS_RESP3_SERIALIZATION_HPP +#endif // BOOST_REDIS_RESP3_SERIALIZATION_HPP diff --git a/include/boost/redis/resp3/type.hpp b/include/boost/redis/resp3/type.hpp index 9f3e9015..873d993e 100644 --- a/include/boost/redis/resp3/type.hpp +++ b/include/boost/redis/resp3/type.hpp @@ -8,9 +8,10 @@ #define BOOST_REDIS_RESP3_TYPE_HPP #include + #include -#include #include +#include namespace boost::redis::resp3 { @@ -20,42 +21,42 @@ namespace boost::redis::resp3 { The RESP3 specification can be found at https://github.com/redis/redis-specifications/blob/master/protocol/RESP3.md. */ enum class type -{ /// Aggregate - array, - /// Aaggregate - push, - /// Aggregate - set, - /// Aggregate - map, - /// Aggregate - attribute, - /// Simple - simple_string, - /// Simple - simple_error, - /// Simple - number, - /// Simple - doublean, - /// Simple - boolean, - /// Simple - big_number, - /// Simple - null, - /// Simple - blob_error, - /// Simple - verbatim_string, - /// Simple - blob_string, - /// Simple - streamed_string, - /// Simple - streamed_string_part, - /// Invalid - invalid +{ /// Aggregate + array, + /// Aaggregate + push, + /// Aggregate + set, + /// Aggregate + map, + /// Aggregate + attribute, + /// Simple + simple_string, + /// Simple + simple_error, + /// Simple + number, + /// Simple + doublean, + /// Simple + boolean, + /// Simple + big_number, + /// Simple + null, + /// Simple + blob_error, + /// Simple + verbatim_string, + /// Simple + blob_string, + /// Simple + streamed_string, + /// Simple + streamed_string_part, + /// Invalid + invalid }; /** \brief Converts the data type to a string. @@ -81,7 +82,7 @@ constexpr auto is_aggregate(type t) noexcept -> bool case type::set: case type::map: case type::attribute: return true; - default: return false; + default: return false; } } @@ -92,7 +93,7 @@ constexpr auto element_multiplicity(type t) noexcept -> std::size_t switch (t) { case type::map: case type::attribute: return 2ULL; - default: return 1ULL; + default: return 1ULL; } } @@ -141,10 +142,10 @@ constexpr auto to_type(char c) noexcept -> type case '*': return type::array; case '|': return type::attribute; case '%': return type::map; - default: return type::invalid; + default: return type::invalid; } } -} // boost::redis::resp3 +} // namespace boost::redis::resp3 -#endif // BOOST_REDIS_RESP3_TYPE_HPP +#endif // BOOST_REDIS_RESP3_TYPE_HPP diff --git a/include/boost/redis/response.hpp b/include/boost/redis/response.hpp index 5af97a09..79e1d909 100644 --- a/include/boost/redis/response.hpp +++ b/include/boost/redis/response.hpp @@ -7,16 +7,16 @@ #ifndef BOOST_REDIS_RESPONSE_HPP #define BOOST_REDIS_RESPONSE_HPP -#include #include +#include + #include -#include #include #include +#include -namespace boost::redis -{ +namespace boost::redis { /** @brief Response with compile-time size. * @ingroup high-level-api @@ -75,6 +75,6 @@ void consume_one(generic_response& r, system::error_code& ec); /// Throwing overload of `consume_one`. void consume_one(generic_response& r); -} // boost::redis +} // namespace boost::redis -#endif // BOOST_REDIS_RESPONSE_HPP +#endif // BOOST_REDIS_RESPONSE_HPP diff --git a/include/boost/redis/src.hpp b/include/boost/redis/src.hpp index 82455ab4..a83af6bc 100644 --- a/include/boost/redis/src.hpp +++ b/include/boost/redis/src.hpp @@ -4,14 +4,14 @@ * accompanying file LICENSE.txt) */ -#include -#include -#include -#include #include -#include -#include +#include +#include +#include #include -#include +#include +#include +#include #include #include +#include diff --git a/include/boost/redis/usage.hpp b/include/boost/redis/usage.hpp index 4f50c2b1..e6dd03a3 100644 --- a/include/boost/redis/usage.hpp +++ b/include/boost/redis/usage.hpp @@ -7,8 +7,7 @@ #ifndef BOOST_REDIS_USAGE_HPP #define BOOST_REDIS_USAGE_HPP -namespace boost::redis -{ +namespace boost::redis { /** @brief Connection usage information. * @ingroup high-level-api @@ -38,6 +37,6 @@ struct usage { std::size_t push_bytes_received = 0; }; -} // boost::redis +} // namespace boost::redis -#endif // BOOST_REDIS_USAGE_HPP +#endif // BOOST_REDIS_USAGE_HPP diff --git a/test/cmake_b2_test/main.cpp b/test/cmake_b2_test/main.cpp index 98df4b07..1a73453b 100644 --- a/test/cmake_b2_test/main.cpp +++ b/test/cmake_b2_test/main.cpp @@ -4,12 +4,11 @@ * accompanying file LICENSE.txt) */ - #include #include int main() { - boost::redis::connection conn(boost::asio::system_executor{}); - return static_cast(!conn.will_reconnect()); + boost::redis::connection conn(boost::asio::system_executor{}); + return static_cast(!conn.will_reconnect()); } diff --git a/test/cmake_install_test/main.cpp b/test/cmake_install_test/main.cpp index 98df4b07..1a73453b 100644 --- a/test/cmake_install_test/main.cpp +++ b/test/cmake_install_test/main.cpp @@ -4,12 +4,11 @@ * accompanying file LICENSE.txt) */ - #include #include int main() { - boost::redis::connection conn(boost::asio::system_executor{}); - return static_cast(!conn.will_reconnect()); + boost::redis::connection conn(boost::asio::system_executor{}); + return static_cast(!conn.will_reconnect()); } diff --git a/test/cmake_subdir_test/main.cpp b/test/cmake_subdir_test/main.cpp index 98df4b07..1a73453b 100644 --- a/test/cmake_subdir_test/main.cpp +++ b/test/cmake_subdir_test/main.cpp @@ -4,12 +4,11 @@ * accompanying file LICENSE.txt) */ - #include #include int main() { - boost::redis::connection conn(boost::asio::system_executor{}); - return static_cast(!conn.will_reconnect()); + boost::redis::connection conn(boost::asio::system_executor{}); + return static_cast(!conn.will_reconnect()); } diff --git a/test/common.cpp b/test/common.cpp index 8f56d207..53399c8e 100644 --- a/test/common.cpp +++ b/test/common.cpp @@ -1,8 +1,10 @@ -#include "common.hpp" -#include -#include -#include #include +#include + +#include "common.hpp" + +#include +#include namespace net = boost::asio; @@ -18,8 +20,7 @@ struct run_callback { } }; -void -run( +void run( std::shared_ptr conn, boost::redis::config cfg, boost::system::error_code ec, @@ -31,22 +32,19 @@ run( static std::string safe_getenv(const char* name, const char* default_value) { - // MSVC doesn't like getenv + // MSVC doesn't like getenv #ifdef BOOST_MSVC #pragma warning(push) #pragma warning(disable : 4996) #endif - const char* res = std::getenv(name); + const char* res = std::getenv(name); #ifdef BOOST_MSVC #pragma warning(pop) #endif - return res ? res : default_value; + return res ? res : default_value; } -std::string get_server_hostname() -{ - return safe_getenv("BOOST_REDIS_TEST_SERVER", "localhost"); -} +std::string get_server_hostname() { return safe_getenv("BOOST_REDIS_TEST_SERVER", "localhost"); } boost::redis::config make_test_config() { @@ -75,4 +73,4 @@ auto start(net::awaitable op) -> int return 1; } -#endif // BOOST_ASIO_HAS_CO_AWAIT +#endif // BOOST_ASIO_HAS_CO_AWAIT diff --git a/test/common.hpp b/test/common.hpp index 83378d5c..46d46aa1 100644 --- a/test/common.hpp +++ b/test/common.hpp @@ -1,28 +1,29 @@ #pragma once -#include -#include -#include -#include #include #include + +#include +#include +#include +#include + #include #ifdef BOOST_ASIO_HAS_CO_AWAIT -inline -auto redir(boost::system::error_code& ec) - { return boost::asio::redirect_error(boost::asio::use_awaitable, ec); } +inline auto redir(boost::system::error_code& ec) +{ + return boost::asio::redirect_error(boost::asio::use_awaitable, ec); +} auto start(boost::asio::awaitable op) -> int; -#endif // BOOST_ASIO_HAS_CO_AWAIT +#endif // BOOST_ASIO_HAS_CO_AWAIT boost::redis::config make_test_config(); std::string get_server_hostname(); -void -run( +void run( std::shared_ptr conn, boost::redis::config cfg = make_test_config(), boost::system::error_code ec = boost::asio::error::operation_aborted, boost::redis::operation op = boost::redis::operation::receive, boost::redis::logger::level l = boost::redis::logger::level::debug); - diff --git a/test/test_any_adapter.cpp b/test/test_any_adapter.cpp index 12e5d44d..b360b744 100644 --- a/test/test_any_adapter.cpp +++ b/test/test_any_adapter.cpp @@ -4,9 +4,10 @@ * accompanying file LICENSE.txt) */ +#include #include #include -#include + #include #define BOOST_TEST_MODULE any_adapter #include @@ -33,17 +34,17 @@ BOOST_AUTO_TEST_CASE(any_adapter_copy_move) { // any_adapter can be copied/moved response r; - any_adapter ad1 {r}; + any_adapter ad1{r}; // copy constructor - any_adapter ad2 {ad1}; - + any_adapter ad2{ad1}; + // move constructor - any_adapter ad3 {std::move(ad2)}; + any_adapter ad3{std::move(ad2)}; // copy assignment BOOST_CHECK_NO_THROW(ad2 = ad1); // move assignment - BOOST_CHECK_NO_THROW(ad2 = std::move(ad1)); + BOOST_CHECK_NO_THROW(ad2 = std::move(ad1)); } diff --git a/test/test_conn_check_health.cpp b/test/test_conn_check_health.cpp index e8d07100..65347877 100644 --- a/test/test_conn_check_health.cpp +++ b/test/test_conn_check_health.cpp @@ -6,12 +6,15 @@ #include #include + #include -#define BOOST_TEST_MODULE check-health +#define BOOST_TEST_MODULE check_health #include + +#include "common.hpp" + #include #include -#include "common.hpp" namespace net = boost::asio; namespace redis = boost::redis; @@ -23,7 +26,7 @@ using boost::redis::operation; using boost::redis::generic_response; using boost::redis::consume_one; -// TODO: Test cancel(health_check) +// TODO: Test cancel(health_check) struct push_callback { connection* conn1; @@ -35,7 +38,7 @@ struct push_callback { void operator()(error_code ec = {}, std::size_t = 0) { - BOOST_ASIO_CORO_REENTER (coro) for (;;) + BOOST_ASIO_CORO_REENTER(coro) for (;;) { BOOST_ASIO_CORO_YIELD conn2->async_receive(*this); @@ -94,7 +97,7 @@ BOOST_AUTO_TEST_CASE(check_health) auto cfg2 = make_test_config(); cfg2.health_check_id = "conn2"; error_code res2; - conn2.async_run(cfg2, {}, [&](auto ec){ + conn2.async_run(cfg2, {}, [&](auto ec) { std::cout << "async_run 2 completed: " << ec.message() << std::endl; res2 = ec; }); @@ -110,8 +113,8 @@ BOOST_AUTO_TEST_CASE(check_health) }); //-------------------------------- - - push_callback{&conn1, &conn2, &resp2, &req1}(); // Starts reading pushes. + + push_callback{&conn1, &conn2, &resp2, &req1}(); // Starts reading pushes. ioc.run(); @@ -122,4 +125,3 @@ BOOST_AUTO_TEST_CASE(check_health) // to fail. std::this_thread::sleep_for(std::chrono::seconds{10}); } - diff --git a/test/test_conn_echo_stress.cpp b/test/test_conn_echo_stress.cpp index de46a8cc..7cb341e5 100644 --- a/test/test_conn_echo_stress.cpp +++ b/test/test_conn_echo_stress.cpp @@ -5,15 +5,18 @@ */ #include + #include -#include #include +#include #include -#define BOOST_TEST_MODULE echo-stress +#define BOOST_TEST_MODULE echo_stress #include -#include + #include "common.hpp" +#include + #ifdef BOOST_ASIO_HAS_CO_AWAIT namespace net = boost::asio; @@ -30,8 +33,7 @@ using boost::redis::error; std::ostream& operator<<(std::ostream& os, usage const& u) { - os - << "Commands sent: " << u.commands_sent << "\n" + os << "Commands sent: " << u.commands_sent << "\n" << "Bytes sent: " << u.bytes_sent << "\n" << "Responses received: " << u.responses_received << "\n" << "Pushes received: " << u.pushes_received << "\n" @@ -65,11 +67,8 @@ auto push_consumer(std::shared_ptr conn, int expected) -> net::await conn->cancel(); } -auto -echo_session( - std::shared_ptr conn, - std::shared_ptr pubs, - int n) -> net::awaitable +auto echo_session(std::shared_ptr conn, std::shared_ptr pubs, int n) + -> net::awaitable { for (auto i = 0; i < n; ++i) co_await conn->async_exec(*pubs, ignore, net::deferred); @@ -80,10 +79,12 @@ auto async_echo_stress(std::shared_ptr conn) -> net::awaitable auto ex = co_await net::this_coro::executor; auto cfg = make_test_config(); cfg.health_check_interval = std::chrono::seconds::zero(); - run(conn, cfg, - boost::asio::error::operation_aborted, - boost::redis::operation::receive, - boost::redis::logger::level::crit); + run( + conn, + cfg, + boost::asio::error::operation_aborted, + boost::redis::operation::receive, + boost::redis::logger::level::crit); request req; req.push("SUBSCRIBE", "channel"); @@ -112,7 +113,7 @@ auto async_echo_stress(std::shared_ptr conn) -> net::awaitable // pushes have been received. net::co_spawn(ex, push_consumer(conn, total_pushes), net::detached); - for (int i = 0; i < sessions; ++i) + for (int i = 0; i < sessions; ++i) net::co_spawn(ex, echo_session(conn, pubs, msgs), net::detached); } @@ -123,15 +124,9 @@ BOOST_AUTO_TEST_CASE(echo_stress) net::co_spawn(ioc, async_echo_stress(conn), net::detached); ioc.run(); - std::cout - << "-------------------\n" - << conn->get_usage() - << std::endl; + std::cout << "-------------------\n" << conn->get_usage() << std::endl; } #else -BOOST_AUTO_TEST_CASE(dummy) -{ - BOOST_TEST(true); -} +BOOST_AUTO_TEST_CASE(dummy) { BOOST_TEST(true); } #endif diff --git a/test/test_conn_exec.cpp b/test/test_conn_exec.cpp index 0ac561cd..f81fcd58 100644 --- a/test/test_conn_exec.cpp +++ b/test/test_conn_exec.cpp @@ -6,14 +6,18 @@ #include #include -#include + #include +#include + #include -#define BOOST_TEST_MODULE conn-exec +#define BOOST_TEST_MODULE conn_exec #include -#include + #include "common.hpp" +#include + // TODO: Test whether HELLO won't be inserted passt commands that have // been already writen. // TODO: Test async_exec with empty request e.g. hgetall with an empty @@ -53,7 +57,7 @@ BOOST_AUTO_TEST_CASE(hello_priority) bool seen2 = false; bool seen3 = false; - conn->async_exec(req1, ignore, [&](auto ec, auto){ + conn->async_exec(req1, ignore, [&](auto ec, auto) { // Second callback to the called. std::cout << "req1" << std::endl; BOOST_TEST(!ec); @@ -62,7 +66,7 @@ BOOST_AUTO_TEST_CASE(hello_priority) seen1 = true; }); - conn->async_exec(req2, ignore, [&](auto ec, auto){ + conn->async_exec(req2, ignore, [&](auto ec, auto) { // Last callback to the called. std::cout << "req2" << std::endl; BOOST_TEST(!ec); @@ -73,7 +77,7 @@ BOOST_AUTO_TEST_CASE(hello_priority) conn->cancel(operation::reconnection); }); - conn->async_exec(req3, ignore, [&](auto ec, auto){ + conn->async_exec(req3, ignore, [&](auto ec, auto) { // Callback that will be called first. std::cout << "req3" << std::endl; BOOST_TEST(!ec); @@ -98,7 +102,7 @@ BOOST_AUTO_TEST_CASE(wrong_response_data_type) auto conn = std::make_shared(ioc); - conn->async_exec(req, resp, [conn](auto ec, auto){ + conn->async_exec(req, resp, [conn](auto ec, auto) { BOOST_CHECK_EQUAL(ec, boost::redis::error::not_a_number); conn->cancel(operation::reconnection); }); @@ -115,7 +119,7 @@ BOOST_AUTO_TEST_CASE(cancel_request_if_not_connected) net::io_context ioc; auto conn = std::make_shared(ioc); - conn->async_exec(req, ignore, [conn](auto ec, auto){ + conn->async_exec(req, ignore, [conn](auto ec, auto) { BOOST_CHECK_EQUAL(ec, boost::redis::error::not_connected); conn->cancel(); }); @@ -137,14 +141,14 @@ BOOST_AUTO_TEST_CASE(correct_database) generic_response resp; - conn->async_exec(req, resp, [&](auto ec, auto n){ - BOOST_TEST(!ec); - std::clog << "async_exec has completed: " << n << std::endl; - conn->cancel(); + conn->async_exec(req, resp, [&](auto ec, auto n) { + BOOST_TEST(!ec); + std::clog << "async_exec has completed: " << n << std::endl; + conn->cancel(); }); - conn->async_run(cfg, {}, [](auto){ - std::clog << "async_run has exited." << std::endl; + conn->async_run(cfg, {}, [](auto) { + std::clog << "async_run has exited." << std::endl; }); ioc.run(); @@ -156,7 +160,7 @@ BOOST_AUTO_TEST_CASE(correct_database) auto const index = std::stoi(index_str); // This check might fail if more than one client is connected to - // redis when the CLIENT LIST command is run. + // redis when the CLIENT LIST command is run. BOOST_CHECK_EQUAL(cfg.database_index.value(), index); } @@ -204,7 +208,7 @@ BOOST_AUTO_TEST_CASE(exec_any_adapter) auto conn = std::make_shared(ioc); - conn->async_exec(req, boost::redis::any_adapter(res), [&](auto ec, auto){ + conn->async_exec(req, boost::redis::any_adapter(res), [&](auto ec, auto) { BOOST_TEST(!ec); conn->cancel(); }); @@ -214,4 +218,3 @@ BOOST_AUTO_TEST_CASE(exec_any_adapter) BOOST_TEST(std::get<0>(res).value() == "PONG"); } - diff --git a/test/test_conn_exec_cancel.cpp b/test/test_conn_exec_cancel.cpp index a890f762..99173f4e 100644 --- a/test/test_conn_exec_cancel.cpp +++ b/test/test_conn_exec_cancel.cpp @@ -5,11 +5,14 @@ */ #include + #include -#define BOOST_TEST_MODULE conn-exec-cancel -#include +#define BOOST_TEST_MODULE conn_exec_cancel #include +#include + #include "common.hpp" + #include #ifdef BOOST_ASIO_HAS_CO_AWAIT @@ -57,10 +60,7 @@ auto implicit_cancel_of_req_written() -> net::awaitable // 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)) - ); + co_await (conn->async_exec(req1, ignore, redir(ec1)) || st.async_wait(redir(ec2))); conn->cancel(); @@ -92,13 +92,11 @@ BOOST_AUTO_TEST_CASE(test_cancel_of_req_written_on_run_canceled) req1.get_config().cancel_if_unresponded = true; req1.push("BLPOP", "any", 0); - auto c1 = [&](auto ec, auto) - { + auto c1 = [&](auto ec, auto) { BOOST_CHECK_EQUAL(ec, net::error::operation_aborted); }; - auto c0 = [&](auto ec, auto) - { + auto c0 = [&](auto ec, auto) { BOOST_TEST(!ec); conn->async_exec(req1, ignore, c1); }; @@ -111,7 +109,7 @@ BOOST_AUTO_TEST_CASE(test_cancel_of_req_written_on_run_canceled) net::steady_timer st{ioc}; st.expires_after(std::chrono::seconds{1}); - st.async_wait([&](auto ec){ + st.async_wait([&](auto ec) { BOOST_TEST(!ec); conn->cancel(operation::run); conn->cancel(operation::reconnection); @@ -121,8 +119,5 @@ BOOST_AUTO_TEST_CASE(test_cancel_of_req_written_on_run_canceled) } #else -BOOST_AUTO_TEST_CASE(dummy) -{ - BOOST_TEST(true); -} +BOOST_AUTO_TEST_CASE(dummy) { BOOST_TEST(true); } #endif diff --git a/test/test_conn_exec_cancel2.cpp b/test/test_conn_exec_cancel2.cpp index f7ed3395..ba464443 100644 --- a/test/test_conn_exec_cancel2.cpp +++ b/test/test_conn_exec_cancel2.cpp @@ -5,11 +5,14 @@ */ #include + #include -#define BOOST_TEST_MODULE conn-exec-cancel -#include +#define BOOST_TEST_MODULE conn_exec_cancel #include +#include + #include "common.hpp" + #include #ifdef BOOST_ASIO_HAS_CO_AWAIT @@ -55,7 +58,7 @@ auto async_ignore_explicit_cancel_of_req_written() -> net::awaitable req1.push("BLPOP", "any", 3); bool seen = false; - conn->async_exec(req1, gresp, [&](auto ec, auto) mutable{ + conn->async_exec(req1, gresp, [&](auto ec, auto) mutable { // No error should occur since the cancelation should be // ignored. std::cout << "async_exec (1): " << ec.message() << std::endl; @@ -90,8 +93,5 @@ BOOST_AUTO_TEST_CASE(test_ignore_explicit_cancel_of_req_written) } #else -BOOST_AUTO_TEST_CASE(dummy) -{ - BOOST_TEST(true); -} +BOOST_AUTO_TEST_CASE(dummy) { BOOST_TEST(true); } #endif diff --git a/test/test_conn_exec_error.cpp b/test/test_conn_exec_error.cpp index 739301b3..96a3214e 100644 --- a/test/test_conn_exec_error.cpp +++ b/test/test_conn_exec_error.cpp @@ -6,10 +6,13 @@ #include #include + #include -#define BOOST_TEST_MODULE conn-exec-error +#define BOOST_TEST_MODULE conn_exec_error #include + #include "common.hpp" + #include namespace net = boost::asio; @@ -38,7 +41,7 @@ BOOST_AUTO_TEST_CASE(no_ignore_error) auto conn = std::make_shared(ioc); - conn->async_exec(req, ignore, [&](auto ec, auto){ + conn->async_exec(req, ignore, [&](auto ec, auto) { BOOST_CHECK_EQUAL(ec, error::resp3_simple_error); conn->cancel(operation::run); conn->cancel(operation::reconnection); @@ -65,7 +68,7 @@ BOOST_AUTO_TEST_CASE(has_diagnostic) auto conn = std::make_shared(ioc); response resp; - conn->async_exec(req, resp, [&](auto ec, auto){ + conn->async_exec(req, resp, [&](auto ec, auto) { BOOST_TEST(!ec); // HELLO @@ -93,8 +96,8 @@ 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. + req1.push("PING", "req1-msg2", "extra arg"); // Error. + req1.push("PING", "req1-msg3"); // Should run ok. response resp1; @@ -106,8 +109,7 @@ BOOST_AUTO_TEST_CASE(resp3_error_in_cmd_pipeline) net::io_context ioc; auto conn = std::make_shared(ioc); - auto c2 = [&](auto ec, auto) - { + auto c2 = [&](auto ec, auto) { BOOST_TEST(!ec); BOOST_TEST(std::get<0>(resp2).has_value()); BOOST_CHECK_EQUAL(std::get<0>(resp2).value(), "req2-msg1"); @@ -115,8 +117,7 @@ BOOST_AUTO_TEST_CASE(resp3_error_in_cmd_pipeline) conn->cancel(operation::reconnection); }; - auto c1 = [&](auto ec, auto) - { + auto c1 = [&](auto ec, auto) { BOOST_TEST(!ec); BOOST_TEST(std::get<2>(resp1).has_error()); BOOST_CHECK_EQUAL(std::get<2>(resp1).error().data_type, resp3::type::simple_error); @@ -142,29 +143,29 @@ BOOST_AUTO_TEST_CASE(error_in_transaction) req.push("HELLO", 3); req.push("MULTI"); req.push("PING"); - req.push("PING", "msg2", "error"); // Error. + 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, // exec - std::string // ping - > resp; - + ignore_t, // hello + ignore_t, // multi + ignore_t, // ping + ignore_t, // ping + ignore_t, // ping + response, // exec + std::string // ping + > + resp; net::io_context ioc; auto conn = std::make_shared(ioc); - conn->async_exec(req, resp, [&](auto ec, auto){ + conn->async_exec(req, resp, [&](auto ec, auto) { BOOST_TEST(!ec); - + BOOST_TEST(std::get<0>(resp).has_value()); BOOST_TEST(std::get<1>(resp).has_value()); BOOST_TEST(std::get<2>(resp).has_value()); @@ -178,7 +179,9 @@ BOOST_AUTO_TEST_CASE(error_in_transaction) // The ping in the transaction that should be an error. BOOST_TEST(std::get<1>(std::get<5>(resp).value()).has_error()); - BOOST_CHECK_EQUAL(std::get<1>(std::get<5>(resp).value()).error().data_type, resp3::type::simple_error); + BOOST_CHECK_EQUAL( + 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)); @@ -220,19 +223,17 @@ BOOST_AUTO_TEST_CASE(subscriber_wrong_syntax) req1.push("PING"); request req2; - req2.push("SUBSCRIBE"); // Wrong command synthax. + req2.push("SUBSCRIBE"); // Wrong command synthax. net::io_context ioc; auto conn = std::make_shared(ioc); - auto c2 = [&](auto ec, auto) - { + auto c2 = [&](auto ec, auto) { std::cout << "async_exec: subscribe" << std::endl; BOOST_TEST(!ec); }; - auto c1 = [&](auto ec, auto) - { + auto c1 = [&](auto ec, auto) { std::cout << "async_exec: hello" << std::endl; BOOST_TEST(!ec); conn->async_exec(req2, ignore, c2); @@ -243,8 +244,7 @@ BOOST_AUTO_TEST_CASE(subscriber_wrong_syntax) generic_response gresp; conn->set_receive_response(gresp); - auto c3 = [&](auto ec, auto) - { + auto c3 = [&](auto ec, auto) { std::cout << "async_receive" << std::endl; BOOST_TEST(!ec); BOOST_TEST(gresp.has_error()); @@ -261,4 +261,3 @@ BOOST_AUTO_TEST_CASE(subscriber_wrong_syntax) ioc.run(); } - diff --git a/test/test_conn_exec_retry.cpp b/test/test_conn_exec_retry.cpp index 9ebde10f..10708d66 100644 --- a/test/test_conn_exec_retry.cpp +++ b/test/test_conn_exec_retry.cpp @@ -5,14 +5,17 @@ */ #include + #include #include -#define BOOST_TEST_MODULE conn-exec-retry +#define BOOST_TEST_MODULE conn_exec_retry #include -#include + #include "common.hpp" +#include + namespace net = boost::asio; using error_code = boost::system::error_code; using connection = boost::redis::connection; @@ -44,7 +47,7 @@ BOOST_AUTO_TEST_CASE(request_retry_false) net::steady_timer st{ioc}; st.expires_after(std::chrono::seconds{1}); - st.async_wait([&](auto){ + st.async_wait([&](auto) { // Cancels the request before receiving the response. This // should cause the third request to complete with error // although it has cancel_on_connection_lost = false. The reason @@ -55,17 +58,17 @@ BOOST_AUTO_TEST_CASE(request_retry_false) std::cout << "async_wait" << std::endl; }); - auto c2 = [&](auto ec, auto){ + auto c2 = [&](auto ec, auto) { std::cout << "c2" << std::endl; BOOST_CHECK_EQUAL(ec, boost::asio::error::operation_aborted); }; - auto c1 = [&](auto ec, auto){ + auto c1 = [&](auto ec, auto) { std::cout << "c1" << std::endl; BOOST_CHECK_EQUAL(ec, boost::asio::error::operation_aborted); }; - auto c0 = [&](auto ec, auto){ + auto c0 = [&](auto ec, auto) { std::cout << "c0" << std::endl; BOOST_TEST(!ec); conn->async_exec(req1, ignore, c1); @@ -75,13 +78,13 @@ BOOST_AUTO_TEST_CASE(request_retry_false) conn->async_exec(req0, ignore, c0); auto cfg = make_test_config(); - conn->async_run(cfg, {boost::redis::logger::level::debug}, - [&](boost::system::error_code const& ec) - { + conn->async_run( + cfg, + {boost::redis::logger::level::debug}, + [&](boost::system::error_code const& ec) { std::cout << "async_run: " << ec.message() << std::endl; conn->cancel(); - } - ); + }); ioc.run(); } @@ -111,7 +114,7 @@ BOOST_AUTO_TEST_CASE(request_retry_true) net::steady_timer st{ioc}; st.expires_after(std::chrono::seconds{1}); - st.async_wait([&](auto){ + st.async_wait([&](auto) { // Cancels the request before receiving the response. This // should cause the third request to not complete with error // since it has cancel_if_unresponded = true and cancellation @@ -119,22 +122,22 @@ BOOST_AUTO_TEST_CASE(request_retry_true) conn->cancel(operation::run); }); - auto c3 = [&](auto ec, auto){ + auto c3 = [&](auto ec, auto) { std::cout << "c3: " << ec.message() << std::endl; BOOST_TEST(!ec); conn->cancel(); }; - auto c2 = [&](auto ec, auto){ + auto c2 = [&](auto ec, auto) { BOOST_TEST(!ec); conn->async_exec(req3, ignore, c3); }; - auto c1 = [](auto ec, auto){ + auto c1 = [](auto ec, auto) { BOOST_CHECK_EQUAL(ec, boost::system::errc::errc_t::operation_canceled); }; - auto c0 = [&](auto ec, auto){ + auto c0 = [&](auto ec, auto) { BOOST_TEST(!ec); conn->async_exec(req1, ignore, c1); conn->async_exec(req2, ignore, c2); @@ -144,7 +147,7 @@ BOOST_AUTO_TEST_CASE(request_retry_true) auto cfg = make_test_config(); cfg.health_check_interval = 5s; - conn->async_run(cfg, {}, [&](auto ec){ + conn->async_run(cfg, {}, [&](auto ec) { std::cout << ec.message() << std::endl; BOOST_TEST(!!ec); }); diff --git a/test/test_conn_push.cpp b/test/test_conn_push.cpp index 2cc13ab4..397184d2 100644 --- a/test/test_conn_push.cpp +++ b/test/test_conn_push.cpp @@ -6,15 +6,18 @@ #include #include -#include -#include -#include + #include -#define BOOST_TEST_MODULE conn-push +#include +#include +#include +#define BOOST_TEST_MODULE conn - push #include -#include + #include "common.hpp" +#include + namespace net = boost::asio; namespace redis = boost::redis; @@ -47,19 +50,16 @@ BOOST_AUTO_TEST_CASE(receives_push_waiting_resps) auto conn = std::make_shared(ioc); - auto c3 =[](auto ec, auto...) - { + auto c3 = [](auto ec, auto...) { std::cout << "c3: " << ec.message() << std::endl; }; - auto c2 =[&, conn](auto ec, auto...) - { + auto c2 = [&, conn](auto ec, auto...) { BOOST_TEST(!ec); conn->async_exec(req3, ignore, c3); }; - auto c1 =[&, conn](auto ec, auto...) - { + auto c1 = [&, conn](auto ec, auto...) { BOOST_TEST(!ec); conn->async_exec(req2, ignore, c2); }; @@ -69,7 +69,7 @@ BOOST_AUTO_TEST_CASE(receives_push_waiting_resps) run(conn, make_test_config(), {}); bool push_received = false; - conn->async_receive([&, conn](auto ec, auto){ + conn->async_receive([&, conn](auto ec, auto) { std::cout << "async_receive" << std::endl; BOOST_TEST(!ec); push_received = true; @@ -94,13 +94,13 @@ BOOST_AUTO_TEST_CASE(push_received1) req.push("SUBSCRIBE", "channel1"); req.push("SUBSCRIBE", "channel2"); - conn->async_exec(req, ignore, [conn](auto ec, auto){ + conn->async_exec(req, ignore, [conn](auto ec, auto) { std::cout << "async_exec" << std::endl; BOOST_TEST(!ec); }); bool push_async_received = false; - conn->async_receive([&, conn](auto ec, auto){ + conn->async_receive([&, conn](auto ec, auto) { std::cout << "(1) async_receive" << std::endl; BOOST_TEST(!ec); @@ -116,7 +116,9 @@ BOOST_AUTO_TEST_CASE(push_received1) // Tries to receive a third push synchronously. ec2 = {}; res = conn->receive(ec2); - BOOST_CHECK_EQUAL(ec2, boost::redis::make_error_code(boost::redis::error::sync_receive_push_failed)); + BOOST_CHECK_EQUAL( + ec2, + boost::redis::make_error_code(boost::redis::error::sync_receive_push_failed)); conn->cancel(); }); @@ -139,11 +141,11 @@ BOOST_AUTO_TEST_CASE(push_filtered_out) req.push("QUIT"); response resp; - conn->async_exec(req, resp, [conn](auto ec, auto){ + conn->async_exec(req, resp, [conn](auto ec, auto) { BOOST_TEST(!ec); }); - conn->async_receive([&, conn](auto ec, auto){ + conn->async_receive([&, conn](auto ec, auto) { BOOST_TEST(!ec); conn->cancel(operation::reconnection); }); @@ -157,8 +159,7 @@ BOOST_AUTO_TEST_CASE(push_filtered_out) } #ifdef BOOST_ASIO_HAS_CO_AWAIT -net::awaitable -push_consumer1(std::shared_ptr conn, bool& push_received) +net::awaitable push_consumer1(std::shared_ptr conn, bool& push_received) { { auto [ec, ev] = co_await conn->async_receive(as_tuple(net::use_awaitable)); @@ -173,26 +174,26 @@ push_consumer1(std::shared_ptr conn, bool& push_received) push_received = true; } -struct response_error_tag{}; +struct response_error_tag { }; response_error_tag error_tag_obj; struct response_error_adapter { - void - operator()( - std::size_t, boost::redis::resp3::basic_node const&, boost::system::error_code& ec) + void operator()( + std::size_t, + boost::redis::resp3::basic_node const&, + boost::system::error_code& ec) { ec = boost::redis::error::incompatible_size; } [[nodiscard]] auto get_supported_response_size() const noexcept - { return static_cast(-1);} + { + return static_cast(-1); + } }; -auto boost_redis_adapt(response_error_tag&) -{ - return response_error_adapter{}; -} +auto boost_redis_adapt(response_error_tag&) { return response_error_adapter{}; } BOOST_AUTO_TEST_CASE(test_push_adapter) { @@ -212,12 +213,12 @@ BOOST_AUTO_TEST_CASE(test_push_adapter) conn->cancel(operation::reconnection); }); - conn->async_exec(req, ignore, [](auto ec, auto){ + conn->async_exec(req, ignore, [](auto ec, auto) { BOOST_CHECK_EQUAL(ec, boost::system::errc::errc_t::operation_canceled); }); auto cfg = make_test_config(); - conn->async_run(cfg, {}, [](auto ec){ + conn->async_run(cfg, {}, [](auto ec) { BOOST_CHECK_EQUAL(ec, redis::error::incompatible_size); }); @@ -255,68 +256,56 @@ BOOST_AUTO_TEST_CASE(many_subscribers) net::io_context ioc; auto conn = std::make_shared(ioc); - auto c11 =[&](auto ec, auto...) - { + auto c11 = [&](auto ec, auto...) { std::cout << "quit sent: " << ec.message() << std::endl; conn->cancel(operation::reconnection); }; - auto c10 =[&](auto ec, auto...) - { + auto c10 = [&](auto ec, auto...) { BOOST_TEST(!ec); conn->async_exec(req3, ignore, c11); }; - auto c9 =[&](auto ec, auto...) - { + auto c9 = [&](auto ec, auto...) { BOOST_TEST(!ec); conn->async_exec(req2, ignore, c10); }; - auto c8 =[&](auto ec, auto...) - { + auto c8 = [&](auto ec, auto...) { BOOST_TEST(!ec); - conn->async_exec(req1, ignore, c9); + conn->async_exec(req1, ignore, c9); }; - auto c7 =[&](auto ec, auto...) - { + auto c7 = [&](auto ec, auto...) { BOOST_TEST(!ec); - conn->async_exec(req2, ignore, c8); + conn->async_exec(req2, ignore, c8); }; - auto c6 =[&](auto ec, auto...) - { + auto c6 = [&](auto ec, auto...) { BOOST_TEST(!ec); - conn->async_exec(req2, ignore, c7); + conn->async_exec(req2, ignore, c7); }; - auto c5 =[&](auto ec, auto...) - { + auto c5 = [&](auto ec, auto...) { BOOST_TEST(!ec); - conn->async_exec(req1, ignore, c6); + conn->async_exec(req1, ignore, c6); }; - auto c4 =[&](auto ec, auto...) - { + auto c4 = [&](auto ec, auto...) { BOOST_TEST(!ec); - conn->async_exec(req2, ignore, c5); + conn->async_exec(req2, ignore, c5); }; - auto c3 =[&](auto ec, auto...) - { + auto c3 = [&](auto ec, auto...) { BOOST_TEST(!ec); - conn->async_exec(req1, ignore, c4); + conn->async_exec(req1, ignore, c4); }; - auto c2 =[&](auto ec, auto...) - { + auto c2 = [&](auto ec, auto...) { BOOST_TEST(!ec); - conn->async_exec(req2, ignore, c3); + conn->async_exec(req2, ignore, c3); }; - auto c1 =[&](auto ec, auto...) - { + auto c1 = [&](auto ec, auto...) { BOOST_TEST(!ec); - conn->async_exec(req2, ignore, c2); + conn->async_exec(req2, ignore, c2); }; - auto c0 =[&](auto ec, auto...) - { + auto c0 = [&](auto ec, auto...) { BOOST_TEST(!ec); - conn->async_exec(req1, ignore, c1); + conn->async_exec(req1, ignore, c1); }; - conn->async_exec(req0, ignore, c0); + conn->async_exec(req0, ignore, c0); run(conn, make_test_config(), {}); diff --git a/test/test_conn_quit.cpp b/test/test_conn_quit.cpp index 5e4759c2..ca345f57 100644 --- a/test/test_conn_quit.cpp +++ b/test/test_conn_quit.cpp @@ -5,12 +5,15 @@ */ #include + #include -#define BOOST_TEST_MODULE conn-quit +#define BOOST_TEST_MODULE conn_quit #include -#include + #include "common.hpp" +#include + namespace net = boost::asio; using boost::redis::connection; using boost::system::error_code; @@ -40,21 +43,18 @@ BOOST_AUTO_TEST_CASE(test_async_run_exits) req3.get_config().cancel_if_not_connected = true; req3.push("PING"); - auto c3 = [](auto ec, auto) - { + auto c3 = [](auto ec, auto) { std::clog << "c3: " << ec.message() << std::endl; BOOST_CHECK_EQUAL(ec, boost::asio::error::operation_aborted); }; - auto c2 = [&](auto ec, auto) - { + auto c2 = [&](auto ec, auto) { std::clog << "c2: " << ec.message() << std::endl; BOOST_TEST(!ec); conn->async_exec(req3, ignore, c3); }; - auto c1 = [&](auto ec, auto) - { + auto c1 = [&](auto ec, auto) { std::cout << "c1: " << ec.message() << std::endl; BOOST_TEST(!ec); conn->async_exec(req2, ignore, c2); @@ -71,4 +71,3 @@ BOOST_AUTO_TEST_CASE(test_async_run_exits) ioc.run(); } - diff --git a/test/test_conn_reconnect.cpp b/test/test_conn_reconnect.cpp index d4605d5a..eda7ddde 100644 --- a/test/test_conn_reconnect.cpp +++ b/test/test_conn_reconnect.cpp @@ -5,12 +5,15 @@ */ #include + #include -#define BOOST_TEST_MODULE conn-reconnect +#define BOOST_TEST_MODULE conn_reconnect #include -#include + #include "common.hpp" +#include + #ifdef BOOST_ASIO_HAS_CO_AWAIT #include @@ -43,7 +46,8 @@ net::awaitable test_reconnect_impl() logger l; co_await conn->async_exec(req, ignore, net::redirect_error(net::use_awaitable, ec1)); //BOOST_TEST(!ec); - std::cout << "test_reconnect: " << i << " " << ec2.message() << " " << ec1.message() << std::endl; + std::cout << "test_reconnect: " << i << " " << ec2.message() << " " << ec1.message() + << std::endl; } conn->cancel(); @@ -76,10 +80,7 @@ auto async_test_reconnect_timeout() -> net::awaitable st.expires_after(std::chrono::seconds{1}); auto cfg = make_test_config(); - co_await ( - conn->async_exec(req1, ignore, redir(ec1)) || - st.async_wait(redir(ec3)) - ); + co_await (conn->async_exec(req1, ignore, redir(ec1)) || st.async_wait(redir(ec3))); //BOOST_TEST(!ec1); //BOOST_TEST(!ec3); @@ -87,14 +88,13 @@ auto async_test_reconnect_timeout() -> net::awaitable request req2; req2.get_config().cancel_if_not_connected = false; req2.get_config().cancel_on_connection_lost = true; - req2.get_config().cancel_if_unresponded= true; + req2.get_config().cancel_if_unresponded = true; req2.push("QUIT"); st.expires_after(std::chrono::seconds{1}); co_await ( conn->async_exec(req1, ignore, net::redirect_error(net::use_awaitable, ec1)) || - st.async_wait(net::redirect_error(net::use_awaitable, ec3)) - ); + st.async_wait(net::redirect_error(net::use_awaitable, ec3))); conn->cancel(); std::cout << "ccc" << std::endl; @@ -109,8 +109,5 @@ BOOST_AUTO_TEST_CASE(test_reconnect_and_idle) ioc.run(); } #else -BOOST_AUTO_TEST_CASE(dummy) -{ - BOOST_TEST(true); -} +BOOST_AUTO_TEST_CASE(dummy) { BOOST_TEST(true); } #endif diff --git a/test/test_conn_tls.cpp b/test/test_conn_tls.cpp index befef457..d507823f 100644 --- a/test/test_conn_tls.cpp +++ b/test/test_conn_tls.cpp @@ -4,11 +4,13 @@ * accompanying file LICENSE.txt) */ +#include + #include #include -#include -#define BOOST_TEST_MODULE conn-tls +#define BOOST_TEST_MODULE conn_tls #include + #include "common.hpp" namespace net = boost::asio; diff --git a/test/test_conversions.cpp b/test/test_conversions.cpp index 35ffa035..fe543db5 100644 --- a/test/test_conversions.cpp +++ b/test/test_conversions.cpp @@ -8,6 +8,7 @@ #include "boost/system/detail/error_code.hpp" #define BOOST_TEST_MODULE conversions #include + #include #include "common.hpp" @@ -21,103 +22,103 @@ using boost::system::error_code; BOOST_AUTO_TEST_CASE(ints) { - // Setup - net::io_context ioc; - auto conn = std::make_shared(ioc); - run(conn); + // Setup + net::io_context ioc; + auto conn = std::make_shared(ioc); + run(conn); - // Get an integer key as all possible C++ integral types - request req; - req.push("SET", "key", 42); - for (int i = 0; i < 10; ++i) - req.push("GET", "key"); + // Get an integer key as all possible C++ integral types + request req; + req.push("SET", "key", 42); + for (int i = 0; i < 10; ++i) + req.push("GET", "key"); - response< - ignore_t, - signed char, - unsigned char, - short, - unsigned short, - int, - unsigned int, - long, - unsigned long, - long long, - unsigned long long - > resp; + response< + ignore_t, + signed char, + unsigned char, + short, + unsigned short, + int, + unsigned int, + long, + unsigned long, + long long, + unsigned long long> + resp; - conn->async_exec(req, resp, [conn](error_code ec, std::size_t) { - BOOST_TEST(!ec); - conn->cancel(); - }); + conn->async_exec(req, resp, [conn](error_code ec, std::size_t) { + BOOST_TEST(!ec); + conn->cancel(); + }); - // Run the operations - ioc.run(); + // Run the operations + ioc.run(); - // Check - BOOST_TEST(std::get<1>(resp).value() == 42); - BOOST_TEST(std::get<2>(resp).value() == 42); - BOOST_TEST(std::get<3>(resp).value() == 42); - BOOST_TEST(std::get<4>(resp).value() == 42); - BOOST_TEST(std::get<5>(resp).value() == 42); - BOOST_TEST(std::get<6>(resp).value() == 42); - BOOST_TEST(std::get<7>(resp).value() == 42); - BOOST_TEST(std::get<8>(resp).value() == 42); - BOOST_TEST(std::get<9>(resp).value() == 42); - BOOST_TEST(std::get<10>(resp).value() == 42); + // Check + BOOST_TEST(std::get<1>(resp).value() == 42); + BOOST_TEST(std::get<2>(resp).value() == 42); + BOOST_TEST(std::get<3>(resp).value() == 42); + BOOST_TEST(std::get<4>(resp).value() == 42); + BOOST_TEST(std::get<5>(resp).value() == 42); + BOOST_TEST(std::get<6>(resp).value() == 42); + BOOST_TEST(std::get<7>(resp).value() == 42); + BOOST_TEST(std::get<8>(resp).value() == 42); + BOOST_TEST(std::get<9>(resp).value() == 42); + BOOST_TEST(std::get<10>(resp).value() == 42); } BOOST_AUTO_TEST_CASE(bools) { - // Setup - net::io_context ioc; - auto conn = std::make_shared(ioc); - run(conn); + // Setup + net::io_context ioc; + auto conn = std::make_shared(ioc); + run(conn); - // Get a boolean - request req; - req.push("SET", "key_true", "t"); - req.push("SET", "key_false", "f"); - req.push("GET", "key_true"); - req.push("GET", "key_false"); + // Get a boolean + request req; + req.push("SET", "key_true", "t"); + req.push("SET", "key_false", "f"); + req.push("GET", "key_true"); + req.push("GET", "key_false"); - response resp; + response resp; - conn->async_exec(req, resp, [conn](error_code ec, std::size_t) { - BOOST_TEST(!ec); - conn->cancel(); - }); + conn->async_exec(req, resp, [conn](error_code ec, std::size_t) { + BOOST_TEST(!ec); + conn->cancel(); + }); - // Run the operations - ioc.run(); + // Run the operations + ioc.run(); - // Check - BOOST_TEST(std::get<2>(resp).value() == true); - BOOST_TEST(std::get<3>(resp).value() == false); + // Check + BOOST_TEST(std::get<2>(resp).value() == true); + BOOST_TEST(std::get<3>(resp).value() == false); } BOOST_AUTO_TEST_CASE(floating_points) { - // Setup - net::io_context ioc; - auto conn = std::make_shared(ioc); - run(conn); + // Setup + net::io_context ioc; + auto conn = std::make_shared(ioc); + run(conn); - // Get a boolean - request req; - req.push("SET", "key", "4.12"); - req.push("GET", "key"); + // Get a boolean + request req; + req.push("SET", "key", "4.12"); + req.push("GET", "key"); - response resp; + response resp; - conn->async_exec(req, resp, [conn](error_code ec, std::size_t) { - BOOST_TEST(!ec); - conn->cancel(); - }); + conn->async_exec(req, resp, [conn](error_code ec, std::size_t) { + BOOST_TEST(!ec); + conn->cancel(); + }); - // Run the operations - ioc.run(); + // Run the operations + ioc.run(); - // Check - BOOST_TEST(std::get<1>(resp).value() == 4.12); + // Check + BOOST_TEST(std::get<1>(resp).value() == 4.12); } diff --git a/test/test_issue_181.cpp b/test/test_issue_181.cpp index 5885573c..1f3e7491 100644 --- a/test/test_issue_181.cpp +++ b/test/test_issue_181.cpp @@ -6,16 +6,19 @@ #include #include + #include #include -#define BOOST_TEST_MODULE conn-quit +#define BOOST_TEST_MODULE conn_quit #include + +#include "common.hpp" + #include #include #include #include #include -#include "common.hpp" namespace net = boost::asio; using boost::redis::request; @@ -40,7 +43,7 @@ BOOST_AUTO_TEST_CASE(issue_181) net::steady_timer timer{ioc}; timer.expires_after(std::chrono::seconds{1}); - auto run_cont = [&](auto ec){ + auto run_cont = [&](auto ec) { std::cout << "async_run1: " << ec.message() << std::endl; }; @@ -51,7 +54,7 @@ BOOST_AUTO_TEST_CASE(issue_181) BOOST_TEST(!conn.run_is_canceled()); // Uses a timer to wait some time until run has been called. - auto timer_cont = [&](auto ec){ + auto timer_cont = [&](auto ec) { std::cout << "timer_cont: " << ec.message() << std::endl; BOOST_TEST(!conn.run_is_canceled()); conn.cancel(operation::run); diff --git a/test/test_issue_50.cpp b/test/test_issue_50.cpp index cc8740b0..a51942fc 100644 --- a/test/test_issue_50.cpp +++ b/test/test_issue_50.cpp @@ -8,19 +8,22 @@ #include #include + #include -#include -#include -#include #include -#include #include -#define BOOST_TEST_MODULE conn-quit +#include +#include +#include +#include +#define BOOST_TEST_MODULE conn - quit #include -#include -#include + #include "common.hpp" +#include +#include + #if defined(BOOST_ASIO_HAS_CO_AWAIT) namespace net = boost::asio; @@ -38,8 +41,7 @@ using boost::asio::redirect_error; using namespace std::chrono_literals; // Push consumer -auto -receiver(std::shared_ptr conn) -> net::awaitable +auto receiver(std::shared_ptr conn) -> net::awaitable { std::cout << "uuu" << std::endl; while (conn->will_reconnect()) { @@ -59,32 +61,31 @@ receiver(std::shared_ptr conn) -> net::awaitable std::cout << "Exiting the receiver." << std::endl; } -auto -periodic_task(std::shared_ptr conn) -> net::awaitable +auto periodic_task(std::shared_ptr conn) -> net::awaitable { - net::steady_timer timer{co_await net::this_coro::executor}; - for (int i = 0; i < 10; ++i) { - std::cout << "In the loop: " << i << std::endl; - timer.expires_after(std::chrono::milliseconds(50)); - co_await timer.async_wait(net::use_awaitable); + net::steady_timer timer{co_await net::this_coro::executor}; + for (int i = 0; i < 10; ++i) { + std::cout << "In the loop: " << i << std::endl; + timer.expires_after(std::chrono::milliseconds(50)); + co_await timer.async_wait(net::use_awaitable); - // Key is not set so it will cause an error since we are passing - // an adapter that does not accept null, this will cause an error - // that result in the connection being closed. - request req; - req.push("GET", "mykey"); - auto [ec, u] = co_await conn->async_exec(req, ignore, net::as_tuple(net::use_awaitable)); - if (ec) { - std::cout << "(1)Error: " << ec << std::endl; - } else { - std::cout << "no error: " << std::endl; - } - } + // Key is not set so it will cause an error since we are passing + // an adapter that does not accept null, this will cause an error + // that result in the connection being closed. + request req; + req.push("GET", "mykey"); + auto [ec, u] = co_await conn->async_exec(req, ignore, net::as_tuple(net::use_awaitable)); + if (ec) { + std::cout << "(1)Error: " << ec << std::endl; + } else { + std::cout << "no error: " << std::endl; + } + } - std::cout << "Periodic task done!" << std::endl; - conn->cancel(operation::run); - conn->cancel(operation::receive); - conn->cancel(operation::reconnection); + std::cout << "Periodic task done!" << std::endl; + conn->cancel(operation::run); + conn->cancel(operation::receive); + conn->cancel(operation::reconnection); } auto co_main(config) -> net::awaitable @@ -105,10 +106,8 @@ BOOST_AUTO_TEST_CASE(issue_50) ioc.run(); } -#else // defined(BOOST_ASIO_HAS_CO_AWAIT) +#else // defined(BOOST_ASIO_HAS_CO_AWAIT) -BOOST_AUTO_TEST_CASE(issue_50) -{ -} +BOOST_AUTO_TEST_CASE(issue_50) { } -#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) +#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) diff --git a/test/test_low_level_sync_sans_io.cpp b/test/test_low_level_sync_sans_io.cpp index a09153ab..0b5dfe1c 100644 --- a/test/test_low_level_sync_sans_io.cpp +++ b/test/test_low_level_sync_sans_io.cpp @@ -4,17 +4,18 @@ * accompanying file LICENSE.txt) */ -#include -#include -#include -#include -#include #include #include -#define BOOST_TEST_MODULE conn-quit +#include +#include +#include +#include +#include +#define BOOST_TEST_MODULE conn_quit #include -#include + #include +#include using boost::redis::request; using boost::redis::config; @@ -38,7 +39,7 @@ BOOST_AUTO_TEST_CASE(low_level_sync_sans_io) char const* wire = "~6\r\n+orange\r\n+apple\r\n+one\r\n+two\r\n+three\r\n+orange\r\n"; deserialize(wire, adapt2(resp)); - for (auto const& e: resp.value()) + for (auto const& e : resp.value()) std::cout << e << std::endl; } catch (std::exception const& e) { @@ -82,7 +83,8 @@ BOOST_AUTO_TEST_CASE(config_to_hello_cmd_clientname) push_hello(cfg, req); - std::string_view const expected = "*4\r\n$5\r\nHELLO\r\n$1\r\n3\r\n$7\r\nSETNAME\r\n$11\r\nBoost.Redis\r\n"; + std::string_view const + expected = "*4\r\n$5\r\nHELLO\r\n$1\r\n3\r\n$7\r\nSETNAME\r\n$11\r\nBoost.Redis\r\n"; BOOST_CHECK_EQUAL(req.payload(), expected); } @@ -96,21 +98,20 @@ BOOST_AUTO_TEST_CASE(config_to_hello_cmd_auth) push_hello(cfg, req); - std::string_view const expected = "*5\r\n$5\r\nHELLO\r\n$1\r\n3\r\n$4\r\nAUTH\r\n$3\r\nfoo\r\n$3\r\nbar\r\n"; + std::string_view const + expected = "*5\r\n$5\r\nHELLO\r\n$1\r\n3\r\n$4\r\nAUTH\r\n$3\r\nfoo\r\n$3\r\nbar\r\n"; BOOST_CHECK_EQUAL(req.payload(), expected); } BOOST_AUTO_TEST_CASE(issue_210_empty_set) { try { - result< - std::tuple< - result, - result>, - result, - result - > - > resp; + result, + result>, + result, + result>> + resp; char const* wire = "*4\r\n:1\r\n~0\r\n$25\r\nthis_should_not_be_in_set\r\n:2\r\n"; @@ -130,16 +131,15 @@ BOOST_AUTO_TEST_CASE(issue_210_empty_set) BOOST_AUTO_TEST_CASE(issue_210_non_empty_set_size_one) { try { - result< - std::tuple< - result, - result>, - result, - result - > - > resp; + result, + result>, + result, + result>> + resp; - char const* wire = "*4\r\n:1\r\n~1\r\n$3\r\nfoo\r\n$25\r\nthis_should_not_be_in_set\r\n:2\r\n"; + char const* + wire = "*4\r\n:1\r\n~1\r\n$3\r\nfoo\r\n$25\r\nthis_should_not_be_in_set\r\n:2\r\n"; deserialize(wire, adapt2(resp)); @@ -158,16 +158,15 @@ BOOST_AUTO_TEST_CASE(issue_210_non_empty_set_size_one) BOOST_AUTO_TEST_CASE(issue_210_non_empty_set_size_two) { try { - result< - std::tuple< - result, - result>, - result, - result - > - > resp; + result, + result>, + result, + result>> + resp; - char const* wire = "*4\r\n:1\r\n~2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$25\r\nthis_should_not_be_in_set\r\n:2\r\n"; + char const* wire = + "*4\r\n:1\r\n~2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$25\r\nthis_should_not_be_in_set\r\n:2\r\n"; deserialize(wire, adapt2(resp)); @@ -185,16 +184,11 @@ BOOST_AUTO_TEST_CASE(issue_210_non_empty_set_size_two) BOOST_AUTO_TEST_CASE(issue_210_no_nested) { try { - result< - std::tuple< - result, - result, - result, - result - > - > resp; + result, result, result, result>> + resp; - char const* wire = "*4\r\n:1\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$25\r\nthis_should_not_be_in_set\r\n"; + char const* + wire = "*4\r\n:1\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$25\r\nthis_should_not_be_in_set\r\n"; deserialize(wire, adapt2(resp)); @@ -278,7 +272,7 @@ BOOST_AUTO_TEST_CASE(multiplexer_push) BOOST_CHECK_EQUAL(resp.value().at(1).value, "one"); BOOST_CHECK_EQUAL(resp.value().at(2).value, "two"); - for (auto const& e: resp.value()) + for (auto const& e : resp.value()) std::cout << e << std::endl; } @@ -348,7 +342,7 @@ BOOST_AUTO_TEST_CASE(multiplexer_pipeline) BOOST_TEST(item3.elem_ptr->is_waiting()); // There are three requests to coalesce, a second call should do - // nothing. + // nothing. BOOST_CHECK_EQUAL(mpx.prepare_write(), 3); BOOST_CHECK_EQUAL(mpx.prepare_write(), 0); @@ -381,7 +375,7 @@ BOOST_AUTO_TEST_CASE(multiplexer_pipeline) // The done status should still be unchanged on requests that // expect a response. BOOST_TEST(!item1.done); - BOOST_TEST( item2.done); + BOOST_TEST(item2.done); BOOST_TEST(!item3.done); // Simulates a socket read by putting some data in the read buffer. @@ -399,8 +393,8 @@ BOOST_AUTO_TEST_CASE(multiplexer_pipeline) BOOST_TEST(mpx.get_read_buffer().empty()); // The last request still did not get a response. - BOOST_TEST( item1.done); - BOOST_TEST( item2.done); + BOOST_TEST(item1.done); + BOOST_TEST(item2.done); BOOST_TEST(!item3.done); // TODO: Check the first request was removed from the queue. diff --git a/test/test_request.cpp b/test/test_request.cpp index 6dd29f04..701ca5f9 100644 --- a/test/test_request.cpp +++ b/test/test_request.cpp @@ -7,10 +7,10 @@ #include #define BOOST_TEST_MODULE request -#include - #include +#include + using boost::redis::request; // TODO: Serialization. @@ -39,9 +39,14 @@ BOOST_AUTO_TEST_CASE(multiple_args) BOOST_AUTO_TEST_CASE(container_and_range) { - std::map in{{"key1", "value1"}, {"key2", "value2"}}; + std::map in{ + {"key1", "value1"}, + {"key2", "value2"} + }; - char const* res = "*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"; + char const* res = + "*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); diff --git a/test/test_run.cpp b/test/test_run.cpp index dda2cc57..d7bf8263 100644 --- a/test/test_run.cpp +++ b/test/test_run.cpp @@ -7,9 +7,11 @@ #include #define BOOST_TEST_MODULE run #include -#include + #include "common.hpp" +#include + namespace net = boost::asio; namespace redis = boost::redis; @@ -22,8 +24,10 @@ using namespace std::chrono_literals; bool is_host_not_found(error_code ec) { - if (ec == net::error::netdb_errors::host_not_found) return true; - if (ec == net::error::netdb_errors::host_not_found_try_again) return true; + if (ec == net::error::netdb_errors::host_not_found) + return true; + if (ec == net::error::netdb_errors::host_not_found_try_again) + return true; return false; } @@ -40,7 +44,7 @@ BOOST_AUTO_TEST_CASE(resolve_bad_host) cfg.reconnect_wait_interval = 0s; auto conn = std::make_shared(ioc); - conn->async_run(cfg, {}, [](auto ec){ + conn->async_run(cfg, {}, [](auto ec) { BOOST_TEST(is_host_not_found(ec)); }); @@ -97,4 +101,3 @@ BOOST_AUTO_TEST_CASE(connect_bad_port) // run(conn, cfg, boost::redis::error::connect_timeout); // ioc.run(); //} -