Skip to content

Commit

Permalink
testing: add boost unit test decorator support
Browse files Browse the repository at this point in the history
the decorators in Boost::test is the unified mechinary to attach
various attribute to automatically registered unit tests. which
the decorator provided by the testing framework, we can

* attach one or more label
* tolerate up to specified number of test failures

see
https://www.boost.org/doc/libs/release/libs/test/doc/html/boost_test/tests_organization/decorators.html
.

the use of Boost::proprocess is modeled after how Boost::test implements
the test framework.

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
  • Loading branch information
tchaikov committed Feb 14, 2023
1 parent decaab0 commit eb4fff4
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 23 deletions.
12 changes: 11 additions & 1 deletion include/seastar/testing/seastar_test.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,23 @@
#include <seastar/util/std-compat.hh>
#include <seastar/testing/entry_point.hh>

#define SEASTAR_TEST_INVOKE(func, ...) func(__VA_ARGS__)

namespace boost::unit_test::decorator {

class collector_t;

}

namespace seastar {

namespace testing {

class seastar_test {
public:
seastar_test(const char* test_name, const char* test_file, int test_line, int expected_failures = 0);
seastar_test(const char* test_name, const char* test_file, int test_line);
seastar_test(const char* test_name, const char* test_file, int test_line,
boost::unit_test::decorator::collector_t& decorators);
virtual ~seastar_test() {}
static const std::string& get_name();
virtual future<> run_test_case() const = 0;
Expand Down
33 changes: 27 additions & 6 deletions include/seastar/testing/test_case.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,35 @@

#pragma once

#include <boost/preprocessor/control/iif.hpp>
#include <boost/preprocessor/comparison/equal.hpp>
#include <boost/preprocessor/variadic/size.hpp>

#include <seastar/core/future.hh>

#include <seastar/testing/seastar_test.hh>

#define SEASTAR_TEST_CASE(name) \
struct name : public seastar::testing::seastar_test { \
using seastar::testing::seastar_test::seastar_test; \
seastar::future<> run_test_case() const override; \
}; \
static const name name ## _instance(#name, __FILE__, __LINE__); /* NOLINT(cert-err58-cpp) */ \
#define SEASTAR_TEST_CASE_WITH_DECO(name, decorators) \
struct name : public seastar::testing::seastar_test { \
using seastar::testing::seastar_test::seastar_test; \
seastar::future<> run_test_case() const override; \
}; \
static const name name ## _instance( \
#name, \
__FILE__, \
__LINE__, \
decorators); /* NOLINT(cert-err58-cpp) */ \
seastar::future<> name::run_test_case() const

#define SEASTAR_TEST_CASE_WITHOUT_DECO(name) \
SEASTAR_TEST_CASE_WITH_DECO( \
name, \
boost::unit_test::decorator::collector_t::instance())

#define SEASTAR_TEST_CASE(...) \
SEASTAR_TEST_INVOKE( \
BOOST_PP_IIF( \
BOOST_PP_EQUAL(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__), 1), \
SEASTAR_TEST_CASE_WITHOUT_DECO, \
SEASTAR_TEST_CASE_WITH_DECO), \
__VA_ARGS__)
44 changes: 31 additions & 13 deletions include/seastar/testing/thread_test_case.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,38 @@
#include <seastar/core/future.hh>
#include <seastar/core/thread.hh>

#include <boost/preprocessor/control/iif.hpp>
#include <boost/preprocessor/comparison/equal.hpp>
#include <boost/preprocessor/variadic/size.hpp>

#include <seastar/testing/seastar_test.hh>

#define SEASTAR_THREAD_TEST_CASE_EXPECTED_FAILURES(name, failures) \
struct name : public seastar::testing::seastar_test { \
using seastar::testing::seastar_test::seastar_test; \
seastar::future<> run_test_case() const override { \
return seastar::async([this] { \
do_run_test_case(); \
}); \
} \
void do_run_test_case() const; \
}; \
static const name name ## _instance(#name, __FILE__, __LINE__, failures); /* NOLINT(cert-err58-cpp) */ \
#define SEASTAR_THREAD_TEST_CASE_WITH_DECO(name, decorators) \
struct name : public seastar::testing::seastar_test { \
using seastar::testing::seastar_test::seastar_test; \
seastar::future<> run_test_case() const override { \
return seastar::async([this] { \
do_run_test_case(); \
}); \
} \
void do_run_test_case() const; \
}; \
static const name name ## _instance( \
#name, \
__FILE__, \
__LINE__, \
decorators); /* NOLINT(cert-err58-cpp) */ \
void name::do_run_test_case() const

#define SEASTAR_THREAD_TEST_CASE(name) \
SEASTAR_THREAD_TEST_CASE_EXPECTED_FAILURES(name, 0)
#define SEASTAR_THREAD_TEST_CASE_WITHOUT_DECO(name) \
SEASTAR_THREAD_TEST_CASE_WITH_DECO( \
name, \
boost::unit_test::decorator::collector_t::instance())

#define SEASTAR_THREAD_TEST_CASE(...) \
SEASTAR_TEST_INVOKE( \
BOOST_PP_IIF( \
BOOST_PP_EQUAL(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__), 1), \
SEASTAR_THREAD_TEST_CASE_WITHOUT_DECO, \
SEASTAR_THREAD_TEST_CASE_WITH_DECO), \
__VA_ARGS__)
11 changes: 9 additions & 2 deletions src/testing/seastar_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,16 @@ void seastar_test::run() {
});
}

seastar_test::seastar_test(const char* test_name, const char* test_file, int test_line, int expected_failures) {

seastar_test::seastar_test(const char* test_name, const char* test_file, int test_line)
: seastar_test(test_name, test_file, test_line, boost::unit_test::decorator::collector_t::instance()) {}

seastar_test::seastar_test(const char* test_name, const char* test_file, int test_line,
boost::unit_test::decorator::collector_t& decorators) {
auto test = boost::unit_test::make_test_case([this] { run(); }, test_name, test_file, test_line);
boost::unit_test::framework::current_auto_test_suite().add(test, expected_failures);
decorators.store_in(*test);
decorators.reset();
boost::unit_test::framework::current_auto_test_suite().add(test);
}

std::string& seastar_test::get_name() {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/thread_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ SEASTAR_TEST_CASE(test_asan_false_positive) {
}
#endif

SEASTAR_THREAD_TEST_CASE_EXPECTED_FAILURES(abc, 2) {
SEASTAR_THREAD_TEST_CASE(abc, *boost::unit_test::expected_failures(2)) {
BOOST_TEST(false);
BOOST_TEST(false);
}
Expand Down

0 comments on commit eb4fff4

Please sign in to comment.