feat(io): add Driver::run, embedded pump_until, and shutdown conformance tests (S-IO-04)
This commit is contained in:
1 parent
ef85ce07b7
commit
50029e1f7b
5 files changed
+1248
No files matched your search
@@ -7,12 +7,15 @@
|
||||
#include <Void/IO/Types.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <memory_resource>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <stop_token>
|
||||
@@ -148,6 +151,69 @@ public:
|
||||
return backend_->shutdown_complete();
|
||||
}
|
||||
|
||||
/// Runs the poll loop until `stop` is requested, `shutdown_complete()` becomes
|
||||
/// true, or an unrecoverable error occurs. Retries `IoErrorCode::busy` and
|
||||
/// idle mock turns with short stop-aware waits so the loop never busy-spins.
|
||||
[[nodiscard]] Void::Result::Result<std::size_t> run(
|
||||
std::stop_token stop,
|
||||
std::chrono::nanoseconds max_wait = std::chrono::milliseconds{5})
|
||||
{
|
||||
if (max_wait < std::chrono::nanoseconds{0}) {
|
||||
max_wait = std::chrono::nanoseconds{0};
|
||||
}
|
||||
auto wait_stop_aware = [&](std::chrono::nanoseconds budget) {
|
||||
if (budget <= std::chrono::nanoseconds{0} ||
|
||||
stop.stop_requested() || shutdown_complete()) {
|
||||
return;
|
||||
}
|
||||
std::mutex mu;
|
||||
std::condition_variable cv;
|
||||
std::stop_callback cb(stop, [&]() noexcept {
|
||||
std::lock_guard lk{mu};
|
||||
cv.notify_all();
|
||||
});
|
||||
std::unique_lock lk{mu};
|
||||
auto remaining = budget;
|
||||
constexpr auto kSlice =
|
||||
std::chrono::nanoseconds{std::chrono::milliseconds{1}};
|
||||
while (remaining > std::chrono::nanoseconds{0} &&
|
||||
!stop.stop_requested() && !shutdown_complete()) {
|
||||
const auto step = std::min(remaining, kSlice);
|
||||
cv.wait_for(lk, step, [&]() {
|
||||
return stop.stop_requested() || shutdown_complete();
|
||||
});
|
||||
remaining -= step;
|
||||
}
|
||||
};
|
||||
|
||||
std::size_t total = 0;
|
||||
while (!stop.stop_requested() && !shutdown_complete()) {
|
||||
auto res = poll_once(max_wait);
|
||||
if (!res.has_value()) {
|
||||
if (res.error().code().domain == Void::Result::kIoErrorDomain &&
|
||||
res.error().code().value ==
|
||||
static_cast<std::int32_t>(IoErrorCode::busy)) {
|
||||
const auto busy_slice =
|
||||
max_wait > std::chrono::nanoseconds{0}
|
||||
? std::min(max_wait,
|
||||
std::chrono::nanoseconds{
|
||||
std::chrono::milliseconds{1}})
|
||||
: std::chrono::nanoseconds{
|
||||
std::chrono::microseconds{200}};
|
||||
wait_stop_aware(busy_slice);
|
||||
continue;
|
||||
}
|
||||
return std::unexpected{std::move(res.error())};
|
||||
}
|
||||
total += *res;
|
||||
if (*res == 0 && active_backend() == BackendPreference::mock &&
|
||||
max_wait > std::chrono::nanoseconds{0}) {
|
||||
wait_stop_aware(max_wait);
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
private:
|
||||
Driver(std::shared_ptr<detail::DriverBackend> backend,
|
||||
std::shared_ptr<detail::OwnedBufferTracker> buffer_tracker) noexcept;
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Async/Detail/Types.hpp>
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/Timer.hpp>
|
||||
#include <Void/IO/Driver.hpp>
|
||||
#include <Void/IO/Types.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <concepts>
|
||||
#include <condition_variable>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
#include <stop_token>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::IO
|
||||
{
|
||||
|
||||
struct PumpOptions {
|
||||
std::chrono::nanoseconds max_wait{0};
|
||||
std::size_t max_turns{1u << 20};
|
||||
Async::OperationOptions operation{};
|
||||
Async::TimerQueue* timers{nullptr};
|
||||
};
|
||||
|
||||
/// Drives `driver.poll_once`, `options.timers->poll_expired()`, and
|
||||
/// `manual.drain()` until `done()` returns true, `driver.shutdown_complete()`
|
||||
/// becomes true, stop/deadline fires, or `max_turns` is exceeded.
|
||||
template <class Done>
|
||||
requires std::predicate<Done&>
|
||||
[[nodiscard]] Void::Result::Result<std::size_t> pump_until(
|
||||
Driver& driver,
|
||||
Async::ManualExecutor& manual,
|
||||
Done&& done,
|
||||
PumpOptions options = {})
|
||||
{
|
||||
const auto wait_stop_aware = [&](std::chrono::nanoseconds slice) {
|
||||
if (slice <= std::chrono::nanoseconds{0} ||
|
||||
options.operation.stop.stop_requested() ||
|
||||
driver.shutdown_complete()) {
|
||||
return;
|
||||
}
|
||||
std::mutex mu;
|
||||
std::condition_variable cv;
|
||||
std::stop_callback cb(options.operation.stop, [&]() noexcept {
|
||||
std::lock_guard lk{mu};
|
||||
cv.notify_all();
|
||||
});
|
||||
std::unique_lock lk{mu};
|
||||
cv.wait_for(lk, slice, [&]() {
|
||||
return options.operation.stop.stop_requested() ||
|
||||
driver.shutdown_complete();
|
||||
});
|
||||
};
|
||||
|
||||
const auto base_wait =
|
||||
options.max_wait < std::chrono::nanoseconds{0}
|
||||
? std::chrono::nanoseconds{0}
|
||||
: options.max_wait;
|
||||
|
||||
std::size_t total_work = 0;
|
||||
std::size_t turns = 0;
|
||||
|
||||
for (;;) {
|
||||
if (done()) {
|
||||
return total_work;
|
||||
}
|
||||
if (options.operation.stop.stop_requested()) {
|
||||
return std::unexpected{make_io_error(
|
||||
IoErrorCode::cancelled, "pump_until stop requested")};
|
||||
}
|
||||
if (Async::is_deadline_expired(options.operation, driver.clock())) {
|
||||
return std::unexpected{make_io_error(
|
||||
IoErrorCode::timeout, "pump_until deadline expired")};
|
||||
}
|
||||
if (turns >= options.max_turns) {
|
||||
return std::unexpected{make_io_error(
|
||||
IoErrorCode::exhausted, "pump_until max_turns exceeded")};
|
||||
}
|
||||
++turns;
|
||||
|
||||
std::chrono::nanoseconds wait = base_wait;
|
||||
if (!manual.empty()) {
|
||||
wait = std::chrono::nanoseconds{0};
|
||||
} else if (options.timers != nullptr) {
|
||||
wait = options.timers->wait_duration(base_wait);
|
||||
}
|
||||
if (options.operation.deadline.has_value()) {
|
||||
const auto rem = Void::Time::remaining_until(
|
||||
driver.clock(), *options.operation.deadline);
|
||||
if (rem <= std::chrono::nanoseconds{0}) {
|
||||
return std::unexpected{make_io_error(
|
||||
IoErrorCode::timeout, "pump_until deadline expired")};
|
||||
}
|
||||
wait = std::min(wait, rem);
|
||||
}
|
||||
if (wait < std::chrono::nanoseconds{0}) {
|
||||
wait = std::chrono::nanoseconds{0};
|
||||
}
|
||||
|
||||
auto poll_res = driver.poll_once(wait);
|
||||
if (!poll_res.has_value()) {
|
||||
if (poll_res.error().code().domain == Void::Result::kIoErrorDomain &&
|
||||
poll_res.error().code().value ==
|
||||
static_cast<std::int32_t>(IoErrorCode::busy)) {
|
||||
const auto slice =
|
||||
wait > std::chrono::nanoseconds{0}
|
||||
? std::min(wait,
|
||||
std::chrono::nanoseconds{
|
||||
std::chrono::milliseconds{1}})
|
||||
: std::chrono::nanoseconds{
|
||||
std::chrono::microseconds{200}};
|
||||
wait_stop_aware(slice);
|
||||
continue;
|
||||
}
|
||||
return std::unexpected{std::move(poll_res.error())};
|
||||
}
|
||||
total_work += *poll_res;
|
||||
|
||||
if (options.timers != nullptr) {
|
||||
total_work += options.timers->poll_expired();
|
||||
}
|
||||
total_work += manual.drain();
|
||||
|
||||
if (done()) {
|
||||
return total_work;
|
||||
}
|
||||
if (driver.shutdown_complete()) {
|
||||
return total_work;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Void::IO
|
||||
@@ -0,0 +1,269 @@
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/Operation.hpp>
|
||||
#include <Void/Async/TaskScope.hpp>
|
||||
#include <Void/Async/Timer.hpp>
|
||||
#include <Void/IO/Driver.hpp>
|
||||
#include <Void/IO/Pump.hpp>
|
||||
#include <Void/IO/Stream.hpp>
|
||||
#include <Void/IO/Types.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <coroutine>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <stop_token>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
using Void::Async::ManualExecutor;
|
||||
using Void::Async::OperationOptions;
|
||||
using Void::Async::TaskScope;
|
||||
using Void::Async::TimerEvent;
|
||||
using Void::Async::TimerQueue;
|
||||
using Void::Async::TimerQueueOptions;
|
||||
using Void::IO::BackendOptions;
|
||||
using Void::IO::BackendPreference;
|
||||
using Void::IO::ByteStream;
|
||||
using Void::IO::Driver;
|
||||
using Void::IO::IoErrorCode;
|
||||
using Void::IO::PumpOptions;
|
||||
using Void::IO::Transfer;
|
||||
using Void::IO::pump_until;
|
||||
using Void::Testing::expect;
|
||||
using Void::Testing::expect_eq;
|
||||
using Void::Testing::require;
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test io_pump_until_scope_join_mock()
|
||||
{
|
||||
ManualExecutor exec;
|
||||
Void::Time::ManualClock clock;
|
||||
auto drv_res = Driver::create(
|
||||
exec, BackendOptions{.preference = BackendPreference::mock}, clock);
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
auto* ctrl = drv.mock_controller();
|
||||
require(ctrl != nullptr);
|
||||
|
||||
auto s_res = drv.create_mock_stream();
|
||||
require(s_res.has_value());
|
||||
ByteStream stream = std::move(*s_res);
|
||||
|
||||
std::array<std::byte, 8> buf{};
|
||||
std::optional<Void::Result::Result<Transfer>> read_res;
|
||||
TaskScope scope{exec};
|
||||
auto rtask = [](ByteStream* st,
|
||||
std::span<std::byte> sp,
|
||||
std::optional<Void::Result::Result<Transfer>>* out)
|
||||
-> Void::Coro::Task<void> { *out = co_await st->read_some(sp); };
|
||||
require(scope.spawn(rtask(&stream, buf, &read_res)).has_value());
|
||||
(void)exec.drain();
|
||||
|
||||
const std::array<std::byte, 3> payload{
|
||||
std::byte{'o'}, std::byte{'k'}, std::byte{'!'}};
|
||||
expect(ctrl->complete_read(stream, payload));
|
||||
|
||||
auto work = pump_until(drv, exec, [&] { return scope.active_count() == 0; });
|
||||
require(work.has_value());
|
||||
expect(*work >= std::size_t{1});
|
||||
require(read_res.has_value() && read_res->has_value());
|
||||
expect_eq((*read_res)->bytes, std::size_t{3});
|
||||
}
|
||||
|
||||
Void::Testing::Test io_pump_until_deadline_injected_clock()
|
||||
{
|
||||
ManualExecutor exec;
|
||||
Void::Time::ManualClock clock{
|
||||
Void::Time::MonotonicClock::time_point{std::chrono::seconds{10}}};
|
||||
auto drv_res = Driver::create(
|
||||
exec, BackendOptions{.preference = BackendPreference::mock}, clock);
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
|
||||
// 1. Pre-expired deadline returns timeout with zero poll turns.
|
||||
auto r0 = pump_until(
|
||||
drv,
|
||||
exec,
|
||||
[] { return false; },
|
||||
PumpOptions{.operation = OperationOptions{.deadline = clock.now()}});
|
||||
require(!r0.has_value());
|
||||
expect_eq(r0.error().code().value,
|
||||
static_cast<std::int32_t>(IoErrorCode::timeout));
|
||||
expect_eq(drv.telemetry().turns, std::uint64_t{0});
|
||||
|
||||
// 2. Bounded turns advancing injected ManualClock expire cleanly on turn 3.
|
||||
const auto dl = clock.now() + 30ms;
|
||||
int checks = 0;
|
||||
auto r1 = pump_until(
|
||||
drv,
|
||||
exec,
|
||||
[&]() {
|
||||
if (++checks >= 2) {
|
||||
(void)clock.advance(10ms);
|
||||
}
|
||||
return false;
|
||||
},
|
||||
PumpOptions{
|
||||
.max_wait = 5ms,
|
||||
.max_turns = 20,
|
||||
.operation = OperationOptions{.deadline = dl},
|
||||
});
|
||||
require(!r1.has_value());
|
||||
expect_eq(r1.error().code().value,
|
||||
static_cast<std::int32_t>(IoErrorCode::timeout));
|
||||
expect(drv.telemetry().turns > std::uint64_t{0});
|
||||
expect(drv.telemetry().turns <= std::uint64_t{5});
|
||||
}
|
||||
|
||||
Void::Testing::Test io_pump_until_max_turns_exhausted()
|
||||
{
|
||||
ManualExecutor exec;
|
||||
Void::Time::ManualClock clock;
|
||||
auto drv_res = Driver::create(
|
||||
exec, BackendOptions{.preference = BackendPreference::mock}, clock);
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
|
||||
auto res = pump_until(
|
||||
drv,
|
||||
exec,
|
||||
[] { return false; },
|
||||
PumpOptions{.max_wait = 0ns, .max_turns = 5});
|
||||
require(!res.has_value());
|
||||
expect_eq(res.error().code().value,
|
||||
static_cast<std::int32_t>(IoErrorCode::exhausted));
|
||||
expect_eq(drv.telemetry().turns, std::uint64_t{5});
|
||||
}
|
||||
|
||||
Void::Testing::Test io_pump_until_drives_timer_queue()
|
||||
{
|
||||
ManualExecutor exec;
|
||||
Void::Time::ManualClock clock;
|
||||
auto drv_res = Driver::create(
|
||||
exec, BackendOptions{.preference = BackendPreference::mock}, clock);
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
|
||||
auto tq_res = TimerQueue::create(exec, clock, TimerQueueOptions{});
|
||||
require(tq_res.has_value());
|
||||
auto& timers = **tq_res;
|
||||
|
||||
bool fired = false;
|
||||
TimerEvent seen = TimerEvent::closed;
|
||||
auto h = timers.schedule_at(clock.now() + 10ms, [&](TimerEvent ev) noexcept {
|
||||
seen = ev;
|
||||
fired = true;
|
||||
});
|
||||
require(h.has_value());
|
||||
|
||||
// Advance ManualClock from an executor coroutine on turn 1 so poll_expired fires on turn 2.
|
||||
TaskScope scope{exec};
|
||||
auto adv_task = [](Void::Time::ManualClock* c) -> Void::Coro::Task<void> {
|
||||
(void)c->advance(10ms);
|
||||
co_return;
|
||||
};
|
||||
require(scope.spawn(adv_task(&clock)).has_value());
|
||||
|
||||
auto res = pump_until(
|
||||
drv,
|
||||
exec,
|
||||
[&]() { return fired; },
|
||||
PumpOptions{.max_wait = 50ms, .max_turns = 16, .timers = &timers});
|
||||
require(res.has_value());
|
||||
expect(fired);
|
||||
expect(seen == TimerEvent::expired);
|
||||
require(scope.join_pumping(exec).has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test io_pump_until_stop_cancelled()
|
||||
{
|
||||
ManualExecutor exec;
|
||||
Void::Time::ManualClock clock;
|
||||
auto drv_res = Driver::create(
|
||||
exec, BackendOptions{.preference = BackendPreference::mock}, clock);
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
|
||||
std::stop_source stop_src;
|
||||
TaskScope scope{exec};
|
||||
auto req_task = [](std::stop_source* src) -> Void::Coro::Task<void> {
|
||||
src->request_stop();
|
||||
co_return;
|
||||
};
|
||||
require(scope.spawn(req_task(&stop_src)).has_value());
|
||||
|
||||
auto res = pump_until(
|
||||
drv,
|
||||
exec,
|
||||
[] { return false; },
|
||||
PumpOptions{
|
||||
.max_turns = 16,
|
||||
.operation = OperationOptions{.stop = stop_src.get_token()},
|
||||
});
|
||||
require(!res.has_value());
|
||||
expect_eq(res.error().code().value,
|
||||
static_cast<std::int32_t>(IoErrorCode::cancelled));
|
||||
require(scope.join_pumping(exec).has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test io_pump_until_exits_after_shutdown_complete()
|
||||
{
|
||||
// 1. Pre-shut-down Driver exits pump_until immediately with 0 poll turns.
|
||||
{
|
||||
ManualExecutor exec;
|
||||
Void::Time::ManualClock clock;
|
||||
auto drv_res = Driver::create(
|
||||
exec, BackendOptions{.preference = BackendPreference::mock}, clock);
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
|
||||
auto rep = drv.shutdown(OperationOptions{});
|
||||
require(rep.has_value());
|
||||
expect(rep->complete);
|
||||
expect(drv.shutdown_complete());
|
||||
|
||||
auto res = pump_until(
|
||||
drv,
|
||||
exec,
|
||||
[] { return false; },
|
||||
PumpOptions{.max_turns = 10});
|
||||
require(res.has_value());
|
||||
expect_eq(drv.telemetry().turns, std::uint64_t{0});
|
||||
}
|
||||
|
||||
// 2. Coroutine calling Driver::shutdown during pump_until causes prompt exit.
|
||||
{
|
||||
ManualExecutor exec;
|
||||
Void::Time::ManualClock clock;
|
||||
auto drv_res = Driver::create(
|
||||
exec, BackendOptions{.preference = BackendPreference::mock}, clock);
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
|
||||
TaskScope scope{exec};
|
||||
auto sd_task = [](Driver* d) -> Void::Coro::Task<void> {
|
||||
(void)d->shutdown(OperationOptions{});
|
||||
co_return;
|
||||
};
|
||||
require(scope.spawn(sd_task(&drv)).has_value());
|
||||
|
||||
auto res = pump_until(
|
||||
drv,
|
||||
exec,
|
||||
[] { return false; },
|
||||
PumpOptions{.max_turns = 10});
|
||||
require(res.has_value());
|
||||
expect(drv.shutdown_complete());
|
||||
expect_eq(drv.telemetry().turns, std::uint64_t{1});
|
||||
require(scope.join_pumping(exec).has_value());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,532 @@
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/Operation.hpp>
|
||||
#include <Void/Async/TaskScope.hpp>
|
||||
#include <Void/IO/Driver.hpp>
|
||||
#include <Void/IO/File.hpp>
|
||||
#include <Void/IO/Stream.hpp>
|
||||
#include <Void/IO/Types.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 <mutex>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <stop_token>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
using Void::Async::ManualExecutor;
|
||||
using Void::Async::OperationOptions;
|
||||
using Void::Async::TaskScope;
|
||||
using Void::IO::BackendOptions;
|
||||
using Void::IO::BackendPreference;
|
||||
using Void::IO::BufferLease;
|
||||
using Void::IO::ByteStream;
|
||||
using Void::IO::Driver;
|
||||
using Void::IO::DriverShutdownReport;
|
||||
using Void::IO::File;
|
||||
using Void::IO::FileOpenOptions;
|
||||
using Void::IO::FileSyscallResult;
|
||||
using Void::IO::FileSyscalls;
|
||||
using Void::IO::IoErrorCode;
|
||||
using Void::IO::IoLimits;
|
||||
using Void::IO::Transfer;
|
||||
using Void::Testing::expect;
|
||||
using Void::Testing::expect_eq;
|
||||
using Void::Testing::require;
|
||||
|
||||
class ScopedTempDir
|
||||
{
|
||||
public:
|
||||
ScopedTempDir()
|
||||
{
|
||||
auto tmpl = (std::filesystem::temp_directory_path() / "void_w11_io4_XXXXXX")
|
||||
.string();
|
||||
std::vector<char> buf(tmpl.begin(), tmpl.end());
|
||||
buf.push_back('\0');
|
||||
char* created = ::mkdtemp(buf.data());
|
||||
if (created != nullptr) {
|
||||
path_ = created;
|
||||
}
|
||||
}
|
||||
|
||||
~ScopedTempDir()
|
||||
{
|
||||
if (!path_.empty()) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove_all(path_, ec);
|
||||
}
|
||||
}
|
||||
|
||||
ScopedTempDir(const ScopedTempDir&) = delete;
|
||||
ScopedTempDir& operator=(const ScopedTempDir&) = delete;
|
||||
|
||||
[[nodiscard]] bool valid() const noexcept { return !path_.empty(); }
|
||||
[[nodiscard]] std::filesystem::path file(std::string_view name) const
|
||||
{
|
||||
return path_ / name;
|
||||
}
|
||||
|
||||
private:
|
||||
std::filesystem::path path_{};
|
||||
};
|
||||
|
||||
class Gate
|
||||
{
|
||||
public:
|
||||
void wait()
|
||||
{
|
||||
std::unique_lock lk{mu_};
|
||||
++waiting_;
|
||||
cv_.notify_all();
|
||||
cv_.wait(lk, [this] { 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 GatedFileSyscalls final : public FileSyscalls
|
||||
{
|
||||
public:
|
||||
Gate gate{};
|
||||
std::atomic<bool> block_pread{true};
|
||||
std::atomic<int> preads{0};
|
||||
std::atomic<int> closes{0};
|
||||
|
||||
FileSyscallResult pread(int fd, std::span<std::byte> buf,
|
||||
std::uint64_t offset) noexcept override
|
||||
{
|
||||
preads.fetch_add(1, std::memory_order_relaxed);
|
||||
if (block_pread.load(std::memory_order_acquire)) {
|
||||
gate.wait();
|
||||
}
|
||||
return FileSyscalls::pread(fd, buf, offset);
|
||||
}
|
||||
|
||||
FileSyscallResult close(int fd) noexcept override
|
||||
{
|
||||
closes.fetch_add(1, std::memory_order_relaxed);
|
||||
return FileSyscalls::close(fd);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test io_shutdown_expired_budget_incomplete_retains_stalled_file_worker()
|
||||
{
|
||||
ScopedTempDir dir;
|
||||
require(dir.valid());
|
||||
const auto path = dir.file("stalled.dat");
|
||||
{
|
||||
std::ofstream ofs(path, std::ios::binary);
|
||||
ofs << "hello_stalled_worker";
|
||||
}
|
||||
|
||||
GatedFileSyscalls sys;
|
||||
ManualExecutor exec;
|
||||
Void::Time::ManualClock clock{
|
||||
Void::Time::MonotonicClock::time_point{std::chrono::seconds{50}}};
|
||||
|
||||
auto drv_res = Driver::create(
|
||||
exec,
|
||||
BackendOptions{
|
||||
.limits = IoLimits{.file_workers = 1, .max_file_queue = 8},
|
||||
.preference = BackendPreference::epoll,
|
||||
.file_syscalls = &sys,
|
||||
},
|
||||
clock);
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
require(drv.start_file_workers().has_value());
|
||||
|
||||
auto f_res = drv.open_file(path, FileOpenOptions{});
|
||||
require(f_res.has_value());
|
||||
File file = std::move(*f_res);
|
||||
|
||||
std::array<std::byte, 16> buf{};
|
||||
std::optional<Void::Result::Result<Transfer>> read_res;
|
||||
int deliveries = 0;
|
||||
|
||||
TaskScope scope{exec};
|
||||
auto read_task = [](File* f,
|
||||
std::span<std::byte> b,
|
||||
std::optional<Void::Result::Result<Transfer>>* out,
|
||||
int* count) -> Void::Coro::Task<void> {
|
||||
*out = co_await f->read_at(0, b);
|
||||
++(*count);
|
||||
};
|
||||
require(scope.spawn(read_task(&file, buf, &read_res, &deliveries)).has_value());
|
||||
(void)exec.drain();
|
||||
|
||||
require(sys.gate.wait_for_waiters(1));
|
||||
expect_eq(drv.telemetry().active_file_requests, std::size_t{1});
|
||||
|
||||
// Expired budget on shutdown leaves worker stalled, fd open, and complete=false.
|
||||
auto r1 = drv.shutdown(OperationOptions{.deadline = clock.now()});
|
||||
require(r1.has_value());
|
||||
expect(!r1->complete);
|
||||
expect_eq(r1->active_file_requests, std::size_t{1});
|
||||
expect(r1->open_descriptors >= std::size_t{1});
|
||||
expect(!drv.shutdown_complete());
|
||||
expect_eq(sys.closes.load(), 0);
|
||||
expect_eq(deliveries, 0);
|
||||
|
||||
// Unblock worker and finish shutdown.
|
||||
sys.gate.open();
|
||||
auto r2 = drv.shutdown(
|
||||
OperationOptions{.deadline = Void::Time::deadline_after(clock, 5s)});
|
||||
require(r2.has_value());
|
||||
expect(r2->complete);
|
||||
expect(drv.shutdown_complete());
|
||||
expect_eq(r2->active_file_requests, std::size_t{0});
|
||||
expect_eq(r2->open_descriptors, std::size_t{0});
|
||||
|
||||
(void)scope.join_pumping(exec);
|
||||
expect_eq(deliveries, 1);
|
||||
require(read_res.has_value() && read_res->has_value());
|
||||
expect_eq((*read_res)->bytes, std::size_t{16});
|
||||
expect_eq(sys.closes.load(), 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test io_shutdown_pending_socket_read_retains_buffer_until_delivery()
|
||||
{
|
||||
// 1. In-flight mock stream read retains borrowed buffer and op across shutdown
|
||||
// until explicitly delivered once by controller.
|
||||
{
|
||||
ManualExecutor exec;
|
||||
auto drv_res = Driver::create(
|
||||
exec, BackendOptions{.preference = BackendPreference::mock});
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
auto* ctrl = drv.mock_controller();
|
||||
require(ctrl != nullptr);
|
||||
|
||||
auto s_res = drv.create_mock_stream();
|
||||
require(s_res.has_value());
|
||||
ByteStream stream = std::move(*s_res);
|
||||
|
||||
std::array<std::byte, 32> buf{};
|
||||
std::optional<Void::Result::Result<Transfer>> res;
|
||||
int deliveries = 0;
|
||||
|
||||
TaskScope scope{exec};
|
||||
auto read_task = [](ByteStream* s,
|
||||
std::span<std::byte> sp,
|
||||
std::optional<Void::Result::Result<Transfer>>* out,
|
||||
int* count) -> Void::Coro::Task<void> {
|
||||
*out = co_await s->read_some(sp);
|
||||
++(*count);
|
||||
};
|
||||
require(scope.spawn(read_task(&stream, buf, &res, &deliveries)).has_value());
|
||||
(void)exec.drain();
|
||||
|
||||
const auto tok = ctrl->try_acquire_read(stream);
|
||||
require(tok.has_value());
|
||||
expect_eq(drv.telemetry().borrowed_buffer_bytes, std::size_t{32});
|
||||
|
||||
auto r1 = drv.shutdown(OperationOptions{});
|
||||
require(r1.has_value());
|
||||
expect(r1->complete);
|
||||
expect_eq(r1->pending_operations, std::size_t{1});
|
||||
expect_eq(r1->retained_buffer_bytes, std::size_t{32});
|
||||
expect_eq(drv.telemetry().borrowed_buffer_bytes, std::size_t{32});
|
||||
expect_eq(deliveries, 0);
|
||||
|
||||
const std::array<std::byte, 4> payload{
|
||||
std::byte{'d'}, std::byte{'a'}, std::byte{'t'}, std::byte{'a'}};
|
||||
expect(ctrl->finish_read_data(*tok, payload, false));
|
||||
(void)scope.join_pumping(exec);
|
||||
|
||||
expect_eq(deliveries, 1);
|
||||
require(res.has_value() && res->has_value());
|
||||
expect_eq((*res)->bytes, std::size_t{4});
|
||||
expect_eq(drv.telemetry().borrowed_buffer_bytes, std::size_t{0});
|
||||
}
|
||||
|
||||
// 2. Epoll socketpair read_some_owned retains owned BufferLease across shutdown
|
||||
// until executor delivers the closed completion once and drops the lease.
|
||||
{
|
||||
ManualExecutor exec;
|
||||
auto drv_res = Driver::create(
|
||||
exec, BackendOptions{.preference = BackendPreference::epoll});
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
|
||||
auto pair_res = drv.create_socket_pair();
|
||||
require(pair_res.has_value());
|
||||
ByteStream a = std::move(pair_res->first);
|
||||
ByteStream b = std::move(pair_res->second);
|
||||
|
||||
auto lease_res = drv.allocate_buffer(64);
|
||||
require(lease_res.has_value());
|
||||
|
||||
std::optional<Void::Result::Result<Transfer>> res;
|
||||
int deliveries = 0;
|
||||
|
||||
TaskScope scope{exec};
|
||||
auto sock_task = [](ByteStream* s,
|
||||
BufferLease lease,
|
||||
std::optional<Void::Result::Result<Transfer>>* out,
|
||||
int* count) -> Void::Coro::Task<void> {
|
||||
*out = co_await s->read_some_owned(lease);
|
||||
++(*count);
|
||||
lease.reset();
|
||||
};
|
||||
require(scope
|
||||
.spawn(sock_task(&a, std::move(*lease_res), &res, &deliveries))
|
||||
.has_value());
|
||||
(void)exec.drain();
|
||||
expect_eq(drv.telemetry().active_operations, std::size_t{1});
|
||||
expect_eq(drv.telemetry().borrowed_buffer_bytes, std::size_t{64});
|
||||
expect_eq(drv.telemetry().owned_buffer_bytes, std::size_t{64});
|
||||
|
||||
auto r2 = drv.shutdown(OperationOptions{});
|
||||
require(r2.has_value());
|
||||
expect(r2->complete);
|
||||
expect(drv.shutdown_complete());
|
||||
expect_eq(r2->open_descriptors, std::size_t{0});
|
||||
expect_eq(drv.telemetry().borrowed_buffer_bytes, std::size_t{0});
|
||||
expect_eq(drv.telemetry().owned_buffer_bytes, std::size_t{64});
|
||||
expect_eq(deliveries, 0);
|
||||
|
||||
(void)scope.join_pumping(exec);
|
||||
expect_eq(deliveries, 1);
|
||||
require(res.has_value());
|
||||
expect(!res->has_value());
|
||||
expect_eq(res->error().code().value,
|
||||
static_cast<std::int32_t>(IoErrorCode::closed));
|
||||
expect_eq(drv.telemetry().owned_buffer_bytes, std::size_t{0});
|
||||
(void)b;
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test io_shutdown_repeated_idempotent_after_complete()
|
||||
{
|
||||
ManualExecutor exec;
|
||||
auto drv_res = Driver::create(
|
||||
exec, BackendOptions{.preference = BackendPreference::epoll});
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
auto r = drv.shutdown(OperationOptions{});
|
||||
require(r.has_value());
|
||||
expect(r->complete);
|
||||
expect(drv.shutdown_complete());
|
||||
expect_eq(r->pending_operations, std::size_t{0});
|
||||
expect_eq(r->open_descriptors, std::size_t{0});
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test io_shutdown_concurrent_with_run_loop_tsan()
|
||||
{
|
||||
ManualExecutor exec;
|
||||
auto drv_res = Driver::create(
|
||||
exec, BackendOptions{.preference = BackendPreference::epoll});
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
|
||||
auto pair_res = drv.create_socket_pair();
|
||||
require(pair_res.has_value());
|
||||
ByteStream a = std::move(pair_res->first);
|
||||
ByteStream b = std::move(pair_res->second);
|
||||
|
||||
std::stop_source stop_src;
|
||||
std::optional<Void::Result::Result<std::size_t>> run_res;
|
||||
std::thread runner([&]() {
|
||||
run_res = drv.run(stop_src.get_token(), 2ms);
|
||||
});
|
||||
|
||||
std::array<std::byte, 8> rbuf{};
|
||||
std::optional<Void::Result::Result<Transfer>> read_res;
|
||||
TaskScope scope{exec};
|
||||
auto rtask = [](ByteStream* s,
|
||||
std::span<std::byte> sp,
|
||||
std::optional<Void::Result::Result<Transfer>>* out)
|
||||
-> Void::Coro::Task<void> {
|
||||
*out = co_await s->read_some(sp);
|
||||
};
|
||||
require(scope.spawn(rtask(&a, rbuf, &read_res)).has_value());
|
||||
(void)exec.drain();
|
||||
|
||||
// Concurrently shut down driver while runner thread executes run() loop.
|
||||
std::this_thread::sleep_for(5ms);
|
||||
auto rep = drv.shutdown(OperationOptions{});
|
||||
require(rep.has_value());
|
||||
expect(rep->complete);
|
||||
|
||||
runner.join();
|
||||
require(run_res.has_value() && run_res->has_value());
|
||||
expect(drv.shutdown_complete());
|
||||
|
||||
(void)scope.join_pumping(exec);
|
||||
require(read_res.has_value());
|
||||
(void)b;
|
||||
}
|
||||
|
||||
Void::Testing::Test io_run_stops_on_token_and_on_shutdown()
|
||||
{
|
||||
// 1. Stops promptly on std::stop_source request.
|
||||
{
|
||||
ManualExecutor exec;
|
||||
auto drv_res = Driver::create(
|
||||
exec, BackendOptions{.preference = BackendPreference::epoll});
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
|
||||
std::stop_source stop_src;
|
||||
Void::Result::Result<std::size_t> res{std::size_t{0}};
|
||||
std::thread t([&]() { res = drv.run(stop_src.get_token(), 50ms); });
|
||||
|
||||
std::this_thread::sleep_for(5ms);
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
stop_src.request_stop();
|
||||
drv.wake();
|
||||
t.join();
|
||||
const auto elapsed = std::chrono::steady_clock::now() - t0;
|
||||
|
||||
require(res.has_value());
|
||||
expect(elapsed < 40ms);
|
||||
expect(!drv.shutdown_complete());
|
||||
require(drv.shutdown(OperationOptions{}).has_value());
|
||||
}
|
||||
|
||||
// 2. Exits when shutdown_complete() becomes true even with unstoppable token.
|
||||
{
|
||||
ManualExecutor exec;
|
||||
auto drv_res = Driver::create(
|
||||
exec, BackendOptions{.preference = BackendPreference::mock});
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
|
||||
Void::Result::Result<std::size_t> res{std::size_t{0}};
|
||||
std::thread t([&]() { res = drv.run(std::stop_token{}, 2ms); });
|
||||
|
||||
std::this_thread::sleep_for(5ms);
|
||||
auto rep = drv.shutdown(OperationOptions{});
|
||||
require(rep.has_value());
|
||||
expect(rep->complete);
|
||||
t.join();
|
||||
|
||||
require(res.has_value());
|
||||
expect(drv.shutdown_complete());
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test io_begin_shutdown_rejects_new_admission()
|
||||
{
|
||||
ManualExecutor exec;
|
||||
auto drv_res = Driver::create(
|
||||
exec, BackendOptions{.preference = BackendPreference::mock});
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
|
||||
drv.begin_shutdown();
|
||||
|
||||
auto sock = drv.create_mock_stream();
|
||||
expect(!sock.has_value());
|
||||
expect_eq(sock.error().code().value,
|
||||
static_cast<std::int32_t>(IoErrorCode::closed));
|
||||
|
||||
auto f = drv.open_file("nonexistent_after_shutdown.bin");
|
||||
expect(!f.has_value());
|
||||
expect_eq(f.error().code().value,
|
||||
static_cast<std::int32_t>(IoErrorCode::closed));
|
||||
|
||||
auto sw = drv.start_file_workers();
|
||||
expect(!sw.has_value());
|
||||
expect_eq(sw.error().code().value,
|
||||
static_cast<std::int32_t>(IoErrorCode::closed));
|
||||
|
||||
auto rep = drv.shutdown(OperationOptions{});
|
||||
require(rep.has_value());
|
||||
expect(rep->complete);
|
||||
expect(drv.quiescent());
|
||||
}
|
||||
|
||||
Void::Testing::Test io_run_busy_and_mock_wait_does_not_spin()
|
||||
{
|
||||
// 1. Mock driver with 5ms max_wait does not spin over ~30ms.
|
||||
{
|
||||
ManualExecutor exec;
|
||||
auto drv_res = Driver::create(
|
||||
exec, BackendOptions{.preference = BackendPreference::mock});
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
|
||||
std::stop_source stop;
|
||||
Void::Result::Result<std::size_t> res{std::size_t{0}};
|
||||
std::thread t([&]() { res = drv.run(stop.get_token(), 5ms); });
|
||||
std::this_thread::sleep_for(30ms);
|
||||
stop.request_stop();
|
||||
t.join();
|
||||
|
||||
require(res.has_value());
|
||||
expect(drv.telemetry().turns < 40);
|
||||
}
|
||||
|
||||
// 2. Epoll busy retry waits stop-aware without returning busy or spinning.
|
||||
{
|
||||
ManualExecutor exec;
|
||||
auto drv_res = Driver::create(
|
||||
exec, BackendOptions{.preference = BackendPreference::epoll});
|
||||
require(drv_res.has_value());
|
||||
Driver& drv = **drv_res;
|
||||
|
||||
std::atomic<bool> holder_started{false};
|
||||
std::thread holder([&]() {
|
||||
holder_started.store(true, std::memory_order_release);
|
||||
(void)drv.poll_once(25ms);
|
||||
});
|
||||
while (!holder_started.load(std::memory_order_acquire)) {
|
||||
std::this_thread::yield();
|
||||
}
|
||||
std::this_thread::sleep_for(2ms);
|
||||
|
||||
std::stop_source stop;
|
||||
Void::Result::Result<std::size_t> res{std::size_t{0}};
|
||||
std::thread runner([&]() { res = drv.run(stop.get_token(), 2ms); });
|
||||
std::this_thread::sleep_for(15ms);
|
||||
stop.request_stop();
|
||||
holder.join();
|
||||
runner.join();
|
||||
|
||||
require(res.has_value());
|
||||
expect(drv.telemetry().turns < 50);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/Operation.hpp>
|
||||
#include <Void/Async/TaskScope.hpp>
|
||||
#include <Void/IO/Driver.hpp>
|
||||
#include <Void/IO/File.hpp>
|
||||
#include <Void/IO/Pump.hpp>
|
||||
#include <Void/IO/Types.hpp>
|
||||
#include <Void/System/AsyncProcess.hpp>
|
||||
#include <Void/System/Process.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <csignal>
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <stop_token>
|
||||
#include <vector>
|
||||
|
||||
#ifndef VOID_PROCESS_HELPER_PATH
|
||||
#define VOID_PROCESS_HELPER_PATH ""
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
using Void::Testing::expect;
|
||||
using Void::Testing::expect_eq;
|
||||
using Void::Testing::require;
|
||||
|
||||
class ScopedTempDir
|
||||
{
|
||||
public:
|
||||
ScopedTempDir()
|
||||
{
|
||||
auto tmpl =
|
||||
(std::filesystem::temp_directory_path() / "void_w11_sysasync_XXXXXX")
|
||||
.string();
|
||||
std::vector<char> buf(tmpl.begin(), tmpl.end());
|
||||
buf.push_back('\0');
|
||||
char* created = ::mkdtemp(buf.data());
|
||||
if (created != nullptr) {
|
||||
path_ = created;
|
||||
}
|
||||
}
|
||||
|
||||
~ScopedTempDir()
|
||||
{
|
||||
if (!path_.empty()) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove_all(path_, ec);
|
||||
}
|
||||
}
|
||||
|
||||
ScopedTempDir(const ScopedTempDir&) = delete;
|
||||
ScopedTempDir& operator=(const ScopedTempDir&) = delete;
|
||||
|
||||
[[nodiscard]] bool valid() const noexcept { return !path_.empty(); }
|
||||
[[nodiscard]] std::filesystem::path file(std::string_view name) const
|
||||
{
|
||||
return path_ / name;
|
||||
}
|
||||
|
||||
private:
|
||||
std::filesystem::path path_{};
|
||||
};
|
||||
|
||||
class Gate
|
||||
{
|
||||
public:
|
||||
void wait()
|
||||
{
|
||||
std::unique_lock lk{mu_};
|
||||
++waiting_;
|
||||
cv_.notify_all();
|
||||
cv_.wait(lk, [this] { 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 GatedFileSyscalls final : public Void::IO::FileSyscalls
|
||||
{
|
||||
public:
|
||||
Gate gate{};
|
||||
|
||||
Void::IO::FileSyscallResult pread(int fd, std::span<std::byte> buf,
|
||||
std::uint64_t offset) noexcept override
|
||||
{
|
||||
gate.wait();
|
||||
return Void::IO::FileSyscalls::pread(fd, buf, offset);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test systemasync_driver_shutdown_pending_pipe_read_retains_and_delivers_once()
|
||||
{
|
||||
ScopedTempDir dir;
|
||||
require(dir.valid());
|
||||
const auto path = dir.file("pipe_gate.bin");
|
||||
{
|
||||
std::ofstream ofs(path, std::ios::binary);
|
||||
ofs << "gate_data_123456";
|
||||
}
|
||||
|
||||
GatedFileSyscalls sys;
|
||||
Void::Time::ManualClock clock{
|
||||
Void::Time::MonotonicClock::time_point{std::chrono::seconds{10}}};
|
||||
Void::Async::ManualExecutor exec;
|
||||
auto drv_res = Void::IO::Driver::create(
|
||||
exec,
|
||||
Void::IO::BackendOptions{
|
||||
.limits = Void::IO::IoLimits{.file_workers = 1, .max_file_queue = 8},
|
||||
.preference = Void::IO::BackendPreference::epoll,
|
||||
.file_syscalls = &sys,
|
||||
},
|
||||
clock);
|
||||
require(drv_res.has_value());
|
||||
Void::IO::Driver& drv = **drv_res;
|
||||
require(drv.start_file_workers().has_value());
|
||||
|
||||
auto f_res = drv.open_file(path, Void::IO::FileOpenOptions{});
|
||||
require(f_res.has_value());
|
||||
Void::IO::File file = std::move(*f_res);
|
||||
|
||||
Void::SystemAsync::ProcessManager mgr{
|
||||
drv,
|
||||
exec,
|
||||
Void::SystemAsync::ProcessManagerOptions{
|
||||
.default_kill_grace = 30ms,
|
||||
}};
|
||||
|
||||
Void::Process::SpawnOptions opts;
|
||||
opts.executable = VOID_PROCESS_HELPER_PATH;
|
||||
opts.arguments = {"ignore-sigterm", "10000"};
|
||||
opts.kill_grace = 20ms;
|
||||
auto child_res = mgr.spawn(opts);
|
||||
require(child_res.has_value());
|
||||
auto child = std::move(*child_res);
|
||||
|
||||
std::array<std::byte, 8> fbuf{};
|
||||
std::optional<Void::Result::Result<Void::IO::Transfer>> file_res;
|
||||
int file_deliveries = 0;
|
||||
|
||||
std::optional<
|
||||
Void::Result::Result<Void::Process::CommunicateResult, Void::Result::Error>>
|
||||
comm_res;
|
||||
int comm_deliveries = 0;
|
||||
|
||||
Void::Async::TaskScope scope{exec};
|
||||
auto file_coro = [](Void::IO::File* f,
|
||||
std::span<std::byte> b,
|
||||
std::optional<Void::Result::Result<Void::IO::Transfer>>* out,
|
||||
int* count) -> Void::Coro::Task<void> {
|
||||
*out = co_await f->read_at(0, b);
|
||||
++(*count);
|
||||
};
|
||||
auto comm_coro = [](Void::SystemAsync::ProcessManager* m,
|
||||
Void::SystemAsync::ManagedChild* ch,
|
||||
std::optional<Void::Result::Result<
|
||||
Void::Process::CommunicateResult,
|
||||
Void::Result::Error>>* out,
|
||||
int* count) -> Void::Coro::Task<void> {
|
||||
*out = co_await m->communicate(*ch);
|
||||
++(*count);
|
||||
};
|
||||
require(scope.spawn(file_coro(&file, fbuf, &file_res, &file_deliveries)).has_value());
|
||||
require(scope.spawn(comm_coro(&mgr, &child, &comm_res, &comm_deliveries)).has_value());
|
||||
|
||||
// Wait for the stalled file worker AND ProcessManager's two pipe leases (2 * 16 KiB).
|
||||
auto wait_reg = Void::IO::pump_until(
|
||||
drv,
|
||||
exec,
|
||||
[&]() {
|
||||
return drv.telemetry().active_file_requests >= 1 &&
|
||||
drv.telemetry().owned_buffer_bytes >= 32768;
|
||||
},
|
||||
Void::IO::PumpOptions{.max_wait = 2ms, .max_turns = 200});
|
||||
require(wait_reg.has_value());
|
||||
require(sys.gate.wait_for_waiters(1));
|
||||
expect_eq(comm_deliveries, 0);
|
||||
expect_eq(file_deliveries, 0);
|
||||
|
||||
// Bounded Driver::shutdown expires while file worker stalls -> incomplete, pipe leases retained.
|
||||
auto rep1 = drv.shutdown(
|
||||
Void::Async::OperationOptions{.deadline = clock.now()});
|
||||
require(rep1.has_value());
|
||||
expect(!rep1->complete);
|
||||
expect_eq(rep1->active_file_requests, std::size_t{1});
|
||||
expect(rep1->open_descriptors >= std::size_t{1});
|
||||
expect_eq(drv.telemetry().owned_buffer_bytes, std::size_t{32768});
|
||||
expect_eq(comm_deliveries, 0);
|
||||
expect_eq(file_deliveries, 0);
|
||||
|
||||
// Unblock file worker and kill child -> Driver shutdown completes and delivers once.
|
||||
sys.gate.open();
|
||||
(void)child.child().send_signal(SIGKILL);
|
||||
auto rep2 = drv.shutdown(
|
||||
Void::Async::OperationOptions{
|
||||
.deadline = Void::Time::deadline_after(clock, 5s)});
|
||||
require(rep2.has_value());
|
||||
expect(rep2->complete);
|
||||
expect(drv.shutdown_complete());
|
||||
expect_eq(rep2->open_descriptors, std::size_t{0});
|
||||
|
||||
(void)scope.join_pumping(exec);
|
||||
expect_eq(file_deliveries, 1);
|
||||
expect_eq(comm_deliveries, 1);
|
||||
require(file_res.has_value() && file_res->has_value());
|
||||
expect_eq((*file_res)->bytes, std::size_t{8});
|
||||
expect(comm_res.has_value());
|
||||
expect_eq(drv.telemetry().owned_buffer_bytes, std::size_t{0});
|
||||
}
|
||||
Reference in new issue
Block a user