MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
test_domainwatcher.cpp File Reference
#include <condition_variable>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <list>
#include <random>
#include <string>
#include <system_error>
#include <thread>
#include <fcntl.h>
#include <unistd.h>
#include <uuid.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <catch2/catch_test_macros.hpp>
#include "mxl-internal/DiscreteFlowWriter.hpp"
#include "mxl-internal/DomainWatcher.hpp"
#include "mxl-internal/Flow.hpp"
#include "mxl-internal/PathUtils.hpp"
#include "mxl/flowinfo.h"
#include "../../tests/Utils.hpp"
Include dependency graph for test_domainwatcher.cpp:

Go to the source code of this file.

Classes

struct  MockFlowFiles
struct  MockWriter

Functions

 TEST_CASE ("DomainWatcher constructor throws on invalid domain path", "[domainwatcher]")
 TEST_CASE_PERSISTENT_FIXTURE (mxl::tests::mxlDomainFixture, "Directory Watcher", "[directory watcher]")
 TEST_CASE_PERSISTENT_FIXTURE (mxl::tests::mxlDomainFixture, "DomainWatcher error handling for invalid inputs", "[domainwatcher]")
 TEST_CASE_PERSISTENT_FIXTURE (mxl::tests::mxlDomainFixture, "DomainWatcher supports concurrent add/remove operations", "[domainwatcher]")
 TEST_CASE_PERSISTENT_FIXTURE (mxl::tests::mxlDomainFixture, "DomainWatcher thread start/stop behavior", "[domainwatcher]")
 TEST_CASE_PERSISTENT_FIXTURE (mxl::tests::mxlDomainFixture, "DomainWatcher triggers callback on file modifications", "[domainwatcher]")

Function Documentation

◆ TEST_CASE()

TEST_CASE ( "DomainWatcher constructor throws on invalid domain path" ,
"" [domainwatcher] )

Definition at line 463 of file test_domainwatcher.cpp.

464{
465 // Ensure constructing on a non‐existent or non‐directory path throws
466 auto const homeDir = std::getenv("HOME");
467 REQUIRE(homeDir != nullptr);
468
469 // Case: path does not exist
470 auto bad1 = std::filesystem::path{homeDir} / "mxl_nonexistent_domain";
471 remove_all(bad1);
472 REQUIRE(!exists(bad1));
473 REQUIRE_THROWS_AS((mxl::lib::DomainWatcher{bad1}), std::filesystem::filesystem_error);
474
475 // Case: path exists but is a regular file
476 auto bad2 = std::filesystem::path{homeDir} / "mxl_not_a_dir";
477 remove_all(bad2);
478 {
479 auto touch = std::ofstream{bad2};
480 touch << "notadir";
481 }
482 REQUIRE(exists(bad2));
483 REQUIRE(is_regular_file(bad2));
484 REQUIRE_THROWS_AS((mxl::lib::DomainWatcher{bad2}), std::filesystem::filesystem_error);
485
486 // Clean up
487 remove_all(bad2);
488}
DomainWatcher(std::filesystem::path const &in_domain)
REQUIRE(timeDelta< 33 '366 '667LL)

◆ TEST_CASE_PERSISTENT_FIXTURE() [1/5]

TEST_CASE_PERSISTENT_FIXTURE ( mxl::tests::mxlDomainFixture ,
"Directory Watcher" ,
"" [directory watcher] )

Definition at line 221 of file test_domainwatcher.cpp.

222{
223 auto watcher = std::make_shared<DomainWatcher>(domain.native());
224
225 auto flowId1 = *uuids::uuid::from_string("5fbec3b1-1b0f-417d-9059-8b94a47197ed");
226 auto flowId2 = *uuids::uuid::from_string("6fbec3b1-1b0f-417d-9059-8b94a47197ed");
227
228 auto mockFiles = MockFlowFiles{domain, flowId1};
229 auto mockFiles2 = MockFlowFiles{domain, flowId2};
230
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);
234 REQUIRE(watcher->size() == 2);
235 auto mockWriter3 = std::make_unique<MockWriter>(domain, flowId2, watcher);
236 mockWriter1.reset();
237 REQUIRE(watcher->count(flowId1) == 1);
238 REQUIRE(watcher->count(flowId2) == 1);
239 REQUIRE(watcher->size() == 2);
240 mockWriter2.reset();
241 REQUIRE(watcher->count(flowId1) == 0);
242 REQUIRE(watcher->count(flowId2) == 1);
243 REQUIRE(watcher->size() == 1);
244 mockWriter3.reset();
245}

◆ TEST_CASE_PERSISTENT_FIXTURE() [2/5]

TEST_CASE_PERSISTENT_FIXTURE ( mxl::tests::mxlDomainFixture ,
"DomainWatcher error handling for invalid inputs" ,
"" [domainwatcher] )

Definition at line 318 of file test_domainwatcher.cpp.

319{
320 // Case 1: Using a valid domain path but adding a flow without creating the files
321 auto watcher = std::make_shared<DomainWatcher>(domain);
322
323 // Attempt to addFlow on a domain where the flow files don't exist (should throw because files are not present)
324 uuids::uuid dummyId = *uuids::uuid::from_string("01234567-89ab-cdef-0123-456789abcdef");
325 auto test = [&]()
326 {
327 auto mockWriter = MockWriter{domain, dummyId, watcher};
328 };
329
330 // The implementation throws std::system_error when inotify_add_watch fails
331 REQUIRE_THROWS_AS(test(), std::system_error);
332
333 // Removing a non-existant writer should not throw
334 REQUIRE_NOTHROW(watcher->removeFlow(nullptr, dummyId));
335 REQUIRE_NOTHROW(watcher->removeFlow(std::bit_cast<DiscreteFlowWriter*>(std::bit_cast<std::uintptr_t>(0x7f11251231231)), dummyId));
336}

◆ TEST_CASE_PERSISTENT_FIXTURE() [3/5]

TEST_CASE_PERSISTENT_FIXTURE ( mxl::tests::mxlDomainFixture ,
"DomainWatcher supports concurrent add/remove operations" ,
"" [domainwatcher] )

Definition at line 380 of file test_domainwatcher.cpp.

381{
382 // This test spawns multiple threads to add and remove flow watchers concurrently,
383 // to ensure thread safety (no crashes or race conditions and correct final state).
384 auto flowId = *uuids::uuid::from_string("12345678-1234-5678-1234-567812345678");
385 auto mockFiles = MockFlowFiles{domain, flowId};
386
387 std::random_device rd{};
388 std::mt19937 rng{rd()};
389
390 std::atomic<std::size_t> readyCount{0};
391 std::atomic<bool> startSignal{false};
392
393 constexpr auto iterations = 1024;
394 constexpr auto nWorkers = 8;
395 constexpr auto slots = 4;
396
397 struct Worker
398 {
399 std::vector<std::optional<MockWriter>> writers;
400 std::optional<std::thread> thread;
401 };
402
403 auto watcher = std::make_shared<DomainWatcher>(domain);
404 auto work = [&](Worker* w)
405 {
406 readyCount.fetch_add(1);
407 startSignal.wait(false);
408
409 auto dist = std::uniform_int_distribution{std::size_t{0}, w->writers.size() - 1};
410
411 for (std::size_t n = 0; n < iterations; ++n)
412 {
413 auto index = dist(rng);
414 auto& slot = w->writers.at(index);
415 if (slot)
416 {
417 slot.reset();
418 }
419 else
420 {
421 slot.emplace(domain, flowId, watcher);
422 }
423
424 if (index > (w->writers.size() / 2))
425 {
426 mockFiles.touch();
427 }
428 }
429 };
430
431 auto workers = std::list<Worker>{};
432 for (auto i = 0; i < nWorkers; ++i)
433 {
434 auto& w = workers.emplace_back(std::vector<std::optional<MockWriter>>{slots}, std::nullopt);
435 w.thread = std::thread{work, &w};
436 }
437
438 while (readyCount.load() != workers.size())
439 {
440 }
441
442 startSignal.store(true);
443 startSignal.notify_all();
444 // work work work...
445 std::ranges::for_each(workers,
446 [](Worker& w)
447 {
448 w.thread->join();
449 w.writers.clear();
450 });
451
452 REQUIRE(watcher->size() == 0);
453 REQUIRE(watcher->count(flowId) == 0);
454 {
455 auto mockWriter = MockWriter{domain, flowId, watcher};
456 REQUIRE(watcher->size() == 1);
457 REQUIRE(watcher->count(flowId) == 1);
458 }
459 REQUIRE(watcher->size() == 0);
460 REQUIRE(watcher->count(flowId) == 0);
461}
constexpr auto w
auto const flowId
uint64_t index

◆ TEST_CASE_PERSISTENT_FIXTURE() [4/5]

TEST_CASE_PERSISTENT_FIXTURE ( mxl::tests::mxlDomainFixture ,
"DomainWatcher thread start/stop behavior" ,
"" [domainwatcher] )

Definition at line 283 of file test_domainwatcher.cpp.

284{
285 // This test checks that modifying a watched file triggers the callback with correct UUID and WatcherType.
286 auto watcher = std::make_shared<DomainWatcher>(domain);
287
288 // Create a flow directory and files, then add watchers for both Reader and Writer types
289 auto const flowId = *uuids::uuid::from_string("11111111-2222-3333-4444-555555555555"); // test UUID
290 auto mockFlow = MockFlowFiles{domain, flowId};
291
292 {
293 auto mockWriter1 = MockWriter{domain, flowId, watcher};
294 auto mockWriter2 = MockWriter{domain, flowId, watcher};
295 REQUIRE(watcher->size() == 2);
296 REQUIRE(watcher->count(flowId) == 2);
297
298 std::this_thread::sleep_for(std::chrono::milliseconds(250));
299
300 mockFlow.touch();
301
302 std::this_thread::sleep_for(std::chrono::milliseconds(250));
303
304 REQUIRE(mockWriter1.checkReadTimeUpdated());
305 REQUIRE(mockWriter2.checkReadTimeUpdated());
306
307 watcher->stop();
308 std::this_thread::sleep_for(std::chrono::milliseconds(250));
309
310 mockFlow.touch();
311 std::this_thread::sleep_for(std::chrono::milliseconds(250));
312
313 REQUIRE(!mockWriter1.checkReadTimeUpdated());
314 REQUIRE(!mockWriter2.checkReadTimeUpdated());
315 }
316}

◆ TEST_CASE_PERSISTENT_FIXTURE() [5/5]

TEST_CASE_PERSISTENT_FIXTURE ( mxl::tests::mxlDomainFixture ,
"DomainWatcher triggers callback on file modifications" ,
"" [domainwatcher] )

Definition at line 247 of file test_domainwatcher.cpp.

248{
249 // This test checks that modifying a watched file triggers the callback with correct UUID and WatcherType.
250 auto watcher = std::make_shared<DomainWatcher>(domain);
251
252 // Create a flow directory and files, then add watchers for both Reader and Writer types
253 auto const flowId = *uuids::uuid::from_string("11111111-2222-3333-4444-555555555555"); // test UUID
254 auto mockFlow = MockFlowFiles{domain, flowId};
255
256 {
257 auto writer1 = MockWriter{domain, flowId, watcher};
258 auto writer2 = MockWriter{domain, flowId, watcher};
259
260 // Give the watcher thread a moment to register the inotify watch
261 std::this_thread::sleep_for(std::chrono::milliseconds(100));
262
263 // Modify the main data file and expect a Writer event
264 mockFlow.touch();
265 std::this_thread::sleep_for(std::chrono::milliseconds(100));
266
267 REQUIRE(writer1.checkReadTimeUpdated());
268 REQUIRE(writer2.checkReadTimeUpdated());
269 REQUIRE(!writer1.checkReadTimeUpdated());
270 REQUIRE(!writer2.checkReadTimeUpdated());
271
272 // Modify the main data file and expect a Writer event
273 mockFlow.touch();
274 std::this_thread::sleep_for(std::chrono::milliseconds(100));
275
276 REQUIRE(writer1.checkReadTimeUpdated());
277 REQUIRE(writer2.checkReadTimeUpdated());
278 }
279
280 REQUIRE(watcher->size() == 0);
281}