]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-08-hangup-on-idle.c
b3840433410b7bfda4068293728c8050b2483856
[ccan] / ccan / io / test / run-08-hangup-on-idle.c
1 #include <ccan/io/io.h>
2 /* Include the C files directly. */
3 #include <ccan/io/poll.c>
4 #include <ccan/io/io.c>
5 #include <ccan/tap/tap.h>
6 #include <sys/wait.h>
7 #include <stdio.h>
8
9 static int fds2[2];
10
11 static struct io_plan timeout_wakeup(struct io_conn *conn, char *buf)
12 {
13         /* This kills the dummy connection. */
14         close(fds2[1]);
15         return io_read(buf, 16, io_close_cb, NULL);
16 }
17
18 int main(void)
19 {
20         int fds[2];
21         struct io_conn *conn;
22         char buf[16];
23
24         plan_tests(4);
25
26         ok1(pipe(fds) == 0);
27
28         /* Write then close. */
29         io_new_conn(fds[1], io_write("hello there world", 16,
30                                      io_close_cb, NULL));
31         conn = io_new_conn(fds[0], io_idle());
32
33         /* To avoid assert(num_waiting) */
34         ok1(pipe(fds2) == 0);
35         io_new_conn(fds2[0], io_read(buf, 16, io_close_cb, NULL));
36
37         /* After half a second, it will read. */
38         io_timeout(conn, time_from_msec(500), timeout_wakeup, buf);
39
40         ok1(io_loop() == NULL);
41         ok1(memcmp(buf, "hello there world", 16) == 0);
42
43         /* This exits depending on whether all tests passed */
44         return exit_status();
45 }