Verify canonical replay under opposing worker completion orders

This commit is contained in:
faumaray committed 2026-12-02 23:02:14 +03:00
1 parent 66af7c5bb9
commit ef980af125
7 files changed
+621 -20

No files matched your search

+102 -1
View File
@@ -6,6 +6,9 @@
#include <atomic>
#include <chrono>
#include <array>
#include <condition_variable>
#include <mutex>
#include <future>
#include <memory>
#include <sstream>
@@ -82,6 +85,8 @@ void check_world_unwind(std::size_t phase, bool submit_failure) {
}
struct [[= Void::Testing::name("ExecutorTests")]] ExecutorTests : Void::Testing::Suite<ExecutorTests> {
[[= Void::Testing::tag("executor")]] Test test_completion_observer_results_and_failures();
[[= Void::Testing::tag("executor")]] Test test_completion_observer_precedes_future_readiness();
[[= Void::Testing::tag("executor")]] Test test_limits_and_nested_submission();
[[= Void::Testing::tag("executor")]] Test test_capacity_retains_move_only_work_and_exceptions();
[[= Void::Testing::tag("executor")]] Test test_failed_callable_transfer_releases_capacity();
@@ -92,6 +97,102 @@ struct [[= Void::Testing::name("ExecutorTests")]] ExecutorTests : Void::Testing:
[[= Void::Testing::tag("executor")]] Test test_world_capacity_one_matches_reference();
};
// Exercise the same wrapper on a worker and on the submitting thread. Keeping
// index zero admitted forces every other submission through inline overload.
Test ExecutorTests::test_completion_observer_results_and_failures() {
for (bool inline_work : {false, true}) {
std::promise<void> release;
auto permit = release.get_future().share();
std::promise<void> entered;
auto running = entered.get_future();
std::array<std::atomic<int>, 8> observed{};
std::array<std::atomic<bool>, 8> returned{};
std::atomic<bool> observed_too_early{false};
WorkerPoolOptions options{.max_admitted = inline_work ? 1U : 4096U};
options.before_submit = [](std::size_t index) {
if (index == 8) throw std::bad_alloc();
};
options.before_execute = [](std::size_t index) {
if (index == 6) throw std::runtime_error("before invocation");
};
options.after_execute = [&](std::size_t index) {
if (index >= observed.size()) throw std::logic_error("observer on failed submission");
if (!returned[index].load()) observed_too_early.store(true);
++observed[index];
if (index == 7) throw std::runtime_error("after invocation");
};
WorkerPool pool(2, std::move(options));
auto release_task = Void::Scope::on_exit([&] { release.set_value(); });
auto held = pool.submit([&] {
entered.set_value();
if (permit.wait_for(5s) != std::future_status::ready) throw std::runtime_error("release timeout");
returned[0] = true;
});
require(running.wait_for(5s) == std::future_status::ready);
const auto caller = std::this_thread::get_id();
auto value = pool.submit([&] {
returned[1] = true;
return std::pair{17, std::this_thread::get_id() == caller};
});
require(value.get() == std::pair{17, inline_work});
int referent = 23;
auto reference = pool.submit([&]() -> int& { returned[2] = true; return referent; });
require(&reference.get() == &referent);
auto nothing = pool.submit([&] { returned[3] = true; });
nothing.get();
auto owned = pool.submit([&, value = std::make_unique<int>(31)]() mutable {
returned[4] = true;
return std::move(value);
});
require(*owned.get() == 31);
auto failure = pool.submit([]() -> int { throw std::runtime_error("invocation failed"); });
expect_throw<std::runtime_error>([&] { static_cast<void>(failure.get()); });
auto before = pool.submit([&] { returned[6] = true; });
expect_throw<std::runtime_error>([&] { before.get(); });
auto after = pool.submit([&] { returned[7] = true; });
expect_throw<std::runtime_error>([&] { after.get(); });
bool submitted_failure_ran = false;
expect_throw<std::bad_alloc>([&] {
static_cast<void>(pool.submit([&] { submitted_failure_ran = true; }));
});
require(!submitted_failure_ran);
release_task.release();
release.set_value();
held.get();
for (std::size_t i = 0; i < observed.size(); ++i) {
require(observed[i] == ((i == 5 || i == 6) ? 0 : 1));
}
require(!returned[6] && !observed_too_early);
}
}
Test ExecutorTests::test_completion_observer_precedes_future_readiness() {
std::promise<void> observed;
auto entered = observed.get_future();
std::promise<void> release;
auto permit = release.get_future().share();
std::atomic<int> calls{0};
WorkerPoolOptions options;
// Observer alone must enable monotonically increasing submission indices.
options.after_execute = [&](std::size_t index) {
if (index != static_cast<std::size_t>(calls.fetch_add(1))) throw std::runtime_error("bad index");
if (index == 0) {
observed.set_value();
if (permit.wait_for(5s) != std::future_status::ready) throw std::runtime_error("release timeout");
}
};
WorkerPool pool(2, std::move(options));
auto release_task = Void::Scope::on_exit([&] { release.set_value(); });
auto result = pool.submit([] { return 11; });
require(entered.wait_for(5s) == std::future_status::ready);
require(result.wait_for(0s) == std::future_status::timeout);
release_task.release();
release.set_value();
require(result.get() == 11);
pool.submit([] {}).get();
require(calls == 2);
}
Test ExecutorTests::test_limits_and_nested_submission() {
expect_throw<std::invalid_argument>([] { WorkerPool pool(1); });
expect_throw<std::invalid_argument>([] { WorkerPool pool(2, {.max_admitted = 0}); });
@@ -118,7 +219,7 @@ Test ExecutorTests::test_capacity_retains_move_only_work_and_exceptions() {
const auto coordinator = std::this_thread::get_id();
std::atomic<int> executions{0};
auto answer = pool.submit([value = std::make_unique<int>(17), coordinator, &executions] {
require(std::this_thread::get_id() == coordinator);
if (std::this_thread::get_id() != coordinator) throw std::runtime_error("expected inline work");
++executions;
return *value;
});