#include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { namespace fs = std::filesystem; using namespace vls::modules; namespace Sched = vls::scheduler; using Void::Testing::expect; using Void::Testing::require; ArtifactKey key(std::string context, std::string inputs = "inputs") { return {"gcc-16.2.0/x86_64", std::move(context), std::move(inputs), "cmi"}; } fs::path write(const fs::path &root, std::string_view name, std::string_view text) { const auto path = root / name; fs::create_directories(path.parent_path()); std::ofstream stream{path, std::ios::binary}; stream << text; stream.close(); require(stream.good()); return path; } std::string read(const fs::path &path) { std::ifstream stream{path, std::ios::binary}; require(stream.good()); return {std::istreambuf_iterator{stream}, {}}; } std::size_t files(const fs::path &root) { std::size_t count{}; for (const auto &entry : fs::directory_iterator{root}) if (entry.is_regular_file()) ++count; return count; } ModulePlan ready_plan(std::vector &keys) { ModulePlan plan; plan.domain.compatibility = "gcc-16.2.0/x86_64"; plan.claims_complete = true; for (std::size_t index = 0; index != 3; ++index) { const auto name = "unit" + std::to_string(index); Build::ContextKey context{ {}, {}, Build::ContextRole::target, name, fs::path{"/project"} / (name + ".cpp")}; CensusMember member{context, {"/project", name + ".o"}, {"command-" + name, "source-" + name, "closure-" + name, {}}, "scan-" + name, {}}; plan.units.push_back({member, UnitKind::primary_interface, {}}); plan.order.push_back(context); keys.push_back(context); } return plan; } } // namespace VOID_TEST(ArtifactCache, publishes_and_finds) { Void::FileSystem::TempDir temp{"vls_artifact_cache_"}; ArtifactCache cache{temp.path() / "cache"}; const auto first = write(temp.path() / "src", "first.gcm", "first artifact"); const auto second = write(temp.path() / "src", "second.gcm", "second artifact"); auto published = cache.publish({{key("one"), first}, {key("two"), second}}); require(published.has_value()); const auto generation = *published; expect(generation->id().size() == 64); expect(cache.current() == generation); const auto one = generation->find(key("one")); require(one.has_value()); expect(read(one->path) == "first artifact" && one->sha256.size() == 64); require(generation->find(key("two")).has_value()); expect(!generation->find(key("missing")).has_value()); expect(cache.generations() == 1); } VOID_TEST(ArtifactCache, failed_publication_leaves_nothing) { Void::FileSystem::TempDir temp{"vls_artifact_cache_"}; ArtifactCache cache{temp.path() / "cache"}; const auto valid = write(temp.path(), "valid.gcm", "valid"); auto published = cache.publish({{key("valid"), valid}, {key("missing"), temp.path() / "missing.gcm"}}); require(!published.has_value()); expect(!cache.current()); expect(cache.generations() == 0); expect(fs::is_empty(temp.path() / "cache" / "tmp")); } VOID_TEST(ArtifactCache, identical_contents_share_one_file) { Void::FileSystem::TempDir temp{"vls_artifact_cache_"}; ArtifactCache cache{temp.path() / "cache"}; const auto first = write(temp.path(), "first.gcm", "same bytes"); const auto second = write(temp.path(), "second.gcm", "same bytes"); auto published = cache.publish({{key("one"), first}, {key("two"), second}}); require(published.has_value()); expect(files((*published)->root()) == 2); // manifest plus one content file } VOID_TEST(ArtifactCache, readers_retain_generations_across_collect) { Void::FileSystem::TempDir temp{"vls_artifact_cache_"}; ArtifactCache cache{temp.path() / "cache", 1}; const auto source = write(temp.path(), "artifact.gcm", "retained"); auto first = cache.publish({{key("one"), source}}); require(first.has_value()); const auto first_root = (*first)->root(); require(cache.publish({{key("two"), source}}).has_value()); require(cache.publish({{key("three"), source}}).has_value()); cache.collect(); expect(fs::is_directory(first_root)); first = {}; cache.collect(); expect(!fs::exists(first_root)); expect(cache.generations() == 2); } VOID_TEST(ArtifactCache, eviction_respects_the_retained_limit) { Void::FileSystem::TempDir temp{"vls_artifact_cache_"}; ArtifactCache cache{temp.path() / "cache", 1}; for (int index = 0; index != 5; ++index) { const auto source = write(temp.path(), "artifact.gcm", "content " + std::to_string(index)); require(cache.publish({{key("unit" + std::to_string(index)), source}}).has_value()); } expect(cache.generations() == 5); cache.collect(); expect(cache.generations() == 2); } VOID_TEST(ArtifactCache, producer_publishes_in_dependency_order) { Void::FileSystem::TempDir temp{"vls_artifact_cache_"}; ArtifactCache cache{temp.path() / "cache"}; std::vector keys; const auto plan = ready_plan(keys); std::vector order; auto produced = produce_module_artifacts( plan, cache, temp.path() / "staging", [&](std::stop_token, const Build::ContextKey &context, const fs::path &output) -> std::expected { order.push_back(context.alternative); fs::create_directories(output.parent_path()); std::ofstream stream{output, std::ios::binary}; stream << "artifact for " << context.alternative; stream.close(); if (!stream) return std::unexpected(std::string{"write failed"}); return output; }); require(produced.has_value()); expect(order.size() == 3); for (std::size_t index = 0; index != 3; ++index) expect(order[index] == keys[index].alternative); expect((*produced) ->find(key(keys[0].alternative, "command-unit0|source-unit0|closure-unit0|")) .has_value()); expect(!fs::exists(temp.path() / "staging")); } VOID_TEST(ArtifactCache, producer_failure_publishes_nothing) { Void::FileSystem::TempDir temp{"vls_artifact_cache_"}; ArtifactCache cache{temp.path() / "cache"}; std::vector keys; const auto plan = ready_plan(keys); std::size_t calls{}; auto produced = produce_module_artifacts( plan, cache, temp.path() / "staging", [&](std::stop_token, const Build::ContextKey &, const fs::path &output) -> std::expected { if (++calls == 2) return std::unexpected("compile failed"); fs::create_directories(output.parent_path()); std::ofstream{output} << "artifact"; return output; }); require(!produced.has_value()); expect(produced.error() == "compile failed"); expect(!cache.current()); expect(cache.generations() == 0); expect(!fs::exists(temp.path() / "staging")); } VOID_TEST(ArtifactCache, producer_cancellation_publishes_nothing) { Void::FileSystem::TempDir temp{"vls_artifact_cache_"}; ArtifactCache cache{temp.path() / "cache"}; std::vector keys; const auto plan = ready_plan(keys); std::stop_source cancellation; cancellation.request_stop(); auto produced = produce_module_artifacts( plan, cache, temp.path() / "staging", [](std::stop_token, const Build::ContextKey &, const fs::path &output) { return std::expected{output}; }, cancellation.get_token()); require(!produced.has_value()); expect(cache.generations() == 0); } VOID_TEST(ArtifactCache, refuses_an_unready_plan) { Void::FileSystem::TempDir temp{"vls_artifact_cache_"}; ArtifactCache cache{temp.path() / "cache"}; ModulePlan plan; plan.domain.compatibility = "gcc-16.2.0/x86_64"; auto produced = produce_module_artifacts( plan, cache, temp.path() / "staging", [](std::stop_token, const Build::ContextKey &, const fs::path &output) { return std::expected{output}; }); require(!produced.has_value()); expect(produced.error() == "module plan is not ready"); } VOID_TEST(ArtifactCache, scheduled_production_publishes_in_order) { Void::FileSystem::TempDir temp{"vls_artifact_cache_"}; ArtifactCache cache{temp.path() / "cache"}; Sched::Scheduler pool{Sched::Budget{.foreground = 1, .background = 1, .artifact = 1, .queued = 8, .memory_mib = 64, .watchdog = std::chrono::seconds{5}}}; std::vector keys; const auto plan = ready_plan(keys); std::vector order; auto produced = produce_module_artifacts_scheduled( plan, cache, temp.path() / "staging", [&](std::stop_token, const Build::ContextKey &context, const fs::path &output) -> std::expected { order.push_back(context.alternative); fs::create_directories(output.parent_path()); std::ofstream{output} << "artifact " << context.alternative; return output; }, pool); require(produced.has_value()); expect(order.size() == 3); for (std::size_t index = 0; index != 3; ++index) expect(order[index] == keys[index].alternative); expect((*produced) ->find(key(keys[0].alternative, "command-unit0|source-unit0|closure-unit0|")) .has_value()); } VOID_TEST(ArtifactCache, superseded_scheduled_production_publishes_nothing) { Void::FileSystem::TempDir temp{"vls_artifact_cache_"}; ArtifactCache cache{temp.path() / "cache"}; Sched::Scheduler pool{Sched::Budget{.foreground = 1, .background = 1, .artifact = 1, .queued = 8, .memory_mib = 64, .watchdog = std::chrono::seconds{5}}}; std::vector keys; const auto plan = ready_plan(keys); std::latch started{1}; std::mutex mutex; std::condition_variable changed; bool release{}; std::expected, std::string> result = std::unexpected("not run"); std::thread producer{[&] { result = produce_module_artifacts_scheduled( plan, cache, temp.path() / "staging", [&](std::stop_token stop, const Build::ContextKey &, const fs::path &output) -> std::expected { started.count_down(); std::unique_lock lock{mutex}; changed.wait(lock, [&] { return release; }); if (stop.stop_requested()) return std::unexpected("cancelled"); fs::create_directories(output.parent_path()); std::ofstream{output} << "artifact"; return output; }, pool); }}; started.wait(); require(pool.submit( {.priority = Sched::Priority::artifact, .key = keys[0].alternative, .revision = 2, .memory_mib = 0}, [](std::stop_token) { return std::string{}; }, [](Sched::Result) {})); { std::lock_guard lock{mutex}; release = true; changed.notify_all(); } producer.join(); require(!result.has_value()); expect(result.error() == "artifact production cancelled"); expect(!cache.current()); expect(cache.generations() == 0); }