4#include <condition_variable>
13#include <system_error>
20#include <catch2/catch_test_macros.hpp>
29namespace fs = std::filesystem;
42 std::filesystem::create_directories(flowDir);
46 _accessFileFd = ::open(accessFilePath.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0644);
49 auto const error = errno;
50 throw std::system_error{error, std::system_category(),
"Failed to open access file"};
53 _dataFileFd = ::open(dataFilePath.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0644);
57 auto const error = errno;
58 throw std::system_error{error, std::system_category(),
"Failed truncate access file"};
63 auto const error = errno;
64 throw std::system_error{error, std::system_category(),
"Failed truncate access file"};
86 auto times = std::array<::timespec, 2>{
87 timespec{.tv_sec = 0, .tv_nsec = UTIME_NOW },
88 timespec{.tv_sec = 0, .tv_nsec = UTIME_OMIT}
90 if (
auto ret = ::futimens(
_accessFileFd, times.data()); ret < 0)
92 auto const error = errno;
93 throw std::system_error(error, std::system_category(),
"Failed to update flow access time");
105 MockWriter(std::filesystem::path
const& domain, uuids::uuid
id, DomainWatcher::ptr
const& watcher)
115 auto const error = errno;
116 throw std::system_error{error, std::system_category(),
"Failed to open grain data file"};
122 auto const error = errno;
123 throw std::system_error{error, std::system_category(),
"Failed to map grain data file"};
167 return _info.runtime;
223 auto watcher = std::make_shared<DomainWatcher>(domain.native());
225 auto flowId1 = *uuids::uuid::from_string(
"5fbec3b1-1b0f-417d-9059-8b94a47197ed");
226 auto flowId2 = *uuids::uuid::from_string(
"6fbec3b1-1b0f-417d-9059-8b94a47197ed");
231 auto mockWriter1 = std::make_unique<MockWriter>(domain, flowId1, watcher);
232 auto mockWriter2 = std::make_unique<MockWriter>(domain, flowId1, watcher);
233 REQUIRE(watcher->count(flowId1) == 2);
235 auto mockWriter3 = std::make_unique<MockWriter>(domain, flowId2, watcher);
237 REQUIRE(watcher->count(flowId1) == 1);
238 REQUIRE(watcher->count(flowId2) == 1);
241 REQUIRE(watcher->count(flowId1) == 0);
242 REQUIRE(watcher->count(flowId2) == 1);
250 auto watcher = std::make_shared<DomainWatcher>(domain);
253 auto const flowId = *uuids::uuid::from_string(
"11111111-2222-3333-4444-555555555555");
261 std::this_thread::sleep_for(std::chrono::milliseconds(100));
265 std::this_thread::sleep_for(std::chrono::milliseconds(100));
267 REQUIRE(writer1.checkReadTimeUpdated());
268 REQUIRE(writer2.checkReadTimeUpdated());
269 REQUIRE(!writer1.checkReadTimeUpdated());
270 REQUIRE(!writer2.checkReadTimeUpdated());
274 std::this_thread::sleep_for(std::chrono::milliseconds(100));
276 REQUIRE(writer1.checkReadTimeUpdated());
277 REQUIRE(writer2.checkReadTimeUpdated());
286 auto watcher = std::make_shared<DomainWatcher>(domain);
289 auto const flowId = *uuids::uuid::from_string(
"11111111-2222-3333-4444-555555555555");
298 std::this_thread::sleep_for(std::chrono::milliseconds(250));
302 std::this_thread::sleep_for(std::chrono::milliseconds(250));
304 REQUIRE(mockWriter1.checkReadTimeUpdated());
305 REQUIRE(mockWriter2.checkReadTimeUpdated());
308 std::this_thread::sleep_for(std::chrono::milliseconds(250));
311 std::this_thread::sleep_for(std::chrono::milliseconds(250));
313 REQUIRE(!mockWriter1.checkReadTimeUpdated());
314 REQUIRE(!mockWriter2.checkReadTimeUpdated());
321 auto watcher = std::make_shared<DomainWatcher>(domain);
324 uuids::uuid dummyId = *uuids::uuid::from_string(
"01234567-89ab-cdef-0123-456789abcdef");
327 auto mockWriter =
MockWriter{domain, dummyId, watcher};
331 REQUIRE_THROWS_AS(test(), std::system_error);
334 REQUIRE_NOTHROW(watcher->removeFlow(
nullptr, dummyId));
335 REQUIRE_NOTHROW(watcher->removeFlow(std::bit_cast<DiscreteFlowWriter*>(std::bit_cast<std::uintptr_t>(0x7f11251231231)), dummyId));
344 auto countOpenFDs = []()
346 auto count =
size_t{0};
347 for (
auto& entry : std::filesystem::directory_iterator(
"/proc/self/fd"))
350 auto const name = entry.path().filename().string();
351 if (name ==
"." || name ==
"..")
360 auto const fdsBefore = countOpenFDs();
362 auto watcher = std::make_shared<DomainWatcher>(domain);
364 auto const flowId = *uuids::uuid::from_string(
"12345678-1234-5678-1234-567812345678");
368 auto const fdsWhileActive = countOpenFDs();
369 REQUIRE(fdsWhileActive > fdsBefore);
372 auto const fdsAfter = countOpenFDs();
376 REQUIRE(fdsAfter == fdsBefore);
384 auto flowId = *uuids::uuid::from_string(
"12345678-1234-5678-1234-567812345678");
387 std::random_device rd{};
388 std::mt19937 rng{rd()};
390 std::atomic<std::size_t> readyCount{0};
391 std::atomic<bool> startSignal{
false};
393 constexpr auto iterations = 1024;
394 constexpr auto nWorkers = 8;
395 constexpr auto slots = 4;
399 std::vector<std::optional<MockWriter>> writers;
400 std::optional<std::thread> thread;
403 auto watcher = std::make_shared<DomainWatcher>(domain);
404 auto work = [&](Worker*
w)
406 readyCount.fetch_add(1);
407 startSignal.wait(
false);
409 auto dist = std::uniform_int_distribution{std::size_t{0},
w->writers.size() - 1};
411 for (std::size_t n = 0; n < iterations; ++n)
413 auto index = dist(rng);
414 auto& slot =
w->writers.at(
index);
421 slot.emplace(domain,
flowId, watcher);
424 if (
index > (
w->writers.size() / 2))
431 auto workers = std::list<Worker>{};
432 for (
auto i = 0; i < nWorkers; ++i)
434 auto&
w = workers.emplace_back(std::vector<std::optional<MockWriter>>{slots}, std::nullopt);
435 w.thread = std::thread{work, &
w};
438 while (readyCount.load() != workers.size())
442 startSignal.store(
true);
443 startSignal.notify_all();
445 std::ranges::for_each(workers,
463TEST_CASE(
"DomainWatcher constructor throws on invalid domain path",
"[domainwatcher]")
466 auto const homeDir = std::getenv(
"HOME");
470 auto bad1 = std::filesystem::path{homeDir} /
"mxl_nonexistent_domain";
476 auto bad2 = std::filesystem::path{homeDir} /
"mxl_not_a_dir";
479 auto touch = std::ofstream{bad2};
483 REQUIRE(is_regular_file(bad2));
Internal shared-memory headers for discrete grains and event slots.
Paths and filenames utilities used by the MXL SDK.
struct mxlGrainInfo_t mxlGrainInfo
Public configuration and runtime metadata for MXL flows.
struct mxlFlowInfo_t mxlFlowInfo
mxlStatus
MXL SDK Status codes.
DomainWatcher(std::filesystem::path const &in_domain)
std::filesystem::path makeFlowAccessFilePath(std::filesystem::path const &flowDirectory)
std::filesystem::path makeFlowDataFilePath(std::filesystem::path const &flowDirectory)
std::size_t count(uuids::uuid id) const noexcept
Returns the number of writers registered for flow id 'id'.
std::filesystem::path makeFlowDirectoryName(std::filesystem::path const &domain, std::string const &uuid)
MockFlowFiles(fs::path domain, uuids::uuid const &id)
virtual bool isExclusive() const override
virtual ~MockWriter() override
virtual FlowData const & getFlowData() const override
virtual::mxlFlowInfo getFlowInfo() const override
std::uint64_t _lastReadTime
virtual::mxlFlowRuntimeInfo getFlowRuntimeInfo() const override
virtual mxlStatus openGrain(std::uint64_t, mxlGrainInfo *, std::uint8_t **) override
bool checkReadTimeUpdated()
virtual mxlStatus commit(mxlGrainInfo const &) override
virtual mxlGrainInfo getGrainInfo(std::uint64_t) const override
virtual::mxlFlowConfigInfo getFlowConfigInfo() const override
MockWriter(std::filesystem::path const &domain, uuids::uuid id, DomainWatcher::ptr const &watcher)
virtual bool makeExclusive() override
virtual mxlStatus cancel() override
DomainWatcher::ptr _watcher
virtual FlowData & getFlowData() override
TEST_CASE("DomainWatcher constructor throws on invalid domain path", "[domainwatcher]")
TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Directory Watcher", "[directory watcher]")
REQUIRE(timeDelta< 33 '366 '667LL)