Debian Codegraph @

test/rist/test_sender_restart.c

raw symbols SCIP
1 /* librist. Copyright © 2024 SipRadius LLC. All right reserved.
2 * Author: Sergio Ammirata, Ph.D. <[email protected]>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 *
6 * Test for sender restart bug fix.
7 * Verifies that creating, using, destroying, and recreating a sender
8 * does not cause "Sender queue is full" errors.
9 */
10
11 #include "librist/librist.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdatomic.h>
16
17 #ifdef _WIN32
18 #include <windows.h>
19 #define usleep(x) Sleep((x)/1000)
20 #else
21 #include <unistd.h>
22 #endif
23
24 static struct rist_logging_settings *logging_settings = NULL;
25 static atomic_int error_count;
26
27 static int log_callback(void *arg, enum rist_log_level level, const char *msg) {
28 (void)arg;
29 fprintf(stdout, "[%s] %s",
30 level <= RIST_LOG_ERROR ? "ERROR" : "INFO", msg);
31
32 if (level <= RIST_LOG_ERROR) {
33 if (strstr(msg, "queue is full") != NULL) {
34 fprintf(stderr, "DETECTED: Sender queue is full error!\n");
35 atomic_fetch_add(&error_count, 1);
36 }
37 }
38 return 0;
39 }
40
41 static int create_and_use_sender(int iteration, const char *url) {
42 struct rist_ctx *ctx = NULL;
43 struct rist_peer *peer = NULL;
44 int ret = 0;
45
46 fprintf(stdout, "=== Iteration %d: Creating sender ===\n", iteration);
47
48 if (rist_sender_create(&ctx, RIST_PROFILE_MAIN, 0, logging_settings) != 0) {
49 fprintf(stderr, "Failed to create sender context\n");
50 return -1;
51 }
52
53 struct rist_peer_config *peer_config = NULL;
54 if (rist_parse_address2(url, (void *)&peer_config) != 0) {
55 fprintf(stderr, "Failed to parse peer config\n");
56 rist_destroy(ctx);
57 return -1;
58 }
59
60 if (rist_peer_create(ctx, &peer, peer_config) != 0) {
61 fprintf(stderr, "Failed to create peer\n");
62 free((void *)peer_config);
63 rist_destroy(ctx);
64 return -1;
65 }
66 free((void *)peer_config);
67
68 if (rist_start(ctx) != 0) {
69 fprintf(stderr, "Failed to start sender\n");
70 rist_destroy(ctx);
71 return -1;
72 }
73
74 fprintf(stdout, "Iteration %d: Sending packets...\n", iteration);
75
76 /* Send some packets to exercise the queue */
77 uint8_t packet[1316];
78 memset(packet, 0x47, sizeof(packet));
79
80 struct rist_data_block data_block = {
81 .payload = packet,
82 .payload_len = sizeof(packet),
83 .virt_src_port = 1234,
84 .virt_dst_port = 1234,
85 };
86
87 int errors_before = atomic_load(&error_count);
88
89 for (int i = 0; i < 1000; i++) {
90 snprintf((char *)packet + 4, sizeof(packet) - 4,
91 "Test packet iteration=%d seq=%d", iteration, i);
92
93 int send_ret = rist_sender_data_write(ctx, &data_block);
94 if (send_ret < 0) {
95 fprintf(stderr, "Iteration %d: Failed to enqueue packet %d (ret=%d)\n",
96 iteration, i, send_ret);
97 ret = -1;
98 break;
99 }
100
101 /* Small delay to let the sender process */
102 if (i % 100 == 0) {
103 usleep(1000);
104 }
105 }
106
107 int errors_after = atomic_load(&error_count);
108 if (errors_after > errors_before) {
109 fprintf(stderr, "Iteration %d: Queue full errors detected!\n", iteration);
110 ret = -1;
111 }
112
113 /* Let the sender flush */
114 usleep(100000);
115
116 fprintf(stdout, "Iteration %d: Destroying sender...\n", iteration);
117 rist_destroy(ctx);
118
119 /* Brief pause between iterations */
120 usleep(50000);
121
122 return ret;
123 }
124
125 int main(int argc, char *argv[]) {
126 int iterations = 5;
127 int ret = 0;
128
129 if (argc > 1) {
130 iterations = atoi(argv[1]);
131 if (iterations < 1) iterations = 5;
132 }
133
134 atomic_init(&error_count, 0);
135
136 if (rist_logging_set(&logging_settings, RIST_LOG_DEBUG, log_callback,
137 NULL, NULL, stderr) != 0) {
138 fprintf(stderr, "Failed to setup logging\n");
139 return 99;
140 }
141
142 fprintf(stdout, "Testing sender restart with %d iterations\n", iterations);
143 fprintf(stdout, "This test verifies that stopping and restarting a sender\n");
144 fprintf(stdout, "does not cause 'Sender queue is full' errors.\n\n");
145
146 /* Use a listener URL so we don't need a receiver */
147 const char *url = "rist://@127.0.0.1:19999?rtt-max=10&rtt-min=1";
148
149 for (int i = 1; i <= iterations; i++) {
150 if (create_and_use_sender(i, url) != 0) {
151 fprintf(stderr, "FAIL: Iteration %d failed\n", i);
152 ret = 1;
153 break;
154 }
155 fprintf(stdout, "Iteration %d: OK\n\n", i);
156 }
157
158 int total_errors = atomic_load(&error_count);
159 if (total_errors > 0) {
160 fprintf(stderr, "\nFAIL: %d 'queue full' errors detected\n", total_errors);
161 ret = 1;
162 }
163
164 free(logging_settings);
165
166 if (ret == 0) {
167 fprintf(stdout, "\nPASS: Sender restart test completed successfully\n");
168 }
169
170 return ret;
171 }