fix(wave11): resolve adversarial review findings in EventBus, IO, and Runtime
This commit is contained in:
1 parent
7bea7df46d
commit
c6de7842f7
10 files changed
+871
-134
No files matched your search
@@ -598,3 +598,172 @@ Void::Testing::Test bus_flush_blocking_self_join_and_manual_invalid()
|
||||
Void::Testing::require(exec->join().has_value());
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test bus_pumping_self_join_returns_self_join()
|
||||
{
|
||||
using namespace Void::EventBus;
|
||||
Void::Async::ManualExecutor manual;
|
||||
EventBus bus{manual};
|
||||
std::optional<Void::Result::Result<void>> fp_res;
|
||||
std::optional<Void::Result::Result<void>> cj_res;
|
||||
|
||||
auto sub = bus.try_subscribe<Ping>([&](const Ping&) {
|
||||
fp_res = bus.flush_pumping(manual);
|
||||
cj_res = bus.close_and_join_pumping(manual);
|
||||
});
|
||||
Void::Testing::require(sub.has_value());
|
||||
Void::Testing::require(bus.try_publish(Ping{7}).has_value());
|
||||
(void)manual.drain();
|
||||
|
||||
Void::Testing::require(fp_res.has_value() && !fp_res->has_value());
|
||||
Void::Testing::expect_eq(
|
||||
async_code(fp_res->error()),
|
||||
static_cast<std::int32_t>(Void::Async::AsyncErrorCode::self_join));
|
||||
Void::Testing::require(cj_res.has_value() && !cj_res->has_value());
|
||||
Void::Testing::expect_eq(
|
||||
async_code(cj_res->error()),
|
||||
static_cast<std::int32_t>(Void::Async::AsyncErrorCode::self_join));
|
||||
Void::Testing::require(bus.close_and_join_pumping(manual).has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test bus_close_and_join_waits_for_slow_capture_destructor()
|
||||
{
|
||||
using namespace Void::EventBus;
|
||||
struct SlowGuard {
|
||||
explicit SlowGuard(std::atomic<bool>* p) : destroyed(p) {}
|
||||
std::atomic<bool>* destroyed{nullptr};
|
||||
~SlowGuard()
|
||||
{
|
||||
if (destroyed != nullptr) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{15});
|
||||
destroyed->store(true, std::memory_order_release);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 1. ThreadExecutor subscription::close_and_join waits until slow capture dtor finishes.
|
||||
{
|
||||
auto exec_res = Void::Async::ThreadExecutor::create(
|
||||
Void::Async::ThreadExecutorOptions{.workers = 2, .max_admitted = 16});
|
||||
Void::Testing::require(exec_res.has_value());
|
||||
auto exec = std::move(*exec_res);
|
||||
Void::Testing::require(exec->start().has_value());
|
||||
|
||||
EventBus bus{*exec};
|
||||
std::atomic<bool> destroyed{false};
|
||||
std::latch entered{1};
|
||||
std::promise<void> rel;
|
||||
auto rel_fut = rel.get_future().share();
|
||||
auto guard = std::make_shared<SlowGuard>(&destroyed);
|
||||
|
||||
auto sub = bus.try_subscribe<Ping>([&, guard](const Ping&) {
|
||||
(void)guard;
|
||||
entered.count_down();
|
||||
rel_fut.wait();
|
||||
});
|
||||
Void::Testing::require(sub.has_value());
|
||||
guard.reset();
|
||||
|
||||
Void::Testing::require(bus.try_publish(Ping{1}).has_value());
|
||||
entered.wait();
|
||||
|
||||
std::thread releaser([&]() {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
||||
rel.set_value();
|
||||
});
|
||||
Void::Testing::require(sub->close_and_join().has_value());
|
||||
// Must ALREADY be destroyed when close_and_join returns!
|
||||
Void::Testing::expect(destroyed.load(std::memory_order_acquire));
|
||||
releaser.join();
|
||||
|
||||
Void::Testing::require(bus.close_and_join().has_value());
|
||||
exec->close();
|
||||
Void::Testing::require(exec->join().has_value());
|
||||
}
|
||||
|
||||
// 2. Mode::Async EventBus::close_and_join waits until slow capture dtor finishes.
|
||||
{
|
||||
EventBus bus{Mode::Async};
|
||||
std::atomic<bool> destroyed{false};
|
||||
std::latch entered{1};
|
||||
std::promise<void> rel;
|
||||
auto rel_fut = rel.get_future().share();
|
||||
auto guard = std::make_shared<SlowGuard>(&destroyed);
|
||||
|
||||
auto sub = bus.try_subscribe<Ping>([&, guard](const Ping&) {
|
||||
(void)guard;
|
||||
entered.count_down();
|
||||
rel_fut.wait();
|
||||
});
|
||||
Void::Testing::require(sub.has_value());
|
||||
guard.reset();
|
||||
|
||||
bus.publish(Ping{2});
|
||||
entered.wait();
|
||||
|
||||
std::thread releaser([&]() {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
||||
rel.set_value();
|
||||
});
|
||||
Void::Testing::require(bus.close_and_join().has_value());
|
||||
Void::Testing::expect(destroyed.load(std::memory_order_acquire));
|
||||
releaser.join();
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test bus_legacy_async_purged_drop_outside_lock()
|
||||
{
|
||||
using namespace Void::EventBus;
|
||||
struct MuLockingDtor {
|
||||
EventBus* bus{nullptr};
|
||||
std::atomic<bool>* dropped{nullptr};
|
||||
std::atomic<int>* calls{nullptr};
|
||||
~MuLockingDtor()
|
||||
{
|
||||
if (bus != nullptr) {
|
||||
bus->unsubscribe(0); // Acquires BusCore::mu; would deadlock under lock.
|
||||
}
|
||||
if (dropped != nullptr) {
|
||||
dropped->store(true, std::memory_order_release);
|
||||
}
|
||||
if (calls != nullptr) {
|
||||
calls->fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
EventBus bus{Mode::Async};
|
||||
std::atomic<bool> dropped{false};
|
||||
std::atomic<int> dtor_calls{0};
|
||||
std::atomic<int> b_ran{0};
|
||||
std::optional<Void::Result::Result<void>> sub_b_join_res;
|
||||
auto sub_b_ptr = std::make_shared<std::optional<EventBus::Subscription>>();
|
||||
|
||||
// Subscribe A before B so run_worker visits A first for the same queued item.
|
||||
auto sub_a = bus.try_subscribe<Ping>([&](const Ping&) {
|
||||
if (*sub_b_ptr) {
|
||||
sub_b_join_res = (*sub_b_ptr)->close_and_join();
|
||||
}
|
||||
});
|
||||
Void::Testing::require(sub_a.has_value());
|
||||
|
||||
{
|
||||
auto guard = std::shared_ptr<MuLockingDtor>(
|
||||
new MuLockingDtor{&bus, &dropped, &dtor_calls});
|
||||
auto sb = bus.try_subscribe<Ping>([&, guard](const Ping&) {
|
||||
(void)guard;
|
||||
b_ran.fetch_add(1, std::memory_order_relaxed);
|
||||
});
|
||||
Void::Testing::require(sb.has_value());
|
||||
*sub_b_ptr = std::move(*sb);
|
||||
}
|
||||
|
||||
bus.publish(Ping{1});
|
||||
bus.flush();
|
||||
Void::Testing::require(sub_b_join_res.has_value());
|
||||
Void::Testing::expect(sub_b_join_res->has_value());
|
||||
Void::Testing::expect_eq(b_ran.load(), 0);
|
||||
Void::Testing::expect(dropped.load(std::memory_order_acquire));
|
||||
Void::Testing::expect_eq(dtor_calls.load(), 1);
|
||||
Void::Testing::require(bus.close_and_join().has_value());
|
||||
}
|
||||
Reference in new issue
Block a user