MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
test_instance.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 <uuid.h>
5#include <catch2/catch_test_macros.hpp>
6#include <mxl/flow.h>
7#include <mxl/mxl.h>
8#include "Utils.hpp"
9
10#ifdef __linux__
11# include <sys/vfs.h>
12# include <linux/magic.h>
13
14namespace
15{
16 bool pathIsTmpFs(char const* path)
17 {
18 struct statfs buf;
19 REQUIRE(statfs(path, &buf) == 0);
20 return (buf.f_type == TMPFS_MAGIC) || (buf.f_type == RAMFS_MAGIC);
21 }
22}
23#endif
24
25TEST_CASE("mxlCreateInstance returns NULL for a NULL domain", "[instance]")
26{
27 REQUIRE(mxlCreateInstance(nullptr, "{}") == nullptr);
28 REQUIRE(mxlCreateInstance(nullptr, nullptr) == nullptr);
29}
30
31TEST_CASE("mxlIsTmpFs returns MXL_ERR_INVALID_ARG for NULL path", "[mxlIsTmpFs]")
32{
33 bool isTmpFs = true;
34 REQUIRE(mxlIsTmpFs(nullptr, &isTmpFs) == MXL_ERR_INVALID_ARG);
35}
36
37TEST_CASE("mxlIsTmpFs returns MXL_ERR_INVALID_ARG for NULL out pointer", "[mxlIsTmpFs]")
38{
39 REQUIRE(mxlIsTmpFs("/tmp", nullptr) == MXL_ERR_INVALID_ARG);
40}
41
42TEST_CASE("mxlIsTmpFs returns error for non-existent path", "[mxlIsTmpFs]")
43{
44 bool isTmpFs = true;
45 REQUIRE(mxlIsTmpFs("/this/path/does/not/exist", &isTmpFs) != MXL_STATUS_OK);
46}
47
48#ifdef __linux__
49TEST_CASE("mxlIsTmpFs matches statfs filesystem type on Linux", "[mxlIsTmpFs]")
50{
51 bool isTmpFs = false;
52 REQUIRE(mxlIsTmpFs("/", &isTmpFs) == MXL_STATUS_OK);
53 REQUIRE(isTmpFs == pathIsTmpFs("/"));
54}
55
56TEST_CASE("mxlIsTmpFs detects /dev/shm as tmpfs on Linux", "[mxlIsTmpFs]")
57{
58 bool isTmpFs = false;
59 REQUIRE(mxlIsTmpFs("/dev/shm", &isTmpFs) == MXL_STATUS_OK);
60 REQUIRE(isTmpFs);
61}
62#endif
63
64TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Flow readers / writers caching", "[instance]")
65{
66 auto const opts = "{}";
67 auto instance = mxlCreateInstance(domain.string().c_str(), opts);
68 REQUIRE(instance != nullptr);
69
70 mxlFlowWriter audioWriter;
71 mxlFlowWriter videoWriter;
72 mxlFlowWriter metaWriter;
74 bool flowWasCreated;
75 auto flowDef = mxl::tests::readFile("data/audio_flow.json");
76 REQUIRE(mxlCreateFlowWriter(instance, flowDef.c_str(), opts, &audioWriter, &configInfo, &flowWasCreated) == MXL_STATUS_OK);
78 auto const audioFlowId = uuids::to_string(configInfo.common.id);
79 flowDef = mxl::tests::readFile("data/v210_flow.json");
80 REQUIRE(mxlCreateFlowWriter(instance, flowDef.c_str(), opts, &videoWriter, &configInfo, &flowWasCreated) == MXL_STATUS_OK);
82 auto const videoFlowId = uuids::to_string(configInfo.common.id);
83 flowDef = mxl::tests::readFile("data/data_flow.json");
84 REQUIRE(mxlCreateFlowWriter(instance, flowDef.c_str(), opts, &metaWriter, &configInfo, &flowWasCreated) == MXL_STATUS_OK);
86 auto const metaFlowId = uuids::to_string(configInfo.common.id);
87
88 REQUIRE(audioFlowId != videoFlowId);
89 REQUIRE(audioFlowId != metaFlowId);
90 REQUIRE(videoFlowId != metaFlowId);
91
92 REQUIRE(audioWriter != videoWriter);
93 REQUIRE(audioWriter != metaWriter);
94 REQUIRE(videoWriter != metaWriter);
95
96 mxlFlowReader audioReader;
97 REQUIRE(mxlCreateFlowReader(instance, audioFlowId.c_str(), "", &audioReader) == MXL_STATUS_OK);
98 REQUIRE(audioReader != nullptr);
99 mxlFlowReader audioReader2;
100 REQUIRE(mxlCreateFlowReader(instance, audioFlowId.c_str(), "", &audioReader2) == MXL_STATUS_OK);
101 REQUIRE(audioReader2 != nullptr);
102 mxlFlowReader videoReader;
103 REQUIRE(mxlCreateFlowReader(instance, videoFlowId.c_str(), "", &videoReader) == MXL_STATUS_OK);
104 REQUIRE(videoReader != nullptr);
105 mxlFlowReader metaReader;
106 REQUIRE(mxlCreateFlowReader(instance, metaFlowId.c_str(), "", &metaReader) == MXL_STATUS_OK);
107 REQUIRE(metaReader != nullptr);
108
109 REQUIRE(audioReader == audioReader2);
110 REQUIRE(audioReader != videoReader);
111 REQUIRE(audioReader != metaReader);
112 REQUIRE(videoReader != metaReader);
113
114 REQUIRE(static_cast<void*>(audioReader) != static_cast<void*>(audioWriter));
115 REQUIRE(static_cast<void*>(audioReader) != static_cast<void*>(videoWriter));
116 REQUIRE(static_cast<void*>(audioReader) != static_cast<void*>(metaWriter));
117
118 REQUIRE(mxlReleaseFlowReader(instance, audioReader) == MXL_STATUS_OK);
119 REQUIRE(mxlReleaseFlowReader(instance, audioReader2) == MXL_STATUS_OK);
120 REQUIRE(mxlReleaseFlowReader(instance, videoReader) == MXL_STATUS_OK);
121 REQUIRE(mxlReleaseFlowReader(instance, metaReader) == MXL_STATUS_OK);
122
123 REQUIRE(mxlReleaseFlowWriter(instance, audioWriter) == MXL_STATUS_OK);
124 REQUIRE(mxlReleaseFlowWriter(instance, videoWriter) == MXL_STATUS_OK);
125 REQUIRE(mxlReleaseFlowWriter(instance, metaWriter) == MXL_STATUS_OK);
126
128}
129
130TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Flow deletion on writer release", "[instance]")
131{
132 auto instanceA = mxlCreateInstance(domain.string().c_str(), nullptr);
133 auto instanceB = mxlCreateInstance(domain.string().c_str(), nullptr);
134
135 mxlFlowWriter writerA = nullptr;
136 mxlFlowWriter writerB = nullptr;
137 mxlFlowConfigInfo flowConfig;
138 bool flowWasCreated = false;
139
140 auto flowDef = mxl::tests::readFile("data/v210_flow.json");
141 REQUIRE(mxlCreateFlowWriter(instanceA, flowDef.c_str(), nullptr, &writerA, &flowConfig, &flowWasCreated) == MXL_STATUS_OK);
143 REQUIRE(mxlCreateFlowWriter(instanceB, flowDef.c_str(), nullptr, &writerB, &flowConfig, &flowWasCreated) == MXL_STATUS_OK);
144 REQUIRE_FALSE(flowWasCreated);
145
146 auto id = uuids::to_string(flowConfig.common.id);
147
148 // The flow directory should exists after the flow has been created
149 REQUIRE(flowDirectoryExists(id));
150
151 // The flow directory should still exists after the first writer has been released
152 mxlReleaseFlowWriter(instanceA, writerA);
153 REQUIRE(flowDirectoryExists(id));
154
155 // The flow directory should have been removed after the second writer has been released
156 mxlReleaseFlowWriter(instanceB, writerB);
157 REQUIRE_FALSE(flowDirectoryExists(id));
158
161}
162
163TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Flow deletion on instance destruction", "[instance]")
164{
165 auto instanceA = mxlCreateInstance(domain.string().c_str(), nullptr);
166 auto instanceB = mxlCreateInstance(domain.string().c_str(), nullptr);
167
168 mxlFlowWriter writerA = nullptr;
169 mxlFlowWriter writerB = nullptr;
170 mxlFlowConfigInfo flowConfig;
171 bool flowWasCreated = false;
172
173 auto flowDef = mxl::tests::readFile("data/v210_flow.json");
174 REQUIRE(mxlCreateFlowWriter(instanceA, flowDef.c_str(), nullptr, &writerA, &flowConfig, &flowWasCreated) == MXL_STATUS_OK);
176 REQUIRE(mxlCreateFlowWriter(instanceB, flowDef.c_str(), nullptr, &writerB, &flowConfig, &flowWasCreated) == MXL_STATUS_OK);
177 REQUIRE_FALSE(flowWasCreated);
178
179 auto id = uuids::to_string(flowConfig.common.id);
180
181 REQUIRE(flowDirectoryExists(id));
182
183 mxlDestroyInstance(instanceA);
184 REQUIRE(flowDirectoryExists(id));
185
186 mxlDestroyInstance(instanceB);
187 REQUIRE_FALSE(flowDirectoryExists(id));
188}
Public flow reader, writer, payload and synchronization APIs.
MXL_EXPORT mxlStatus mxlReleaseFlowReader(mxlInstance instance, mxlFlowReader reader)
Definition flow.cpp:177
struct mxlFlowReader_t * mxlFlowReader
Definition flow.h:205
struct mxlFlowWriter_t * mxlFlowWriter
Definition flow.h:206
MXL_EXPORT mxlStatus mxlReleaseFlowWriter(mxlInstance instance, mxlFlowWriter writer)
Definition flow.cpp:262
MXL_EXPORT mxlStatus mxlCreateFlowWriter(mxlInstance instance, char const *flowDef, char const *options, mxlFlowWriter *writer, mxlFlowConfigInfo *configInfo, bool *created)
Definition flow.cpp:198
MXL_EXPORT mxlStatus mxlCreateFlowReader(mxlInstance instance, char const *flowId, char const *options, mxlFlowReader *reader)
Definition flow.cpp:136
struct mxlFlowConfigInfo_t mxlFlowConfigInfo
MXL_EXPORT mxlStatus mxlDestroyInstance(mxlInstance in_instance)
Definition mxl.cpp:106
@ MXL_ERR_INVALID_ARG
Definition mxl.h:35
@ MXL_STATUS_OK
Definition mxl.h:23
MXL_EXPORT mxlInstance mxlCreateInstance(char const *in_mxlDomain, char const *in_options)
Definition mxl.cpp:45
MXL_EXPORT mxlStatus mxlIsTmpFs(char const *in_path, bool *out_isTmpFs)
Definition mxl.cpp:73
std::string readFile(std::filesystem::path const &filepath)
Definition Utils.hpp:78
mxlCommonFlowConfigInfo common
Definition flowinfo.h:178
bool flowWasCreated
mxlFlowConfigInfo configInfo
auto flowDef
REQUIRE(timeDelta< 33 '366 '667LL)
TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Flow readers / writers caching", "[instance]")
TEST_CASE("mxlCreateInstance returns NULL for a NULL domain", "[instance]")