774 lines
24 KiB
C++
774 lines
24 KiB
C++
#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 <latch>
|
|
#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();
|
|
}
|
|
|
|
Void::Testing::Test runtime_begin_shutdown_during_start_prevents_ready()
|
|
{
|
|
const std::size_t baseline = settle_baseline_tasks();
|
|
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);
|
|
|
|
bool invoked = false;
|
|
auto sr = host.start(StartOptions{
|
|
.before_step = [&](StartStep step) -> Void::Result::Result<void> {
|
|
if (step == StartStep::components && !invoked) {
|
|
invoked = true;
|
|
std::thread t([&]() { host.begin_shutdown(); });
|
|
t.join();
|
|
}
|
|
return {};
|
|
},
|
|
});
|
|
require(sr.has_value());
|
|
expect(invoked);
|
|
expect(!host.is_ready());
|
|
expect(host.state() == HostState::stopping_admission);
|
|
|
|
std::atomic<bool> flag{false};
|
|
auto spw = host.spawn(set_flag_task(&flag));
|
|
expect(!spw.has_value());
|
|
expect(spw.error() == Void::Async::AdmissionError::stopped);
|
|
|
|
auto rep = host.shutdown_blocking();
|
|
require(rep.has_value());
|
|
expect(rep->complete);
|
|
expect(rep->state == HostState::stopped);
|
|
expect_eq(wait_for_task_count(baseline), baseline);
|
|
}
|
|
|
|
Void::Testing::Test runtime_concurrent_shutdown_waits_for_start_tsan()
|
|
{
|
|
const std::size_t baseline = settle_baseline_tasks();
|
|
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);
|
|
|
|
std::latch entered_components{1};
|
|
std::optional<Void::Result::Result<Void::Runtime::ShutdownReport>> sd_res;
|
|
std::thread stopper([&]() {
|
|
entered_components.wait();
|
|
sd_res = host.shutdown_blocking();
|
|
});
|
|
|
|
auto sr = host.start(StartOptions{
|
|
.before_step = [&](StartStep step) -> Void::Result::Result<void> {
|
|
if (step == StartStep::components) {
|
|
entered_components.count_down();
|
|
std::this_thread::sleep_for(10ms);
|
|
}
|
|
return {};
|
|
},
|
|
});
|
|
stopper.join();
|
|
|
|
require(sr.has_value());
|
|
require(sd_res.has_value() && sd_res->has_value());
|
|
expect((*sd_res)->complete);
|
|
expect(host.state() == HostState::stopped);
|
|
expect(!host.is_ready());
|
|
expect_eq(wait_for_task_count(baseline), baseline);
|
|
}
|