244 lines
7.2 KiB
C++
244 lines
7.2 KiB
C++
#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});
|
|
}
|