#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { using namespace std::chrono_literals; using Void::Async::AdmissionError; using Void::Async::ManualExecutor; using Void::Async::OperationOptions; using Void::Async::TimerQueue; using Void::Async::TimerQueueOptions; using Void::Runtime::Component; using Void::Runtime::EmbeddedResources; using Void::Runtime::Host; using Void::Runtime::HostBuilder; using Void::Runtime::HostContext; using Void::Runtime::HostState; using Void::Runtime::Options; using Void::Runtime::ResourceKind; using Void::Runtime::RuntimeErrorCode; using Void::Runtime::ShutdownPhase; using Void::Runtime::ShutdownReport; using Void::Runtime::SignalGate; using Void::Runtime::StartOptions; using Void::Testing::expect; using Void::Testing::expect_eq; using Void::Testing::require; [[nodiscard]] std::size_t count_tasks() { std::size_t count = 0; std::error_code ec; for (const auto& _ : std::filesystem::directory_iterator("/proc/self/task", ec)) { (void)_; ++count; } return count; } // Linux pthread_join() returns at mm_release(), slightly before // exit_notify()/release_task() removes the thread from /proc/self/task. [[nodiscard]] std::size_t wait_for_task_count( std::size_t expected, std::chrono::milliseconds timeout = 500ms) { const auto deadline = std::chrono::steady_clock::now() + timeout; std::size_t current = count_tasks(); while (current != expected && std::chrono::steady_clock::now() < deadline) { std::this_thread::sleep_for(1ms); current = count_tasks(); } return current; } [[nodiscard]] std::size_t settle_baseline_tasks() { std::size_t prev = count_tasks(); if (prev <= 1) { return prev; } const auto deadline = std::chrono::steady_clock::now() + 500ms; int stable_samples = 0; while (std::chrono::steady_clock::now() < deadline) { std::this_thread::sleep_for(2ms); const std::size_t cur = count_tasks(); if (cur <= 1) { return cur; } if (cur == prev) { if (++stable_samples >= 3) { return cur; } } else { prev = cur; stable_samples = 0; } } return prev; } class Gate { public: void wait() { std::unique_lock lk{mu_}; ++waiting_; cv_.notify_all(); cv_.wait(lk, [&] { return open_; }); } [[nodiscard]] bool wait_for_waiters(int n, std::chrono::milliseconds timeout = 5000ms) { std::unique_lock lk{mu_}; return cv_.wait_for(lk, timeout, [&] { return waiting_ >= n; }); } void open() { std::lock_guard lk{mu_}; open_ = true; cv_.notify_all(); } private: std::mutex mu_{}; std::condition_variable cv_{}; bool open_{false}; int waiting_{0}; }; class CountingSlowComponent final : public Component { public: std::atomic stop_calls{0}; Void::Result::Result start(HostContext&) override { return {}; } Void::Coro::Task> stop(OperationOptions) override { stop_calls.fetch_add(1, std::memory_order_relaxed); std::this_thread::sleep_for(15ms); co_return Void::Result::Result{}; } }; class CompletionProbeComponent final : public Component { public: std::atomic stop_ran{false}; Void::Result::Result start(HostContext&) override { return {}; } Void::Coro::Task> stop(OperationOptions) override { stop_ran.store(true, std::memory_order_release); co_return Void::Result::Result{}; } }; struct ExportOrderLog { std::mutex mu{}; std::vector events{}; void record(std::string ev) { std::lock_guard lk{mu}; events.push_back(std::move(ev)); } }; class MockExporterComponent final : public Component { public: MockExporterComponent(std::shared_ptr log, Void::IO::ByteStream* out_stream, Void::IO::BufferLease* out_lease, std::atomic* stop_started) : log_(std::move(log)) , stream_out_(out_stream) , lease_out_(out_lease) , stop_started_(stop_started) { } ~MockExporterComponent() override { log_->record("exporter_destroyed"); } Void::Result::Result start(HostContext& ctx) override { io_ = ctx.io(); auto s = io_->create_mock_stream(); if (!s.has_value()) { return std::unexpected{std::move(s.error())}; } stream_ = std::move(*s); *stream_out_ = Void::IO::ByteStream{}; // kept internal; pass raw ptr via field stream_ptr_ = &stream_; auto lr = io_->allocate_buffer(128); if (!lr.has_value()) { return std::unexpected{std::move(lr.error())}; } lease_ = std::move(*lr); (void)lease_out_; return {}; } [[nodiscard]] Void::IO::ByteStream& stream() noexcept { return stream_; } Void::Coro::Task> stop(OperationOptions) override { stop_started_->store(true, std::memory_order_release); std::array buf{}; // Ignores OperationOptions::stop intentionally to exercise IO/worker retention past flush deadline. (void)co_await stream_.read_some(buf); lease_.reset(); log_->record("exporter_stopped"); co_return Void::Result::Result{}; } private: std::shared_ptr log_; Void::IO::Driver* io_{nullptr}; Void::IO::ByteStream stream_{}; Void::IO::ByteStream* stream_ptr_{nullptr}; Void::IO::ByteStream* stream_out_{nullptr}; Void::IO::BufferLease lease_{}; Void::IO::BufferLease* lease_out_{nullptr}; std::atomic* stop_started_{nullptr}; }; class EmbeddedTimerComponent final : public Component { public: EmbeddedTimerComponent(TimerQueue* tq, const Void::Time::MonotonicClock* clock, std::atomic* stop_done) : tq_(tq) , clock_(clock) , stop_done_(stop_done) { } Void::Result::Result start(HostContext&) override { return {}; } Void::Coro::Task> stop(OperationOptions opts) override { (void)opts; auto r = co_await tq_->sleep_until(clock_->now() + 10ms); stop_done_->store(true, std::memory_order_release); co_return r; } private: TimerQueue* tq_; const Void::Time::MonotonicClock* clock_; std::atomic* stop_done_; }; class TrackingGate final : public SignalGate { public: std::atomic uninstall_calls{0}; bool last_on_owner{false}; Void::Result::Result install() override { return {}; } void uninstall(bool on_owner_thread) noexcept override { last_on_owner = on_owner_thread; uninstall_calls.fetch_add(1, std::memory_order_relaxed); } }; class SelfJoinFileSyscalls final : public Void::IO::FileSyscalls { public: Host* host{nullptr}; std::optional> res{}; Void::IO::FileSyscallResult pread(int fd, std::span buf, std::uint64_t offset) noexcept override { if (host != nullptr) { res = host->shutdown_blocking(); } return Void::IO::FileSyscalls::pread(fd, buf, offset); } }; [[nodiscard]] Void::Coro::Task block_on_gate_task( Gate* g, std::atomic* finished) { g->wait(); if (finished != nullptr) { finished->store(true, std::memory_order_release); } co_return; } [[nodiscard]] Void::Coro::Task quick_done_task(std::atomic* finished) { finished->store(true, std::memory_order_release); co_return; } } // namespace Void::Testing::Test runtime_shutdown_concurrent_callers_single_teardown_tsan() { auto comp = std::make_shared(); Options opts; opts.cpu_workers = 2; opts.file_workers = 1; opts.io.preference = Void::IO::BackendPreference::mock; HostBuilder builder; builder.with_options(opts); require(builder.add_component("slow", ResourceKind::service, comp).has_value()); auto host_res = builder.build(); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start().has_value()); std::vector>> reports(4); std::vector callers; for (std::size_t i = 0; i < 4; ++i) { callers.emplace_back([&, i]() { reports[i] = host.shutdown_blocking(); }); } for (auto& t : callers) { t.join(); } for (const auto& r : reports) { require(r.has_value() && r->has_value()); expect((*r)->complete); expect((*r)->state == HostState::stopped); } expect_eq(comp->stop_calls.load(), 1); } Void::Testing::Test runtime_shutdown_full_normal_queue_still_delivers_completion() { auto probe = std::make_shared(); Options opts; opts.cpu_workers = 1; opts.file_workers = 1; opts.limits.max_cpu_tasks = 2; opts.shutdown.drain_grace = 5ms; opts.io.preference = Void::IO::BackendPreference::mock; HostBuilder builder; builder.with_options(opts); require(builder.add_component("probe", ResourceKind::service, probe).has_value()); auto host_res = builder.build(); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start().has_value()); Gate gate; std::atomic done1{false}; std::atomic done2{false}; auto j1 = host.spawn(block_on_gate_task(&gate, &done1)); auto j2 = host.spawn(block_on_gate_task(&gate, &done2)); require(j1.has_value() && j2.has_value()); // Normal queue is completely full -> 3rd normal-lane spawn rejected as overloaded. auto j3 = host.spawn(quick_done_task(&done1)); require(!j3.has_value()); expect(j3.error() == AdmissionError::overloaded); // Open gate after first worker enters wait; 2nd task sits queued behind it. std::thread releaser([&]() { (void)gate.wait_for_waiters(1); std::this_thread::sleep_for(15ms); gate.open(); }); auto rep = host.shutdown_blocking(); releaser.join(); require(rep.has_value()); expect(rep->complete); expect(probe->stop_ran.load(std::memory_order_acquire)); expect(done1.load() && done2.load()); } Void::Testing::Test runtime_shutdown_inflight_export_retains_io_workers_through_flush_expiry_then_completes() { auto log = std::make_shared(); Void::IO::ByteStream dummy_s; Void::IO::BufferLease dummy_l; std::atomic exp_stop_started{false}; auto exporter = std::make_shared( log, &dummy_s, &dummy_l, &exp_stop_started); Options opts; opts.cpu_workers = 1; opts.file_workers = 1; opts.io.preference = Void::IO::BackendPreference::mock; opts.shutdown.drain_grace = 5ms; opts.shutdown.export_flush_timeout = 15ms; opts.shutdown.total_timeout = 1000ms; auto host_res = [&]() { HostBuilder builder; builder.with_options(opts); require(builder.add_component("exp", ResourceKind::exporter, exporter) .has_value()); return builder.build(); }(); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start(StartOptions{ .on_phase = [&](ShutdownPhase p) { if (p == ShutdownPhase::releasing) { log->record("executor_joined"); } }, }).has_value()); auto* ctrl = host.io()->mock_controller(); require(ctrl != nullptr); // First bounded shutdown expires at flushing_exporters after export_flush_timeout. std::optional> rep1; std::thread stopper([&]() { rep1 = host.shutdown_blocking(OperationOptions{ .deadline = Void::Time::deadline_after(host.clock(), 40ms)}); }); while (!exp_stop_started.load(std::memory_order_acquire)) { std::this_thread::sleep_for(1ms); } std::optional tok; for (int i = 0; i < 200 && !tok.has_value(); ++i) { tok = ctrl->try_acquire_read(exporter->stream()); if (!tok.has_value()) { std::this_thread::sleep_for(1ms); } } require(tok.has_value()); stopper.join(); require(rep1.has_value() && rep1->has_value()); expect(!(*rep1)->complete); expect((*rep1)->state == HostState::shutdown_incomplete); expect((*rep1)->phase == ShutdownPhase::flushing_exporters); expect((*rep1)->export_flush_expired); expect((*rep1)->pending_io_operations >= std::size_t{1}); expect((*rep1)->retained_buffer_bytes > std::size_t{0}); expect_eq(host.started_worker_count(), std::size_t{3}); // Complete held mock read, drop caller shared_ptr so Host owns last exporter ref. exporter.reset(); const std::array ok{std::byte{'o'}, std::byte{'k'}}; expect(ctrl->finish_read_data(*tok, ok, true)); auto rep2 = host.shutdown_blocking(); require(rep2.has_value()); expect(rep2->complete); expect(rep2->state == HostState::stopped); expect_eq(rep2->pending_io_operations, std::size_t{0}); expect_eq(host.started_worker_count(), std::size_t{0}); std::lock_guard lk{log->mu}; require(log->events.size() == 3); expect_eq(log->events[0], std::string{"exporter_stopped"}); expect_eq(log->events[1], std::string{"exporter_destroyed"}); expect_eq(log->events[2], std::string{"executor_joined"}); } Void::Testing::Test runtime_shutdown_stalled_business_task_incomplete_retains() { Options opts; opts.cpu_workers = 1; opts.file_workers = 1; opts.io.preference = Void::IO::BackendPreference::mock; opts.shutdown.drain_grace = 5ms; opts.shutdown.export_flush_timeout = 100ms; opts.shutdown.total_timeout = 1000ms; auto host_res = Host::create(opts); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start().has_value()); Gate gate; std::atomic done{false}; auto jh = host.spawn(block_on_gate_task(&gate, &done)); require(jh.has_value()); require(gate.wait_for_waiters(1)); auto r1 = host.shutdown_blocking(OperationOptions{ .deadline = Void::Time::deadline_after(host.clock(), 15ms)}); require(r1.has_value()); expect(!r1->complete); expect(r1->state == HostState::shutdown_incomplete); expect(r1->phase == ShutdownPhase::joining_business); expect(r1->drain_grace_expired); expect_eq(r1->pending_business_tasks, std::size_t{1}); expect_eq(host.started_worker_count(), std::size_t{3}); gate.open(); auto r2 = host.shutdown_blocking(); require(r2.has_value()); expect(r2->complete); expect_eq(r2->pending_business_tasks, std::size_t{0}); expect(done.load(std::memory_order_acquire)); } Void::Testing::Test runtime_shutdown_child_spawn_during_close_rejected() { Options opts; opts.cpu_workers = 1; opts.file_workers = 1; opts.io.preference = Void::IO::BackendPreference::mock; auto host_res = Host::create(opts); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start().has_value()); host.begin_shutdown(); std::atomic ran{false}; auto late = host.spawn(quick_done_task(&ran)); require(!late.has_value()); expect(late.error() == AdmissionError::stopped); expect(!ran.load(std::memory_order_acquire)); auto rep = host.shutdown_blocking(); require(rep.has_value()); expect(rep->complete); } Void::Testing::Test runtime_shutdown_budget_expired_caller_deadline_incomplete_then_resumes() { Void::Time::ManualClock clock{ Void::Time::MonotonicClock::time_point{std::chrono::seconds{20}}}; Void::Async::ManualExecutor manual; auto drv_res = Void::IO::Driver::create( manual, Void::IO::BackendOptions{.preference = Void::IO::BackendPreference::mock}, clock); require(drv_res.has_value()); Options opts; opts.shutdown.drain_grace = 500ms; opts.shutdown.export_flush_timeout = 100ms; opts.shutdown.total_timeout = 1000ms; auto host_res = Host::create_embedded( opts, EmbeddedResources{ .executor = &manual, .io = drv_res->get(), .clock = &clock, }); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start().has_value()); std::atomic task_done{false}; // Schedule a task suspended waiting on ManualExecutor until drained later. auto jh = host.spawn(quick_done_task(&task_done)); require(jh.has_value()); // Already-expired caller deadline stops before draining -> min(caller, policy). auto r1 = host.poll_shutdown(OperationOptions{ .deadline = clock.now() - 1ms}); require(r1.has_value()); expect(!r1->complete); expect(r1->state == HostState::shutdown_incomplete); expect(r1->phase == ShutdownPhase::draining); expect(!r1->drain_grace_expired); expect(!task_done.load()); // Advance ManualClock? Not needed; give non-expired deadline to pump and complete. auto r2 = host.poll_shutdown(OperationOptions{ .deadline = clock.now() + 100ms}); require(r2.has_value()); expect(r2->complete); expect(r2->state == HostState::stopped); expect(task_done.load()); } Void::Testing::Test runtime_shutdown_budget_bytes_retained_while_incomplete_zero_after_complete() { Options opts; opts.cpu_workers = 1; opts.file_workers = 1; opts.io.preference = Void::IO::BackendPreference::mock; opts.shutdown.drain_grace = 5ms; auto host_res = Host::create(opts); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start().has_value()); Gate gate; auto hold_mem_task = [](Gate* g, Void::Memory::BudgetResource* budget) -> Void::Coro::Task { std::pmr::vector buf(4096, budget); buf[0] = std::byte{1}; g->wait(); co_return; }; auto jh = host.spawn(hold_mem_task(&gate, &host.memory_budget())); require(jh.has_value()); require(gate.wait_for_waiters(1)); auto r1 = host.shutdown_blocking(OperationOptions{ .deadline = Void::Time::deadline_after(host.clock(), 15ms)}); require(r1.has_value()); expect(!r1->complete); expect(r1->retained_buffer_bytes >= std::size_t{4096}); expect(host.memory_budget().stats().committed_live >= std::size_t{4096}); expect(host.cpu_admitted() >= std::size_t{1}); gate.open(); auto r2 = host.shutdown_blocking(); require(r2.has_value()); expect(r2->complete); expect_eq(r2->retained_buffer_bytes, std::size_t{0}); expect_eq(host.memory_budget().stats().committed_live, std::size_t{0}); expect_eq(host.cpu_admitted(), std::size_t{0}); } Void::Testing::Test runtime_shutdown_on_worker_self_join() { // 1. Self-join from owned CPU worker thread. { Options opts; opts.cpu_workers = 1; opts.file_workers = 1; opts.io.preference = Void::IO::BackendPreference::mock; auto host_res = Host::create(opts); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start().has_value()); std::optional> worker_res; auto self_task = [](Host* h, std::optional>* out) -> Void::Coro::Task { *out = h->shutdown_blocking(); co_return; }; auto jh = host.spawn(self_task(&host, &worker_res)); require(jh.has_value()); require(jh->join_blocking().has_value()); require(worker_res.has_value() && !worker_res->has_value()); expect_eq(worker_res->error().code().value, static_cast(RuntimeErrorCode::self_join)); require(host.shutdown_blocking().has_value()); } // 2. Self-join from owned IO file-worker thread. { SelfJoinFileSyscalls sys; const auto tmp = std::filesystem::temp_directory_path() / "void_w11_rt_selfjoin.bin"; { std::ofstream ofs(tmp, std::ios::binary); ofs << "abc"; } Options opts; opts.cpu_workers = 1; opts.file_workers = 1; opts.io.preference = Void::IO::BackendPreference::epoll; opts.io.file_syscalls = &sys; auto host_res = Host::create(opts); require(host_res.has_value()); Host host = std::move(*host_res); sys.host = &host; require(host.start().has_value()); auto f = host.io()->open_file(tmp); require(f.has_value()); auto fread_task = [](Void::IO::File file) -> Void::Coro::Task { std::array b{}; (void)co_await file.read_at(0, b); }; auto jh = host.spawn(fread_task(std::move(*f))); require(jh.has_value()); require(jh->join_blocking().has_value()); require(host.shutdown_blocking().has_value()); std::error_code ec; std::filesystem::remove(tmp, ec); require(sys.res.has_value() && !sys.res->has_value()); expect_eq(sys.res->error().code().value, static_cast(RuntimeErrorCode::self_join)); } } Void::Testing::Test runtime_shutdown_non_owner_defers_gate_release() { TrackingGate gate; Options opts; opts.cpu_workers = 1; opts.file_workers = 1; opts.io.preference = Void::IO::BackendPreference::mock; auto host_res = Host::create(opts); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start(StartOptions{.signal_gate = &gate}).has_value()); std::optional> off_res; std::thread other([&]() { off_res = host.shutdown_blocking(); }); other.join(); require(off_res.has_value() && off_res->has_value()); expect((*off_res)->complete); expect((*off_res)->state == HostState::stopped); expect((*off_res)->owner_release_pending); expect_eq(gate.uninstall_calls.load(), 0); // Owner thread call completes gate uninstall once. auto owner_res = host.shutdown_blocking(); require(owner_res.has_value()); expect(owner_res->complete); expect(!owner_res->owner_release_pending); expect_eq(gate.uninstall_calls.load(), 1); expect(gate.last_on_owner); } Void::Testing::Test runtime_shutdown_embedded_pumping() { const std::size_t baseline_tasks = settle_baseline_tasks(); ManualExecutor manual; Void::Time::ManualClock clock{ Void::Time::MonotonicClock::time_point{std::chrono::seconds{100}}}; auto drv_res = Void::IO::Driver::create( manual, Void::IO::BackendOptions{.preference = Void::IO::BackendPreference::mock}, clock); require(drv_res.has_value()); auto tq_res = TimerQueue::create(manual, clock, TimerQueueOptions{}); require(tq_res.has_value()); std::atomic comp_stop_done{false}; auto comp = std::make_shared( tq_res->get(), &clock, &comp_stop_done); HostBuilder builder; builder.with_embedded(EmbeddedResources{ .executor = &manual, .io = drv_res->get(), .clock = &clock, }); require(builder.add_component("timer_comp", ResourceKind::service, comp).has_value()); auto host_res = builder.build(); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start().has_value()); // Advance ManualClock from another thread or pump_until helper? Use TimerQueue advance on separate tick: std::atomic stop_advancer{false}; std::thread advancer([&]() { while (!stop_advancer.load(std::memory_order_acquire)) { (void)clock.advance(2ms); std::this_thread::sleep_for(1ms); } }); auto rep = host.shutdown_pumping(manual, OperationOptions{}, tq_res->get()); stop_advancer.store(true, std::memory_order_release); advancer.join(); require(rep.has_value()); expect(rep->complete); expect(comp_stop_done.load(std::memory_order_acquire)); expect_eq(wait_for_task_count(baseline_tasks), baseline_tasks); } Void::Testing::Test runtime_embedded_shutdown_never_shuts_borrowed_driver_or_executor() { ManualExecutor manual; Void::Time::ManualClock clock; auto drv_res = Void::IO::Driver::create( manual, Void::IO::BackendOptions{.preference = Void::IO::BackendPreference::mock}, clock); require(drv_res.has_value()); { auto host_res = Host::create_embedded( Options{}, EmbeddedResources{ .executor = &manual, .io = drv_res->get(), .clock = &clock, }); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start().has_value()); auto rep = host.shutdown_pumping(manual); require(rep.has_value()); expect(rep->complete); } // Borrowed driver and executor MUST remain open and operational! expect(!(*drv_res)->shutdown_complete()); expect((*drv_res)->create_mock_stream().has_value()); expect(manual.is_accepting()); } Void::Testing::Test runtime_embedded_shutdown_blocking_invalid_argument() { ManualExecutor manual; Void::Time::ManualClock clock; auto drv_res = Void::IO::Driver::create( manual, Void::IO::BackendOptions{.preference = Void::IO::BackendPreference::mock}, clock); require(drv_res.has_value()); auto host_res = Host::create_embedded( Options{}, EmbeddedResources{ .executor = &manual, .io = drv_res->get(), .clock = &clock, }); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start().has_value()); auto bad = host.shutdown_blocking(); require(!bad.has_value()); expect_eq(bad.error().code().value, static_cast(RuntimeErrorCode::invalid_argument)); require(host.shutdown_pumping(manual).has_value()); } Void::Testing::Test runtime_managed_shutdown_pumping_invalid_argument() { Options opts; opts.cpu_workers = 1; opts.file_workers = 1; opts.io.preference = Void::IO::BackendPreference::mock; auto host_res = Host::create(opts); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start().has_value()); ManualExecutor other; auto bad = host.shutdown_pumping(other); require(!bad.has_value()); expect_eq(bad.error().code().value, static_cast(RuntimeErrorCode::invalid_argument)); require(host.shutdown_blocking().has_value()); } Void::Testing::Test runtime_destructor_joins_after_start() { const std::size_t baseline = settle_baseline_tasks(); std::atomic task_finished{false}; { Options opts; opts.cpu_workers = 2; opts.file_workers = 1; opts.io.preference = Void::IO::BackendPreference::mock; auto host_res = Host::create(opts); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start().has_value()); auto jh = host.spawn(quick_done_task(&task_finished)); require(jh.has_value()); } // ~Host() blocks until shutdown & thread join complete. expect(task_finished.load(std::memory_order_acquire)); expect_eq(wait_for_task_count(baseline), baseline); } Void::Testing::Test runtime_destructor_blocks_until_complete_after_incomplete_shutdown() { const std::size_t baseline = settle_baseline_tasks(); std::atomic task_finished{false}; Gate gate; std::thread releaser; { Options opts; opts.cpu_workers = 1; opts.file_workers = 1; opts.io.preference = Void::IO::BackendPreference::mock; opts.shutdown.drain_grace = 5ms; opts.shutdown.export_flush_timeout = 100ms; opts.shutdown.total_timeout = 1000ms; auto host_res = Host::create(opts); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start().has_value()); auto jh = host.spawn(block_on_gate_task(&gate, &task_finished)); require(jh.has_value()); require(gate.wait_for_waiters(1)); auto r1 = host.shutdown_blocking(OperationOptions{ .deadline = host.clock().now()}); require(r1.has_value()); expect(!r1->complete); expect(!task_finished.load(std::memory_order_acquire)); releaser = std::thread([&]() { std::this_thread::sleep_for(20ms); gate.open(); }); } // ~Host() MUST block ignoring total_timeout cap until releaser opens gate & task completes! releaser.join(); expect(task_finished.load(std::memory_order_acquire)); expect_eq(wait_for_task_count(baseline), baseline); } Void::Testing::Test runtime_embedded_manual_poll_shutdown_completes_exporter() { ManualExecutor manual; Void::Time::ManualClock clock; auto drv_res = Void::IO::Driver::create( manual, Void::IO::BackendOptions{.preference = Void::IO::BackendPreference::mock}, clock); require(drv_res.has_value()); auto exp = std::make_shared(); HostBuilder builder; builder.with_embedded(EmbeddedResources{ .executor = &manual, .io = drv_res->get(), .clock = &clock, }); require(builder.add_component("exp", ResourceKind::exporter, exp).has_value()); auto host_res = builder.build(); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start().has_value()); std::optional rep; for (int i = 0; i < 10; ++i) { auto pr = host.poll_shutdown(); require(pr.has_value()); rep = *pr; if (rep->complete) { break; } } require(rep.has_value()); expect(rep->complete); expect(rep->state == HostState::stopped); expect(exp->stop_ran.load(std::memory_order_acquire)); } Void::Testing::Test runtime_embedded_thread_executor_shutdown_blocking_succeeds() { const std::size_t baseline = settle_baseline_tasks(); auto exec_res = Void::Async::ThreadExecutor::create( Void::Async::ThreadExecutorOptions{.workers = 2, .max_admitted = 32}); require(exec_res.has_value()); auto exec = std::move(*exec_res); require(exec->start().has_value()); auto drv_res = Void::IO::Driver::create( *exec, Void::IO::BackendOptions{.preference = Void::IO::BackendPreference::mock}); require(drv_res.has_value()); auto svc = std::make_shared(); std::atomic task_done{false}; { HostBuilder builder; builder.with_embedded(EmbeddedResources{ .executor = exec.get(), .io = drv_res->get(), }); require(builder.add_component("svc", ResourceKind::service, svc).has_value()); auto host_res = builder.build(); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start().has_value()); auto jh = host.spawn(quick_done_task(&task_done)); require(jh.has_value()); auto rep = host.shutdown_blocking(); require(rep.has_value()); expect(rep->complete); expect(rep->state == HostState::stopped); } expect(task_done.load(std::memory_order_acquire)); expect(svc->stop_ran.load(std::memory_order_acquire)); expect(!(*drv_res)->shutdown_complete()); expect(exec->try_reserve_slot().has_value()); exec->release_reserved(); auto drep = (*drv_res)->shutdown(OperationOptions{}); require(drep.has_value() && drep->complete); exec->close(); require(exec->join().has_value()); expect_eq(wait_for_task_count(baseline), baseline); }