Files
scylladb/tests/test_runner.cc
Tomasz Grabiec a1fecad8cb tests: Introduce test_runner class
It uses app_template to launch seastar framework and can be used from
outside threads to inject tasks.
2015-01-29 10:30:14 +02:00

81 lines
1.8 KiB
C++

/*
* Copyright (C) 2015 Cloudius Systems, Ltd.
*/
#include <iostream>
#include "core/app-template.hh"
#include "core/future-util.hh"
#include "test_runner.hh"
static test_runner instance;
test_runner::~test_runner() {
if (_thread) {
stop();
_thread->join();
}
}
void
test_runner::start(std::function<void()> pre_start) {
bool expected = false;
if (!_started.compare_exchange_strong(expected, true, std::memory_order_acquire)) {
return;
}
_thread = std::make_unique<posix_thread>([this, pre_start = std::move(pre_start)]() mutable {
char* av[] = {};
int ac = 0;
pre_start();
app_template app;
auto exit_code = app.run(ac, av, [&] {
return do_until([this] { return _done; }, [this] {
// this will block the reactor briefly, but we don't care
auto func = _task.take();
return func();
});
});
if (exit_code) {
exit(exit_code);
}
});
}
void
test_runner::stop() {
assert(_started.load());
_task.give([this] {
_done = true;
engine().exit(0);
return now();
});
}
void
test_runner::run_sync(std::function<future<>()> task) {
exchanger<std::experimental::optional<std::exception_ptr>> e;
_task.give([task = std::move(task), &e] {
return task().rescue([&e] (auto get) {
try {
get();
e.give({});
} catch (...) {
e.give({std::current_exception()});
}
});
});
auto maybe_exception = e.take();
if (maybe_exception) {
std::rethrow_exception(*maybe_exception);
}
}
test_runner&
test_runner::launch_or_get(std::function<void()> pre_start) {
instance.start(std::move(pre_start));
return instance;
}