feat(test): support CTest-style fixtures in the test runner
VoidBuildSystem CI / sanitizers-gcc16 (push) Failing after 6s
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 13s
VoidBuildSystem CI / fuzz-clang (push) Failing after 11s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 14s
VoidBuildSystem CI / wine-windows-gcc16 (push) Failing after 56s
VoidBuildSystem CI / sanitizers-gcc16 (push) Failing after 6s
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 13s
VoidBuildSystem CI / fuzz-clang (push) Failing after 11s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 14s
VoidBuildSystem CI / wine-windows-gcc16 (push) Failing after 56s
This commit is contained in:
1 parent
617d783e84
commit
6befa6b5e2
10 files changed
+268
-22
No files matched your search
+106
-18
@@ -59,6 +59,7 @@
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
@@ -848,44 +849,119 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
|
||||
std::string name;
|
||||
std::filesystem::path executable;
|
||||
std::vector<std::string> resources;
|
||||
std::vector<std::size_t> prerequisites;
|
||||
};
|
||||
std::vector<TestJob> test_jobs;
|
||||
for (const auto &t : graph->targets) {
|
||||
std::map<std::size_t, std::size_t> job_by_target; // graph target index -> job index
|
||||
|
||||
// Candidate test targets passing the target filter (labels never exclude fixture
|
||||
// helpers - they are pulled in by the tests that require them).
|
||||
std::vector<std::size_t> candidates;
|
||||
for (std::size_t i = 0; i < graph->targets.size(); ++i) {
|
||||
const auto &t = graph->targets[i];
|
||||
if (t.kind != TargetKind::Test || t.primary_outputs.empty()) {
|
||||
continue;
|
||||
}
|
||||
if (!opts.targets.empty() && !std::ranges::contains(opts.targets, t.name)) {
|
||||
continue;
|
||||
}
|
||||
candidates.push_back(i);
|
||||
}
|
||||
std::map<std::string, std::vector<std::size_t>, std::less<>> setups_by_fixture;
|
||||
std::map<std::string, std::vector<std::size_t>, std::less<>> cleanups_by_fixture;
|
||||
for (const auto i : candidates) {
|
||||
for (const auto &fixture : graph->targets[i].fixture_setup) {
|
||||
setups_by_fixture[fixture].push_back(i);
|
||||
}
|
||||
for (const auto &fixture : graph->targets[i].fixture_cleanup) {
|
||||
cleanups_by_fixture[fixture].push_back(i);
|
||||
}
|
||||
}
|
||||
std::map<std::string, std::vector<std::size_t>, std::less<>> fixture_users;
|
||||
std::set<std::size_t> adding; // recursion guard for fixture cycles
|
||||
const auto add_prerequisite = [](TestJob &job, std::size_t prerequisite) {
|
||||
if (!std::ranges::contains(job.prerequisites, prerequisite)) {
|
||||
job.prerequisites.push_back(prerequisite);
|
||||
}
|
||||
};
|
||||
std::function<std::size_t(std::size_t)> add_job =
|
||||
[&](std::size_t target_index) -> std::size_t {
|
||||
if (const auto it = job_by_target.find(target_index); it != job_by_target.end()) {
|
||||
return it->second;
|
||||
}
|
||||
const auto &t = graph->targets[target_index];
|
||||
const std::size_t job_index = test_jobs.size();
|
||||
test_jobs.push_back(TestJob{t.name, t.primary_outputs.front(), t.resources, {}});
|
||||
job_by_target[target_index] = job_index;
|
||||
if (!adding.insert(target_index).second) {
|
||||
return job_index; // Fixture cycle: stop recursing.
|
||||
}
|
||||
for (const auto &fixture : t.fixture_required) {
|
||||
const auto setup_it = setups_by_fixture.find(fixture);
|
||||
if (setup_it != setups_by_fixture.end() && !setup_it->second.empty()) {
|
||||
add_prerequisite(test_jobs[job_index], add_job(setup_it->second.front()));
|
||||
}
|
||||
fixture_users[fixture].push_back(job_index);
|
||||
}
|
||||
adding.erase(target_index);
|
||||
return job_index;
|
||||
};
|
||||
for (const auto i : candidates) {
|
||||
const auto &t = graph->targets[i];
|
||||
if (!opts.labels.empty() &&
|
||||
!std::ranges::any_of(opts.labels, [&](const std::string &label) {
|
||||
return std::ranges::contains(t.labels, label);
|
||||
})) {
|
||||
continue;
|
||||
}
|
||||
test_jobs.push_back(TestJob{t.name, t.primary_outputs.front(), t.resources});
|
||||
add_job(i);
|
||||
}
|
||||
// A fixture's cleanup runs after every test that used it (and after its setup).
|
||||
for (const auto &[fixture, users] : fixture_users) {
|
||||
const auto cleanup_it = cleanups_by_fixture.find(fixture);
|
||||
if (cleanup_it == cleanups_by_fixture.end()) {
|
||||
continue;
|
||||
}
|
||||
for (const auto cleanup_target : cleanup_it->second) {
|
||||
const auto cleanup_job = add_job(cleanup_target);
|
||||
for (const auto user : users) {
|
||||
add_prerequisite(test_jobs[cleanup_job], user);
|
||||
}
|
||||
const auto setup_it = setups_by_fixture.find(fixture);
|
||||
if (setup_it != setups_by_fixture.end() && !setup_it->second.empty()) {
|
||||
add_prerequisite(test_jobs[cleanup_job],
|
||||
job_by_target.at(setup_it->second.front()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Tests run in parallel up to --jobs, except that tests sharing a resource are
|
||||
// serialized (a resource is held exclusively while its test runs).
|
||||
// serialized (a resource is held exclusively while its test runs) and fixture
|
||||
// prerequisites run first.
|
||||
std::vector<std::optional<TestRunRecord>> records(test_jobs.size());
|
||||
std::mutex test_mutex;
|
||||
std::condition_variable test_cv;
|
||||
std::set<std::string> held_resources;
|
||||
std::vector<bool> claimed(test_jobs.size(), false);
|
||||
std::vector<bool> completed(test_jobs.size(), false);
|
||||
std::vector<bool> failed(test_jobs.size(), false);
|
||||
std::size_t remaining = test_jobs.size();
|
||||
const std::size_t worker_count = std::max<std::size_t>(
|
||||
1, std::min<std::size_t>(
|
||||
jobs == 0 ? std::max(1u, std::thread::hardware_concurrency()) : jobs,
|
||||
std::max<std::size_t>(1, test_jobs.size())));
|
||||
const auto prerequisites_done = [&](std::size_t i) {
|
||||
return std::ranges::all_of(test_jobs[i].prerequisites,
|
||||
[&](std::size_t p) { return completed[p]; });
|
||||
};
|
||||
const auto resources_free = [&](std::size_t i) {
|
||||
return std::ranges::none_of(test_jobs[i].resources, [&](const std::string &r) {
|
||||
return held_resources.contains(r);
|
||||
});
|
||||
};
|
||||
const auto any_runnable = [&] {
|
||||
for (std::size_t i = 0; i < test_jobs.size(); ++i) {
|
||||
if (claimed[i]) {
|
||||
continue;
|
||||
}
|
||||
if (std::ranges::none_of(test_jobs[i].resources, [&](const std::string &r) {
|
||||
return held_resources.contains(r);
|
||||
})) {
|
||||
if (!claimed[i] && prerequisites_done(i) && resources_free(i)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -901,19 +977,29 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
|
||||
return;
|
||||
}
|
||||
for (; index < test_jobs.size(); ++index) {
|
||||
if (claimed[index]) {
|
||||
continue;
|
||||
}
|
||||
if (std::ranges::none_of(test_jobs[index].resources,
|
||||
[&](const std::string &r) {
|
||||
return held_resources.contains(r);
|
||||
})) {
|
||||
if (!claimed[index] && prerequisites_done(index) &&
|
||||
resources_free(index)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
claimed[index] = true;
|
||||
for (const auto &r : test_jobs[index].resources) {
|
||||
held_resources.insert(r);
|
||||
// A failed fixture setup skips its dependents.
|
||||
const bool blocked =
|
||||
std::ranges::any_of(test_jobs[index].prerequisites,
|
||||
[&](std::size_t p) { return failed[p]; });
|
||||
if (blocked) {
|
||||
Void::Log::warn("Test", "[{}] SKIPPED '{}' (fixture setup failed)",
|
||||
prof_name, test_jobs[index].name);
|
||||
completed[index] = true;
|
||||
--remaining;
|
||||
} else {
|
||||
for (const auto &r : test_jobs[index].resources) {
|
||||
held_resources.insert(r);
|
||||
}
|
||||
}
|
||||
if (blocked) {
|
||||
test_cv.notify_all();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
const auto &job = test_jobs[index];
|
||||
@@ -955,6 +1041,8 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
|
||||
held_resources.erase(r);
|
||||
}
|
||||
records[index] = std::move(record);
|
||||
completed[index] = true;
|
||||
failed[index] = !passed;
|
||||
--remaining;
|
||||
}
|
||||
test_cv.notify_all();
|
||||
|
||||
Reference in new issue
Block a user