#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { struct Ping { int value{0}; }; [[nodiscard]] std::size_t current_thread_count() { std::size_t count = 0; std::error_code ec; for (const auto& _ : std::filesystem::directory_iterator("/proc/self/task", ec)) { (void)_; ++count; } return count; } [[nodiscard]] std::int32_t async_code(const Void::Result::Error& err) noexcept { return err.code().value; } } // namespace Void::Testing::Test bus_injected_no_private_thread_manual_pumping() { using namespace Void::EventBus; const std::size_t threads_before = current_thread_count(); Void::Async::ManualExecutor manual; EventBus bus{manual}; const std::size_t threads_after = current_thread_count(); Void::Testing::expect_eq(threads_after, threads_before); Void::Testing::expect(bus.mode() == Mode::Injected); int sum = 0; std::thread::id caller_id = std::this_thread::get_id(); std::thread::id seen_id{}; auto sub = bus.try_subscribe([&](const Ping& p) { seen_id = std::this_thread::get_id(); sum += p.value; }); Void::Testing::require(sub.has_value()); Void::Testing::require(bus.try_publish(Ping{10}).has_value()); Void::Testing::require(bus.try_publish(Ping{25}).has_value()); Void::Testing::expect_eq(sum, 0); Void::Testing::require(bus.flush_pumping(manual).has_value()); Void::Testing::expect_eq(sum, 35); Void::Testing::expect(seen_id == caller_id); Void::Testing::require(bus.close_and_join_pumping(manual).has_value()); } Void::Testing::Test bus_injected_subscription_limit_overloaded() { using namespace Void::EventBus; Void::Async::ManualExecutor manual; EventBus bus{manual, BusOptions{.max_subscriptions = 2}}; auto s1 = bus.try_subscribe([](const Ping&) {}); auto s2 = bus.try_subscribe([](const Ping&) {}); Void::Testing::require(s1.has_value()); Void::Testing::require(s2.has_value()); auto s3 = bus.try_subscribe([](const Ping&) {}); Void::Testing::require(!s3.has_value()); Void::Testing::expect_eq( async_code(s3.error()), static_cast(Void::Async::AsyncErrorCode::overloaded)); s1->reset(); auto s4 = bus.try_subscribe([](const Ping&) {}); Void::Testing::expect(s4.has_value()); Void::Testing::require(bus.close_and_join_pumping(manual).has_value()); } Void::Testing::Test bus_injected_dispatch_limit_overloaded() { using namespace Void::EventBus; Void::Async::ManualExecutor manual; EventBus bus{manual, BusOptions{.max_admitted_dispatches = 2}}; int hits = 0; auto sub = bus.try_subscribe([&](const Ping& p) { hits += p.value; }); Void::Testing::require(sub.has_value()); Void::Testing::require(bus.try_publish(Ping{1}).has_value()); Void::Testing::require(bus.try_publish(Ping{2}).has_value()); auto third = bus.try_publish(Ping{3}); Void::Testing::require(!third.has_value()); Void::Testing::expect_eq( async_code(third.error()), static_cast(Void::Async::AsyncErrorCode::overloaded)); Void::Testing::require(bus.flush_pumping(manual).has_value()); Void::Testing::expect_eq(hits, 3); Void::Testing::require(bus.try_publish(Ping{4}).has_value()); Void::Testing::require(bus.flush_pumping(manual).has_value()); Void::Testing::expect_eq(hits, 7); Void::Testing::require(bus.close_and_join_pumping(manual).has_value()); } Void::Testing::Test bus_close_and_join_waits_inflight_releases_capture_tsan() { using namespace Void::EventBus; auto exec_res = Void::Async::ThreadExecutor::create( Void::Async::ThreadExecutorOptions{.workers = 2, .max_admitted = 32}); Void::Testing::require(exec_res.has_value()); auto exec = std::move(*exec_res); Void::Testing::require(exec->start().has_value()); { EventBus bus{*exec}; auto sentinel = std::make_shared(99); std::latch started{1}; std::promise release_promise; auto release_future = release_promise.get_future().share(); std::atomic finished{false}; auto sub = bus.try_subscribe( [&, sentinel](const Ping&) { started.count_down(); release_future.wait(); finished.store(true, std::memory_order_release); }); Void::Testing::require(sub.has_value()); Void::Testing::expect(sentinel.use_count() >= 2); Void::Testing::require(bus.try_publish(Ping{1}).has_value()); started.wait(); std::atomic join_done{false}; Void::Result::Result join_res; std::thread joiner([&]() { join_res = sub->close_and_join(); join_done.store(true, std::memory_order_release); }); std::this_thread::sleep_for(std::chrono::milliseconds{15}); Void::Testing::expect(!join_done.load(std::memory_order_acquire)); release_promise.set_value(); joiner.join(); Void::Testing::expect(join_res.has_value()); Void::Testing::expect(finished.load(std::memory_order_acquire)); Void::Testing::expect_eq(sentinel.use_count(), 1L); Void::Testing::require(bus.close_and_join().has_value()); } exec->close(); Void::Testing::require(exec->join().has_value()); } Void::Testing::Test bus_unsubscribe_during_dispatch_prevents_later_admission() { using namespace Void::EventBus; EventBus bus{Mode::Sync}; int first_hits = 0; int second_hits = 0; HandlerId id2 = 0; bus.subscribe([&](const Ping&) { ++first_hits; bus.unsubscribe(id2); }); id2 = bus.subscribe([&](const Ping&) { ++second_hits; }); bus.publish(Ping{1}); Void::Testing::expect_eq(first_hits, 1); Void::Testing::expect_eq(second_hits, 0); bus.publish(Ping{2}); Void::Testing::expect_eq(first_hits, 2); Void::Testing::expect_eq(second_hits, 0); } Void::Testing::Test bus_self_unsubscribe_ok_self_join_rejected() { using namespace Void::EventBus; EventBus bus{Mode::Sync}; EventBus::Subscription sub; Void::Result::Result join_res; int calls = 0; auto sub_res = bus.try_subscribe([&](const Ping&) { ++calls; join_res = sub.close_and_join(); sub.reset(); }); Void::Testing::require(sub_res.has_value()); sub = std::move(*sub_res); bus.publish(Ping{1}); bus.publish(Ping{2}); Void::Testing::expect_eq(calls, 1); Void::Testing::require(!join_res.has_value()); Void::Testing::expect_eq( async_code(join_res.error()), static_cast(Void::Async::AsyncErrorCode::self_join)); Void::Testing::expect(!static_cast(sub)); } Void::Testing::Test bus_callback_throw_to_error_sink_others_run() { using namespace Void::EventBus; Void::Async::ManualExecutor manual; std::vector errors; EventBus bus{ manual, BusOptions{.error_sink = [&](std::exception_ptr ex) { try { if (ex) { std::rethrow_exception(ex); } } catch (const std::exception& e) { errors.emplace_back(e.what()); } }}}; int second_hits = 0; auto s1 = bus.try_subscribe([](const Ping&) { throw std::runtime_error("boom_injected"); }); auto s2 = bus.try_subscribe([&](const Ping& p) { second_hits += p.value; }); Void::Testing::require(s1.has_value() && s2.has_value()); Void::Testing::require(bus.try_publish(Ping{9}).has_value()); Void::Testing::require(bus.flush_pumping(manual).has_value()); Void::Testing::expect_eq(second_hits, 9); Void::Testing::expect_eq(bus.callback_failures(), std::uint64_t{1}); Void::Testing::require(errors.size() == 1); Void::Testing::expect_eq(errors[0], std::string{"boom_injected"}); // Legacy Sync publish retains exception propagation. EventBus sync_bus{Mode::Sync}; sync_bus.subscribe([](const Ping&) { throw std::runtime_error("sync_boom"); }); bool caught = false; try { sync_bus.publish(Ping{1}); } catch (const std::runtime_error&) { caught = true; } Void::Testing::expect(caught); Void::Testing::require(bus.close_and_join_pumping(manual).has_value()); } Void::Testing::Test bus_error_sink_throw_caught_and_counted() { using namespace Void::EventBus; EventBus bus{ Mode::Async, BusOptions{.error_sink = [](std::exception_ptr) { throw std::runtime_error("sink_boom"); }}}; std::atomic second_hits{0}; bus.subscribe([](const Ping&) { throw std::runtime_error("cb_boom"); }); bus.subscribe([&](const Ping& p) { second_hits.fetch_add(p.value, std::memory_order_relaxed); }); Void::Testing::require(bus.try_publish(Ping{7}).has_value()); Void::Testing::require(bus.flush_blocking().has_value()); Void::Testing::expect_eq(second_hits.load(), 7); // 1 for handler throw + 1 for throwing error_sink Void::Testing::expect_eq(bus.callback_failures(), std::uint64_t{2}); Void::Testing::require(bus.close_and_join().has_value()); } Void::Testing::Test bus_legacy_async_unsubscribe_keeps_queued_copy() { using namespace Void::EventBus; struct Unblock {}; EventBus bus{Mode::Async}; std::latch blocker_entered{1}; std::promise release_blocker; auto release_future = release_blocker.get_future().share(); bus.subscribe([&](const Unblock&) { blocker_entered.count_down(); release_future.wait(); }); std::atomic target_hits{0}; const HandlerId target_id = bus.subscribe([&](const Ping& p) { target_hits.fetch_add(p.value, std::memory_order_relaxed); }); bus.publish(Unblock{}); blocker_entered.wait(); bus.publish(Ping{13}); bus.unsubscribe(target_id); bus.publish(Ping{100}); release_blocker.set_value(); bus.flush(); Void::Testing::expect_eq(target_hits.load(), 13); Void::Testing::require(bus.close_and_join().has_value()); } Void::Testing::Test bus_sync_reentrant_subscribe_unsubscribe_asan() { using namespace Void::EventBus; EventBus bus{Mode::Sync}; int late_hits = 0; int primary_hits = 0; HandlerId removable_id = 0; bus.subscribe([&](const Ping& p) { ++primary_hits; if (p.value == 1) { bus.subscribe([&](const Ping&) { ++late_hits; }); bus.unsubscribe(removable_id); } }); int removed_hits = 0; removable_id = bus.subscribe([&](const Ping&) { ++removed_hits; }); bus.publish(Ping{1}); Void::Testing::expect_eq(primary_hits, 1); Void::Testing::expect_eq(removed_hits, 0); Void::Testing::expect_eq(late_hits, 0); bus.publish(Ping{2}); Void::Testing::expect_eq(primary_hits, 2); Void::Testing::expect_eq(removed_hits, 0); Void::Testing::expect_eq(late_hits, 1); } Void::Testing::Test bus_publish_after_close_closed() { using namespace Void::EventBus; EventBus bus{Mode::Async}; Void::Testing::require(bus.close_and_join().has_value()); auto sub_res = bus.try_subscribe([](const Ping&) {}); Void::Testing::require(!sub_res.has_value()); Void::Testing::expect_eq( async_code(sub_res.error()), static_cast(Void::Async::AsyncErrorCode::closed)); auto pub_res = bus.try_publish(Ping{42}); Void::Testing::require(!pub_res.has_value()); Void::Testing::expect_eq( async_code(pub_res.error()), static_cast(Void::Async::AsyncErrorCode::closed)); } Void::Testing::Test bus_sync_unsubscribe_releases_captured_state() { using namespace Void::EventBus; EventBus bus{Mode::Sync}; auto s1 = std::make_shared(1); const HandlerId id = bus.subscribe([s1](const Ping&) { (void)s1; }); Void::Testing::expect_eq(s1.use_count(), 2L); bus.unsubscribe(id); Void::Testing::expect_eq(s1.use_count(), 1L); auto s2 = std::make_shared(2); auto scoped = bus.try_subscribe([s2](const Ping&) { (void)s2; }); Void::Testing::require(scoped.has_value()); Void::Testing::expect_eq(s2.use_count(), 2L); scoped->reset(); Void::Testing::expect_eq(s2.use_count(), 1L); } Void::Testing::Test bus_legacy_async_unsubscribe_then_flush_releases_captured_state() { using namespace Void::EventBus; struct Hold {}; EventBus bus{Mode::Async}; std::latch entered{1}; std::promise rel; auto rel_fut = rel.get_future().share(); bus.subscribe([&](const Hold&) { entered.count_down(); rel_fut.wait(); }); auto sentinel = std::make_shared(77); const HandlerId id = bus.subscribe([sentinel](const Ping&) { (void)sentinel; }); bus.publish(Hold{}); entered.wait(); bus.publish(Ping{1}); bus.unsubscribe(id); Void::Testing::expect(sentinel.use_count() >= 2L); rel.set_value(); bus.flush(); Void::Testing::expect_eq(sentinel.use_count(), 1L); Void::Testing::require(bus.close_and_join().has_value()); } Void::Testing::Test bus_legacy_async_close_and_join_releases_capture_with_queued_copies() { using namespace Void::EventBus; struct Hold {}; EventBus bus{Mode::Async}; std::latch entered{1}; std::promise rel; auto rel_fut = rel.get_future().share(); bus.subscribe([&](const Hold&) { entered.count_down(); rel_fut.wait(); }); auto sentinel = std::make_shared(88); auto sub = bus.try_subscribe([sentinel](const Ping&) { (void)sentinel; }); Void::Testing::require(sub.has_value()); bus.publish(Hold{}); entered.wait(); // Queue copies on the worker queue while Hold keeps worker busy. bus.publish(Ping{1}); bus.publish(Ping{2}); Void::Testing::expect(sentinel.use_count() >= 3L); // Must purge queued copies for sub and release captured state immediately // without blocking on Hold or executing queued Ping callbacks. Void::Testing::require(sub->close_and_join().has_value()); Void::Testing::expect_eq(sentinel.use_count(), 1L); rel.set_value(); Void::Testing::require(bus.close_and_join().has_value()); } Void::Testing::Test bus_destructor_gates_admitted_deliveries() { using namespace Void::EventBus; // 1. Queued ManualExecutor deliveries are gated when ~EventBus destroys. { Void::Async::ManualExecutor manual; int delivered = 0; { EventBus bus{manual}; auto sub = bus.try_subscribe([&](const Ping&) { ++delivered; }); Void::Testing::require(sub.has_value()); Void::Testing::require(bus.try_publish(Ping{1}).has_value()); Void::Testing::require(bus.try_publish(Ping{2}).has_value()); } manual.drain(); Void::Testing::expect_eq(delivered, 0); } // 2. Destruction on a thread waits for OTHER thread's in-flight callback, // gates queued unstarted work, and self-destruction in callback doesn't deadlock. { auto exec_res = Void::Async::ThreadExecutor::create( Void::Async::ThreadExecutorOptions{.workers = 1, .max_admitted = 32}); Void::Testing::require(exec_res.has_value()); auto exec = std::move(*exec_res); Void::Testing::require(exec->start().has_value()); std::atomic started_count{0}; std::atomic gated_count{0}; std::latch first_in_flight{1}; std::promise allow_first_finish; auto finish_fut = allow_first_finish.get_future().share(); { std::optional bus{std::in_place, *exec}; auto s1 = bus->try_subscribe([&](const Ping& p) { if (p.value == 1) { started_count.fetch_add(1, std::memory_order_relaxed); first_in_flight.count_down(); finish_fut.wait(); } else { gated_count.fetch_add(1, std::memory_order_relaxed); } }); Void::Testing::require(s1.has_value()); Void::Testing::require(bus->try_publish(Ping{1}).has_value()); first_in_flight.wait(); Void::Testing::require(bus->try_publish(Ping{2}).has_value()); std::atomic destroyed{false}; std::thread destroyer([&]() { bus.reset(); destroyed.store(true, std::memory_order_release); }); std::this_thread::sleep_for(std::chrono::milliseconds{15}); Void::Testing::expect(!destroyed.load(std::memory_order_acquire)); allow_first_finish.set_value(); destroyer.join(); } exec->close(); Void::Testing::require(exec->join().has_value()); Void::Testing::expect_eq(started_count.load(), 1); Void::Testing::expect_eq(gated_count.load(), 0); } // 3. Destroying EventBus from inside its own Sync callback doesn't deadlock. { std::optional bus{std::in_place, Mode::Sync}; int ran = 0; bus->subscribe([&](const Ping&) { ++ran; bus.reset(); }); bus->publish(Ping{1}); Void::Testing::expect_eq(ran, 1); Void::Testing::expect(!bus.has_value()); } } Void::Testing::Test bus_flush_blocking_self_join_and_manual_invalid() { using namespace Void::EventBus; // ManualExecutor rejects flush_blocking and close_and_join with invalid_argument. { Void::Async::ManualExecutor manual; EventBus bus{manual}; auto fb = bus.flush_blocking(); Void::Testing::require(!fb.has_value()); Void::Testing::expect_eq( async_code(fb.error()), static_cast(Void::Async::AsyncErrorCode::invalid_argument)); auto cj = bus.close_and_join(); Void::Testing::require(!cj.has_value()); Void::Testing::expect_eq( async_code(cj.error()), static_cast(Void::Async::AsyncErrorCode::invalid_argument)); } // Executor worker thread calling flush_blocking or close_and_join gets self_join. { auto exec_res = Void::Async::ThreadExecutor::create( Void::Async::ThreadExecutorOptions{.workers = 1, .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::optional> fb_res; std::optional> cj_res; auto sub = bus.try_subscribe([&](const Ping&) { fb_res = bus.flush_blocking(); cj_res = bus.close_and_join(); }); Void::Testing::require(sub.has_value()); Void::Testing::require(bus.try_publish(Ping{1}).has_value()); Void::Testing::require(bus.flush_blocking().has_value()); Void::Testing::require(fb_res.has_value() && !fb_res->has_value()); Void::Testing::expect_eq( async_code(fb_res->error()), static_cast(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(Void::Async::AsyncErrorCode::self_join)); // Wrong executor passed to flush_pumping returns invalid_argument. Void::Async::ManualExecutor other_manual; auto fp = bus.flush_pumping(other_manual); Void::Testing::require(!fp.has_value()); Void::Testing::expect_eq( async_code(fp.error()), static_cast(Void::Async::AsyncErrorCode::invalid_argument)); Void::Testing::require(bus.close_and_join().has_value()); exec->close(); 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> fp_res; std::optional> cj_res; auto sub = bus.try_subscribe([&](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(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(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* p) : destroyed(p) {} std::atomic* 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 destroyed{false}; std::latch entered{1}; std::promise rel; auto rel_fut = rel.get_future().share(); auto guard = std::make_shared(&destroyed); auto sub = bus.try_subscribe([&, 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 destroyed{false}; std::latch entered{1}; std::promise rel; auto rel_fut = rel.get_future().share(); auto guard = std::make_shared(&destroyed); auto sub = bus.try_subscribe([&, 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* dropped{nullptr}; std::atomic* 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 dropped{false}; std::atomic dtor_calls{0}; std::atomic b_ran{0}; std::optional> sub_b_join_res; auto sub_b_ptr = std::make_shared>(); // Subscribe A before B so run_worker visits A first for the same queued item. auto sub_a = bus.try_subscribe([&](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( new MuLockingDtor{&bus, &dropped, &dtor_calls}); auto sb = bus.try_subscribe([&, 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()); }