]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-08-hangup-on-idle.c
io: change io_idle() to io_wait()
[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 static struct io_plan never(struct io_conn *conn, void *unused)
19 {
20         abort();
21 }
22
23 int main(void)
24 {
25         int fds[2];
26         struct io_conn *conn;
27         char buf[16];
28
29         plan_tests(4);
30
31         ok1(pipe(fds) == 0);
32
33         /* Write then close. */
34         io_new_conn(fds[1], io_write("hello there world", 16,
35                                      io_close_cb, NULL));
36         conn = io_new_conn(fds[0], io_wait(buf, never, NULL));
37
38         /* To avoid assert(num_waiting) */
39         ok1(pipe(fds2) == 0);
40         io_new_conn(fds2[0], io_read(buf, 16, io_close_cb, NULL));
41
42         /* After half a second, it will read. */
43         io_timeout(conn, time_from_msec(500), timeout_wakeup, buf);
44
45         ok1(io_loop() == NULL);
46         ok1(memcmp(buf, "hello there world", 16) == 0);
47
48         /* This exits depending on whether all tests passed */
49         return exit_status();
50 }