]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-08-hangup-on-idle.c
7463dd98c83e592525fc09b0a30ea250681fef20
[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, 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, io_close, NULL));
30         conn = io_new_conn(fds[0], io_idle());
31
32         /* To avoid assert(num_waiting) */
33         ok1(pipe(fds2) == 0);
34         io_new_conn(fds2[0], io_read(buf, 16, io_close, NULL));
35
36         /* After half a second, it will read. */
37         io_timeout(conn, time_from_msec(500), timeout_wakeup, buf);
38
39         ok1(io_loop() == NULL);
40         ok1(memcmp(buf, "hello there world", 16) == 0);
41
42         /* This exits depending on whether all tests passed */
43         return exit_status();
44 }