feat(async): add EventBus injected/joined lifecycle and Observable reentrancy queue (M-ASYNC-05)
This commit is contained in:
1 parent
1e752da590
commit
ef85ce07b7
4 files changed
+2156
-288
No files matched your search
@@ -0,0 +1,600 @@
|
||||
#include <Void/Async/EventBus.hpp>
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/Task.hpp>
|
||||
#include <Void/Async/ThreadExecutor.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <coroutine>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <future>
|
||||
#include <latch>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
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<Ping>([&](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<Ping>([](const Ping&) {});
|
||||
auto s2 = bus.try_subscribe<Ping>([](const Ping&) {});
|
||||
Void::Testing::require(s1.has_value());
|
||||
Void::Testing::require(s2.has_value());
|
||||
|
||||
auto s3 = bus.try_subscribe<Ping>([](const Ping&) {});
|
||||
Void::Testing::require(!s3.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
async_code(s3.error()),
|
||||
static_cast<std::int32_t>(Void::Async::AsyncErrorCode::overloaded));
|
||||
|
||||
s1->reset();
|
||||
auto s4 = bus.try_subscribe<Ping>([](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<Ping>([&](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<std::int32_t>(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<int>(99);
|
||||
std::latch started{1};
|
||||
std::promise<void> release_promise;
|
||||
auto release_future = release_promise.get_future().share();
|
||||
std::atomic<bool> finished{false};
|
||||
|
||||
auto sub = bus.try_subscribe<Ping>(
|
||||
[&, 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<bool> join_done{false};
|
||||
Void::Result::Result<void> 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<Ping>([&](const Ping&) {
|
||||
++first_hits;
|
||||
bus.unsubscribe(id2);
|
||||
});
|
||||
id2 = bus.subscribe<Ping>([&](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<void> join_res;
|
||||
int calls = 0;
|
||||
|
||||
auto sub_res = bus.try_subscribe<Ping>([&](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<std::int32_t>(Void::Async::AsyncErrorCode::self_join));
|
||||
Void::Testing::expect(!static_cast<bool>(sub));
|
||||
}
|
||||
|
||||
Void::Testing::Test bus_callback_throw_to_error_sink_others_run()
|
||||
{
|
||||
using namespace Void::EventBus;
|
||||
Void::Async::ManualExecutor manual;
|
||||
std::vector<std::string> 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<Ping>([](const Ping&) {
|
||||
throw std::runtime_error("boom_injected");
|
||||
});
|
||||
auto s2 = bus.try_subscribe<Ping>([&](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<Ping>([](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<int> second_hits{0};
|
||||
bus.subscribe<Ping>([](const Ping&) {
|
||||
throw std::runtime_error("cb_boom");
|
||||
});
|
||||
bus.subscribe<Ping>([&](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<void> release_blocker;
|
||||
auto release_future = release_blocker.get_future().share();
|
||||
|
||||
bus.subscribe<Unblock>([&](const Unblock&) {
|
||||
blocker_entered.count_down();
|
||||
release_future.wait();
|
||||
});
|
||||
|
||||
std::atomic<int> target_hits{0};
|
||||
const HandlerId target_id = bus.subscribe<Ping>([&](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<Ping>([&](const Ping& p) {
|
||||
++primary_hits;
|
||||
if (p.value == 1) {
|
||||
bus.subscribe<Ping>([&](const Ping&) { ++late_hits; });
|
||||
bus.unsubscribe(removable_id);
|
||||
}
|
||||
});
|
||||
int removed_hits = 0;
|
||||
removable_id = bus.subscribe<Ping>([&](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<Ping>([](const Ping&) {});
|
||||
Void::Testing::require(!sub_res.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
async_code(sub_res.error()),
|
||||
static_cast<std::int32_t>(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<std::int32_t>(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<int>(1);
|
||||
const HandlerId id = bus.subscribe<Ping>([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<int>(2);
|
||||
auto scoped = bus.try_subscribe<Ping>([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<void> rel;
|
||||
auto rel_fut = rel.get_future().share();
|
||||
bus.subscribe<Hold>([&](const Hold&) {
|
||||
entered.count_down();
|
||||
rel_fut.wait();
|
||||
});
|
||||
|
||||
auto sentinel = std::make_shared<int>(77);
|
||||
const HandlerId id = bus.subscribe<Ping>([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<void> rel;
|
||||
auto rel_fut = rel.get_future().share();
|
||||
bus.subscribe<Hold>([&](const Hold&) {
|
||||
entered.count_down();
|
||||
rel_fut.wait();
|
||||
});
|
||||
|
||||
auto sentinel = std::make_shared<int>(88);
|
||||
auto sub = bus.try_subscribe<Ping>([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<Ping>([&](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<int> started_count{0};
|
||||
std::atomic<int> gated_count{0};
|
||||
std::latch first_in_flight{1};
|
||||
std::promise<void> allow_first_finish;
|
||||
auto finish_fut = allow_first_finish.get_future().share();
|
||||
|
||||
{
|
||||
std::optional<EventBus> bus{std::in_place, *exec};
|
||||
auto s1 = bus->try_subscribe<Ping>([&](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<bool> 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<EventBus> bus{std::in_place, Mode::Sync};
|
||||
int ran = 0;
|
||||
bus->subscribe<Ping>([&](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<std::int32_t>(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<std::int32_t>(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<Void::Result::Result<void>> fb_res;
|
||||
std::optional<Void::Result::Result<void>> cj_res;
|
||||
auto sub = bus.try_subscribe<Ping>([&](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<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));
|
||||
|
||||
// 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<std::int32_t>(Void::Async::AsyncErrorCode::invalid_argument));
|
||||
|
||||
Void::Testing::require(bus.close_and_join().has_value());
|
||||
exec->close();
|
||||
Void::Testing::require(exec->join().has_value());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user