]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-06-idle.c
ccan/io: io_set_finish()
[ccan] / ccan / io / test / run-06-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 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11
12 #ifndef PORT
13 #define PORT "65006"
14 #endif
15
16 static struct io_conn *idler;
17
18 struct data {
19         int state;
20         char buf[4];
21 };
22
23 static struct io_plan read_done(struct io_conn *conn, struct data *d)
24 {
25         ok1(d->state == 2 || d->state == 3);
26         d->state++;
27         return io_close(conn, NULL);
28 }
29
30 static void finish_waker(struct io_conn *conn, struct data *d)
31 {
32         io_wake(idler, io_read(d->buf, sizeof(d->buf), read_done, d));
33         ok1(d->state == 1);
34         d->state++;
35 }
36
37 static void finish_idle(struct io_conn *conn, struct data *d)
38 {
39         ok1(d->state == 3);
40         d->state++;
41         io_break(d, io_idle());
42 }
43
44 static struct io_plan never(struct io_conn *conn, void *arg)
45 {
46         abort();
47 }
48
49 static void init_conn(int fd, struct data *d)
50 {
51         int fd2;
52
53         ok1(d->state == 0);
54         d->state++;
55         idler = io_new_conn(fd, io_idle());
56         io_set_finish(idler, finish_idle, d);
57
58         /* This will wake us up, as read will fail. */
59         fd2 = open("/dev/null", O_RDONLY);
60         ok1(fd2 >= 0);
61         io_set_finish(io_new_conn(fd2, io_read(idler, 1, never, NULL)),
62                       finish_waker, d);
63 }
64
65 static int make_listen_fd(const char *port, struct addrinfo **info)
66 {
67         int fd, on = 1;
68         struct addrinfo *addrinfo, hints;
69
70         memset(&hints, 0, sizeof(hints));
71         hints.ai_family = AF_UNSPEC;
72         hints.ai_socktype = SOCK_STREAM;
73         hints.ai_flags = AI_PASSIVE;
74         hints.ai_protocol = 0;
75
76         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
77                 return -1;
78
79         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
80                     addrinfo->ai_protocol);
81         if (fd < 0)
82                 return -1;
83
84         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
85         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
86                 close(fd);
87                 return -1;
88         }
89         if (listen(fd, 1) != 0) {
90                 close(fd);
91                 return -1;
92         }
93         *info = addrinfo;
94         return fd;
95 }
96
97 int main(void)
98 {
99         struct data *d = malloc(sizeof(*d));
100         struct addrinfo *addrinfo;
101         struct io_listener *l;
102         int fd, status;
103
104         /* This is how many tests you plan to run */
105         plan_tests(13);
106         d->state = 0;
107         fd = make_listen_fd(PORT, &addrinfo);
108         ok1(fd >= 0);
109         l = io_new_listener(fd, init_conn, d);
110         ok1(l);
111         fflush(stdout);
112         if (!fork()) {
113                 int i;
114
115                 io_close_listener(l);
116                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
117                             addrinfo->ai_protocol);
118                 if (fd < 0)
119                         exit(1);
120                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
121                         exit(2);
122                 signal(SIGPIPE, SIG_IGN);
123                 for (i = 0; i < strlen("hellothere"); i++) {
124                         if (write(fd, "hellothere" + i, 1) != 1)
125                                 break;
126                 }
127                 close(fd);
128                 freeaddrinfo(addrinfo);
129                 free(d);
130                 exit(0);
131         }
132         freeaddrinfo(addrinfo);
133
134         ok1(io_loop() == d);
135         ok1(d->state == 4);
136         ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
137         free(d);
138         io_close_listener(l);
139
140         ok1(wait(&status));
141         ok1(WIFEXITED(status));
142         ok1(WEXITSTATUS(status) == 0);
143
144         /* This exits depending on whether all tests passed */
145         return exit_status();
146 }