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_add(&d->timers, &d->timer,
51 timeabs_add(time_now(), time_from_usec(d->timeout_usec)));
53 return io_read(conn, d->buf, sizeof(d->buf), no_timeout, d);
56 static int make_listen_fd(const char *port, struct addrinfo **info)
59 struct addrinfo *addrinfo, hints;
61 memset(&hints, 0, sizeof(hints));
62 hints.ai_family = AF_UNSPEC;
63 hints.ai_socktype = SOCK_STREAM;
64 hints.ai_flags = AI_PASSIVE;
65 hints.ai_protocol = 0;
67 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
70 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
71 addrinfo->ai_protocol);
75 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
76 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
80 if (listen(fd, 1) != 0) {
90 struct data *d = malloc(sizeof(*d));
91 struct addrinfo *addrinfo;
92 struct io_listener *l;
93 struct timer *expired;
96 /* This is how many tests you plan to run */
99 d->timeout_usec = 100000;
100 timers_init(&d->timers, time_now());
101 timer_init(&d->timer);
102 fd = make_listen_fd(PORT, &addrinfo);
104 l = io_new_listener(NULL, fd, init_conn, d);
111 io_close_listener(l);
112 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
113 addrinfo->ai_protocol);
116 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
118 signal(SIGPIPE, SIG_IGN);
120 for (i = 0; i < strlen("hellothere"); i++) {
121 if (write(fd, "hellothere" + i, 1) != 1)
125 freeaddrinfo(addrinfo);
126 timers_cleanup(&d->timers);
130 ok1(io_loop(&d->timers, &expired) == NULL);
132 /* One element, d->timer. */
133 ok1(expired == &d->timer);
134 ok1(!timers_expire(&d->timers, time_now()));
139 /* Finished will be called, d will be returned */
140 ok1(io_loop(&d->timers, &expired) == d);
141 ok1(expired == NULL);
144 /* It should have died. */
146 ok1(WIFEXITED(status));
147 ok1(WEXITSTATUS(status) < sizeof(d->buf));
149 /* This one shouldn't time out. */
151 d->timeout_usec = 500000;
157 io_close_listener(l);
158 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
159 addrinfo->ai_protocol);
162 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
164 signal(SIGPIPE, SIG_IGN);
166 for (i = 0; i < strlen("hellothere"); i++) {
167 if (write(fd, "hellothere" + i, 1) != 1)
171 freeaddrinfo(addrinfo);
172 timers_cleanup(&d->timers);
176 ok1(io_loop(&d->timers, &expired) == d);
178 ok1(expired == NULL);
180 ok1(WIFEXITED(status));
181 ok1(WEXITSTATUS(status) >= sizeof(d->buf));
183 io_close_listener(l);
184 freeaddrinfo(addrinfo);
185 timers_cleanup(&d->timers);
188 /* This exits depending on whether all tests passed */
189 return exit_status();