A synchronization group must keep synchronizing for as long as it exists.
47 : Repeated waits
", "[
mxl sync groups]
")
48{
49 auto const opts = "{}";
50
51 auto instance = mxlCreateInstance(domain.string().c_str(), opts);
52 REQUIRE(instance != nullptr);
53
54 // Two discrete flows sharing a grain rate. The data flow is written late on purpose.
55 mxlFlowWriter videoWriter;
56 mxlFlowWriter dataWriter;
57 mxlFlowConfigInfo configInfo;
58 bool created = false;
59 REQUIRE(
60 mxlCreateFlowWriter(instance, mxl::tests::readFile("data/v210_flow.json").c_str(), "", &videoWriter, &configInfo, &created) == MXL_STATUS_OK);
61 REQUIRE(
62 mxlCreateFlowWriter(instance, mxl::tests::readFile("data/data_flow.json").c_str(), "", &dataWriter, &configInfo, &created) == MXL_STATUS_OK);
63
64 mxlFlowReader videoReader;
65 mxlFlowReader dataReader;
66 REQUIRE(mxlCreateFlowReader(instance, VIDEO_FLOW_ID, "", &videoReader) == MXL_STATUS_OK);
67 REQUIRE(mxlCreateFlowReader(instance, DATA_FLOW_ID, "", &dataReader) == MXL_STATUS_OK);
68
69 mxlFlowSynchronizationGroup group;
70 REQUIRE(mxlCreateFlowSynchronizationGroup(instance, &group) == MXL_STATUS_OK);
71 REQUIRE(mxlFlowSynchronizationGroupAddReader(group, videoReader) == MXL_STATUS_OK);
72 REQUIRE(mxlFlowSynchronizationGroupAddReader(group, dataReader) == MXL_STATUS_OK);
73
74 // First wait: both grains arrive, the second member later than the first.
75 auto const firstIndex = mxlTimestampToIndex(&GRAIN_RATE, mxlGetTime()) + 1;
76 auto const firstTimestamp = mxlIndexToTimestamp(&GRAIN_RATE, firstIndex);
77
78 auto videoStatus = MXL_ERR_UNKNOWN;
79 auto dataStatus = MXL_ERR_UNKNOWN;
80 auto videoThread = std::thread{[&]
81 {
82 videoStatus = commitGrainAfter(videoWriter, firstIndex, std::chrono::milliseconds{50});
83 }};
84 auto dataThread = std::thread{[&]
85 {
86 dataStatus = commitGrainAfter(dataWriter, firstIndex, std::chrono::milliseconds{250});
87 }};
88
89 auto const firstWaitStatus = mxlFlowSynchronizationGroupWaitForDataAt(group, firstTimestamp, 2'000'000'000ULL);
90
91 videoThread.join();
92 dataThread.join();
93
94 REQUIRE(videoStatus == MXL_STATUS_OK);
95 REQUIRE(dataStatus == MXL_STATUS_OK);
96 REQUIRE(firstWaitStatus == MXL_STATUS_OK);
97
98 // Second wait, on the same group, for a grain that nobody will ever write. The group
99 // must block until the timeout expires instead of reporting the data as available.
100 auto const missingTimestamp = mxlIndexToTimestamp(&GRAIN_RATE, firstIndex + 1000);
101
102 auto const start = std::chrono::steady_clock::now();
103 auto const secondWaitStatus = mxlFlowSynchronizationGroupWaitForDataAt(group, missingTimestamp, 200'000'000ULL);
104 auto const elapsed = std::chrono::steady_clock::now() - start;
105
106 CHECK(secondWaitStatus != MXL_STATUS_OK);
107 CHECK(elapsed >= std::chrono::milliseconds{150});
108
109 REQUIRE(mxlReleaseFlowSynchronizationGroup(instance, group) == MXL_STATUS_OK);
110 REQUIRE(mxlReleaseFlowReader(instance, videoReader) == MXL_STATUS_OK);
111 REQUIRE(mxlReleaseFlowReader(instance, dataReader) == MXL_STATUS_OK);
112 REQUIRE(mxlReleaseFlowWriter(instance, videoWriter) == MXL_STATUS_OK);
113 REQUIRE(mxlReleaseFlowWriter(instance, dataWriter) == MXL_STATUS_OK);
114 mxlDestroyInstance(instance);
115}