Commit cdfe2c22 authored by Dominik Charousset's avatar Dominik Charousset

Add convenience functions to fixture

parent 63106cb5
...@@ -519,6 +519,31 @@ protected: ...@@ -519,6 +519,31 @@ protected:
std::function<void ()> check_; std::function<void ()> check_;
}; };
template <class... Ts>
struct test_coordinator_fixture_fetch_helper {
template <class Self, class Interface>
std::tuple<Ts...>
operator()(caf::response_handle<Self, Interface, true>& from) const {
std::tuple<Ts...> result;
from.receive(
[&](Ts&... xs) { result = std::make_tuple(std::move(xs)...); },
[&](caf::error& err) { FAIL(from.self()->system().render(err)); });
return result;
}
};
template <class T>
struct test_coordinator_fixture_fetch_helper<T> {
template <class Self, class Interface>
T operator()(caf::response_handle<Self, Interface, true>& from) const {
T result;
from.receive(
[&](T& x) { result = std::move(x); },
[&](caf::error& err) { FAIL(from.self()->system().render(err)); });
return result;
}
};
/// A fixture with a deterministic scheduler setup. /// A fixture with a deterministic scheduler setup.
template <class Config = caf::actor_system_config> template <class Config = caf::actor_system_config>
struct test_coordinator_fixture { struct test_coordinator_fixture {
...@@ -584,21 +609,23 @@ struct test_coordinator_fixture { ...@@ -584,21 +609,23 @@ struct test_coordinator_fixture {
sched.run_dispatch_loop(predicate, streaming_cycle); sched.run_dispatch_loop(predicate, streaming_cycle);
} }
template <class T = int> /// Sends a request to `from`, then calls `run_exhaustively`, and finally
caf::expected<T> fetch_result() { /// fetches and returns the result.
caf::expected<T> result = caf::error{}; template <class T, class... Ts, class Handle, class... Us>
self->receive( typename std::conditional<sizeof...(Ts) == 0, T, std::tuple<T, Ts...>>::type
[&](T& x) { request(Handle from, Us... args) {
result = std::move(x); auto res_hdl = self->request(from, caf::infinite, std::move(args)...);
}, run_exhaustively();
[&](caf::error& x) { test_coordinator_fixture_fetch_helper<T, Ts...> f;
result = std::move(x); return f(res_hdl);
}, }
caf::after(std::chrono::seconds(0)) >> [&] {
result = caf::sec::request_timeout; /// Unboxes an expected value or fails the test if it doesn't exist.
} template <class T>
); T unbox(caf::expected<T> x) {
return result; if (!x)
FAIL(sys.render(x.error()));
return std::move(*x);
} }
template <class T> template <class T>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment