Debian Codegraph @

src/libsystemd/sd-future/test-fiber-ops.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <poll.h>
5 #include <unistd.h>
6
7 #include "sd-event.h"
8 #include "sd-future.h"
9
10 #include "alloc-util.h"
11 #include "cleanup-util.h"
12 #include "fd-util.h"
13 #include "io-util.h"
14 #include "pidref.h"
15 #include "process-util.h"
16 #include "tests.h"
17 #include "time-util.h"
18
19 /* Test: wait_for_terminate basic functionality */
20 static int wait_simple_fiber(void *userdata) {
21 _cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
22 siginfo_t si;
23 int r;
24
25 /* Fork a child that exits immediately */
26 r = pidref_safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_LOG, &pidref);
27 if (r < 0)
28 return r;
29
30 if (r == 0)
31 _exit(42);
32
33 /* Parent - wait for child */
34 r = pidref_wait_for_terminate(&pidref, &si);
35 if (r < 0)
36 return r;
37
38 pidref_done(&pidref);
39
40 /* Verify child exited with status 42 */
41 if (si.si_code != CLD_EXITED || si.si_status != 42)
42 return -EIO;
43
44 return 0;
45 }
46
47 TEST(wait_for_terminate_fiber_basic) {
48 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
49 ASSERT_OK(sd_event_new(&e));
50 ASSERT_OK(sd_event_set_exit_on_idle(e, true));
51
52 _cleanup_(sd_future_unrefp) sd_future *f = NULL;
53 ASSERT_OK(sd_fiber_new(e, "wait-simple", wait_simple_fiber, NULL, /* destroy= */ NULL, &f));
54
55 ASSERT_OK(sd_event_loop(e));
56 ASSERT_OK(sd_future_result(f));
57 }
58
59 /* Test: wait_for_terminate with multiple children */
60 static int wait_multiple_fiber(void *userdata) {
61 PidRef pidrefs[3] = { PIDREF_NULL, PIDREF_NULL, PIDREF_NULL };
62 siginfo_t si;
63 int r;
64
65 /* Fork three children with different exit codes */
66 for (size_t i = 0; i < 3; i++) {
67 r = pidref_safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_LOG, &pidrefs[i]);
68 if (r < 0)
69 goto cleanup;
70
71 if (r == 0)
72 /* Child process */
73 _exit(10 + i);
74 }
75
76 /* Wait for all three in order */
77 for (size_t i = 0; i < 3; i++) {
78 r = pidref_wait_for_terminate(&pidrefs[i], &si);
79 if (r < 0)
80 goto cleanup;
81
82 pidref_done(&pidrefs[i]);
83
84 if (si.si_code != CLD_EXITED || si.si_status != (int) (10 + i)) {
85 r = -EIO;
86 goto cleanup;
87 }
88 }
89
90 return 0;
91
92 cleanup:
93 for (size_t i = 0; i < 3; i++)
94 pidref_done(&pidrefs[i]);
95
96 return r;
97 }
98
99 TEST(wait_for_terminate_fiber_multiple) {
100 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
101 ASSERT_OK(sd_event_new(&e));
102 ASSERT_OK(sd_event_set_exit_on_idle(e, true));
103
104 _cleanup_(sd_future_unrefp) sd_future *f = NULL;
105 ASSERT_OK(sd_fiber_new(e, "wait-multiple", wait_multiple_fiber, NULL, /* destroy= */ NULL, &f));
106
107 ASSERT_OK(sd_event_loop(e));
108 ASSERT_OK(sd_future_result(f));
109 }
110
111 static int concurrent_wait_fiber(void *userdata) {
112 _cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
113 siginfo_t si;
114 int r;
115
116 r = pidref_safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_LOG, &pidref);
117 if (r < 0)
118 return r;
119
120 if (r == 0)
121 /* Child exits with specified status */
122 _exit(PTR_TO_INT(userdata));
123
124 r = pidref_wait_for_terminate(&pidref, &si);
125 if (r < 0)
126 return r;
127
128 pidref_done(&pidref);
129
130 if (si.si_code != CLD_EXITED || si.si_status != PTR_TO_INT(userdata))
131 return -EIO;
132
133 return 0;
134 }
135
136 TEST(wait_for_terminate_fiber_concurrent) {
137 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
138 ASSERT_OK(sd_event_new(&e));
139 ASSERT_OK(sd_event_set_exit_on_idle(e, true));
140
141 sd_future *fibers[3] = {};
142 CLEANUP_ELEMENTS(fibers, sd_future_unref_array_clear);
143
144 /* Create 3 fibers, each waiting for a different child */
145 for (size_t i = 0; i < ELEMENTSOF(fibers); i++)
146 ASSERT_OK(sd_fiber_new(e, "concurrent-wait", concurrent_wait_fiber, INT_TO_PTR(20 + i), /* destroy= */ NULL, &fibers[i]));
147
148 ASSERT_OK(sd_event_loop(e));
149
150 /* All fibers should complete successfully */
151 for (size_t i = 0; i < ELEMENTSOF(fibers); i++)
152 ASSERT_OK(sd_future_result(fibers[i]));
153 }
154
155 typedef struct LoopIOContext {
156 int *pipefd;
157 const char *data;
158 size_t len;
159 int order;
160 } LoopIOContext;
161
162 static int loop_read_suspend_fiber(void *userdata) {
163 LoopIOContext *ctx = ASSERT_PTR(userdata);
164 char buf[64];
165
166 ASSERT_EQ(ctx->order, 0);
167 ctx->order = 1;
168
169 ssize_t n = loop_read(ctx->pipefd[0], buf, sizeof(buf), /* do_poll= */ true);
170
171 /* While we were suspended, the writer fiber should have run. */
172 ASSERT_EQ(ctx->order, 2);
173
174 if (n < 0)
175 return (int) n;
176 if ((size_t) n != ctx->len || memcmp(buf, ctx->data, ctx->len) != 0)
177 return -EIO;
178
179 return (int) n;
180 }
181
182 static int loop_write_suspend_fiber(void *userdata) {
183 LoopIOContext *ctx = ASSERT_PTR(userdata);
184
185 ASSERT_EQ(ctx->order, 1);
186 ctx->order = 2;
187
188 int r = loop_write(ctx->pipefd[1], ctx->data, ctx->len);
189 if (r < 0)
190 return r;
191
192 /* Close the write end so the reader sees EOF after reading the data. */
193 ctx->pipefd[1] = safe_close(ctx->pipefd[1]);
194 return 0;
195 }
196
197 /* Test: two fibers cooperatively pass a small payload through a blocking pipe using the suspending
198 * loop helpers. Exercises the non-blocking flip, event-loop yielding, and the blocking-mode restore. */
199 TEST(loop_read_write_suspend) {
200 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
201 ASSERT_OK(sd_event_new(&e));
202 ASSERT_OK(sd_event_set_exit_on_idle(e, true));
203
204 _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
205 ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC));
206
207 static const char payload[] = "loop-suspend";
208 LoopIOContext ctx = {
209 .pipefd = pipefd,
210 .data = payload,
211 .len = sizeof(payload) - 1,
212 };
213
214 _cleanup_(sd_future_unrefp) sd_future *fr = NULL, *fw = NULL;
215 ASSERT_OK(sd_fiber_new(e, "loop-read", loop_read_suspend_fiber, &ctx, /* destroy= */ NULL, &fr));
216 ASSERT_OK(sd_future_set_priority(fr, 0));
217 ASSERT_OK(sd_fiber_new(e, "loop-write", loop_write_suspend_fiber, &ctx, /* destroy= */ NULL, &fw));
218 ASSERT_OK(sd_future_set_priority(fw, 1));
219
220 ASSERT_OK(sd_event_loop(e));
221
222 ASSERT_OK_EQ(sd_future_result(fr), (int) ctx.len);
223 ASSERT_OK_ZERO(sd_future_result(fw));
224
225 /* The read fd started out blocking and loop_read() must have restored it before returning. */
226 ASSERT_OK_ZERO(fcntl(pipefd[0], F_GETFL) & O_NONBLOCK);
227 }
228
229 static int loop_read_exact_short_fiber(void *userdata) {
230 int fd = PTR_TO_INT(userdata);
231 char buf[16];
232
233 /* Requesting more bytes than the peer writes should return -EIO once EOF is hit. */
234 return loop_read_exact(fd, buf, sizeof(buf), /* do_poll= */ true);
235 }
236
237 /* Test: loop_read_exact() returns -EIO when the peer closes early. */
238 TEST(loop_read_exact_short) {
239 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
240 ASSERT_OK(sd_event_new(&e));
241 ASSERT_OK(sd_event_set_exit_on_idle(e, true));
242
243 _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
244 ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC));
245
246 _cleanup_(sd_future_unrefp) sd_future *f = NULL;
247 ASSERT_OK(sd_fiber_new(e, "loop-read-exact", loop_read_exact_short_fiber,
248 INT_TO_PTR(pipefd[0]), /* destroy= */ NULL, &f));
249
250 /* Write a few bytes and close the write end — less than the fiber asked for. */
251 ASSERT_OK_EQ_ERRNO(write(pipefd[1], "abc", 3), (ssize_t) 3);
252 pipefd[1] = safe_close(pipefd[1]);
253
254 ASSERT_OK(sd_event_loop(e));
255
256 ASSERT_ERROR(sd_future_result(f), EIO);
257 }
258
259 typedef struct LoopWriteTimeoutContext {
260 int fd;
261 int result;
262 } LoopWriteTimeoutContext;
263
264 static int loop_write_timeout_fiber(void *userdata) {
265 LoopWriteTimeoutContext *ctx = ASSERT_PTR(userdata);
266
267 /* Try to write much more than the pipe buffer can hold with a short timeout. The write will
268 * succeed partially and then hit -ETIME after exhausting the timeout while blocked. */
269 static const char big_buf[128 * 1024] = { 0 };
270 ctx->result = loop_write_full(ctx->fd, big_buf, sizeof(big_buf), 100 * USEC_PER_MSEC);
271 return 0;
272 }
273
274 /* Test: loop_write_full() returns -ETIME when the peer never drains. */
275 TEST(loop_write_full_timeout) {
276 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
277 ASSERT_OK(sd_event_new(&e));
278 ASSERT_OK(sd_event_set_exit_on_idle(e, true));
279
280 _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
281 ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC));
282
283 /* Shrink the pipe buffer to its minimum (one page) so the 128K write below is guaranteed to block
284 * regardless of the architecture's page size. The default pipe buffer is 16 pages, which on
285 * 64K-page architectures (e.g. ppc64le) is 1 MiB — enough to absorb the entire write without ever
286 * blocking, defeating the purpose of the timeout. */
287 ASSERT_OK_ERRNO(fcntl(pipefd[1], F_SETPIPE_SZ, 1));
288
289 LoopWriteTimeoutContext ctx = { .fd = pipefd[1], .result = 0 };
290 _cleanup_(sd_future_unrefp) sd_future *f = NULL;
291 ASSERT_OK(sd_fiber_new(e, "loop-write-timeout", loop_write_timeout_fiber, &ctx, /* destroy= */ NULL, &f));
292
293 ASSERT_OK(sd_event_loop(e));
294
295 ASSERT_OK_ZERO(sd_future_result(f));
296 ASSERT_ERROR(ctx.result, ETIME);
297 }
298
299 typedef struct PpollDispatchContext {
300 int *pipefd;
301 int order;
302 } PpollDispatchContext;
303
304 static int ppoll_dispatch_read_fiber(void *userdata) {
305 PpollDispatchContext *ctx = ASSERT_PTR(userdata);
306 struct pollfd pfd = {
307 .fd = ctx->pipefd[0],
308 .events = POLLIN,
309 };
310
311 ASSERT_EQ(ctx->order, 0);
312 ctx->order = 1;
313
314 /* Direct ppoll_usec() call from a fiber must dispatch through sd_fiber_poll(), suspending the
315 * fiber instead of blocking the entire thread. If dispatch fails, the writer fiber never gets a
316 * chance to run and the test deadlocks. */
317 int r = ppoll_usec(&pfd, 1, USEC_INFINITY);
318 if (r < 0)
319 return r;
320
321 ASSERT_EQ(ctx->order, 2);
322
323 if (r != 1 || !FLAGS_SET(pfd.revents, POLLIN))
324 return -EIO;
325
326 return 0;
327 }
328
329 static int ppoll_dispatch_write_fiber(void *userdata) {
330 PpollDispatchContext *ctx = ASSERT_PTR(userdata);
331
332 ASSERT_EQ(ctx->order, 1);
333 ctx->order = 2;
334
335 if (write(ctx->pipefd[1], "x", 1) < 0)
336 return -errno;
337
338 return 0;
339 }
340
341 /* Test: ppoll_usec() called from a fiber dispatches through the FiberOps hook to sd_fiber_poll(),
342 * yielding to the event loop instead of blocking. */
343 TEST(ppoll_usec_dispatch) {
344 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
345 ASSERT_OK(sd_event_new(&e));
346 ASSERT_OK(sd_event_set_exit_on_idle(e, true));
347
348 _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
349 ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK));
350
351 PpollDispatchContext ctx = { .pipefd = pipefd };
352
353 _cleanup_(sd_future_unrefp) sd_future *fr = NULL, *fw = NULL;
354 ASSERT_OK(sd_fiber_new(e, "ppoll-read", ppoll_dispatch_read_fiber, &ctx, /* destroy= */ NULL, &fr));
355 ASSERT_OK(sd_future_set_priority(fr, 0));
356 ASSERT_OK(sd_fiber_new(e, "ppoll-write", ppoll_dispatch_write_fiber, &ctx, /* destroy= */ NULL, &fw));
357 ASSERT_OK(sd_future_set_priority(fw, 1));
358
359 ASSERT_OK(sd_event_loop(e));
360 ASSERT_OK(sd_future_result(fr));
361 ASSERT_OK(sd_future_result(fw));
362 }
363
364 static int loop_write_zero_timeout_nonblock_fiber(void *userdata) {
365 int fd = PTR_TO_INT(userdata);
366
367 /* Fill the pipe so the next write would block. The fd is non-blocking, so on a fiber
368 * loop_write_full(timeout=0) must take the non-fiber path and return -EAGAIN immediately
369 * rather than suspending. */
370 static const char big_buf[128 * 1024] = { 0 };
371 return loop_write_full(fd, big_buf, sizeof(big_buf), /* timeout= */ 0);
372 }
373
374 /* Test: timeout == 0 on a non-blocking fd from a fiber preserves the "don't wait" semantic and
375 * returns -EAGAIN when the pipe buffer is full, instead of suspending the fiber. */
376 TEST(loop_write_zero_timeout_nonblock) {
377 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
378 ASSERT_OK(sd_event_new(&e));
379 ASSERT_OK(sd_event_set_exit_on_idle(e, true));
380
381 _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
382 ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK));
383 ASSERT_OK_ERRNO(fcntl(pipefd[1], F_SETPIPE_SZ, 1));
384
385 _cleanup_(sd_future_unrefp) sd_future *f = NULL;
386 ASSERT_OK(sd_fiber_new(e, "loop-write-zt-nb", loop_write_zero_timeout_nonblock_fiber,
387 INT_TO_PTR(pipefd[1]), /* destroy= */ NULL, &f));
388
389 ASSERT_OK(sd_event_loop(e));
390 ASSERT_ERROR(sd_future_result(f), EAGAIN);
391 }
392
393 typedef struct LoopWriteZeroBlockingContext {
394 int *pipefd;
395 size_t total;
396 int order;
397 } LoopWriteZeroBlockingContext;
398
399 static int loop_write_zero_blocking_writer_fiber(void *userdata) {
400 LoopWriteZeroBlockingContext *ctx = ASSERT_PTR(userdata);
401
402 ASSERT_EQ(ctx->order, 0);
403 ctx->order = 1;
404
405 /* timeout == 0 on a *blocking* fd from a fiber: the fast EAGAIN return isn't possible, so
406 * loop_write_full() takes the fiber path. The reader fiber drains the pipe, letting our
407 * write complete via fiber suspension/resume. */
408 _cleanup_free_ char *big_buf = malloc0(ctx->total);
409 ASSERT_NOT_NULL(big_buf);
410 int r = loop_write_full(ctx->pipefd[1], big_buf, ctx->total, /* timeout= */ 0);
411
412 ASSERT_EQ(ctx->order, 2);
413 return r;
414 }
415
416 static int loop_write_zero_blocking_reader_fiber(void *userdata) {
417 LoopWriteZeroBlockingContext *ctx = ASSERT_PTR(userdata);
418
419 ASSERT_EQ(ctx->order, 1);
420 ctx->order = 2;
421
422 _cleanup_free_ char *buf = malloc(ctx->total);
423 ASSERT_NOT_NULL(buf);
424 ssize_t n = loop_read(ctx->pipefd[0], buf, ctx->total, /* do_poll= */ true);
425 if (n < 0)
426 return (int) n;
427 return (int) n;
428 }
429
430 /* Test: timeout == 0 on a blocking fd from a fiber takes the fiber path (suspends until the peer
431 * drains) instead of blocking the entire thread. */
432 TEST(loop_write_zero_timeout_blocking) {
433 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
434 ASSERT_OK(sd_event_new(&e));
435 ASSERT_OK(sd_event_set_exit_on_idle(e, true));
436
437 _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
438 ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC));
439 ASSERT_OK_ERRNO(fcntl(pipefd[1], F_SETPIPE_SZ, 1));
440
441 /* F_SETPIPE_SZ rounds up to the kernel's pipe minimum (typically a page); query the actual
442 * size and write more than that, so the write must wait on the reader regardless of page size. */
443 int pipe_sz = fcntl(pipefd[1], F_GETPIPE_SZ);
444 ASSERT_OK_ERRNO(pipe_sz);
445
446 LoopWriteZeroBlockingContext ctx = { .pipefd = pipefd, .total = (size_t) pipe_sz * 2 };
447
448 _cleanup_(sd_future_unrefp) sd_future *fw = NULL, *fr = NULL;
449 ASSERT_OK(sd_fiber_new(e, "loop-write-zt-blk", loop_write_zero_blocking_writer_fiber,
450 &ctx, /* destroy= */ NULL, &fw));
451 ASSERT_OK(sd_future_set_priority(fw, 0));
452 ASSERT_OK(sd_fiber_new(e, "loop-read-zt-blk", loop_write_zero_blocking_reader_fiber,
453 &ctx, /* destroy= */ NULL, &fr));
454 ASSERT_OK(sd_future_set_priority(fr, 1));
455
456 ASSERT_OK(sd_event_loop(e));
457 ASSERT_OK(sd_future_result(fw));
458 ASSERT_OK_EQ(sd_future_result(fr), (int) ctx.total);
459 }
460
461 static int loop_read_no_poll_nonblock_fiber(void *userdata) {
462 int fd = PTR_TO_INT(userdata);
463 char buf[64];
464
465 /* Empty non-blocking pipe + do_poll=false: on a fiber loop_read() must take the non-fiber
466 * path and return -EAGAIN immediately rather than suspending. */
467 return (int) loop_read(fd, buf, sizeof(buf), /* do_poll= */ false);
468 }
469
470 /* Test: do_poll == false on a non-blocking fd from a fiber preserves the "don't wait" semantic
471 * and returns -EAGAIN when no data is available, instead of suspending the fiber. */
472 TEST(loop_read_no_poll_nonblock) {
473 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
474 ASSERT_OK(sd_event_new(&e));
475 ASSERT_OK(sd_event_set_exit_on_idle(e, true));
476
477 _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
478 ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK));
479
480 _cleanup_(sd_future_unrefp) sd_future *f = NULL;
481 ASSERT_OK(sd_fiber_new(e, "loop-read-np-nb", loop_read_no_poll_nonblock_fiber,
482 INT_TO_PTR(pipefd[0]), /* destroy= */ NULL, &f));
483
484 ASSERT_OK(sd_event_loop(e));
485 ASSERT_ERROR(sd_future_result(f), EAGAIN);
486 }
487
488 typedef struct LoopReadNoPollBlockingContext {
489 int *pipefd;
490 const char *data;
491 size_t len;
492 int order;
493 } LoopReadNoPollBlockingContext;
494
495 static int loop_read_no_poll_blocking_reader_fiber(void *userdata) {
496 LoopReadNoPollBlockingContext *ctx = ASSERT_PTR(userdata);
497 char buf[64];
498
499 ASSERT_EQ(ctx->order, 0);
500 ctx->order = 1;
501
502 /* do_poll == false on a *blocking* fd from a fiber: the fast EAGAIN return isn't possible,
503 * so loop_read() takes the fiber path and suspends until the writer fiber feeds data. */
504 ssize_t n = loop_read(ctx->pipefd[0], buf, sizeof(buf), /* do_poll= */ false);
505
506 ASSERT_EQ(ctx->order, 2);
507
508 if (n < 0)
509 return (int) n;
510 if ((size_t) n != ctx->len || memcmp(buf, ctx->data, ctx->len) != 0)
511 return -EIO;
512
513 return (int) n;
514 }
515
516 static int loop_read_no_poll_blocking_writer_fiber(void *userdata) {
517 LoopReadNoPollBlockingContext *ctx = ASSERT_PTR(userdata);
518
519 ASSERT_EQ(ctx->order, 1);
520 ctx->order = 2;
521
522 int r = loop_write(ctx->pipefd[1], ctx->data, ctx->len);
523 if (r < 0)
524 return r;
525
526 ctx->pipefd[1] = safe_close(ctx->pipefd[1]);
527 return 0;
528 }
529
530 /* Test: do_poll == false on a blocking fd from a fiber takes the fiber path (suspends until the
531 * peer feeds data) instead of blocking the entire thread. */
532 TEST(loop_read_no_poll_blocking) {
533 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
534 ASSERT_OK(sd_event_new(&e));
535 ASSERT_OK(sd_event_set_exit_on_idle(e, true));
536
537 _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
538 ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC));
539
540 static const char payload[] = "no-poll";
541 LoopReadNoPollBlockingContext ctx = {
542 .pipefd = pipefd,
543 .data = payload,
544 .len = sizeof(payload) - 1,
545 };
546
547 _cleanup_(sd_future_unrefp) sd_future *fr = NULL, *fw = NULL;
548 ASSERT_OK(sd_fiber_new(e, "loop-read-np-blk", loop_read_no_poll_blocking_reader_fiber,
549 &ctx, /* destroy= */ NULL, &fr));
550 ASSERT_OK(sd_future_set_priority(fr, 0));
551 ASSERT_OK(sd_fiber_new(e, "loop-write-np-blk", loop_read_no_poll_blocking_writer_fiber,
552 &ctx, /* destroy= */ NULL, &fw));
553 ASSERT_OK(sd_future_set_priority(fw, 1));
554
555 ASSERT_OK(sd_event_loop(e));
556 ASSERT_OK_EQ(sd_future_result(fr), (int) ctx.len);
557 ASSERT_OK_ZERO(sd_future_result(fw));
558 }
559
560 /* Test: loop_*() helpers transparently fall back to blocking I/O when called outside any
561 * fiber context. */
562 TEST(loop_read_write_fallback) {
563 _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
564 ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC));
565
566 ASSERT_OK(loop_write(pipefd[1], "fallback", STRLEN("fallback")));
567
568 char buf[16];
569 ssize_t n = loop_read(pipefd[0], buf, STRLEN("fallback"), /* do_poll= */ true);
570 ASSERT_OK_EQ(n, (ssize_t) STRLEN("fallback"));
571 ASSERT_EQ(memcmp(buf, "fallback", STRLEN("fallback")), 0);
572 }
573
574 DEFINE_TEST_MAIN(LOG_DEBUG);