Debian Codegraph @

src/libsystemd/sd-event/test-event-future.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <unistd.h>
5
6 #include "sd-event.h"
7 #include "sd-future.h"
8
9 #include "fd-util.h"
10 #include "tests.h"
11 #include "time-util.h"
12
13 static int timer_callback(sd_event_source *s, uint64_t usec, void *userdata) {
14 int *count = ASSERT_PTR(userdata);
15 int r;
16
17 (*count)++;
18
19 r = sd_event_source_set_time_relative(s, 5 * USEC_PER_MSEC);
20 if (r < 0)
21 return r;
22
23 if (sd_fiber_is_running() && *count >= 3)
24 return sd_event_exit(sd_event_source_get_event(s), 0);
25
26 return 0;
27 }
28
29 static int event_run_fiber_func(void *userdata) {
30 _cleanup_(sd_event_unrefp) sd_event *inner = NULL;
31 _cleanup_(sd_event_source_unrefp) sd_event_source *inner_timer = NULL;
32 int r;
33
34 /* Create inner event loop from within the fiber */
35 r = sd_event_new(&inner);
36 if (r < 0)
37 return r;
38
39 /* Add a timer to the inner event loop that fires every 5ms */
40 r = sd_event_add_time_relative(inner, &inner_timer, CLOCK_MONOTONIC,
41 5 * USEC_PER_MSEC, 0, timer_callback,
42 userdata);
43 if (r < 0)
44 return r;
45
46 r = sd_event_source_set_enabled(inner_timer, SD_EVENT_ON);
47 if (r < 0)
48 return r;
49
50 return sd_event_loop(inner);
51 }
52
53 TEST(sd_event_loop_fiber) {
54 /* Create outer event loop for the fiber scheduler */
55 _cleanup_(sd_event_unrefp) sd_event *outer = NULL;
56 ASSERT_OK(sd_event_new(&outer));
57 ASSERT_OK(sd_event_set_exit_on_idle(outer, true));
58
59 /* Add a timer to the outer event loop that fires every 5ms */
60 _cleanup_(sd_event_source_unrefp) sd_event_source *outer_timer = NULL;
61 int outer_timer_count = 0;
62 ASSERT_OK(sd_event_add_time_relative(outer, &outer_timer, CLOCK_MONOTONIC,
63 5 * USEC_PER_MSEC, 0, timer_callback,
64 &outer_timer_count));
65
66 /* Create a fiber that will create and run the inner event loop */
67 _cleanup_(sd_future_unrefp) sd_future *f = NULL;
68 int inner_timer_count = 0;
69 ASSERT_OK(sd_fiber_new(outer, "event-runner", event_run_fiber_func, &inner_timer_count, /* destroy= */ NULL, &f));
70
71 /* Run the outer event loop */
72 ASSERT_OK(sd_event_loop(outer));
73
74 /* Fiber should have completed successfully */
75 ASSERT_OK(sd_future_result(f));
76
77 /* Both timers should have fired at least once */
78 ASSERT_EQ(inner_timer_count, 3);
79 ASSERT_GT(outer_timer_count, 0);
80 }
81
82 static int event_run_fiber_timeout_func(void *userdata) {
83 _cleanup_(sd_event_unrefp) sd_event *inner = NULL;
84 int r;
85
86 /* Create inner event loop from within the fiber */
87 r = sd_event_new(&inner);
88 if (r < 0)
89 return r;
90
91 /* Run with a short timeout - should timeout since there are no events */
92 return sd_event_run(inner, 10 * USEC_PER_MSEC);
93 }
94
95 TEST(sd_event_run_fiber_timeout) {
96 /* Create outer event loop for the fiber scheduler */
97 _cleanup_(sd_event_unrefp) sd_event *outer = NULL;
98 ASSERT_OK(sd_event_new(&outer));
99 ASSERT_OK(sd_event_set_exit_on_idle(outer, true));
100
101 /* Create a fiber that will run sd_event_run() with timeout */
102 _cleanup_(sd_future_unrefp) sd_future *f = NULL;
103 ASSERT_OK(sd_fiber_new(outer, "event-timeout", event_run_fiber_timeout_func, NULL, /* destroy= */ NULL, &f));
104
105 /* Run the outer event loop */
106 ASSERT_OK(sd_event_loop(outer));
107
108 /* Fiber should have completed successfully (timeout returns 0) */
109 ASSERT_OK_ZERO(sd_future_result(f));
110 }
111
112 /* Test: sd_event_run() with zero timeout returns immediately */
113 static int sd_event_run_zero_timeout_fiber(void *userdata) {
114 _cleanup_(sd_event_unrefp) sd_event *inner = NULL;
115 int r;
116
117 r = sd_event_new(&inner);
118 if (r < 0)
119 return r;
120
121 /* With zero timeout on an empty event loop, should return 0 immediately */
122 r = sd_event_run(inner, 0);
123 if (r != 0)
124 return r < 0 ? r : -EIO;
125
126 return 0;
127 }
128
129 TEST(sd_event_run_zero_timeout) {
130 _cleanup_(sd_event_unrefp) sd_event *outer = NULL;
131 ASSERT_OK(sd_event_new(&outer));
132 ASSERT_OK(sd_event_set_exit_on_idle(outer, true));
133
134 _cleanup_(sd_future_unrefp) sd_future *f = NULL;
135 ASSERT_OK(sd_fiber_new(outer, "run-suspend-zero", sd_event_run_zero_timeout_fiber, NULL, /* destroy= */ NULL, &f));
136
137 ASSERT_OK(sd_event_loop(outer));
138 ASSERT_OK_ZERO(sd_future_result(f));
139 }
140
141 /* Test: sd_event_run() dispatches immediately pending IO */
142 static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
143 int *counter = ASSERT_PTR(userdata);
144 char buf[64];
145
146 (*counter)++;
147
148 /* Drain the fd */
149 (void) read(fd, buf, sizeof(buf));
150
151 return sd_event_exit(sd_event_source_get_event(s), 0);
152 }
153
154 static int sd_event_run_immediate_fiber(void *userdata) {
155 int *pipefd = ASSERT_PTR(userdata);
156 _cleanup_(sd_event_unrefp) sd_event *inner = NULL;
157 _cleanup_(sd_event_source_unrefp) sd_event_source *source = NULL;
158 int counter = 0, r;
159
160 r = sd_event_new(&inner);
161 if (r < 0)
162 return r;
163
164 /* Add IO source watching the read end of the pipe */
165 r = sd_event_add_io(inner, &source, pipefd[0], EPOLLIN, io_callback, &counter);
166 if (r < 0)
167 return r;
168
169 /* Data is already available on the pipe (written before fiber started), so
170 * sd_event_run() should dispatch immediately without suspending */
171 r = sd_event_run(inner, USEC_INFINITY);
172 if (r < 0)
173 return r;
174
175 /* The IO callback should have fired */
176 if (counter != 1)
177 return -EIO;
178
179 return 0;
180 }
181
182 TEST(sd_event_run_immediate) {
183 _cleanup_(sd_event_unrefp) sd_event *outer = NULL;
184 ASSERT_OK(sd_event_new(&outer));
185 ASSERT_OK(sd_event_set_exit_on_idle(outer, true));
186
187 _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
188 ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK));
189
190 /* Write data before starting the fiber so it's immediately available */
191 ASSERT_OK_EQ_ERRNO(write(pipefd[1], "X", 1), 1);
192
193 _cleanup_(sd_future_unrefp) sd_future *f = NULL;
194 ASSERT_OK(sd_fiber_new(outer, "run-suspend-immediate", sd_event_run_immediate_fiber, pipefd, /* destroy= */ NULL, &f));
195
196 ASSERT_OK(sd_event_loop(outer));
197 ASSERT_OK_ZERO(sd_future_result(f));
198 }
199
200 /* Test: sd_event_run() with IO arriving during suspension */
201 static int sd_event_run_io_fiber(void *userdata) {
202 int *pipefd = ASSERT_PTR(userdata);
203 _cleanup_(sd_event_unrefp) sd_event *inner = NULL;
204 _cleanup_(sd_event_source_unrefp) sd_event_source *source = NULL;
205 int counter = 0, r;
206
207 r = sd_event_new(&inner);
208 if (r < 0)
209 return r;
210
211 r = sd_event_add_io(inner, &source, pipefd[0], EPOLLIN, io_callback, &counter);
212 if (r < 0)
213 return r;
214
215 /* No data available yet, so this will suspend the fiber until IO arrives */
216 r = sd_event_run(inner, USEC_INFINITY);
217 if (r < 0)
218 return r;
219
220 if (counter != 1)
221 return -EIO;
222
223 return 0;
224 }
225
226 TEST(sd_event_run_io) {
227 _cleanup_(sd_event_unrefp) sd_event *outer = NULL;
228 ASSERT_OK(sd_event_new(&outer));
229 ASSERT_OK(sd_event_set_exit_on_idle(outer, true));
230
231 _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
232 ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK));
233
234 _cleanup_(sd_future_unrefp) sd_future *f = NULL;
235 ASSERT_OK(sd_fiber_new(outer, "run-suspend-io", sd_event_run_io_fiber, pipefd, /* destroy= */ NULL, &f));
236
237 /* First iteration: fiber runs, adds IO source, suspends because no data */
238 ASSERT_OK_POSITIVE(sd_event_run(outer, 0));
239
240 /* Write data to the pipe to wake the inner event loop */
241 ASSERT_OK_EQ_ERRNO(write(pipefd[1], "Y", 1), 1);
242
243 /* Complete: fiber resumes, dispatches IO, finishes */
244 ASSERT_OK(sd_event_loop(outer));
245 ASSERT_OK_ZERO(sd_future_result(f));
246 }
247
248 /* Test: event_run called in a loop keeps event loop state consistent.
249 * This is a regression test for a bug where error paths after sd_event_prepare()
250 * could leave the inner event loop stuck in SD_EVENT_ARMED state. */
251 static int sd_event_run_loop_fiber(void *userdata) {
252 int *pipefd = ASSERT_PTR(userdata);
253 _cleanup_(sd_event_unrefp) sd_event *inner = NULL;
254 _cleanup_(sd_event_source_unrefp) sd_event_source *source = NULL;
255 int counter = 0, r;
256
257 r = sd_event_new(&inner);
258 if (r < 0)
259 return r;
260
261 r = sd_event_add_io(inner, &source, pipefd[0], EPOLLIN, io_callback, &counter);
262 if (r < 0)
263 return r;
264
265 /* Call sd_event_run() multiple times with short timeouts.
266 * Each call should leave the inner event loop in a clean state for the next call. */
267 for (int i = 0; i < 5; i++) {
268 r = sd_event_run(inner, 10 * USEC_PER_MSEC);
269 if (r < 0)
270 return r;
271 if (r > 0)
272 break;
273 }
274
275 /* After multiple timeouts, the event loop should still be usable.
276 * Write data and do one more run to verify. */
277 if (counter == 0) {
278 /* Data wasn't written yet, do a final run with longer timeout */
279 r = sd_event_run(inner, USEC_INFINITY);
280 if (r < 0)
281 return r;
282 }
283
284 if (counter != 1)
285 return -EIO;
286
287 return 0;
288 }
289
290 TEST(sd_event_run_loop) {
291 _cleanup_(sd_event_unrefp) sd_event *outer = NULL;
292 ASSERT_OK(sd_event_new(&outer));
293 ASSERT_OK(sd_event_set_exit_on_idle(outer, true));
294
295 _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
296 ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK));
297
298 _cleanup_(sd_future_unrefp) sd_future *f = NULL;
299 ASSERT_OK(sd_fiber_new(outer, "run-suspend-loop", sd_event_run_loop_fiber, pipefd, /* destroy= */ NULL, &f));
300
301 /* Let the fiber run through a few timeout iterations */
302 for (int i = 0; i < 10; i++)
303 ASSERT_OK(sd_event_run(outer, 50 * USEC_PER_MSEC));
304
305 /* Write data to unblock the fiber */
306 ASSERT_OK_EQ_ERRNO(write(pipefd[1], "Z", 1), 1);
307
308 ASSERT_OK(sd_event_loop(outer));
309 ASSERT_OK_ZERO(sd_future_result(f));
310 }
311
312 /* Test: sd_event_run() with an inner timer that fires during suspension */
313 static int inner_timer_handler(sd_event_source *s, uint64_t usec, void *userdata) {
314 int *counter = ASSERT_PTR(userdata);
315 (*counter)++;
316 return sd_event_exit(sd_event_source_get_event(s), 0);
317 }
318
319 static int sd_event_run_timer_fiber(void *userdata) {
320 _cleanup_(sd_event_unrefp) sd_event *inner = NULL;
321 _cleanup_(sd_event_source_unrefp) sd_event_source *source = NULL;
322 int counter = 0, r;
323
324 r = sd_event_new(&inner);
325 if (r < 0)
326 return r;
327
328 /* Add a timer that fires after 10ms */
329 r = sd_event_add_time_relative(inner, &source, CLOCK_MONOTONIC,
330 10 * USEC_PER_MSEC, 0, inner_timer_handler,
331 &counter);
332 if (r < 0)
333 return r;
334
335 /* Should suspend, then resume when the timer fires */
336 r = sd_event_run(inner, USEC_INFINITY);
337 if (r < 0)
338 return r;
339
340 if (counter != 1)
341 return -EIO;
342
343 return 0;
344 }
345
346 TEST(sd_event_run_timer) {
347 _cleanup_(sd_event_unrefp) sd_event *outer = NULL;
348 ASSERT_OK(sd_event_new(&outer));
349 ASSERT_OK(sd_event_set_exit_on_idle(outer, true));
350
351 _cleanup_(sd_future_unrefp) sd_future *f = NULL;
352 ASSERT_OK(sd_fiber_new(outer, "run-suspend-timer", sd_event_run_timer_fiber, NULL, /* destroy= */ NULL, &f));
353
354 ASSERT_OK(sd_event_loop(outer));
355 ASSERT_OK_ZERO(sd_future_result(f));
356 }
357
358 DEFINE_TEST_MAIN(LOG_DEBUG);