]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-08-hangup-on-idle.c
ccan/io: rewrite.
[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 *read_in(struct io_conn *conn, char *buf)
12 {
13         return io_read(conn, buf, 16, io_close_cb, NULL);
14 }
15
16 static struct io_plan *setup_waiter(struct io_conn *conn, char *buf)
17 {
18         return io_wait(conn, buf, IO_IN, read_in, buf);
19 }
20
21 static struct io_plan *wake_and_close(struct io_conn *conn, char *buf)
22 {
23         io_wake(buf);
24         return io_close(conn);
25 }
26
27 static struct io_plan *setup_waker(struct io_conn *conn, char *buf)
28 {
29         return io_read(conn, buf, 1, wake_and_close, buf);
30 }
31
32 int main(void)
33 {
34         int fds[2];
35         char buf[16];
36
37         plan_tests(4);
38
39         ok1(pipe(fds) == 0);
40
41         io_new_conn(NULL, fds[0], setup_waiter, buf);
42         ok1(pipe(fds2) == 0);
43         io_new_conn(NULL, fds2[0], setup_waker, buf);
44
45         if (fork() == 0) {
46                 write(fds[1], "hello there world", 16);
47                 close(fds[1]);
48
49                 /* Now wake it. */
50                 sleep(1);
51                 write(fds2[1], "", 1);
52                 exit(0);
53         }
54
55         ok1(io_loop() == NULL);
56         ok1(memcmp(buf, "hello there world", 16) == 0);
57
58         /* This exits depending on whether all tests passed */
59         return exit_status();
60 }