183 lines
7.0 KiB
C++
183 lines
7.0 KiB
C++
#include <Void/Build/Daemon/Daemon.hpp>
|
|
#include <Void/Data/StrictJson.hpp>
|
|
#include <Void/System/TempDir.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <system_error>
|
|
#include <thread>
|
|
#include <utility>
|
|
#include <variant>
|
|
|
|
namespace {
|
|
|
|
using JsonValue = Void::Json::Strict::Value;
|
|
using JsonObject = JsonValue::Object;
|
|
|
|
std::optional<JsonValue> parse_json(const std::string &line) {
|
|
auto parsed = Void::Json::Strict::parse(line, 16);
|
|
if (!parsed) {
|
|
return std::nullopt;
|
|
}
|
|
return std::move(*parsed);
|
|
}
|
|
|
|
const JsonObject *as_object(const JsonValue &value) {
|
|
return std::get_if<JsonObject>(&value.value);
|
|
}
|
|
|
|
const JsonValue *field(const JsonObject &object, std::string_view name) {
|
|
const auto entry = object.find(name);
|
|
return entry == object.end() ? nullptr : &entry->second;
|
|
}
|
|
|
|
bool bool_field(const JsonObject &object, std::string_view name, bool fallback = false) {
|
|
const auto *value = field(object, name);
|
|
if (value == nullptr) {
|
|
return fallback;
|
|
}
|
|
const auto *flag = std::get_if<bool>(&value->value);
|
|
return flag != nullptr ? *flag : fallback;
|
|
}
|
|
|
|
std::int64_t int_field(const JsonObject &object, std::string_view name) {
|
|
const auto *value = field(object, name);
|
|
if (value == nullptr) {
|
|
return -1;
|
|
}
|
|
const auto *number = std::get_if<std::int64_t>(&value->value);
|
|
return number != nullptr ? *number : -1;
|
|
}
|
|
|
|
std::string str_field(const JsonObject &object, std::string_view name) {
|
|
const auto *value = field(object, name);
|
|
if (value == nullptr) {
|
|
return {};
|
|
}
|
|
const auto *text = std::get_if<std::string>(&value->value);
|
|
return text != nullptr ? *text : std::string{};
|
|
}
|
|
|
|
bool wait_for_path(const std::filesystem::path &path, std::chrono::milliseconds timeout) {
|
|
const auto deadline = std::chrono::steady_clock::now() + timeout;
|
|
while (std::chrono::steady_clock::now() < deadline) {
|
|
if (std::filesystem::exists(path)) {
|
|
return true;
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{10});
|
|
}
|
|
return std::filesystem::exists(path);
|
|
}
|
|
|
|
Void::Build::Daemon::DaemonOptions make_options(const std::filesystem::path &root) {
|
|
Void::Build::Daemon::DaemonOptions options;
|
|
options.project_root = root / "project";
|
|
options.build_root = root / "build";
|
|
options.poll_interval = std::chrono::milliseconds{10};
|
|
std::error_code ec;
|
|
std::filesystem::create_directories(options.project_root, ec);
|
|
std::filesystem::create_directories(options.build_root, ec);
|
|
return options;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
VOID_TEST(Daemon, lifecycle_ping_status_gc_shutdown_and_unknown_op) {
|
|
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
|
|
Void::Testing::require(temp.has_value());
|
|
const auto options = make_options(temp->path());
|
|
|
|
auto server = Void::Build::Daemon::DaemonServer::create(options);
|
|
Void::Testing::require(server.has_value());
|
|
const auto socket = server->socket_path();
|
|
Void::Testing::expect_eq(socket, options.build_root / ".vbs" / "vbs-daemon.sock");
|
|
|
|
Void::Result::Result<void, Void::Result::Error> run_result;
|
|
std::jthread worker{[&] { run_result = server->run(); }};
|
|
|
|
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
|
|
|
|
auto ping = Void::Build::Daemon::request(socket, R"({"op":"ping"})");
|
|
Void::Testing::require(ping.has_value());
|
|
const auto ping_json = parse_json(*ping);
|
|
Void::Testing::require(ping_json.has_value());
|
|
const auto *ping_object = as_object(*ping_json);
|
|
Void::Testing::require(ping_object != nullptr);
|
|
Void::Testing::expect(bool_field(*ping_object, "ok"));
|
|
Void::Testing::expect_eq(str_field(*ping_object, "op"), std::string{"ping"});
|
|
|
|
auto status = Void::Build::Daemon::request(socket, R"({"op":"status"})");
|
|
Void::Testing::require(status.has_value());
|
|
const auto status_json = parse_json(*status);
|
|
Void::Testing::require(status_json.has_value());
|
|
const auto *status_object = as_object(*status_json);
|
|
Void::Testing::require(status_object != nullptr);
|
|
Void::Testing::expect(bool_field(*status_object, "ok"));
|
|
Void::Testing::expect(int_field(*status_object, "pid") > 0);
|
|
Void::Testing::expect(int_field(*status_object, "watched_directories") >= 1);
|
|
Void::Testing::expect(int_field(*status_object, "sequence") >= 0);
|
|
|
|
auto gc = Void::Build::Daemon::request(socket, R"({"op":"gc","max_bytes":0})");
|
|
Void::Testing::require(gc.has_value());
|
|
const auto gc_json = parse_json(*gc);
|
|
Void::Testing::require(gc_json.has_value());
|
|
const auto *gc_object = as_object(*gc_json);
|
|
Void::Testing::require(gc_object != nullptr);
|
|
Void::Testing::expect(bool_field(*gc_object, "ok"));
|
|
Void::Testing::expect(int_field(*gc_object, "removed_blobs") >= 0);
|
|
Void::Testing::expect(int_field(*gc_object, "retained_blobs") >= 0);
|
|
|
|
auto unknown = Void::Build::Daemon::request(socket, R"({"op":"not-an-op"})");
|
|
Void::Testing::require(unknown.has_value());
|
|
const auto unknown_json = parse_json(*unknown);
|
|
Void::Testing::require(unknown_json.has_value());
|
|
const auto *unknown_object = as_object(*unknown_json);
|
|
Void::Testing::require(unknown_object != nullptr);
|
|
Void::Testing::expect(!bool_field(*unknown_object, "ok", true));
|
|
Void::Testing::expect(field(*unknown_object, "error") != nullptr);
|
|
|
|
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
|
|
Void::Testing::require(shutdown.has_value());
|
|
const auto shutdown_json = parse_json(*shutdown);
|
|
Void::Testing::require(shutdown_json.has_value());
|
|
const auto *shutdown_object = as_object(*shutdown_json);
|
|
Void::Testing::require(shutdown_object != nullptr);
|
|
Void::Testing::expect(bool_field(*shutdown_object, "ok"));
|
|
|
|
worker.join();
|
|
Void::Testing::expect(run_result.has_value());
|
|
Void::Testing::expect(!std::filesystem::exists(socket));
|
|
}
|
|
|
|
VOID_TEST(Daemon, second_create_reports_already_running) {
|
|
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
|
|
Void::Testing::require(temp.has_value());
|
|
const auto options = make_options(temp->path());
|
|
|
|
auto first = Void::Build::Daemon::DaemonServer::create(options);
|
|
Void::Testing::require(first.has_value());
|
|
const auto socket = first->socket_path();
|
|
|
|
Void::Result::Result<void, Void::Result::Error> run_result;
|
|
std::jthread worker{[&] { run_result = first->run(); }};
|
|
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
|
|
|
|
auto second = Void::Build::Daemon::DaemonServer::create(options);
|
|
Void::Testing::expect(!second.has_value());
|
|
if (!second) {
|
|
Void::Testing::expect(second.error().message().find("already running") !=
|
|
std::string::npos);
|
|
}
|
|
|
|
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
|
|
Void::Testing::expect(shutdown.has_value());
|
|
worker.join();
|
|
Void::Testing::expect(run_result.has_value());
|
|
Void::Testing::expect(!std::filesystem::exists(socket));
|
|
}
|