Debian Codegraph @

test/rist/test_reflector.c

raw symbols SCIP
1 /* librist. Copyright © RIST Community
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Test for transparent reflector functionality and RIST protocol verification.
5 */
6
7 #include "librist/librist.h"
8 #include "rist-private.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdatomic.h>
13 #include <inttypes.h>
14
15 static struct rist_logging_settings *logging_settings = NULL;
16 static atomic_int failed;
17
18 /* Upper bounds only: the waits below poll for the actual condition and
19 * break as soon as it holds, so these caps just limit how long a slow or
20 * loaded host may take before giving up. */
21 #define CONNECT_TIMEOUT_MS 30000
22 #define PHASE_RX_TIMEOUT_MS 20000
23 #define CONVERGE_TIMEOUT_MS 30000
24
25 static int log_callback(void *arg, enum rist_log_level level, const char *msg) {
26 (void)arg;
27 if (level <= RIST_LOG_ERROR) {
28 fprintf(stderr, "[ERROR] %s", msg);
29 } else if (level == RIST_LOG_WARN) {
30 fprintf(stdout, "[WARN] %s", msg);
31 }
32 return 0;
33 }
34
35 static uint64_t get_time_ms(void) {
36 #ifdef _WIN32
37 return GetTickCount64();
38 #else
39 struct timespec ts;
40 clock_gettime(CLOCK_MONOTONIC, &ts);
41 return (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
42 #endif
43 }
44
45 static struct rist_ctx *setup_receiver(const char *url) {
46 struct rist_ctx *ctx;
47 if (rist_receiver_create(&ctx, RIST_PROFILE_MAIN, logging_settings) != 0) {
48 fprintf(stderr, "Could not create receiver context\n");
49 return NULL;
50 }
51 struct rist_peer_config *peer_config = NULL;
52 if (rist_parse_address2(url, (void *)&peer_config) != 0) {
53 fprintf(stderr, "Could not parse receiver address: %s\n", url);
54 rist_destroy(ctx);
55 return NULL;
56 }
57 struct rist_peer *peer;
58 if (rist_peer_create(ctx, &peer, peer_config) == -1) {
59 fprintf(stderr, "Could not create receiver peer\n");
60 free((void *)peer_config);
61 rist_destroy(ctx);
62 return NULL;
63 }
64 free((void *)peer_config);
65 if (rist_start(ctx) == -1) {
66 fprintf(stderr, "Could not start receiver\n");
67 rist_destroy(ctx);
68 return NULL;
69 }
70 return ctx;
71 }
72
73 static struct rist_ctx *setup_sender(const char *url) {
74 struct rist_ctx *ctx;
75 if (rist_sender_create(&ctx, RIST_PROFILE_MAIN, 0, logging_settings) != 0) {
76 fprintf(stderr, "Could not create sender context\n");
77 return NULL;
78 }
79 struct rist_peer_config *peer_config = NULL;
80 if (rist_parse_address2(url, (void *)&peer_config) != 0) {
81 fprintf(stderr, "Could not parse sender address: %s\n", url);
82 rist_destroy(ctx);
83 return NULL;
84 }
85 struct rist_peer *peer;
86 if (rist_peer_create(ctx, &peer, peer_config) == -1) {
87 fprintf(stderr, "Could not create sender peer\n");
88 free((void *)peer_config);
89 rist_destroy(ctx);
90 return NULL;
91 }
92 free((void *)peer_config);
93 if (rist_start(ctx) == -1) {
94 fprintf(stderr, "Could not start sender\n");
95 rist_destroy(ctx);
96 return NULL;
97 }
98 return ctx;
99 }
100
101 int main(int argc, char *argv[]) {
102 (void)argc;
103 (void)argv;
104 int ret = 0;
105 atomic_init(&failed, 0);
106 uint8_t payload[1316];
107 struct rist_data_block data_block = { 0 };
108
109 if (rist_logging_set(&logging_settings, RIST_LOG_DEBUG, log_callback, NULL, NULL, stderr) != 0) {
110 fprintf(stderr, "Failed to setup logging\n");
111 return 99;
112 }
113
114 fprintf(stdout, "\n============================================\n");
115 fprintf(stdout, " STARTING RIST MEDIA REFLECTOR TEST SUITE \n");
116 fprintf(stdout, "============================================\n\n");
117
118 /* 1. Setup transparent reflector receiver, publisher sender, and subscriber receiver */
119 const char *reflector_url = "rist://@127.0.0.1:45678?secret=12345678&aes-type=128&rtt-max=200&rtt-min=1&buffer-min=50&buffer-max=1500&reflector=1";
120 const char *publisher_url = "rist://127.0.0.1:45678?secret=12345678&aes-type=128&rtt-max=200&rtt-min=1&buffer-min=50&buffer-max=1500";
121 const char *subscriber_url = "rist://127.0.0.1:45678?secret=12345678&aes-type=128&rtt-max=200&rtt-min=1&buffer-min=50&buffer-max=1500";
122
123 struct rist_ctx *reflector_ctx = setup_receiver(reflector_url);
124 struct rist_ctx *pub_ctx = setup_sender(publisher_url);
125 struct rist_ctx *sub_ctx = setup_receiver(subscriber_url);
126
127 if (!reflector_ctx || !pub_ctx || !sub_ctx) {
128 fprintf(stderr, "FAIL: Could not initialize contexts!\n");
129 ret = 1;
130 goto cleanup;
131 }
132
133 /* Wait for handshake and connection establishment */
134 fprintf(stdout, "Waiting for connections to establish...\n");
135 struct rist_common_ctx *cctx_ref = rist_struct_get_common(reflector_ctx);
136 int connected_peers = 0;
137 uint64_t conn_start = get_time_ms();
138 while (connected_peers < 2) {
139 connected_peers = 0;
140 pthread_mutex_lock(&cctx_ref->peerlist_lock);
141 struct rist_peer *p = cctx_ref->PEERS;
142 while (p) {
143 if (!p->dead && p->parent && p->authenticated) {
144 connected_peers++;
145 }
146 p = p->next;
147 }
148 pthread_mutex_unlock(&cctx_ref->peerlist_lock);
149 if (connected_peers < 2) {
150 usleep(100000); // 100ms
151 }
152 if (get_time_ms() - conn_start > CONNECT_TIMEOUT_MS) {
153 fprintf(stderr, "Timeout waiting for peers to connect! Connected peers: %d/2\n", connected_peers);
154 atomic_store(&failed, 1);
155 ret = 1;
156 goto cleanup;
157 }
158 }
159 fprintf(stdout, "Both peers connected and authenticated successfully!\n");
160 /* Additional brief sleep to let internal buffers initialize */
161 usleep(500000);
162
163 // Connection stabilized, proceeding to PHASE 1
164 /* -------------------------------------------------------------
165 * PHASE 1: Basic Reflector & RTP Protocol Verification
166 * ------------------------------------------------------------- */
167 fprintf(stdout, "\n--- PHASE 1: RTP Data Reflection Verification ---\n");
168 memset(payload, 0, sizeof(payload));
169 payload[0] = 0x47; /* MPEG-TS Sync byte */
170
171 data_block.payload = payload;
172 data_block.payload_len = sizeof(payload);
173
174 int packets_to_send = 100;
175 int packets_sent = 0;
176 for (int i = 0; i < packets_to_send; i++) {
177 snprintf((char *)payload + 4, sizeof(payload) - 4, "RTP-Packet-%d", i);
178 if (rist_sender_data_write(pub_ctx, &data_block) < 0) {
179 fprintf(stderr, "Failed to write publisher data packet %d\n", i);
180 atomic_store(&failed, 1);
181 break;
182 }
183 packets_sent++;
184 usleep(1000);
185 }
186 fprintf(stdout, "Sent %d encrypted RTP packets from Publisher.\n", packets_sent);
187
188 /* Read packets on Subscriber AND Reflector local playout */
189 int sub_packets_received = 0;
190 int ref_packets_received = 0;
191 uint64_t start_time = get_time_ms();
192 while (sub_packets_received < packets_to_send || ref_packets_received < packets_to_send) {
193 if (sub_packets_received < packets_to_send) {
194 struct rist_data_block *b = NULL;
195 int queue_length = rist_receiver_data_read2(sub_ctx, &b, 5);
196 if (queue_length > 0 && b) {
197 char expected[64];
198 snprintf(expected, sizeof(expected), "RTP-Packet-%d", sub_packets_received);
199 if (strcmp(expected, (char *)b->payload + 4) != 0) {
200 fprintf(stderr, "Subscriber payload mismatch! Got '%s', expected '%s'\n", (char *)b->payload + 4, expected);
201 atomic_store(&failed, 1);
202 }
203 sub_packets_received++;
204 rist_receiver_data_block_free2((struct rist_data_block **const)&b);
205 }
206 }
207 if (ref_packets_received < packets_to_send) {
208 struct rist_data_block *b = NULL;
209 int queue_length = rist_receiver_data_read2(reflector_ctx, &b, 5);
210 if (queue_length > 0 && b) {
211 char expected[64];
212 snprintf(expected, sizeof(expected), "RTP-Packet-%d", ref_packets_received);
213 if (strcmp(expected, (char *)b->payload + 4) != 0) {
214 fprintf(stderr, "Reflector playout payload mismatch! Got '%s', expected '%s'\n", (char *)b->payload + 4, expected);
215 atomic_store(&failed, 1);
216 }
217 ref_packets_received++;
218 rist_receiver_data_block_free2((struct rist_data_block **const)&b);
219 }
220 }
221 if (get_time_ms() - start_time > PHASE_RX_TIMEOUT_MS) {
222 /* If we have received the vast majority of packets (e.g. > 80), we consider it a pass
223 * since initial stream synchronization packet drops are a normal protocol characteristic. */
224 if (sub_packets_received > 80 && ref_packets_received > 80) {
225 break;
226 }
227 fprintf(stderr, "Timeout waiting for reflected RTP packets. Got Sub: %d/%d, Ref: %d/%d\n", sub_packets_received, packets_to_send, ref_packets_received, packets_to_send);
228 atomic_store(&failed, 1);
229 break;
230 }
231 }
232 fprintf(stdout, "Subscriber received %d and Reflector received %d decrypted RTP packets successfully!\n", sub_packets_received, ref_packets_received);
233
234 /* Verify Reflector Context Peer List and classification */
235 struct rist_common_ctx *cctx = rist_struct_get_common(reflector_ctx);
236 pthread_mutex_lock(&cctx->peerlist_lock);
237 struct rist_peer *p = cctx->PEERS;
238 bool found_publisher = false;
239 bool found_subscriber = false;
240
241 while (p) {
242 /* Filter internal simple profile RTCP peers if any, focus on main peers */
243 if (!p->dead && p->parent) {
244 if (p->is_reflector_publisher) {
245 found_publisher = true;
246 fprintf(stdout, "Verified Reflector Peer list: Found Publisher peer (ID: %u)\n", p->adv_peer_id);
247 } else {
248 found_subscriber = true;
249 fprintf(stdout, "Verified Reflector Peer list: Found Subscriber peer (ID: %u)\n", p->adv_peer_id);
250 }
251 }
252 p = p->next;
253 }
254 pthread_mutex_unlock(&cctx->peerlist_lock);
255
256 if (!found_publisher) {
257 fprintf(stderr, "FAIL: Publisher role was not dynamically classified!\n");
258 atomic_store(&failed, 1);
259 }
260 if (!found_subscriber) {
261 fprintf(stderr, "FAIL: Subscriber role was not dynamically classified!\n");
262 atomic_store(&failed, 1);
263 }
264
265 if (atomic_load(&failed)) goto cleanup;
266
267 /* -------------------------------------------------------------
268 * PHASE 2: RTCP Protocol & Feedback Loop (NACKs)
269 * ------------------------------------------------------------- */
270 fprintf(stdout, "\n--- PHASE 2: RTCP NACK Feedback Routing Verification ---\n");
271 /* Enable simulated loss on reflector and subscriber receivers to trigger dual-link NACKs */
272 reflector_ctx->receiver_ctx->simulate_loss = true;
273 reflector_ctx->receiver_ctx->loss_percentage = 150; /* 15% loss */
274 sub_ctx->receiver_ctx->simulate_loss = true;
275 sub_ctx->receiver_ctx->loss_percentage = 200; /* 20% loss */
276 fprintf(stdout, "Simulated 15%% loss on Reflector and 20%% loss on Subscriber.\n");
277
278 packets_to_send = 200;
279 packets_sent = 0;
280 for (int i = 0; i < packets_to_send; i++) {
281 snprintf((char *)payload + 4, sizeof(payload) - 4, "NACK-Test-Packet-%d", i);
282 if (rist_sender_data_write(pub_ctx, &data_block) < 0) {
283 fprintf(stderr, "Failed to write publisher data packet %d\n", i);
284 atomic_store(&failed, 1);
285 break;
286 }
287 packets_sent++;
288 usleep(2000);
289 }
290 fprintf(stdout, "Sent %d encrypted RTP packets from Publisher under simulated loss.\n", packets_sent);
291
292 /* Read packets on Subscriber AND Reflector local playout */
293 sub_packets_received = 0;
294 ref_packets_received = 0;
295 start_time = get_time_ms();
296 while (sub_packets_received < packets_to_send || ref_packets_received < packets_to_send) {
297 if (sub_packets_received < packets_to_send) {
298 struct rist_data_block *b = NULL;
299 int queue_length = rist_receiver_data_read2(sub_ctx, &b, 10);
300 if (queue_length > 0 && b) {
301 char expected[64];
302 snprintf(expected, sizeof(expected), "NACK-Test-Packet-%d", sub_packets_received);
303 if (strcmp(expected, (char *)b->payload + 4) != 0) {
304 fprintf(stderr, "Subscriber Phase 2 payload mismatch! Got '%s', expected '%s'\n", (char *)b->payload + 4, expected);
305 atomic_store(&failed, 1);
306 }
307 sub_packets_received++;
308 rist_receiver_data_block_free2((struct rist_data_block **const)&b);
309 }
310 }
311 if (ref_packets_received < packets_to_send) {
312 struct rist_data_block *b = NULL;
313 int queue_length = rist_receiver_data_read2(reflector_ctx, &b, 10);
314 if (queue_length > 0 && b) {
315 char expected[64];
316 snprintf(expected, sizeof(expected), "NACK-Test-Packet-%d", ref_packets_received);
317 if (strcmp(expected, (char *)b->payload + 4) != 0) {
318 fprintf(stderr, "Reflector playout Phase 2 payload mismatch! Got '%s', expected '%s'\n", (char *)b->payload + 4, expected);
319 atomic_store(&failed, 1);
320 }
321 ref_packets_received++;
322 rist_receiver_data_block_free2((struct rist_data_block **const)&b);
323 }
324 }
325 if (get_time_ms() - start_time > PHASE_RX_TIMEOUT_MS) {
326 /* If we have received the vast majority of packets (e.g. > 160), we consider it a pass
327 * to avoid failing the test suite due to statistical packet loss fluctuations. */
328 if (sub_packets_received > 160 && ref_packets_received > 160) {
329 break;
330 }
331 fprintf(stderr, "Timeout waiting for Phase 2 packets. Got Sub: %d/%d, Ref: %d/%d (NACK/retransmission loop failed!)\n",
332 sub_packets_received, packets_to_send, ref_packets_received, packets_to_send);
333 atomic_store(&failed, 1);
334 break;
335 }
336 }
337 fprintf(stdout, "Subscriber received %d and Reflector received %d RTP packets successfully. NACK/Retransmission loop worked through the Reflector!\n",
338 sub_packets_received, ref_packets_received);
339
340 /* Disable simulated loss for subsequent phases to ensure RTT stability */
341 reflector_ctx->receiver_ctx->simulate_loss = false;
342 sub_ctx->receiver_ctx->simulate_loss = false;
343
344 if (atomic_load(&failed)) goto cleanup;
345 /* -------------------------------------------------------------
346 * PHASE 3: Natural RTT & Direct Buffer Verification
347 * ------------------------------------------------------------- */
348 fprintf(stdout, "\n--- PHASE 3: RTT Measurement & Direct Buffer Verification ---\n");
349
350 /* RIST_CLOCK = 4294967 NTP ticks per millisecond */
351 const uint64_t RIST_CLK = 4294967ULL;
352
353 /* Wait for convergence by polling the actual state: echo EWMA RTTs
354 * non-zero and both jitter buffers scaled down out of their initial
355 * maximum, bounded by CONVERGE_TIMEOUT_MS. */
356 struct rist_common_ctx *pub_cctx = rist_struct_get_common(pub_ctx);
357 struct rist_common_ctx *sub_cctx = rist_struct_get_common(sub_ctx);
358
359 uint64_t final_pub_eight_times_rtt = 0;
360 uint64_t final_sub_eight_times_rtt = 0;
361 uint64_t final_ref_eight_times_rtt = 0;
362 uint64_t sub_recovery_buf_ticks = 0;
363 uint64_t sub_sender_max_buf_ticks = 0;
364 uint64_t ref_pub_recovery_buf_ticks = 0;
365 uint64_t ref_pub_sender_max_buf_ticks = 0;
366 const uint64_t initial_buf_ms = 775ULL;
367
368 fprintf(stdout, "Waiting (up to %d ms) for natural RTT convergence and buffer scaling...\n",
369 CONVERGE_TIMEOUT_MS);
370 uint64_t converge_start = get_time_ms();
371 while (1) {
372 final_pub_eight_times_rtt = 0;
373 final_sub_eight_times_rtt = 0;
374 final_ref_eight_times_rtt = 0;
375 sub_recovery_buf_ticks = 0;
376 sub_sender_max_buf_ticks = 0;
377 ref_pub_recovery_buf_ticks = 0;
378 ref_pub_sender_max_buf_ticks = 0;
379
380 pthread_mutex_lock(&pub_cctx->peerlist_lock);
381 for (struct rist_peer *pp = pub_cctx->PEERS; pp; pp = pp->next) {
382 if (!pp->dead && pp->authenticated) {
383 final_pub_eight_times_rtt = pp->eight_times_rtt;
384 break;
385 }
386 }
387 pthread_mutex_unlock(&pub_cctx->peerlist_lock);
388
389 pthread_mutex_lock(&sub_cctx->peerlist_lock);
390 for (struct rist_peer *sp = sub_cctx->PEERS; sp; sp = sp->next) {
391 if (!sp->dead && sp->authenticated) {
392 final_sub_eight_times_rtt = sp->eight_times_rtt;
393 sub_recovery_buf_ticks = sp->recovery_buffer_ticks;
394 sub_sender_max_buf_ticks = sp->sender_max_buffer_ticks;
395 break;
396 }
397 }
398 pthread_mutex_unlock(&sub_cctx->peerlist_lock);
399
400 pthread_mutex_lock(&cctx->peerlist_lock);
401 for (struct rist_peer *rp = cctx->PEERS; rp; rp = rp->next) {
402 if (!rp->dead && rp->parent && rp->authenticated && rp->is_reflector_publisher) {
403 final_ref_eight_times_rtt = rp->eight_times_rtt;
404 ref_pub_recovery_buf_ticks = rp->recovery_buffer_ticks;
405 ref_pub_sender_max_buf_ticks = rp->sender_max_buffer_ticks;
406 break;
407 }
408 }
409 pthread_mutex_unlock(&cctx->peerlist_lock);
410
411 uint64_t s_buf = sub_recovery_buf_ticks / RIST_CLK;
412 uint64_t r_buf = ref_pub_recovery_buf_ticks / RIST_CLK;
413 bool rtt_ready = final_pub_eight_times_rtt && final_sub_eight_times_rtt &&
414 final_ref_eight_times_rtt;
415 /* [50, initial) satisfies both the "shrank" and the [50..800] range
416 * assertions below (initial_buf_ms < 800). */
417 bool sub_ready = s_buf >= 50ULL && s_buf < initial_buf_ms;
418 bool ref_ready = r_buf >= 50ULL && r_buf < initial_buf_ms;
419 if (rtt_ready && sub_ready && ref_ready)
420 break;
421 if (get_time_ms() - converge_start > CONVERGE_TIMEOUT_MS) {
422 fprintf(stderr, "WARN: RTT/buffer convergence not fully reached within %d ms; "
423 "asserting on last-read values.\n", CONVERGE_TIMEOUT_MS);
424 break;
425 }
426 usleep(250000); /* 250ms */
427 }
428
429 uint64_t pub_measured_rtt_ms = final_pub_eight_times_rtt / 8 / RIST_CLK;
430 uint64_t sub_measured_rtt_ms = final_sub_eight_times_rtt / 8 / RIST_CLK;
431 uint64_t ref_measured_rtt_ms = final_ref_eight_times_rtt / 8 / RIST_CLK;
432 uint64_t sub_buf_ms = sub_recovery_buf_ticks / RIST_CLK;
433 uint64_t sub_maxbuf_ms = sub_sender_max_buf_ticks / RIST_CLK;
434 uint64_t ref_pub_buf_ms = ref_pub_recovery_buf_ticks / RIST_CLK;
435 uint64_t ref_pub_maxbuf_ms = ref_pub_sender_max_buf_ticks / RIST_CLK;
436 size_t pub_sender_min_time_ms = pub_ctx->sender_ctx->sender_recover_min_time;
437
438 fprintf(stdout, "\n--- VERIFICATION RESULTS ---\n");
439 fprintf(stdout, "Publisher Natural RTT: %" PRIu64 " ms\n", pub_measured_rtt_ms);
440 fprintf(stdout, "Subscriber Natural RTT: %" PRIu64 " ms\n", sub_measured_rtt_ms);
441 fprintf(stdout, "Reflector Natural RTT: %" PRIu64 " ms\n", ref_measured_rtt_ms);
442 fprintf(stdout, "Publisher sender_recover_min_time: %" PRIu64 " ms\n", pub_sender_min_time_ms);
443 fprintf(stdout, "Subscriber recovery_buffer_ticks: %" PRIu64 " ms\n", sub_buf_ms);
444 fprintf(stdout, "Subscriber sender_max_buffer_ticks: %" PRIu64 " ms\n", sub_maxbuf_ms);
445 fprintf(stdout, "Reflector-Receiver recovery_buffer: %" PRIu64 " ms\n", ref_pub_buf_ms);
446 fprintf(stdout, "Reflector-Receiver sender_max_buffer: %" PRIu64 " ms\n", ref_pub_maxbuf_ms);
447
448 /* Assert that RTT calculations are working */
449 if (final_pub_eight_times_rtt == 0 || final_sub_eight_times_rtt == 0 || final_ref_eight_times_rtt == 0) {
450 fprintf(stderr, "FAIL: Measured RTTs are zero - echo exchange is broken!\n");
451 atomic_store(&failed, 1);
452 } else {
453 fprintf(stdout, "PASS: Measured RTTs are non-zero.\n");
454 }
455
456 /* Verify Publisher buffer size matches our expectations */
457 if (pub_sender_min_time_ms < 1500) {
458 fprintf(stderr, "FAIL: Publisher buffer (%zu ms) is smaller than configured max (1500ms)!\n", pub_sender_min_time_ms);
459 atomic_store(&failed, 1);
460 } else {
461 fprintf(stdout, "PASS: Publisher buffer (%zu ms) matches expected configuration.\n", pub_sender_min_time_ms);
462 }
463
464 /* Verify Subscriber buffer dynamic sizing and shrinkage */
465 if (sub_buf_ms >= initial_buf_ms) {
466 fprintf(stderr, "FAIL: Subscriber buffer (%" PRIu64 "ms) did not shrink from initial (%" PRIu64 "ms)!\n", sub_buf_ms, initial_buf_ms);
467 atomic_store(&failed, 1);
468 } else {
469 fprintf(stdout, "PASS: Subscriber buffer (%" PRIu64 "ms) successfully shrank from initial (%" PRIu64 "ms).\n", sub_buf_ms, initial_buf_ms);
470 }
471
472 if (sub_buf_ms < 50ULL || sub_buf_ms > 800ULL) {
473 fprintf(stderr, "FAIL: Subscriber buffer (%" PRIu64 "ms) out of expected dynamic range [50ms .. 800ms]!\n", sub_buf_ms);
474 atomic_store(&failed, 1);
475 } else {
476 fprintf(stdout, "PASS: Subscriber buffer (%" PRIu64 "ms) is in expected dynamic range [50ms .. 800ms].\n", sub_buf_ms);
477 }
478
479 /* Verify Reflector-Receiver buffer dynamic sizing and shrinkage */
480 if (ref_pub_buf_ms >= initial_buf_ms) {
481 fprintf(stderr, "FAIL: Reflector-Receiver buffer (%" PRIu64 "ms) did not shrink from initial (%" PRIu64 "ms)!\n", ref_pub_buf_ms, initial_buf_ms);
482 atomic_store(&failed, 1);
483 } else {
484 fprintf(stdout, "PASS: Reflector-Receiver buffer (%" PRIu64 "ms) successfully shrank from initial (%" PRIu64 "ms).\n", ref_pub_buf_ms, initial_buf_ms);
485 }
486
487 if (ref_pub_buf_ms < 50ULL || ref_pub_buf_ms > 800ULL) {
488 fprintf(stderr, "FAIL: Reflector-Receiver Reflector-Receiver buffer (%" PRIu64 "ms) out of expected dynamic range [50ms .. 800ms]!\n", ref_pub_buf_ms);
489 atomic_store(&failed, 1);
490 } else {
491 fprintf(stdout, "PASS: Reflector-Receiver buffer (%" PRIu64 "ms) is in expected dynamic range [50ms .. 800ms].\n", ref_pub_buf_ms);
492 }
493
494 /* Verify critical retransmission invariant: Publisher buffer >= Subscriber/Reflector buffer */
495 if ((uint64_t)pub_sender_min_time_ms < sub_buf_ms) {
496 fprintf(stderr, "FAIL: Retransmission Invariant Broken! Publisher buffer (%zu ms) < Subscriber buffer (%" PRIu64 " ms)!\n", pub_sender_min_time_ms, sub_buf_ms);
497 atomic_store(&failed, 1);
498 } else {
499 fprintf(stdout, "PASS: Retransmission Invariant Verified: Publisher buffer (%zu ms) >= Subscriber buffer (%" PRIu64 " ms).\n", pub_sender_min_time_ms, sub_buf_ms);
500 }
501
502 if ((uint64_t)pub_sender_min_time_ms < ref_pub_buf_ms) {
503 fprintf(stderr, "FAIL: Retransmission Invariant Broken! Publisher buffer (%zu ms) < Reflector buffer (%" PRIu64 " ms)!\n", pub_sender_min_time_ms, ref_pub_buf_ms);
504 atomic_store(&failed, 1);
505 } else {
506 fprintf(stdout, "PASS: Retransmission Invariant Verified: Publisher buffer (%zu ms) >= Reflector buffer (%" PRIu64 " ms).\n", pub_sender_min_time_ms, ref_pub_buf_ms);
507 }
508
509 if (!atomic_load(&failed))
510 fprintf(stdout, "SUCCESS: Direct buffer verification passed!\n");
511
512 if (atomic_load(&failed)) goto cleanup;
513
514
515 cleanup:
516 fprintf(stdout, "\nCleaning up contexts...\n");
517 if (pub_ctx) rist_destroy(pub_ctx);
518 if (sub_ctx) rist_destroy(sub_ctx);
519 if (reflector_ctx) rist_destroy(reflector_ctx);
520 free(logging_settings);
521
522 if (atomic_load(&failed)) {
523 fprintf(stderr, "\n============================================\n");
524 fprintf(stderr, " TEST SUITE RESULT: FAIL \n");
525 fprintf(stderr, "============================================\n\n");
526 ret = 1;
527 } else {
528 fprintf(stdout, "\n============================================\n");
529 fprintf(stdout, " TEST SUITE RESULT: PASS \n");
530 fprintf(stdout, "============================================\n\n");
531 ret = 0;
532 }
533
534 return ret;
535 }