MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
test_read_write_conflict.cpp File Reference
#include <cstdint>
#include <limits>
#include <string>
#include <catch2/catch_test_macros.hpp>
#include <mxl/flow.h>
#include <mxl/mxl.h>
#include <mxl/time.h>
#include "Utils.hpp"
Include dependency graph for test_read_write_conflict.cpp:

Go to the source code of this file.

Functions

 TEST_CASE_PERSISTENT_FIXTURE (mxl::tests::mxlDomainFixture, "Audio Flow : tail read during open write", "[mxl flows]")
 TEST_CASE_PERSISTENT_FIXTURE (mxl::tests::mxlDomainFixture, "Video Flow : tail read during open write", "[mxl flows]")

Function Documentation

◆ TEST_CASE_PERSISTENT_FIXTURE() [1/2]

TEST_CASE_PERSISTENT_FIXTURE ( mxl::tests::mxlDomainFixture ,
"Audio Flow : tail read during open write" ,
"" [mxl flows] )

Definition at line 222 of file test_read_write_conflict.cpp.

222 : 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{};
233 auto configInfo = mxlFlowConfigInfo{};
234 auto flowWasCreated = false;
235
236 REQUIRE(mxlCreateFlowWriter(instance, flowDef.c_str(), "", &writer, &configInfo, &flowWasCreated) == MXL_STATUS_OK);
237 REQUIRE(flowWasCreated);
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
262 REQUIRE(mxlFlowWriterCommitSamples(writer) == MXL_STATUS_OK);
263
264 tmpWriteIndex += halfBufferLength;
265 }
266 }
267
268 // Init a reader and sanity check the flow's headIndex.
269 auto reader = mxlFlowReader{};
270 REQUIRE(mxlCreateFlowReader(instance, flowId, "", &reader) == MXL_STATUS_OK);
271
272 auto runtimeInfo = mxlFlowRuntimeInfo{};
273 REQUIRE(mxlFlowReaderGetRuntimeInfo(reader, &runtimeInfo) == MXL_STATUS_OK);
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 = {};
288 REQUIRE(mxlFlowReaderGetSamplesNonBlocking(reader, boundaryIndex - 1, 1U, &readBuffer) == MXL_ERR_OUT_OF_RANGE_TOO_LATE);
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.
297 REQUIRE(mxlFlowReaderGetRuntimeInfo(reader, &runtimeInfo) == MXL_STATUS_OK);
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 = {};
307 REQUIRE(mxlFlowReaderGetSamplesNonBlocking(reader, ringTailIndex, 1U, &readBuffer) == MXL_ERR_OUT_OF_RANGE_TOO_LATE);
308
309 // A read just before boundaryIndex should also fail with
310 // TOO_LATE.
311 readBuffer = {};
312 REQUIRE(mxlFlowReaderGetSamplesNonBlocking(reader, boundaryIndex - 1, 1U, &readBuffer) == MXL_ERR_OUT_OF_RANGE_TOO_LATE);
313
314 // Commit the write and update local headIndex.
315 REQUIRE(mxlFlowWriterCommitSamples(writer) == MXL_STATUS_OK);
316
317 headIndex = headIndex + halfBufferLength;
318
319 // Sanity check that the local headIndex matches the flow's
320 // headIndex.
321 REQUIRE(mxlFlowReaderGetRuntimeInfo(reader, &runtimeInfo) == MXL_STATUS_OK);
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.
331 REQUIRE(mxlReleaseFlowReader(instance, reader) == MXL_STATUS_OK);
332
333 REQUIRE(mxlReleaseFlowWriter(instance, writer) == MXL_STATUS_OK);
334
335 REQUIRE(mxlDestroyInstance(instance) == MXL_STATUS_OK);
336}

◆ TEST_CASE_PERSISTENT_FIXTURE() [2/2]

TEST_CASE_PERSISTENT_FIXTURE ( mxl::tests::mxlDomainFixture ,
"Video Flow : tail read during open write" ,
"" [mxl flows] )

Definition at line 33 of file test_read_write_conflict.cpp.

33 : 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{};
44 auto configInfo = mxlFlowConfigInfo{};
45 auto flowWasCreated = false;
46
47 REQUIRE(mxlCreateFlowWriter(instance, flowDef.c_str(), "", &writer, &configInfo, &flowWasCreated) == MXL_STATUS_OK);
48 REQUIRE(flowWasCreated);
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
73 REQUIRE(mxlFlowWriterCommitGrain(writer, &grainInfo) == MXL_STATUS_OK);
74 }
75
76 // Init a reader and sanity check the flow's headIndex.
77 auto reader = mxlFlowReader{};
78 REQUIRE(mxlCreateFlowReader(instance, flowId, "", &reader) == MXL_STATUS_OK);
79
80 auto runtimeInfo = mxlFlowRuntimeInfo{};
81 REQUIRE(mxlFlowReaderGetRuntimeInfo(reader, &runtimeInfo) == MXL_STATUS_OK);
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.
120 REQUIRE(mxlFlowReaderGetRuntimeInfo(reader, &runtimeInfo) == MXL_STATUS_OK);
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;
171 REQUIRE(mxlFlowWriterCommitGrain(writer, &writeGrainInfo) == MXL_STATUS_OK);
172
173 headIndex = headIndex + 1;
174 tailIndex = headIndex - grainCount + 1;
175
176 // Sanity check local headIndex matches flow's headIndex.
177 REQUIRE(mxlFlowReaderGetRuntimeInfo(reader, &runtimeInfo) == MXL_STATUS_OK);
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.
193 REQUIRE(mxlReleaseFlowReader(instance, reader) == MXL_STATUS_OK);
194
195 REQUIRE(mxlReleaseFlowWriter(instance, writer) == MXL_STATUS_OK);
196
197 REQUIRE(mxlDestroyInstance(instance) == MXL_STATUS_OK);
198}
uint8_t * buffer
Set a mark at the beginning and the end of the grain payload.
mxlFlowWriter writer
uint64_t index