Files
scylladb/test/lib/test_utils.hh
Botond Dénes 841b982e51 test/lib/test_utils: return OK from check() variants
The various require() and check() methods in test_utils.hh were
introduced to replace BOOST_REQUIRE() and BOOST_CHECK() respectively in
multi-shard concurrent tests, specifically those in
tests/boost/multishard_mutation_query_test.cc.
This was done literally, just replacing BOOST_REQUIRE() with require()
and BOOST_CHECK() with check(). The problem is that check() is missing a
feature BOOST_CHECK() had: while BOOST_CHECK() doesn't cause an
immediate test failure, just logging an error if the condition fails, it
remembers this failure and will fail the test in the end. check() did
not have this feature and this caused potential errors to just be logged
while the test could still pass fine, causing false-positive tests
passes. This patch fixes this by returning a [[nodiscard]] bool from the
check() methods. The caller can & these together over all calls to
check() methods and manually fail the test in the end. We choose this
method over a hidden global (like BOOST_CHECK() does) for simplicity
sake.
2022-02-21 12:29:25 +02:00

50 lines
1.7 KiB
C++

/*
* Copyright (C) 2020-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <experimental/source_location>
#include <string>
#include <fmt/format.h>
// Thread safe alternatives to BOOST_REQUIRE_*, BOOST_CHECK_* and BOOST_FAIL().
// Use these if instead of the BOOST provided macros if you want to use them on
// multiple shards, to avoid problems due to the BOOST versions not being thread
// safe.
namespace tests {
[[nodiscard]] bool do_check(bool condition, std::experimental::source_location sl, std::string_view msg);
[[nodiscard]] inline bool check(bool condition, std::experimental::source_location sl = std::experimental::source_location::current()) {
return do_check(condition, sl, {});
}
template <typename LHS, typename RHS>
[[nodiscard]] bool check_equal(const LHS& lhs, const RHS& rhs, std::experimental::source_location sl = std::experimental::source_location::current()) {
const auto condition = (lhs == rhs);
return do_check(condition, sl, fmt::format("{} {}= {}", lhs, condition ? "=" : "!", rhs));
}
void do_require(bool condition, std::experimental::source_location sl, std::string_view msg);
inline void require(bool condition, std::experimental::source_location sl = std::experimental::source_location::current()) {
do_require(condition, sl, {});
}
template <typename LHS, typename RHS>
void require_equal(const LHS& lhs, const RHS& rhs, std::experimental::source_location sl = std::experimental::source_location::current()) {
const auto condition = (lhs == rhs);
do_require(condition, sl, fmt::format("{} {}= {}", lhs, condition ? "=" : "!", rhs));
}
void fail(std::string_view msg, std::experimental::source_location sl = std::experimental::source_location::current());
}