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