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>
24 static void finish_ok(struct io_conn *conn, struct data *d)
30 static struct io_plan *no_timeout(struct io_conn *conn, struct data *d)
34 return io_close(conn);
37 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
43 io_set_finish(conn, finish_ok, d);
45 timer_add(&d->timers, &d->timer,
46 timeabs_add(time_now(), time_from_usec(d->timeout_usec)));
48 return io_read(conn, d->buf, sizeof(d->buf), no_timeout, d);
51 static int make_listen_fd(const char *port, struct addrinfo **info)
54 struct addrinfo *addrinfo, hints;
56 memset(&hints, 0, sizeof(hints));
57 hints.ai_family = AF_UNSPEC;
58 hints.ai_socktype = SOCK_STREAM;
59 hints.ai_flags = AI_PASSIVE;
60 hints.ai_protocol = 0;
62 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
65 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
66 addrinfo->ai_protocol);
70 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
71 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
75 if (listen(fd, 1) != 0) {
85 struct data *d = malloc(sizeof(*d));
86 struct addrinfo *addrinfo;
87 struct io_listener *l;
88 struct list_head expired;
91 /* This is how many tests you plan to run */
94 d->timeout_usec = 100000;
95 timers_init(&d->timers, time_now());
96 fd = make_listen_fd(PORT, &addrinfo);
98 l = io_new_listener(NULL, fd, init_conn, d);
105 io_close_listener(l);
106 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
107 addrinfo->ai_protocol);
110 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
112 signal(SIGPIPE, SIG_IGN);
114 for (i = 0; i < strlen("hellothere"); i++) {
115 if (write(fd, "hellothere" + i, 1) != 1)
119 freeaddrinfo(addrinfo);
120 timers_cleanup(&d->timers);
124 ok1(io_loop(&d->timers, &expired) == NULL);
126 /* One element, d->timer. */
127 ok1(list_pop(&expired, struct timer, list) == &d->timer);
128 ok1(list_empty(&expired));
133 /* Finished will be called, d will be returned */
134 ok1(io_loop(&d->timers, &expired) == d);
135 ok1(list_empty(&expired));
138 /* It should have died. */
140 ok1(WIFEXITED(status));
141 ok1(WEXITSTATUS(status) < sizeof(d->buf));
143 /* This one shouldn't time out. */
145 d->timeout_usec = 500000;
151 io_close_listener(l);
152 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
153 addrinfo->ai_protocol);
156 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
158 signal(SIGPIPE, SIG_IGN);
160 for (i = 0; i < strlen("hellothere"); i++) {
161 if (write(fd, "hellothere" + i, 1) != 1)
165 freeaddrinfo(addrinfo);
166 timers_cleanup(&d->timers);
170 ok1(io_loop(&d->timers, &expired) == d);
172 ok1(list_empty(&expired));
174 ok1(WIFEXITED(status));
175 ok1(WEXITSTATUS(status) >= sizeof(d->buf));
177 io_close_listener(l);
178 freeaddrinfo(addrinfo);
179 timers_cleanup(&d->timers);
182 /* This exits depending on whether all tests passed */
183 return exit_status();