Skip to content

Commit

Permalink
testing: coroutinize test_runner::start_thread()
Browse files Browse the repository at this point in the history
for better readability

Signed-off-by: Kefu Chai <[email protected]>
  • Loading branch information
tchaikov committed Feb 18, 2025
1 parent c0370fb commit 4ba75a0
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions src/testing/test_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,39 +74,36 @@ bool test_runner::start_thread(int ac, char** av) {
("fail-on-abandoned-failed-futures", bpo::value<bool>()->default_value(true), "Fail the test if there are any abandoned failed futures");
// We guarantee that only one thread is running.
// We only read this after that one thread is joined, so this is safe.
_exit_code = app.run(ac, av, [this, &app, init_outcome = init_outcome.get()] {
_exit_code = app.run(ac, av, [this, &app, init_outcome = init_outcome.get()] () -> future<int> {
init_outcome->give(true);
auto init = [&app] {

// TODO: fix the indent
auto conf_seed = app.configuration()["random-seed"];
auto seed = conf_seed.empty() ? std::random_device()(): conf_seed.as<unsigned>();
std::cout << "random-seed=" << seed << std::endl;
return smp::invoke_on_all([seed] {
co_await smp::invoke_on_all([seed] {
auto local_seed = seed + this_shard_id();
local_random_engine.seed(local_seed);
});
};

return init().then([this] {
return do_until([this] { return _done; }, [this] {
// this will block the reactor briefly, but we don't care
try {
auto func = _task.take();
return func();
} catch (const stop_execution&) {
_done = true;
return make_ready_future<>();
}
}).or_terminate();
}).then([&app] {
for (;;) {
// this will block the reactor briefly, but we don't care
try {
auto func = _task.take();
co_await func().or_terminate();
} catch (const stop_execution&) {
break;
}
}
// TODO: fix the indent
if (engine().abandoned_failed_futures()) {
std::cerr << "*** " << engine().abandoned_failed_futures() << " abandoned failed future(s) detected" << std::endl;
if (app.configuration()["fail-on-abandoned-failed-futures"].as<bool>()) {
std::cerr << "Failing the test because fail was requested by --fail-on-abandoned-failed-futures" << std::endl;
return 3;
co_return 3;
}
}
return 0;
});
co_return 0;
});
init_outcome->give(false);
});
Expand Down

0 comments on commit 4ba75a0

Please sign in to comment.