MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
test_read_write_conflict.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
3
4#include <cstdint>
5#include <limits>
6#include <string>
7#include <catch2/catch_test_macros.hpp>
8#include <mxl/flow.h>
9#include <mxl/mxl.h>
10#include <mxl/time.h>
11#include "Utils.hpp"
12
13/*
14 This test verifies that a read request for the ring buffer's
15 tailIndex grain returns TOO_LATE when the writer has a write
16 operation open at headIndex+1.
17
18 +-----------+-- --+-------------+-----------+-------------+--
19 | index 0 | | index N-2 | index N-1 | index 0 |
20 | tailIndex | ... | headIndex-1 | headIndex | headIndex+1 | ...
21 +-----------+-- --+-------------+-----------+-------------+--
22 ^
23 ring wrap
24
25 N = grainCount
26
27 The diagram illustrates that tailIndex and headIndex+1 address the
28 same ring-buffer index.
29
30 Note: index 0 is used here for illustration. The actual ring-buffer
31 starting index is tailIndex % grainCount.
32*/
33TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Video Flow : tail read during open write", "[mxl flows]")
34{
35 auto const flowId = "5fbec3b1-1b0f-417d-9059-8b94a47197ed";
36 auto const flowDef = mxl::tests::readFile("data/v210_flow.json");
37
38 // Init the domain instance.
39 auto instance = mxlCreateInstance(domain.string().c_str(), "");
40 REQUIRE(instance != nullptr);
41
42 // Init a flow writer.
43 auto writer = mxlFlowWriter{};
45 auto flowWasCreated = false;
46
49
50 // Init headIndex from the current MXL time.
51 auto const now = mxlGetTime();
52 auto headIndex = mxlTimestampToIndex(&configInfo.common.grainRate, now);
53 REQUIRE(headIndex != MXL_UNDEFINED_INDEX);
54
55 auto const grainCount = static_cast<std::uint64_t>(configInfo.discrete.grainCount);
56
57 REQUIRE(grainCount > 1U);
58 REQUIRE(headIndex > grainCount);
59
60 // Init each grain in the ring buffer.
61 auto tailIndex = headIndex - grainCount + 1;
62 for (auto i = std::uint64_t{0}; i < grainCount; ++i)
63 {
64 auto grainInfo = mxlGrainInfo{};
65 std::uint8_t* buffer = nullptr;
66
67 REQUIRE(mxlFlowWriterOpenGrain(writer, tailIndex + i, &grainInfo, &buffer) == MXL_STATUS_OK);
68
69 buffer[0] = 0xCA;
70 buffer[grainInfo.grainSize - 1] = 0xFE;
71 grainInfo.validSlices = grainInfo.totalSlices;
72
74 }
75
76 // Init a reader and sanity check the flow's headIndex.
77 auto reader = mxlFlowReader{};
79
80 auto runtimeInfo = mxlFlowRuntimeInfo{};
82 REQUIRE(runtimeInfo.headIndex == headIndex);
83
84 // Reading the tailIndex grain should be TOO_LATE when write is
85 // not in progress.
86 auto readGrainInfo = mxlGrainInfo{};
87 std::uint8_t* readBuffer = nullptr;
88 auto const grainReadStatusOutsideWrite = mxlFlowReaderGetGrainNonBlocking(reader, tailIndex, &readGrainInfo, &readBuffer);
89 {
90 INFO("The tailIndex grain read should return TOO_LATE even when no write "
91 "is in progress because the tail grain is reserved for the writer.");
92 CHECK(grainReadStatusOutsideWrite == MXL_ERR_OUT_OF_RANGE_TOO_LATE);
93 }
94
95 // Reading the tailIndex grain through the slice API should be
96 // TOO_LATE when write is not in progress.
97 readBuffer = nullptr;
98 readGrainInfo = {};
99 auto const sliceReadStatusOutsideWrite = mxlFlowReaderGetGrainSliceNonBlocking(
100 reader, tailIndex, MXL_GRAIN_VALID_SLICES_ANY, &readGrainInfo, &readBuffer);
101 {
102 INFO("The tailIndex slice read should return TOO_LATE even when no write "
103 "is in progress because the tail grain is reserved for the writer.");
104 CHECK(sliceReadStatusOutsideWrite == MXL_ERR_OUT_OF_RANGE_TOO_LATE);
105 }
106
107 // Open a write to the next sequential grain at headIndex+1. This
108 // grain shares a ring-buffer position with the tailIndex grain.
109 auto writeGrainInfo = mxlGrainInfo{};
110 std::uint8_t* writeBuffer = nullptr;
111 {
112 REQUIRE(mxlFlowWriterOpenGrain(writer, headIndex + 1, &writeGrainInfo, &writeBuffer) == MXL_STATUS_OK);
113
114 INFO("The newly opened grain should identify headIndex+1.");
115 CAPTURE(writeGrainInfo.index, headIndex + 1, writeGrainInfo.validSlices, writeGrainInfo.totalSlices);
116 CHECK(writeGrainInfo.index == headIndex + 1);
117 }
118
119 // Sanity check: verify that headIndex has not yet moved.
121 REQUIRE(runtimeInfo.headIndex == headIndex);
122
123 // Reading a grain at tailIndex during an open write of
124 // headIndex+1 should return TOO_LATE to prevent reader/writer
125 // conflict during the writer's open/commit interval.
126 constexpr auto NOT_CHANGED_INDEX_SENTINEL_VALUE = std::numeric_limits<decltype(readGrainInfo.index)>::max();
127 readBuffer = nullptr;
128 readGrainInfo = {};
129 readGrainInfo.index = NOT_CHANGED_INDEX_SENTINEL_VALUE;
130 auto const grainReadStatusInsideWrite = mxlFlowReaderGetGrainNonBlocking(reader, tailIndex, &readGrainInfo, &readBuffer);
131 {
132 INFO("A tailIndex read should return TOO_LATE while a write of "
133 "headIndex+1 is open because both logical grains alias the "
134 "same physical ring-buffer position."
135
136 );
137 CHECK(grainReadStatusInsideWrite == MXL_ERR_OUT_OF_RANGE_TOO_LATE);
138 }
139 {
140 INFO("Because the tailIndex grain read is expected to return TOO_LATE, "
141 "it should not return a payload or modify the grain index.");
142 CHECK(readBuffer == nullptr);
143 CHECK(readGrainInfo.index == NOT_CHANGED_INDEX_SENTINEL_VALUE);
144 }
145
146 // Reading tailIndex through the slice API during an open write of
147 // headIndex+1 should return TOO_LATE, should not return a payload,
148 // and should not modify readGrainInfo.validSlices.
149 constexpr auto NOT_CHANGED_SLICE_SENTINEL_VALUE = std::numeric_limits<decltype(readGrainInfo.validSlices)>::max();
150 readBuffer = nullptr;
151 readGrainInfo = {};
152 readGrainInfo.validSlices = NOT_CHANGED_SLICE_SENTINEL_VALUE;
153 auto const sliceReadStatus = mxlFlowReaderGetGrainSliceNonBlocking(reader, tailIndex, MXL_GRAIN_VALID_SLICES_ANY, &readGrainInfo, &readBuffer);
154 {
155 INFO("A tailIndex slice read should return TOO_LATE while a write of "
156 "headIndex+1 is open because both logical grains alias the "
157 "same physical ring-buffer position.");
158 CHECK(sliceReadStatus == MXL_ERR_OUT_OF_RANGE_TOO_LATE);
159 }
160 {
161 INFO("Because the tailIndex slice read is expected to return TOO_LATE, "
162 "it should not return a payload or modify validSlices.");
163 CHECK(readBuffer == nullptr);
164 CHECK(readGrainInfo.validSlices == NOT_CHANGED_SLICE_SENTINEL_VALUE);
165 }
166
167 // Commit the write and update local headIndex.
168 writeBuffer[0] = 0xCA;
169 writeBuffer[writeGrainInfo.grainSize - 1] = 0xFE;
170 writeGrainInfo.validSlices = writeGrainInfo.totalSlices;
172
173 headIndex = headIndex + 1;
174 tailIndex = headIndex - grainCount + 1;
175
176 // Sanity check local headIndex matches flow's headIndex.
178 REQUIRE(runtimeInfo.headIndex == headIndex);
179
180 // Post commit read of the tailIndex grain should continue to be
181 // TOO_LATE.
182 readBuffer = nullptr;
183 readGrainInfo = {};
184 auto const grainReadStatusPostCommit = mxlFlowReaderGetGrainNonBlocking(reader, tailIndex, &readGrainInfo, &readBuffer);
185 {
186 INFO("A post commit tailIndex read should return TOO_LATE after a "
187 "write of headIndex+1 is committed because both logical grains "
188 "alias the same physical ring-buffer position.");
189 CHECK(grainReadStatusPostCommit == MXL_ERR_OUT_OF_RANGE_TOO_LATE);
190 }
191
192 // Tear down.
194
196
198}
199
200/*
201 This test verifies that the audio reader boundary between the
202 writer's half-ring and the reader's half-ring is enforced regardless
203 of whether the writer has a write operation open.
204
205 +--------------------------+--------------------------+--------------------------+--
206 | physical half 0 | physical half 1 | physical half 0 |
207 | writer's half-ring | reader's half-ring | writer's half-ring | ...
208 +--------------------------+--------------------------+--------------------------+--
209 ^ ^ ^
210 ringTailIndex boundaryIndex headIndex
211 ring wrap
212
213 ringTailIndex = headIndex - bufferLength + 1
214 boundaryIndex = headIndex - bufferLength/2 + 1
215
216 Reads at boundaryIndex are allowed. Reads at boundaryIndex-1 and
217 earlier are TOO_LATE.
218
219 Note: physical half 0/1 are used here to illustrate the two
220 half-ring regions and their reuse after ring wrap.
221*/
222TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Audio Flow : tail read during open write", "[mxl flows]")
223{
224 auto const flowId = "b3bb5be7-9fe9-4324-a5bb-4c70e1084449";
225 auto const flowDef = mxl::tests::readFile("data/audio_flow.json");
226
227 // Init the domain instance.
228 auto instance = mxlCreateInstance(domain.string().c_str(), "");
229 REQUIRE(instance != nullptr);
230
231 // Init a flow writer.
232 auto writer = mxlFlowWriter{};
234 auto flowWasCreated = false;
235
238
239 // Init headIndex from the current MXL time.
240 auto const now = mxlGetTime();
241 auto headIndex = mxlTimestampToIndex(&configInfo.common.grainRate, now);
242 REQUIRE(headIndex != MXL_UNDEFINED_INDEX);
243
244 auto const bufferLength = static_cast<std::uint64_t>(configInfo.continuous.bufferLength);
245 auto const halfBufferLength = bufferLength / 2U;
246
247 REQUIRE(halfBufferLength > 0U);
248 REQUIRE(headIndex > bufferLength);
249
250 // Init the full ring buffer using two sequential half-buffer
251 // writes ending at headIndex. Each write respects the writer's
252 // half-ring limit.
253 {
254 auto const boundaryIndex = headIndex - halfBufferLength + 1;
255 auto tmpWriteIndex = boundaryIndex - 1;
256 for (auto i = std::uint64_t{0}; i < 2U; ++i)
257 {
258 auto writeBuffer = mxlMutableWrappedMultiBufferSlice{};
259
260 REQUIRE(mxlFlowWriterOpenSamples(writer, tmpWriteIndex, halfBufferLength, &writeBuffer) == MXL_STATUS_OK);
261
263
264 tmpWriteIndex += halfBufferLength;
265 }
266 }
267
268 // Init a reader and sanity check the flow's headIndex.
269 auto reader = mxlFlowReader{};
271
272 auto runtimeInfo = mxlFlowRuntimeInfo{};
274 REQUIRE(runtimeInfo.headIndex == headIndex);
275
276 // boundaryIndex separates the writer's half-ring from the reader's
277 // half-ring.
278 auto const boundaryIndex = headIndex - halfBufferLength + 1;
279 auto const ringTailIndex = headIndex - bufferLength + 1;
280
281 auto readBuffer = mxlWrappedMultiBufferSlice{};
282
283 // Reading at boundaryIndex is OK.
284 REQUIRE(mxlFlowReaderGetSamplesNonBlocking(reader, boundaryIndex, 1U, &readBuffer) == MXL_STATUS_OK);
285
286 // Reading one index before boundaryIndex is TOO_LATE.
287 readBuffer = {};
289
290 // Open a write to the next sequential half-buffer at
291 // headIndex+halfBufferLength. This does *not* overlap the
292 // reader's half-ring.
293 auto writeBuffer = mxlMutableWrappedMultiBufferSlice{};
294 REQUIRE(mxlFlowWriterOpenSamples(writer, headIndex + halfBufferLength, halfBufferLength, &writeBuffer) == MXL_STATUS_OK);
295
296 // Sanity check: verify that headIndex has not yet moved.
298 REQUIRE(runtimeInfo.headIndex == headIndex);
299
300 // The reader boundary is unchanged while the write is open.
301 readBuffer = {};
302 REQUIRE(mxlFlowReaderGetSamplesNonBlocking(reader, boundaryIndex, 1U, &readBuffer) == MXL_STATUS_OK);
303
304 // A read at the tail is also in the writer's half-ring and should
305 // fail with TOO_LATE.
306 readBuffer = {};
308
309 // A read just before boundaryIndex should also fail with
310 // TOO_LATE.
311 readBuffer = {};
313
314 // Commit the write and update local headIndex.
316
317 headIndex = headIndex + halfBufferLength;
318
319 // Sanity check that the local headIndex matches the flow's
320 // headIndex.
322 REQUIRE(runtimeInfo.headIndex == headIndex);
323
324 // Post commit read at the new boundaryIndex is OK.
325 auto const newBoundaryIndex = headIndex - halfBufferLength + 1;
326 readBuffer = {};
327
328 REQUIRE(mxlFlowReaderGetSamplesNonBlocking(reader, newBoundaryIndex, 1U, &readBuffer) == MXL_STATUS_OK);
329
330 // Tear down.
332
334
336}
Public flow reader, writer, payload and synchronization APIs.
MXL_EXPORT mxlStatus mxlFlowWriterOpenGrain(mxlFlowWriter writer, uint64_t index, mxlGrainInfo *mxlGrainInfo, uint8_t **payload)
Definition flow.cpp:446
MXL_EXPORT mxlStatus mxlReleaseFlowReader(mxlInstance instance, mxlFlowReader reader)
Definition flow.cpp:177
struct mxlFlowReader_t * mxlFlowReader
Definition flow.h:205
struct mxlGrainInfo_t mxlGrainInfo
MXL_EXPORT mxlStatus mxlFlowReaderGetGrainNonBlocking(mxlFlowReader reader, uint64_t index, mxlGrainInfo *grain, uint8_t **payload)
Definition flow.cpp:392
MXL_EXPORT mxlStatus mxlFlowReaderGetSamplesNonBlocking(mxlFlowReader reader, uint64_t index, size_t count, mxlWrappedMultiBufferSlice *payloadBuffersSlices)
Definition flow.cpp:558
MXL_EXPORT mxlStatus mxlFlowWriterCommitSamples(mxlFlowWriter writer)
Definition flow.cpp:646
MXL_EXPORT mxlStatus mxlFlowReaderGetGrainSliceNonBlocking(mxlFlowReader reader, uint64_t index, uint16_t minValidSlices, mxlGrainInfo *grain, uint8_t **payload)
Definition flow.cpp:399
struct mxlWrappedMultiBufferSlice_t mxlWrappedMultiBufferSlice
struct mxlFlowWriter_t * mxlFlowWriter
Definition flow.h:206
struct mxlMutableWrappedMultiBufferSlice_t mxlMutableWrappedMultiBufferSlice
#define MXL_GRAIN_VALID_SLICES_ANY
Definition flow.h:39
MXL_EXPORT mxlStatus mxlReleaseFlowWriter(mxlInstance instance, mxlFlowWriter writer)
Definition flow.cpp:262
MXL_EXPORT mxlStatus mxlFlowReaderGetRuntimeInfo(mxlFlowReader reader, mxlFlowRuntimeInfo *info)
Definition flow.cpp:339
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
MXL_EXPORT mxlStatus mxlFlowWriterOpenSamples(mxlFlowWriter writer, uint64_t index, size_t count, mxlMutableWrappedMultiBufferSlice *payloadBuffersSlices)
Definition flow.cpp:605
MXL_EXPORT mxlStatus mxlFlowWriterCommitGrain(mxlFlowWriter writer, mxlGrainInfo const *grain)
Definition flow.cpp:487
struct mxlFlowRuntimeInfo_t mxlFlowRuntimeInfo
struct mxlFlowConfigInfo_t mxlFlowConfigInfo
MXL_EXPORT mxlStatus mxlDestroyInstance(mxlInstance in_instance)
Definition mxl.cpp:106
@ MXL_STATUS_OK
Definition mxl.h:23
@ MXL_ERR_OUT_OF_RANGE_TOO_LATE
Definition mxl.h:30
MXL_EXPORT mxlInstance mxlCreateInstance(char const *in_mxlDomain, char const *in_options)
Definition mxl.cpp:45
std::string readFile(std::filesystem::path const &filepath)
Definition Utils.hpp:78
#define MXL_UNDEFINED_INDEX
Definition rational.h:17
uint8_t * buffer
Set a mark at the beginning and the end of the grain payload.
bool flowWasCreated
mxlFlowConfigInfo configInfo
mxlFlowReader reader
mxlFlowWriter writer
auto const flowId
auto const now
auto flowDef
TEST_CASE_PERSISTENT_FIXTURE(mxl::tests::mxlDomainFixture, "Video Flow : tail read during open write", "[mxl flows]")
REQUIRE(timeDelta< 33 '366 '667LL)
MXL_EXPORT uint64_t mxlTimestampToIndex(mxlRational const *editRate, uint64_t timestamp)
Definition time.cpp:31
MXL_EXPORT uint64_t mxlGetTime(void)
Definition time.cpp:11