feat(runtime): add 8-step Host start, SignalGate, and 11-phase structured shutdown (S-RUNTIME-02/03)
This commit is contained in:
1 parent
50029e1f7b
commit
7bea7df46d
6 files changed
+3816
-27
No files matched your search
File diff suppressed because it is too large.
Load diff
+231
-27
@@ -1,15 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Async/Detail/Types.hpp>
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/Task.hpp>
|
||||
#include <Void/Async/TaskScope.hpp>
|
||||
#include <Void/Async/Timer.hpp>
|
||||
#include <Void/IO/Driver.hpp>
|
||||
#include <Void/Memory/BudgetResource.hpp>
|
||||
#include <Void/Runtime/Lifecycle.hpp>
|
||||
#include <Void/Runtime/Options.hpp>
|
||||
#include <Void/Runtime/ResourceGraph.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@@ -60,6 +68,14 @@ struct ShutdownReport {
|
||||
HostState state{HostState::stopped};
|
||||
std::size_t pending_owners{0};
|
||||
std::vector<std::string> pending_owner_ids{};
|
||||
ShutdownPhase phase{ShutdownPhase::not_started};
|
||||
std::size_t pending_business_tasks{0};
|
||||
std::size_t pending_cpu_admitted{0};
|
||||
std::size_t pending_io_operations{0};
|
||||
std::size_t retained_buffer_bytes{0};
|
||||
bool drain_grace_expired{false};
|
||||
bool export_flush_expired{false};
|
||||
bool owner_release_pending{false};
|
||||
};
|
||||
|
||||
[[nodiscard]] inline ResourceGraph make_default_resource_graph(bool embedded_mode)
|
||||
@@ -79,9 +95,16 @@ struct ShutdownReport {
|
||||
return g;
|
||||
}
|
||||
|
||||
// Inert P1 Runtime composition handle: validates options, memory/time/IO limits,
|
||||
// borrowed resources, and topological resource lifecycle order without implicitly
|
||||
// spawning threads or opening descriptors on construction.
|
||||
} // namespace Void::Runtime
|
||||
|
||||
#include <Void/Runtime/Detail/HostCore.hpp>
|
||||
|
||||
namespace Void::Runtime
|
||||
{
|
||||
|
||||
/// Runtime composition handle managing startup, readiness, business execution,
|
||||
/// and phased structured shutdown. Destruction blocks until shutdown is complete
|
||||
/// in managed mode (or in embedded mode with a ManualExecutor).
|
||||
class Host
|
||||
{
|
||||
public:
|
||||
@@ -107,7 +130,8 @@ public:
|
||||
std::move(options),
|
||||
std::move(*plan),
|
||||
EmbeddedResources{},
|
||||
std::move(budget)};
|
||||
std::move(budget),
|
||||
{}};
|
||||
}
|
||||
|
||||
[[nodiscard]] static Void::Result::Result<Host> create_embedded(
|
||||
@@ -134,25 +158,152 @@ public:
|
||||
std::move(options),
|
||||
std::move(*plan),
|
||||
embedded,
|
||||
std::move(budget)};
|
||||
std::move(budget),
|
||||
{}};
|
||||
}
|
||||
|
||||
[[nodiscard]] HostState state() const noexcept { return state_; }
|
||||
[[nodiscard]] bool is_running() const noexcept { return state_ == HostState::running; }
|
||||
[[nodiscard]] std::size_t started_worker_count() const noexcept { return started_workers_; }
|
||||
[[nodiscard]] const Options& options() const noexcept { return options_; }
|
||||
[[nodiscard]] const LifecyclePlan& lifecycle_plan() const noexcept { return plan_; }
|
||||
[[nodiscard]] const EmbeddedResources& embedded_resources() const noexcept { return embedded_; }
|
||||
[[nodiscard]] bool valid() const noexcept { return core_ != nullptr; }
|
||||
|
||||
[[nodiscard]] HostState state() const noexcept
|
||||
{
|
||||
return core_ ? core_->current_state() : HostState::stopped;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool is_running() const noexcept
|
||||
{
|
||||
return state() == HostState::running;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool is_ready() const noexcept
|
||||
{
|
||||
return core_ ? core_->current_ready() : false;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t started_worker_count() const noexcept
|
||||
{
|
||||
return core_ ? core_->current_workers() : 0;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t cpu_admitted() const noexcept
|
||||
{
|
||||
return core_ ? core_->cpu_admitted_count() : 0;
|
||||
}
|
||||
|
||||
[[nodiscard]] Async::Executor* executor() noexcept
|
||||
{
|
||||
return core_ ? core_->active_executor() : nullptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] IO::Driver* io() noexcept
|
||||
{
|
||||
return core_ ? core_->get_io() : nullptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::optional<std::thread::id> owner_thread() const noexcept
|
||||
{
|
||||
return core_ ? core_->current_owner_thread() : std::nullopt;
|
||||
}
|
||||
|
||||
/// Precondition: `valid()`.
|
||||
[[nodiscard]] const Options& options() const noexcept
|
||||
{
|
||||
assert(valid());
|
||||
return core_->options;
|
||||
}
|
||||
|
||||
/// Precondition: `valid()`.
|
||||
[[nodiscard]] const LifecyclePlan& lifecycle_plan() const noexcept
|
||||
{
|
||||
assert(valid());
|
||||
return core_->plan;
|
||||
}
|
||||
|
||||
/// Precondition: `valid()`.
|
||||
[[nodiscard]] const EmbeddedResources& embedded_resources() const noexcept
|
||||
{
|
||||
assert(valid());
|
||||
return core_->embedded;
|
||||
}
|
||||
|
||||
/// Precondition: `valid()`.
|
||||
[[nodiscard]] const Void::Time::MonotonicClock& clock() const noexcept
|
||||
{
|
||||
return embedded_.clock ? *embedded_.clock
|
||||
: Void::Time::SystemMonotonicClock::instance();
|
||||
assert(valid());
|
||||
return core_->active_clock();
|
||||
}
|
||||
|
||||
/// Precondition: `valid()`.
|
||||
[[nodiscard]] Void::Memory::BudgetResource& memory_budget() noexcept
|
||||
{
|
||||
return embedded_.memory_budget ? *embedded_.memory_budget : *owned_budget_;
|
||||
assert(valid());
|
||||
return core_->active_budget();
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> start(StartOptions opts = {})
|
||||
{
|
||||
if (!core_) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_state,
|
||||
"Cannot start a moved-from Host")};
|
||||
}
|
||||
return core_->start_impl(std::move(opts));
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<Async::JoinHandle, Async::AdmissionError> spawn(
|
||||
Coro::Task<void>&& task, Async::OperationOptions opts = {})
|
||||
{
|
||||
if (!core_) {
|
||||
return std::unexpected{Async::AdmissionError::stopped};
|
||||
}
|
||||
return core_->spawn_impl(std::move(task), std::move(opts));
|
||||
}
|
||||
|
||||
void begin_shutdown() noexcept
|
||||
{
|
||||
if (core_) {
|
||||
core_->begin_shutdown_impl();
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<ShutdownReport>
|
||||
poll_shutdown(Async::OperationOptions opts = {})
|
||||
{
|
||||
if (!core_) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_state,
|
||||
"Cannot poll_shutdown on a moved-from Host")};
|
||||
}
|
||||
return core_->poll_shutdown_impl(std::move(opts));
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<ShutdownReport>
|
||||
shutdown_blocking(Async::OperationOptions opts = {})
|
||||
{
|
||||
if (!core_) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_state,
|
||||
"Cannot shutdown_blocking on a moved-from Host")};
|
||||
}
|
||||
return core_->shutdown_blocking_impl(std::move(opts), false);
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<ShutdownReport> shutdown_pumping(
|
||||
Async::ManualExecutor& manual,
|
||||
Async::OperationOptions opts = {},
|
||||
Async::TimerQueue* timers = nullptr)
|
||||
{
|
||||
if (!core_) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_state,
|
||||
"Cannot shutdown_pumping on a moved-from Host")};
|
||||
}
|
||||
return core_->shutdown_pumping_impl(
|
||||
manual, std::move(opts), timers, false);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::optional<ShutdownReport> last_shutdown_report() const
|
||||
{
|
||||
return core_ ? core_->current_last_report() : std::nullopt;
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -161,20 +312,18 @@ private:
|
||||
Host(Options options,
|
||||
LifecyclePlan plan,
|
||||
EmbeddedResources embedded,
|
||||
std::unique_ptr<Void::Memory::BudgetResource> owned_budget) noexcept
|
||||
: options_(std::move(options))
|
||||
, plan_(std::move(plan))
|
||||
, embedded_(embedded)
|
||||
, owned_budget_(std::move(owned_budget))
|
||||
std::unique_ptr<Void::Memory::BudgetResource> owned_budget,
|
||||
std::vector<std::shared_ptr<detail::ComponentEntry>> components)
|
||||
: core_(std::make_unique<detail::HostCore>(
|
||||
std::move(options),
|
||||
std::move(plan),
|
||||
embedded,
|
||||
std::move(owned_budget),
|
||||
std::move(components)))
|
||||
{
|
||||
}
|
||||
|
||||
HostState state_{HostState::constructed};
|
||||
std::size_t started_workers_{0};
|
||||
Options options_{};
|
||||
LifecyclePlan plan_{};
|
||||
EmbeddedResources embedded_{};
|
||||
std::unique_ptr<Void::Memory::BudgetResource> owned_budget_{};
|
||||
std::unique_ptr<detail::HostCore> core_{};
|
||||
};
|
||||
|
||||
class HostBuilder
|
||||
@@ -190,6 +339,7 @@ public:
|
||||
options_ = std::move(options);
|
||||
if (!embedded_.has_value()) {
|
||||
graph_ = make_default_resource_graph(options_.embedded_mode);
|
||||
components_.clear();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@@ -199,6 +349,7 @@ public:
|
||||
options_.embedded_mode = true;
|
||||
embedded_ = embedded;
|
||||
graph_ = make_default_resource_graph(true);
|
||||
components_.clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -214,6 +365,34 @@ public:
|
||||
std::move(dependent), std::move(dependency), required);
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> add_component(
|
||||
std::string id,
|
||||
ResourceKind kind,
|
||||
std::shared_ptr<Component> component,
|
||||
std::vector<std::string> deps = {},
|
||||
bool critical = true)
|
||||
{
|
||||
if (id.empty() || !component) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_argument,
|
||||
"Component id must be non-empty and component pointer must be non-null")};
|
||||
}
|
||||
if (auto nr = graph_.add_node(
|
||||
ResourceNode{id, kind, ResourceOwnership::owned, critical});
|
||||
!nr.has_value()) {
|
||||
return nr;
|
||||
}
|
||||
for (auto& d : deps) {
|
||||
if (auto dr = graph_.add_dependency(id, std::move(d), true);
|
||||
!dr.has_value()) {
|
||||
return dr;
|
||||
}
|
||||
}
|
||||
components_.push_back(ComponentSpec{
|
||||
std::move(id), kind, std::move(component), critical});
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<Host> build() const
|
||||
{
|
||||
Options opts = options_;
|
||||
@@ -237,13 +416,38 @@ public:
|
||||
budget = std::make_unique<Void::Memory::BudgetResource>(
|
||||
opts.limits.max_owned_memory_bytes);
|
||||
}
|
||||
return Host{std::move(opts), std::move(*plan), emb, std::move(budget)};
|
||||
|
||||
std::vector<std::shared_ptr<detail::ComponentEntry>> entries;
|
||||
entries.reserve(components_.size());
|
||||
for (const auto& c : components_) {
|
||||
auto entry = std::make_shared<detail::ComponentEntry>();
|
||||
entry->id = c.id;
|
||||
entry->kind = c.kind;
|
||||
entry->critical = c.critical;
|
||||
entry->component = c.component;
|
||||
entries.push_back(std::move(entry));
|
||||
}
|
||||
|
||||
return Host{
|
||||
std::move(opts),
|
||||
std::move(*plan),
|
||||
emb,
|
||||
std::move(budget),
|
||||
std::move(entries)};
|
||||
}
|
||||
|
||||
private:
|
||||
struct ComponentSpec {
|
||||
std::string id{};
|
||||
ResourceKind kind{ResourceKind::service};
|
||||
std::shared_ptr<Component> component{};
|
||||
bool critical{true};
|
||||
};
|
||||
|
||||
Options options_{};
|
||||
std::optional<EmbeddedResources> embedded_{};
|
||||
ResourceGraph graph_;
|
||||
std::vector<ComponentSpec> components_{};
|
||||
};
|
||||
|
||||
} // namespace Void::Runtime
|
||||
} // namespace Void::Runtime
|
||||
@@ -0,0 +1,121 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Async/Detail/Types.hpp>
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/Task.hpp>
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/IO/Driver.hpp>
|
||||
#include <Void/Memory/BudgetResource.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <stop_token>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Runtime
|
||||
{
|
||||
|
||||
enum class StartStep : std::uint8_t {
|
||||
signal_gate,
|
||||
memory_budget,
|
||||
cpu_executor,
|
||||
io_driver,
|
||||
file_workers,
|
||||
reactor,
|
||||
components,
|
||||
readiness
|
||||
};
|
||||
|
||||
enum class ShutdownPhase : std::uint8_t {
|
||||
not_started,
|
||||
closing_admission,
|
||||
draining,
|
||||
cancelling,
|
||||
joining_business,
|
||||
stopping_components,
|
||||
flushing_exporters,
|
||||
releasing_components,
|
||||
stopping_io,
|
||||
stopping_workers,
|
||||
releasing,
|
||||
stopped
|
||||
};
|
||||
|
||||
/// Abstract OS-signal gate installed before Runtime threads start and
|
||||
/// uninstalled after worker threads join. Runtime keeps no System dependency;
|
||||
/// callers inject concrete gates (e.g. wrapping Process::SignalWatcher).
|
||||
class SignalGate
|
||||
{
|
||||
public:
|
||||
virtual ~SignalGate() = default;
|
||||
|
||||
[[nodiscard]] virtual Void::Result::Result<void> install() = 0;
|
||||
virtual void uninstall(bool on_owner_thread) noexcept = 0;
|
||||
};
|
||||
|
||||
/// Live composition context handed to Component::start outside Host locks.
|
||||
class HostContext
|
||||
{
|
||||
public:
|
||||
HostContext(Async::Executor& executor,
|
||||
IO::Driver* io_driver,
|
||||
const Void::Time::MonotonicClock& clock,
|
||||
Void::Memory::BudgetResource& memory_budget,
|
||||
std::stop_token business_stop) noexcept
|
||||
: executor_(&executor)
|
||||
, io_(io_driver)
|
||||
, clock_(&clock)
|
||||
, memory_budget_(&memory_budget)
|
||||
, business_stop_(std::move(business_stop))
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] Async::Executor& executor() noexcept { return *executor_; }
|
||||
[[nodiscard]] IO::Driver* io() noexcept { return io_; }
|
||||
[[nodiscard]] const Void::Time::MonotonicClock& clock() const noexcept
|
||||
{
|
||||
return *clock_;
|
||||
}
|
||||
[[nodiscard]] Void::Memory::BudgetResource& memory_budget() noexcept
|
||||
{
|
||||
return *memory_budget_;
|
||||
}
|
||||
[[nodiscard]] std::stop_token business_stop() const noexcept
|
||||
{
|
||||
return business_stop_;
|
||||
}
|
||||
|
||||
private:
|
||||
Async::Executor* executor_{nullptr};
|
||||
IO::Driver* io_{nullptr};
|
||||
const Void::Time::MonotonicClock* clock_{nullptr};
|
||||
Void::Memory::BudgetResource* memory_budget_{nullptr};
|
||||
std::stop_token business_stop_{};
|
||||
};
|
||||
|
||||
/// Lifecycle-managed runtime service or exporter component.
|
||||
class Component
|
||||
{
|
||||
public:
|
||||
virtual ~Component() = default;
|
||||
|
||||
[[nodiscard]] virtual Void::Result::Result<void> start(HostContext&) = 0;
|
||||
[[nodiscard]] virtual Void::Coro::Task<Void::Result::Result<void>>
|
||||
stop(Async::OperationOptions) = 0;
|
||||
};
|
||||
|
||||
struct StartOptions {
|
||||
SignalGate* signal_gate{nullptr};
|
||||
std::function<Void::Result::Result<void>(StartStep)> before_step{};
|
||||
std::function<Void::Result::Result<void>(StartStep, std::size_t thread_index)>
|
||||
before_thread_start{};
|
||||
std::function<void(ShutdownPhase)> on_phase{};
|
||||
std::chrono::milliseconds readiness_timeout{std::chrono::milliseconds{5000}};
|
||||
std::chrono::milliseconds cleanup_timeout{std::chrono::milliseconds{5000}};
|
||||
};
|
||||
|
||||
} // namespace Void::Runtime
|
||||
@@ -0,0 +1,941 @@
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/Task.hpp>
|
||||
#include <Void/Async/Timer.hpp>
|
||||
#include <Void/IO/Driver.hpp>
|
||||
#include <Void/IO/File.hpp>
|
||||
#include <Void/IO/Stream.hpp>
|
||||
#include <Void/IO/Types.hpp>
|
||||
#include <Void/Memory/BudgetResource.hpp>
|
||||
#include <Void/Runtime/Host.hpp>
|
||||
#include <Void/Runtime/Lifecycle.hpp>
|
||||
#include <Void/Runtime/Options.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <memory_resource>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <stop_token>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
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<int> stop_calls{0};
|
||||
|
||||
Void::Result::Result<void> start(HostContext&) override { return {}; }
|
||||
|
||||
Void::Coro::Task<Void::Result::Result<void>> stop(OperationOptions) override
|
||||
{
|
||||
stop_calls.fetch_add(1, std::memory_order_relaxed);
|
||||
std::this_thread::sleep_for(15ms);
|
||||
co_return Void::Result::Result<void>{};
|
||||
}
|
||||
};
|
||||
|
||||
class CompletionProbeComponent final : public Component
|
||||
{
|
||||
public:
|
||||
std::atomic<bool> stop_ran{false};
|
||||
|
||||
Void::Result::Result<void> start(HostContext&) override { return {}; }
|
||||
|
||||
Void::Coro::Task<Void::Result::Result<void>> stop(OperationOptions) override
|
||||
{
|
||||
stop_ran.store(true, std::memory_order_release);
|
||||
co_return Void::Result::Result<void>{};
|
||||
}
|
||||
};
|
||||
|
||||
struct ExportOrderLog {
|
||||
std::mutex mu{};
|
||||
std::vector<std::string> 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<ExportOrderLog> log,
|
||||
Void::IO::ByteStream* out_stream,
|
||||
Void::IO::BufferLease* out_lease,
|
||||
std::atomic<bool>* 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<void> 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<Void::Result::Result<void>> stop(OperationOptions) override
|
||||
{
|
||||
stop_started_->store(true, std::memory_order_release);
|
||||
std::array<std::byte, 64> 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<void>{};
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<ExportOrderLog> 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<bool>* stop_started_{nullptr};
|
||||
};
|
||||
|
||||
class EmbeddedTimerComponent final : public Component
|
||||
{
|
||||
public:
|
||||
EmbeddedTimerComponent(TimerQueue* tq,
|
||||
const Void::Time::MonotonicClock* clock,
|
||||
std::atomic<bool>* stop_done)
|
||||
: tq_(tq)
|
||||
, clock_(clock)
|
||||
, stop_done_(stop_done)
|
||||
{
|
||||
}
|
||||
|
||||
Void::Result::Result<void> start(HostContext&) override { return {}; }
|
||||
|
||||
Void::Coro::Task<Void::Result::Result<void>>
|
||||
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<bool>* stop_done_;
|
||||
};
|
||||
|
||||
class TrackingGate final : public SignalGate
|
||||
{
|
||||
public:
|
||||
std::atomic<int> uninstall_calls{0};
|
||||
bool last_on_owner{false};
|
||||
|
||||
Void::Result::Result<void> 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<Void::Result::Result<ShutdownReport>> res{};
|
||||
|
||||
Void::IO::FileSyscallResult pread(int fd, std::span<std::byte> 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<void> block_on_gate_task(
|
||||
Gate* g, std::atomic<bool>* finished)
|
||||
{
|
||||
g->wait();
|
||||
if (finished != nullptr) {
|
||||
finished->store(true, std::memory_order_release);
|
||||
}
|
||||
co_return;
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Coro::Task<void> quick_done_task(std::atomic<bool>* 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<CountingSlowComponent>();
|
||||
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<std::optional<Void::Result::Result<ShutdownReport>>> reports(4);
|
||||
std::vector<std::thread> 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<CompletionProbeComponent>();
|
||||
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<bool> done1{false};
|
||||
std::atomic<bool> 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<ExportOrderLog>();
|
||||
Void::IO::ByteStream dummy_s;
|
||||
Void::IO::BufferLease dummy_l;
|
||||
std::atomic<bool> exp_stop_started{false};
|
||||
auto exporter = std::make_shared<MockExporterComponent>(
|
||||
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<Void::Result::Result<ShutdownReport>> 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<std::uint64_t> 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<std::byte, 2> 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<bool> 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<bool> 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<bool> 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<void> {
|
||||
std::pmr::vector<std::byte> 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<Void::Result::Result<ShutdownReport>> worker_res;
|
||||
auto self_task = [](Host* h,
|
||||
std::optional<Void::Result::Result<ShutdownReport>>* out)
|
||||
-> Void::Coro::Task<void> {
|
||||
*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<std::int32_t>(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<void> {
|
||||
std::array<std::byte, 3> 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<std::int32_t>(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<Void::Result::Result<ShutdownReport>> 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<bool> comp_stop_done{false};
|
||||
auto comp = std::make_shared<EmbeddedTimerComponent>(
|
||||
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<bool> 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<std::int32_t>(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<std::int32_t>(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<bool> 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<bool> 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);
|
||||
}
|
||||
@@ -0,0 +1,298 @@
|
||||
#include <Void/Async/Task.hpp>
|
||||
#include <Void/Runtime/Host.hpp>
|
||||
#include <Void/Runtime/Lifecycle.hpp>
|
||||
#include <Void/Runtime/Options.hpp>
|
||||
#include <Void/System/SignalWatcher.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using Void::Runtime::Host;
|
||||
using Void::Runtime::HostState;
|
||||
using Void::Runtime::Options;
|
||||
using Void::Runtime::SignalGate;
|
||||
using Void::Runtime::StartOptions;
|
||||
using Void::Runtime::StartStep;
|
||||
using Void::Testing::expect;
|
||||
using Void::Testing::expect_eq;
|
||||
using Void::Testing::require;
|
||||
|
||||
[[nodiscard]] std::size_t count_open_fds()
|
||||
{
|
||||
std::size_t count = 0;
|
||||
std::error_code ec;
|
||||
for (const auto& _ : std::filesystem::directory_iterator("/proc/self/fd", ec)) {
|
||||
(void)_;
|
||||
++count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool is_sig_blocked(int sig) noexcept
|
||||
{
|
||||
sigset_t cur{};
|
||||
if (::pthread_sigmask(SIG_SETMASK, nullptr, &cur) != 0) {
|
||||
return false;
|
||||
}
|
||||
return ::sigismember(&cur, sig) == 1;
|
||||
}
|
||||
|
||||
void drain_and_unblock_sigusr2() noexcept
|
||||
{
|
||||
sigset_t set{};
|
||||
::sigemptyset(&set);
|
||||
::sigaddset(&set, SIGUSR2);
|
||||
// Drain any queued SIGUSR2 safely while blocked before unblocking.
|
||||
timespec zero{0, 0};
|
||||
siginfo_t info{};
|
||||
while (::sigtimedwait(&set, &info, &zero) == SIGUSR2) {
|
||||
}
|
||||
::pthread_sigmask(SIG_UNBLOCK, &set, nullptr);
|
||||
}
|
||||
|
||||
struct Sigusr2CleanupGuard {
|
||||
~Sigusr2CleanupGuard() { drain_and_unblock_sigusr2(); }
|
||||
};
|
||||
|
||||
class ProcessSignalGate final : public SignalGate
|
||||
{
|
||||
public:
|
||||
std::atomic<int> install_calls{0};
|
||||
std::atomic<int> uninstall_calls{0};
|
||||
std::vector<int> drained_signals{};
|
||||
bool drain_before_uninstall{true};
|
||||
|
||||
Void::Result::Result<void> install() override
|
||||
{
|
||||
install_calls.fetch_add(1, std::memory_order_relaxed);
|
||||
auto w = Void::Process::SignalWatcher::create({SIGUSR2});
|
||||
if (!w.has_value()) {
|
||||
return std::unexpected{std::move(w.error())};
|
||||
}
|
||||
watcher_ = std::move(*w);
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] int drain_pending()
|
||||
{
|
||||
int count = 0;
|
||||
if (!watcher_.has_value()) {
|
||||
return count;
|
||||
}
|
||||
for (;;) {
|
||||
auto r = watcher_->poll();
|
||||
if (!r.has_value() || !r->has_value()) {
|
||||
break;
|
||||
}
|
||||
drained_signals.push_back(**r);
|
||||
++count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void uninstall(bool on_owner_thread) noexcept override
|
||||
{
|
||||
(void)on_owner_thread;
|
||||
if (drain_before_uninstall && watcher_.has_value()) {
|
||||
(void)drain_pending();
|
||||
}
|
||||
watcher_.reset();
|
||||
uninstall_calls.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
private:
|
||||
std::optional<Void::Process::SignalWatcher> watcher_{};
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test runtime_system_gate_mask_inherited_by_workers()
|
||||
{
|
||||
Sigusr2CleanupGuard sig_guard;
|
||||
drain_and_unblock_sigusr2();
|
||||
expect(!is_sig_blocked(SIGUSR2));
|
||||
|
||||
ProcessSignalGate gate;
|
||||
Options opts;
|
||||
opts.cpu_workers = 2;
|
||||
opts.file_workers = 2;
|
||||
opts.io.preference = Void::IO::BackendPreference::mock;
|
||||
|
||||
auto host_res = Host::create(opts);
|
||||
require(host_res.has_value());
|
||||
Host host = std::move(*host_res);
|
||||
|
||||
std::atomic<int> blocked_hooks{0};
|
||||
std::atomic<int> total_hooks{0};
|
||||
StartOptions start_opts;
|
||||
start_opts.signal_gate = &gate;
|
||||
start_opts.before_thread_start =
|
||||
[&](StartStep, std::size_t) -> Void::Result::Result<void> {
|
||||
total_hooks.fetch_add(1, std::memory_order_relaxed);
|
||||
if (is_sig_blocked(SIGUSR2)) {
|
||||
blocked_hooks.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
return {};
|
||||
};
|
||||
require(host.start(start_opts).has_value());
|
||||
expect(is_sig_blocked(SIGUSR2));
|
||||
// Owner mask installed before any thread started, so every worker hook saw it.
|
||||
expect_eq(total_hooks.load(), 5);
|
||||
expect_eq(blocked_hooks.load(), 5);
|
||||
|
||||
std::atomic<bool> worker_saw_blocked{false};
|
||||
auto check_task = [](std::atomic<bool>* out) -> Void::Coro::Task<void> {
|
||||
out->store(is_sig_blocked(SIGUSR2), std::memory_order_release);
|
||||
co_return;
|
||||
};
|
||||
auto jh = host.spawn(check_task(&worker_saw_blocked));
|
||||
require(jh.has_value());
|
||||
require(jh->join_blocking().has_value());
|
||||
expect(worker_saw_blocked.load(std::memory_order_acquire));
|
||||
|
||||
require(host.shutdown_blocking().has_value());
|
||||
expect(!is_sig_blocked(SIGUSR2));
|
||||
}
|
||||
|
||||
Void::Testing::Test runtime_system_gate_mask_restored_on_owner_after_failed_start_and_teardown()
|
||||
{
|
||||
Sigusr2CleanupGuard sig_guard;
|
||||
drain_and_unblock_sigusr2();
|
||||
expect(!is_sig_blocked(SIGUSR2));
|
||||
|
||||
// 1. Failed start restores SIGUSR2 mask on owner thread during reverse cleanup.
|
||||
{
|
||||
ProcessSignalGate 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);
|
||||
|
||||
StartOptions so;
|
||||
so.signal_gate = &gate;
|
||||
so.before_step = [](StartStep step) -> Void::Result::Result<void> {
|
||||
if (step == StartStep::components) {
|
||||
return std::unexpected{Void::Runtime::make_runtime_error(
|
||||
Void::Runtime::RuntimeErrorCode::internal,
|
||||
"inject_comp_fail")};
|
||||
}
|
||||
return {};
|
||||
};
|
||||
auto sr = host.start(so);
|
||||
require(!sr.has_value());
|
||||
expect(host.state() == HostState::failed_start);
|
||||
expect(!is_sig_blocked(SIGUSR2));
|
||||
expect_eq(gate.uninstall_calls.load(), 1);
|
||||
}
|
||||
|
||||
// 2. Normal start -> teardown restores SIGUSR2 mask on owner thread.
|
||||
{
|
||||
ProcessSignalGate 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());
|
||||
expect(is_sig_blocked(SIGUSR2));
|
||||
require(host.shutdown_blocking().has_value());
|
||||
expect(!is_sig_blocked(SIGUSR2));
|
||||
expect_eq(gate.uninstall_calls.load(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test runtime_signal_during_start_stays_pending_and_is_drained()
|
||||
{
|
||||
Sigusr2CleanupGuard sig_guard;
|
||||
drain_and_unblock_sigusr2();
|
||||
|
||||
ProcessSignalGate gate;
|
||||
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);
|
||||
|
||||
StartOptions so;
|
||||
so.signal_gate = &gate;
|
||||
so.before_step = [](StartStep step) -> Void::Result::Result<void> {
|
||||
if (step == StartStep::readiness) {
|
||||
// Send SIGUSR2 mid-start while workers exist; all block SIGUSR2.
|
||||
(void)::kill(::getpid(), SIGUSR2);
|
||||
}
|
||||
return {};
|
||||
};
|
||||
|
||||
require(host.start(so).has_value());
|
||||
expect(is_sig_blocked(SIGUSR2));
|
||||
|
||||
// Signal remained pending in signalfd without killing the process and is drained once.
|
||||
const int n = gate.drain_pending();
|
||||
expect_eq(n, 1);
|
||||
require(gate.drained_signals.size() == 1);
|
||||
expect_eq(gate.drained_signals[0], SIGUSR2);
|
||||
expect_eq(gate.drain_pending(), 0);
|
||||
|
||||
require(host.shutdown_blocking().has_value());
|
||||
expect(!is_sig_blocked(SIGUSR2));
|
||||
}
|
||||
|
||||
Void::Testing::Test runtime_offowner_destroy_closes_gate_fd_mask_not_restored()
|
||||
{
|
||||
Sigusr2CleanupGuard sig_guard;
|
||||
drain_and_unblock_sigusr2();
|
||||
expect(!is_sig_blocked(SIGUSR2));
|
||||
|
||||
const std::size_t fds_before = count_open_fds();
|
||||
ProcessSignalGate 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());
|
||||
|
||||
expect(is_sig_blocked(SIGUSR2));
|
||||
expect(count_open_fds() > fds_before);
|
||||
|
||||
// Move host to a non-owner thread and destroy it there.
|
||||
std::thread off_owner([moved = std::move(host)]() mutable {});
|
||||
off_owner.join();
|
||||
|
||||
// Gate signalfd fd is closed, while owner thread mask remains blocked (not restored off-owner).
|
||||
expect_eq(gate.uninstall_calls.load(), 1);
|
||||
expect_eq(count_open_fds(), fds_before);
|
||||
expect(is_sig_blocked(SIGUSR2));
|
||||
|
||||
// P3-3: explicitly restore owner mask cleanly before leaving test.
|
||||
drain_and_unblock_sigusr2();
|
||||
expect(!is_sig_blocked(SIGUSR2));
|
||||
}
|
||||
@@ -0,0 +1,694 @@
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/Task.hpp>
|
||||
#include <Void/IO/Driver.hpp>
|
||||
#include <Void/Runtime/Host.hpp>
|
||||
#include <Void/Runtime/Lifecycle.hpp>
|
||||
#include <Void/Runtime/Options.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
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::SignalGate;
|
||||
using Void::Runtime::StartOptions;
|
||||
using Void::Runtime::StartStep;
|
||||
using Void::Testing::expect;
|
||||
using Void::Testing::expect_eq;
|
||||
using Void::Testing::require;
|
||||
|
||||
[[nodiscard]] std::size_t count_dir_entries(const char* path)
|
||||
{
|
||||
std::size_t count = 0;
|
||||
std::error_code ec;
|
||||
for (const auto& _ : std::filesystem::directory_iterator(path, 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_dir_entries("/proc/self/task");
|
||||
while (current != expected && std::chrono::steady_clock::now() < deadline) {
|
||||
std::this_thread::sleep_for(1ms);
|
||||
current = count_dir_entries("/proc/self/task");
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t settle_baseline_tasks()
|
||||
{
|
||||
std::size_t prev = count_dir_entries("/proc/self/task");
|
||||
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_dir_entries("/proc/self/task");
|
||||
if (cur <= 1) {
|
||||
return cur;
|
||||
}
|
||||
if (cur == prev) {
|
||||
if (++stable_samples >= 3) {
|
||||
return cur;
|
||||
}
|
||||
} else {
|
||||
prev = cur;
|
||||
stable_samples = 0;
|
||||
}
|
||||
}
|
||||
return prev;
|
||||
}
|
||||
|
||||
class FakeSignalGate final : public SignalGate
|
||||
{
|
||||
public:
|
||||
std::atomic<int> install_calls{0};
|
||||
std::atomic<int> uninstall_calls{0};
|
||||
std::optional<std::size_t> task_count_at_install{};
|
||||
bool last_uninstall_on_owner{false};
|
||||
bool fail_install{false};
|
||||
|
||||
Void::Result::Result<void> install() override
|
||||
{
|
||||
install_calls.fetch_add(1, std::memory_order_relaxed);
|
||||
task_count_at_install = count_dir_entries("/proc/self/task");
|
||||
if (fail_install) {
|
||||
return std::unexpected{Void::Runtime::make_runtime_error(
|
||||
RuntimeErrorCode::internal, "fake signal gate install fail")};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void uninstall(bool on_owner_thread) noexcept override
|
||||
{
|
||||
last_uninstall_on_owner = on_owner_thread;
|
||||
uninstall_calls.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
};
|
||||
|
||||
class TraceComponent final : public Component
|
||||
{
|
||||
public:
|
||||
TraceComponent(std::string name,
|
||||
std::vector<std::string>* start_log,
|
||||
std::vector<std::string>* stop_log,
|
||||
bool fail_start = false)
|
||||
: name_(std::move(name))
|
||||
, start_log_(start_log)
|
||||
, stop_log_(stop_log)
|
||||
, fail_start_(fail_start)
|
||||
{
|
||||
}
|
||||
|
||||
Void::Result::Result<void> start(HostContext&) override
|
||||
{
|
||||
if (start_log_ != nullptr) {
|
||||
start_log_->push_back(name_);
|
||||
}
|
||||
if (fail_start_) {
|
||||
return std::unexpected{Void::Runtime::make_runtime_error(
|
||||
RuntimeErrorCode::conflict, name_ + "_start_failed")};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Coro::Task<Void::Result::Result<void>>
|
||||
stop(Void::Async::OperationOptions) override
|
||||
{
|
||||
if (stop_log_ != nullptr) {
|
||||
stop_log_->push_back(name_);
|
||||
}
|
||||
co_return Void::Result::Result<void>{};
|
||||
}
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::vector<std::string>* start_log_;
|
||||
std::vector<std::string>* stop_log_;
|
||||
bool fail_start_{false};
|
||||
};
|
||||
|
||||
class StalledStopComponent final : public Component
|
||||
{
|
||||
public:
|
||||
void release()
|
||||
{
|
||||
std::lock_guard lk{mu_};
|
||||
released_ = true;
|
||||
cv_.notify_all();
|
||||
}
|
||||
|
||||
[[nodiscard]] bool stop_entered() const
|
||||
{
|
||||
std::lock_guard lk{mu_};
|
||||
return entered_;
|
||||
}
|
||||
|
||||
Void::Result::Result<void> start(HostContext&) override { return {}; }
|
||||
|
||||
Void::Coro::Task<Void::Result::Result<void>>
|
||||
stop(Void::Async::OperationOptions) override
|
||||
{
|
||||
std::unique_lock lk{mu_};
|
||||
entered_ = true;
|
||||
cv_.notify_all();
|
||||
cv_.wait(lk, [&] { return released_; });
|
||||
co_return Void::Result::Result<void>{};
|
||||
}
|
||||
|
||||
private:
|
||||
mutable std::mutex mu_{};
|
||||
std::condition_variable cv_{};
|
||||
bool entered_{false};
|
||||
bool released_{false};
|
||||
};
|
||||
|
||||
[[nodiscard]] Void::Coro::Task<void> set_flag_task(std::atomic<bool>* flag)
|
||||
{
|
||||
flag->store(true, std::memory_order_release);
|
||||
co_return;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test runtime_start_spawns_workers_only_on_start_ready()
|
||||
{
|
||||
const std::size_t baseline_tasks = settle_baseline_tasks();
|
||||
Options opts;
|
||||
opts.cpu_workers = 2;
|
||||
opts.file_workers = 2;
|
||||
opts.io.preference = Void::IO::BackendPreference::epoll;
|
||||
|
||||
auto host_res = Host::create(opts);
|
||||
require(host_res.has_value());
|
||||
Host host = std::move(*host_res);
|
||||
|
||||
expect(host.state() == HostState::constructed);
|
||||
expect(!host.is_running());
|
||||
expect(!host.is_ready());
|
||||
expect_eq(host.started_worker_count(), std::size_t{0});
|
||||
expect_eq(count_dir_entries("/proc/self/task"), baseline_tasks);
|
||||
|
||||
require(host.start().has_value());
|
||||
expect(host.state() == HostState::running);
|
||||
expect(host.is_running());
|
||||
expect(host.is_ready());
|
||||
expect_eq(host.started_worker_count(), std::size_t{5});
|
||||
expect(host.owner_thread().has_value() &&
|
||||
*host.owner_thread() == std::this_thread::get_id());
|
||||
expect(host.executor() != nullptr);
|
||||
expect(host.io() != nullptr);
|
||||
expect_eq(wait_for_task_count(baseline_tasks + 5), baseline_tasks + 5);
|
||||
|
||||
std::atomic<bool> ran{false};
|
||||
auto jh = host.spawn(set_flag_task(&ran));
|
||||
require(jh.has_value());
|
||||
require(jh->join_blocking().has_value());
|
||||
expect(ran.load(std::memory_order_acquire));
|
||||
|
||||
auto rep = host.shutdown_blocking();
|
||||
require(rep.has_value());
|
||||
expect(rep->complete);
|
||||
expect(rep->state == HostState::stopped);
|
||||
expect(!host.is_ready());
|
||||
expect_eq(host.started_worker_count(), std::size_t{0});
|
||||
expect_eq(wait_for_task_count(baseline_tasks), baseline_tasks);
|
||||
}
|
||||
|
||||
Void::Testing::Test runtime_start_twice_invalid_state()
|
||||
{
|
||||
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());
|
||||
|
||||
auto second = host.start();
|
||||
require(!second.has_value());
|
||||
expect_eq(second.error().code().value,
|
||||
static_cast<std::int32_t>(RuntimeErrorCode::invalid_state));
|
||||
expect(host.is_running());
|
||||
|
||||
require(host.shutdown_blocking().has_value());
|
||||
auto third = host.start();
|
||||
require(!third.has_value());
|
||||
expect_eq(third.error().code().value,
|
||||
static_cast<std::int32_t>(RuntimeErrorCode::invalid_state));
|
||||
}
|
||||
|
||||
Void::Testing::Test runtime_failed_start_each_step_reverse_cleanup_no_threads_no_fds()
|
||||
{
|
||||
const std::vector<StartStep> steps = {
|
||||
StartStep::signal_gate,
|
||||
StartStep::memory_budget,
|
||||
StartStep::cpu_executor,
|
||||
StartStep::io_driver,
|
||||
StartStep::file_workers,
|
||||
StartStep::reactor,
|
||||
StartStep::components,
|
||||
StartStep::readiness,
|
||||
};
|
||||
|
||||
for (const StartStep target_step : steps) {
|
||||
const std::size_t tasks_before = settle_baseline_tasks();
|
||||
const std::size_t fds_before = count_dir_entries("/proc/self/fd");
|
||||
|
||||
FakeSignalGate gate;
|
||||
std::vector<std::string> start_log;
|
||||
std::vector<std::string> stop_log;
|
||||
|
||||
Options opts;
|
||||
opts.cpu_workers = 2;
|
||||
opts.file_workers = 2;
|
||||
opts.io.preference = Void::IO::BackendPreference::epoll;
|
||||
|
||||
HostBuilder builder;
|
||||
builder.with_options(opts);
|
||||
require(builder
|
||||
.add_component(
|
||||
"trace",
|
||||
ResourceKind::service,
|
||||
std::make_shared<TraceComponent>(
|
||||
"trace", &start_log, &stop_log))
|
||||
.has_value());
|
||||
|
||||
auto host_res = builder.build();
|
||||
require(host_res.has_value());
|
||||
Host host = std::move(*host_res);
|
||||
|
||||
StartOptions start_opts;
|
||||
start_opts.signal_gate = &gate;
|
||||
start_opts.before_step = [target_step](StartStep s)
|
||||
-> Void::Result::Result<void> {
|
||||
if (s == target_step) {
|
||||
return std::unexpected{Void::Runtime::make_runtime_error(
|
||||
RuntimeErrorCode::not_found, "injected_step_failure")};
|
||||
}
|
||||
return {};
|
||||
};
|
||||
|
||||
auto sr = host.start(start_opts);
|
||||
require(!sr.has_value());
|
||||
expect_eq(sr.error().code().value,
|
||||
static_cast<std::int32_t>(RuntimeErrorCode::not_found));
|
||||
expect(host.state() == HostState::failed_start);
|
||||
expect(!host.is_ready());
|
||||
expect_eq(host.started_worker_count(), std::size_t{0});
|
||||
|
||||
if (target_step == StartStep::signal_gate) {
|
||||
expect_eq(gate.install_calls.load(), 0);
|
||||
expect_eq(gate.uninstall_calls.load(), 0);
|
||||
} else {
|
||||
expect_eq(gate.install_calls.load(), 1);
|
||||
expect_eq(gate.uninstall_calls.load(), 1);
|
||||
expect(gate.last_uninstall_on_owner);
|
||||
}
|
||||
if (target_step == StartStep::readiness) {
|
||||
expect_eq(start_log.size(), std::size_t{1});
|
||||
expect_eq(stop_log.size(), std::size_t{1});
|
||||
} else {
|
||||
expect_eq(start_log.size(), std::size_t{0});
|
||||
expect_eq(stop_log.size(), std::size_t{0});
|
||||
}
|
||||
|
||||
// Verify zero leaked threads or fds while failed_start Host still exists.
|
||||
expect_eq(wait_for_task_count(tasks_before), tasks_before);
|
||||
expect_eq(count_dir_entries("/proc/self/fd"), fds_before);
|
||||
}
|
||||
|
||||
// A subsequent healthy Host starts cleanly after all step failures.
|
||||
Options ok_opts;
|
||||
ok_opts.cpu_workers = 1;
|
||||
ok_opts.file_workers = 1;
|
||||
ok_opts.io.preference = Void::IO::BackendPreference::epoll;
|
||||
auto ok_host = Host::create(ok_opts);
|
||||
require(ok_host.has_value());
|
||||
require(ok_host->start().has_value());
|
||||
expect(ok_host->is_ready());
|
||||
require(ok_host->shutdown_blocking().has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test runtime_failed_start_thread_fault_each_threaded_step_reverse_cleanup()
|
||||
{
|
||||
struct FaultCase {
|
||||
StartStep step;
|
||||
std::size_t index;
|
||||
};
|
||||
const std::vector<FaultCase> cases = {
|
||||
{StartStep::cpu_executor, 0},
|
||||
{StartStep::cpu_executor, 1},
|
||||
{StartStep::file_workers, 0},
|
||||
{StartStep::file_workers, 1},
|
||||
{StartStep::reactor, 0},
|
||||
};
|
||||
|
||||
for (const auto& c : cases) {
|
||||
const std::size_t tasks_before = settle_baseline_tasks();
|
||||
const std::size_t fds_before = count_dir_entries("/proc/self/fd");
|
||||
|
||||
FakeSignalGate gate;
|
||||
Options opts;
|
||||
opts.cpu_workers = 2;
|
||||
opts.file_workers = 2;
|
||||
opts.io.preference = Void::IO::BackendPreference::epoll;
|
||||
|
||||
auto host_res = Host::create(opts);
|
||||
require(host_res.has_value());
|
||||
Host host = std::move(*host_res);
|
||||
|
||||
StartOptions start_opts;
|
||||
start_opts.signal_gate = &gate;
|
||||
start_opts.before_thread_start =
|
||||
[c](StartStep step, std::size_t idx) -> Void::Result::Result<void> {
|
||||
if (step == c.step && idx == c.index) {
|
||||
return std::unexpected{Void::Runtime::make_runtime_error(
|
||||
RuntimeErrorCode::internal, "injected_thread_start_failure")};
|
||||
}
|
||||
return {};
|
||||
};
|
||||
|
||||
auto sr = host.start(start_opts);
|
||||
require(!sr.has_value());
|
||||
if (c.step == StartStep::file_workers) {
|
||||
expect_eq(sr.error().code().domain, Void::Result::kIoErrorDomain);
|
||||
expect_eq(sr.error().code().value,
|
||||
static_cast<std::int32_t>(Void::IO::IoErrorCode::os_error));
|
||||
} else {
|
||||
expect_eq(sr.error().code().domain, Void::Result::kRuntimeErrorDomain);
|
||||
expect_eq(sr.error().code().value,
|
||||
static_cast<std::int32_t>(RuntimeErrorCode::internal));
|
||||
}
|
||||
expect(host.state() == HostState::failed_start);
|
||||
expect_eq(host.started_worker_count(), std::size_t{0});
|
||||
expect_eq(gate.uninstall_calls.load(), 1);
|
||||
expect_eq(wait_for_task_count(tasks_before), tasks_before);
|
||||
expect_eq(count_dir_entries("/proc/self/fd"), fds_before);
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test runtime_failed_start_component_cleanup_timeout_retains_then_completes()
|
||||
{
|
||||
auto comp_a = std::make_shared<StalledStopComponent>();
|
||||
auto comp_b = std::make_shared<TraceComponent>(
|
||||
"b", nullptr, nullptr, /*fail_start=*/true);
|
||||
FakeSignalGate gate;
|
||||
|
||||
Options opts;
|
||||
opts.cpu_workers = 1;
|
||||
opts.file_workers = 1;
|
||||
opts.io.preference = Void::IO::BackendPreference::mock;
|
||||
|
||||
HostBuilder builder;
|
||||
builder.with_options(opts);
|
||||
require(builder.add_component("a", ResourceKind::service, comp_a).has_value());
|
||||
require(
|
||||
builder.add_component("b", ResourceKind::service, comp_b, {"a"}).has_value());
|
||||
|
||||
auto host_res = builder.build();
|
||||
require(host_res.has_value());
|
||||
Host host = std::move(*host_res);
|
||||
|
||||
auto sr = host.start(StartOptions{
|
||||
.signal_gate = &gate,
|
||||
.cleanup_timeout = 15ms,
|
||||
});
|
||||
require(!sr.has_value());
|
||||
expect_eq(sr.error().code().value,
|
||||
static_cast<std::int32_t>(RuntimeErrorCode::conflict));
|
||||
expect(host.state() == HostState::failed_start);
|
||||
expect(comp_a->stop_entered());
|
||||
// Resources retained because cleanup timed out on comp_a::stop.
|
||||
expect(host.started_worker_count() > std::size_t{0});
|
||||
expect(host.executor() != nullptr);
|
||||
expect(host.io() != nullptr);
|
||||
expect_eq(gate.uninstall_calls.load(), 0);
|
||||
|
||||
// Release comp_a and resume cleanup via shutdown_blocking.
|
||||
comp_a->release();
|
||||
auto rep = host.shutdown_blocking();
|
||||
require(rep.has_value());
|
||||
expect(rep->complete);
|
||||
expect(rep->state == HostState::stopped);
|
||||
expect_eq(host.started_worker_count(), std::size_t{0});
|
||||
expect_eq(gate.uninstall_calls.load(), 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test runtime_component_start_failure_unwinds_reverse()
|
||||
{
|
||||
std::vector<std::string> start_log;
|
||||
std::vector<std::string> stop_log;
|
||||
|
||||
Options opts;
|
||||
opts.cpu_workers = 1;
|
||||
opts.file_workers = 1;
|
||||
opts.io.preference = Void::IO::BackendPreference::mock;
|
||||
|
||||
HostBuilder builder;
|
||||
builder.with_options(opts);
|
||||
require(builder
|
||||
.add_component(
|
||||
"c1",
|
||||
ResourceKind::service,
|
||||
std::make_shared<TraceComponent>(
|
||||
"c1", &start_log, &stop_log))
|
||||
.has_value());
|
||||
require(builder
|
||||
.add_component(
|
||||
"c2",
|
||||
ResourceKind::service,
|
||||
std::make_shared<TraceComponent>(
|
||||
"c2", &start_log, &stop_log),
|
||||
{"c1"})
|
||||
.has_value());
|
||||
require(builder
|
||||
.add_component(
|
||||
"c3",
|
||||
ResourceKind::service,
|
||||
std::make_shared<TraceComponent>(
|
||||
"c3", &start_log, &stop_log, /*fail_start=*/true),
|
||||
{"c2"})
|
||||
.has_value());
|
||||
|
||||
auto host_res = builder.build();
|
||||
require(host_res.has_value());
|
||||
Host host = std::move(*host_res);
|
||||
|
||||
auto sr = host.start();
|
||||
require(!sr.has_value());
|
||||
expect(host.state() == HostState::failed_start);
|
||||
require(start_log.size() == 3);
|
||||
expect_eq(start_log[0], std::string{"c1"});
|
||||
expect_eq(start_log[1], std::string{"c2"});
|
||||
expect_eq(start_log[2], std::string{"c3"});
|
||||
|
||||
require(stop_log.size() == 2);
|
||||
expect_eq(stop_log[0], std::string{"c2"});
|
||||
expect_eq(stop_log[1], std::string{"c1"});
|
||||
}
|
||||
|
||||
Void::Testing::Test runtime_signal_gate_before_threads_uninstall_on_owner()
|
||||
{
|
||||
const std::size_t tasks_before = settle_baseline_tasks();
|
||||
FakeSignalGate gate;
|
||||
|
||||
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(StartOptions{.signal_gate = &gate}).has_value());
|
||||
|
||||
require(gate.task_count_at_install.has_value());
|
||||
expect_eq(*gate.task_count_at_install, tasks_before);
|
||||
expect_eq(gate.uninstall_calls.load(), 0);
|
||||
|
||||
auto rep = host.shutdown_blocking();
|
||||
require(rep.has_value());
|
||||
expect(rep->complete);
|
||||
expect(!rep->owner_release_pending);
|
||||
expect_eq(gate.uninstall_calls.load(), 1);
|
||||
expect(gate.last_uninstall_on_owner);
|
||||
expect_eq(wait_for_task_count(tasks_before), tasks_before);
|
||||
}
|
||||
|
||||
Void::Testing::Test runtime_signal_gate_uninstalled_at_most_once()
|
||||
{
|
||||
FakeSignalGate 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());
|
||||
require(host.shutdown_blocking().has_value());
|
||||
require(host.shutdown_blocking().has_value());
|
||||
}
|
||||
expect_eq(gate.install_calls.load(), 1);
|
||||
expect_eq(gate.uninstall_calls.load(), 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test runtime_embedded_start_no_threads()
|
||||
{
|
||||
const std::size_t tasks_before = settle_baseline_tasks();
|
||||
Void::Async::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());
|
||||
|
||||
expect(host.is_running());
|
||||
expect(host.is_ready());
|
||||
expect_eq(host.started_worker_count(), std::size_t{0});
|
||||
expect_eq(host.cpu_admitted(), std::size_t{0});
|
||||
expect(host.executor() == &manual);
|
||||
expect(host.io() == drv_res->get());
|
||||
expect_eq(count_dir_entries("/proc/self/task"), tasks_before);
|
||||
|
||||
auto rep = host.shutdown_pumping(manual);
|
||||
require(rep.has_value());
|
||||
expect(rep->complete);
|
||||
expect_eq(wait_for_task_count(tasks_before), tasks_before);
|
||||
}
|
||||
|
||||
Void::Testing::Test runtime_move_after_start_keeps_resources_valid()
|
||||
{
|
||||
Options opts;
|
||||
opts.cpu_workers = 1;
|
||||
opts.file_workers = 1;
|
||||
opts.io.preference = Void::IO::BackendPreference::mock;
|
||||
|
||||
auto h1_res = Host::create(opts);
|
||||
require(h1_res.has_value());
|
||||
Host h1 = std::move(*h1_res);
|
||||
require(h1.start().has_value());
|
||||
auto* orig_exec = h1.executor();
|
||||
auto* orig_io = h1.io();
|
||||
|
||||
Host h2 = std::move(h1);
|
||||
auto h3_res = Host::create(opts);
|
||||
require(h3_res.has_value());
|
||||
Host h3 = std::move(*h3_res);
|
||||
h3 = std::move(h2);
|
||||
|
||||
expect(h3.valid());
|
||||
expect(h3.is_running());
|
||||
expect(h3.is_ready());
|
||||
expect(h3.executor() == orig_exec);
|
||||
expect(h3.io() == orig_io);
|
||||
|
||||
std::atomic<bool> done{false};
|
||||
auto jh = h3.spawn(set_flag_task(&done));
|
||||
require(jh.has_value());
|
||||
require(jh->join_blocking().has_value());
|
||||
expect(done.load(std::memory_order_acquire));
|
||||
|
||||
auto rep = h3.shutdown_blocking();
|
||||
require(rep.has_value());
|
||||
expect(rep->complete);
|
||||
}
|
||||
|
||||
Void::Testing::Test runtime_moved_from_host_safe_defaults()
|
||||
{
|
||||
auto h1_res = Host::create();
|
||||
require(h1_res.has_value());
|
||||
Host h1 = std::move(*h1_res);
|
||||
Host h2 = std::move(h1);
|
||||
expect(h2.valid());
|
||||
|
||||
expect(!h1.valid());
|
||||
expect(h1.state() == HostState::stopped);
|
||||
expect(!h1.is_running());
|
||||
expect(!h1.is_ready());
|
||||
expect_eq(h1.started_worker_count(), std::size_t{0});
|
||||
expect_eq(h1.cpu_admitted(), std::size_t{0});
|
||||
expect(h1.executor() == nullptr);
|
||||
expect(h1.io() == nullptr);
|
||||
expect(!h1.owner_thread().has_value());
|
||||
expect(!h1.last_shutdown_report().has_value());
|
||||
|
||||
auto st = h1.start();
|
||||
expect(!st.has_value());
|
||||
expect_eq(st.error().code().value,
|
||||
static_cast<std::int32_t>(RuntimeErrorCode::invalid_state));
|
||||
|
||||
auto ps = h1.poll_shutdown();
|
||||
expect(!ps.has_value());
|
||||
expect_eq(ps.error().code().value,
|
||||
static_cast<std::int32_t>(RuntimeErrorCode::invalid_state));
|
||||
|
||||
auto sb = h1.shutdown_blocking();
|
||||
expect(!sb.has_value());
|
||||
expect_eq(sb.error().code().value,
|
||||
static_cast<std::int32_t>(RuntimeErrorCode::invalid_state));
|
||||
|
||||
Void::Async::ManualExecutor manual;
|
||||
auto sp = h1.shutdown_pumping(manual);
|
||||
expect(!sp.has_value());
|
||||
expect_eq(sp.error().code().value,
|
||||
static_cast<std::int32_t>(RuntimeErrorCode::invalid_state));
|
||||
|
||||
std::atomic<bool> flag{false};
|
||||
auto spw = h1.spawn(set_flag_task(&flag));
|
||||
expect(!spw.has_value());
|
||||
expect(spw.error() == Void::Async::AdmissionError::stopped);
|
||||
|
||||
h1.begin_shutdown();
|
||||
}
|
||||
Reference in new issue
Block a user