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 <ccan/time/time.h>
26 static void finish_ok(struct io_conn *conn, struct data *d)
32 static struct io_plan *no_timeout(struct io_conn *conn, struct data *d)
36 return io_close(conn);
39 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
42 io_set_debug(conn, true);
48 io_set_finish(conn, finish_ok, d);
50 timer_addrel(&d->timers, &d->timer, time_from_usec(d->timeout_usec));
52 return io_read(conn, d->buf, sizeof(d->buf), no_timeout, d);
55 static int make_listen_fd(const char *port, struct addrinfo **info)
58 struct addrinfo *addrinfo, hints;
60 memset(&hints, 0, sizeof(hints));
61 hints.ai_family = AF_UNSPEC;
62 hints.ai_socktype = SOCK_STREAM;
63 hints.ai_flags = AI_PASSIVE;
64 hints.ai_protocol = 0;
66 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
69 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
70 addrinfo->ai_protocol);
74 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
75 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
79 if (listen(fd, 1) != 0) {
89 struct data *d = malloc(sizeof(*d));
90 struct addrinfo *addrinfo;
91 struct io_listener *l;
92 struct timer *expired;
95 /* This is how many tests you plan to run */
98 d->timeout_usec = 100000;
99 timers_init(&d->timers, time_mono());
100 timer_init(&d->timer);
101 fd = make_listen_fd(PORT, &addrinfo);
103 l = io_new_listener(NULL, fd, init_conn, d);
110 io_close_listener(l);
111 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
112 addrinfo->ai_protocol);
115 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
117 signal(SIGPIPE, SIG_IGN);
119 for (i = 0; i < strlen("hellothere"); i++) {
120 if (write(fd, "hellothere" + i, 1) != 1)
124 freeaddrinfo(addrinfo);
125 timers_cleanup(&d->timers);
129 ok1(io_loop(&d->timers, &expired) == NULL);
131 /* One element, d->timer. */
132 ok1(expired == &d->timer);
133 ok1(!timers_expire(&d->timers, time_mono()));
138 /* Finished will be called, d will be returned */
139 ok1(io_loop(&d->timers, &expired) == d);
140 ok1(expired == NULL);
143 /* It should have died. */
145 ok1(WIFEXITED(status));
146 ok1(WEXITSTATUS(status) < sizeof(d->buf));
148 /* This one shouldn't time out. */
150 d->timeout_usec = 500000;
156 io_close_listener(l);
157 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
158 addrinfo->ai_protocol);
161 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
163 signal(SIGPIPE, SIG_IGN);
165 for (i = 0; i < strlen("hellothere"); i++) {
166 if (write(fd, "hellothere" + i, 1) != 1)
170 freeaddrinfo(addrinfo);
171 timers_cleanup(&d->timers);
175 ok1(io_loop(&d->timers, &expired) == d);
177 ok1(expired == NULL);
179 ok1(WIFEXITED(status));
180 ok1(WEXITSTATUS(status) >= sizeof(d->buf));
182 io_close_listener(l);
183 freeaddrinfo(addrinfo);
184 timers_cleanup(&d->timers);
187 /* This exits depending on whether all tests passed */
188 return exit_status();