MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
test_options.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
3
4#include <cstdlib>
5#include <filesystem>
6#include <fstream>
7#include <string>
8#include <catch2/catch_test_macros.hpp>
13#include "../../tests/Utils.hpp"
14
15using namespace mxl::lib;
16
17namespace
18{
19 auto const options_500 = R"({"urn:x-mxl:option:history_duration/v1.0": 500000000})";
20 auto const options_250 = R"({"urn:x-mxl:option:history_duration/v1.0": 250000000})";
21}
22
23TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Options : Default value", "[options]")
24{
25 auto domainWatcher = std::make_shared<DomainWatcher>(domain);
26 auto flowIoFactory = std::make_unique<mxl::lib::PosixFlowIoFactory>(domainWatcher);
27 auto const instance = std::make_shared<Instance>(domain, "", std::move(flowIoFactory), domainWatcher);
28
29 REQUIRE(instance->getHistoryDurationNs() == 200'000'000ULL); // default value
30}
31
32TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Options : Domain config", "[options]")
33{
34 // write the domain options file
35 auto const domainOptionsFile = makeDomainOptionsFilePath(domain);
36 std::ofstream ofs(domainOptionsFile);
37 REQUIRE(ofs.is_open());
38 ofs << options_500;
39 ofs.close();
40
41 auto domainWatcher = std::make_shared<DomainWatcher>(domain);
42 auto flowIoFactory = std::make_unique<mxl::lib::PosixFlowIoFactory>(domainWatcher);
43 auto const instance = std::make_shared<Instance>(domain, "", std::move(flowIoFactory), domainWatcher);
44
45 REQUIRE(instance->getHistoryDurationNs() == 500'000'000ULL); // domain value
46}
47
48TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Options : Instance config", "[options]")
49{
50 auto domainWatcher = std::make_shared<DomainWatcher>(domain);
51 auto flowIoFactory = std::make_unique<mxl::lib::PosixFlowIoFactory>(domainWatcher);
52 auto const instance = std::make_shared<Instance>(domain, options_250, std::move(flowIoFactory), domainWatcher);
53
54 // We should get the default value, not the instance config value.
55 // We don't want per-instance history durations.
56 REQUIRE(instance->getHistoryDurationNs() == 200'000'000ULL);
57}
58
59TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Options : Domain + Instance config", "[options]")
60{
61 // write the domain options file
62 auto const domainOptionsFile = makeDomainOptionsFilePath(domain);
63 std::ofstream ofs(domainOptionsFile);
64 REQUIRE(ofs.is_open());
65 ofs << options_500;
66 ofs.close();
67
68 auto domainWatcher = std::make_shared<DomainWatcher>(domain);
69 auto flowIoFactory = std::make_unique<mxl::lib::PosixFlowIoFactory>(domainWatcher);
70 auto const instance = std::make_shared<Instance>(domain, options_250, std::move(flowIoFactory), domainWatcher);
71
72 // We should use the domain config, not the instance config. We don't want per-instance history durations.
73 REQUIRE(instance->getHistoryDurationNs() == 500'000'000ULL);
74}
75
76TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Options : invalid configs", "[options]")
77{
78 // write the domain options file
79 auto const domainOptionsFile = makeDomainOptionsFilePath(domain);
80 std::ofstream ofs(domainOptionsFile);
81 REQUIRE(ofs.is_open());
82 ofs << "abc";
83 ofs.close();
84
85 auto domainWatcher = std::make_shared<DomainWatcher>(domain);
86 auto flowIoFactory = std::make_unique<mxl::lib::PosixFlowIoFactory>(domainWatcher);
87 auto const instance = std::make_shared<Instance>(domain, "def", std::move(flowIoFactory), domainWatcher);
88
89 REQUIRE(instance->getHistoryDurationNs() == 200'000'000ULL); // default value
90}
91
92TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Options : Missing and inaccessible domain config", "[options]")
93{
94 auto const path = makeDomainOptionsFilePath(domain);
95 std::filesystem::remove(path); // The persistent fixture is shared between sections.
96 SECTION("Missing options use the fallback")
97 {}
98 SECTION("A dangling symlink uses the fallback")
99 {
100 std::filesystem::create_symlink("missing-options.json", path);
101 }
102 SECTION("An unresolvable symlink uses the fallback")
103 {
104 std::filesystem::create_symlink(path.filename(), path);
105 }
106 auto domainWatcher = std::make_shared<DomainWatcher>(domain);
107 auto flowIoFactory = std::make_unique<mxl::lib::PosixFlowIoFactory>(domainWatcher);
108 auto const instance = std::make_shared<Instance>(domain, "", std::move(flowIoFactory), domainWatcher);
109 REQUIRE(instance->getHistoryDurationNs() == 200'000'000ULL);
110}
Paths and filenames utilities used by the MXL SDK.
std::filesystem::path makeDomainOptionsFilePath(std::filesystem::path const &domain)
Definition PathUtils.cpp:59
TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Options : Default value", "[options]")
REQUIRE(timeDelta< 33 '366 '667LL)