Files
scylladb/tests/test-utils.hh
Tomasz Grabiec 11a501d884 tests: fix assertion failure at test program exit
Seastar's allocator has an assertion which checks that the memory
block is freed on the same CPU on which it was allocated. This is
reasonable because all allocations in seastar need to be CPU-local.

The problem is that boost libraries (program_options,
unit_testing_framework) make heavy use of lazily-allocated static
variables, for instance:

    const variable_value&
    variables_map::get(const std::string& name) const
    {
        static variable_value empty;
        // ...
    }

Such variable will be allocated in in the current threads but freed in
the main thread when exit handlers are executed.

This results in:

output_stream_test: core/memory.cc:415: void memory::cpu_pages::free(void*):
Assertion `((reinterpret_cast<uintptr_t>(ptr) >> cpu_id_shift) & 0xff) == cpu_id' failed.

This change works around the problem by forcing initialization in the
main thread.

See issue #10.
2014-12-04 13:37:02 +01:00

76 lines
2.0 KiB
C++

/*
* Copyright (C) 2014 Cloudius Systems, Ltd.
*/
#ifndef _TEST_UTILS_HH
#define _TEST_UTILS_HH
#include <iostream>
#include <boost/test/included/unit_test.hpp>
#include "core/future.hh"
#include "core/reactor.hh"
#include "core/app-template.hh"
using namespace boost::unit_test;
class seastar_test {
public:
seastar_test();
virtual ~seastar_test() {}
virtual const char* get_name() = 0;
virtual future<> run_test_case() = 0;
void run() {
namespace bpo = boost::program_options;
// HACK: please see https://github.com/cloudius-systems/seastar/issues/10
BOOST_REQUIRE(true);
// HACK: please see https://github.com/cloudius-systems/seastar/issues/10
bpo::variables_map()["dummy"];
posix_thread t([this] () mutable {
boost::program_options::variables_map configuration;
auto opts = reactor::get_options_description();
bpo::store(bpo::command_line_parser(0, nullptr).options(opts).run(), configuration);
engine.configure(configuration);
engine.when_started().then([this] {
return run_test_case();
}).rescue([] (auto get) {
try {
get();
engine.exit(0);
} catch (...) {
std::terminate();
}
});
engine.run();
});
t.join();
}
};
static std::vector<seastar_test*> tests;
seastar_test::seastar_test() {
tests.push_back(this);
}
test_suite* init_unit_test_suite(int argc, char* argv[]) {
test_suite* ts = BOOST_TEST_SUITE("seastar-tests");
for (seastar_test* test : tests) {
ts->add(boost::unit_test::make_test_case([test] { test->run(); }, test->get_name()));
}
return ts;
}
#define SEASTAR_TEST_CASE(name) \
struct name : public seastar_test { \
const char* get_name() override { return #name; } \
future<> run_test_case() override; \
}; \
static name name ## _instance; \
future<> name::run_test_case()
#endif