]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-08-read-after-hangup.c
ccan/io: eliminate dir argument from io_wait and io_always.
[ccan] / ccan / io / test / run-08-read-after-hangup.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 #include <signal.h>
9
10 static char inbuf[8];
11
12 static struct io_plan *wake_it(struct io_conn *conn, struct io_conn *reader)
13 {
14         io_wake(inbuf);
15         return io_close(conn);
16 }
17
18 static struct io_plan *read_buf(struct io_conn *conn, void *unused)
19 {
20         return io_read(conn, inbuf, 8, io_close_cb, NULL);
21 }
22
23 static struct io_plan *init_writer(struct io_conn *conn, struct io_conn *wakeme)
24 {
25         return io_write(conn, "EASYTEST", 8, wake_it, wakeme);
26 }
27
28 static struct io_plan *init_waiter(struct io_conn *conn, void *unused)
29 {
30         return io_wait(conn, inbuf, read_buf, NULL);
31 }
32
33 int main(void)
34 {
35         int fds[2];
36         struct io_conn *conn;
37
38         plan_tests(3);
39
40         ok1(pipe(fds) == 0);
41         conn = io_new_conn(NULL, fds[0], init_waiter, NULL);
42         io_new_conn(conn, fds[1], init_writer, conn);
43
44         ok1(io_loop() == NULL);
45         ok1(memcmp(inbuf, "EASYTEST", sizeof(inbuf)) == 0);
46
47         /* This exits depending on whether all tests passed */
48         return exit_status();
49 }