Commit f53be211 authored by Dominik Charousset's avatar Dominik Charousset

Add until predicate to run_dispatch_loop

The predicate allows users to run the loop only until a condition
becomes true.
parent 7d945c6c
......@@ -114,6 +114,13 @@ public:
/// messages.
size_t dispatch();
/// Loops until no job or delayed message remains or `predicate` returns
/// `true`. Returns the total number of events (first) and dispatched delayed
/// messages (second). Advances time by `cycle` between to calls to
/// `dispatch()`.
std::pair<size_t, size_t> run_dispatch_loop(std::function<bool()> predicate,
timespan cycle = timespan{0});
/// Loops until no job or delayed message remains. Returns the total number
/// of events (first) and dispatched delayed messages (second). Advances time
/// by `cycle` between to calls to `dispatch()`.
......
......@@ -153,19 +153,34 @@ size_t test_coordinator::dispatch() {
return clock().dispatch();
}
std::pair<size_t, size_t> test_coordinator::run_dispatch_loop(timespan cycle) {
std::pair<size_t, size_t>
test_coordinator::run_dispatch_loop(std::function<bool()> predicate,
timespan cycle) {
std::pair<size_t, size_t> res{0, 0};
for (;;) {
auto x = run();
size_t progress = 0;
while (try_run_once()) {
++progress;
res.first += 1;
if (predicate())
return res;
}
clock().current_time += cycle;
auto y = dispatch();
if (x + y == 0)
while (dispatch_once()) {
++progress;
res.second += 1;
if (predicate())
return res;
}
if (progress == 0)
return res;
res.first += x;
res.second += y;
}
}
std::pair<size_t, size_t> test_coordinator::run_dispatch_loop(timespan cycle) {
return run_dispatch_loop([] { return false; }, cycle);
}
void test_coordinator::inline_next_enqueue() {
after_next_enqueue([=] { run_once_lifo(); });
}
......
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