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>
22 static void finish_ok(struct io_conn *conn, struct data *d)
28 static struct io_plan *no_timeout(struct io_conn *conn, struct data *d)
32 return io_close(conn);
35 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
41 io_set_finish(conn, finish_ok, d);
43 timer_addrel(&d->timers, &d->timer, time_from_usec(d->timeout_usec));
45 return io_read(conn, d->buf, sizeof(d->buf), no_timeout, d);
48 static int make_listen_fd(const char *port, struct addrinfo **info)
51 struct addrinfo *addrinfo, hints;
53 memset(&hints, 0, sizeof(hints));
54 hints.ai_family = AF_UNSPEC;
55 hints.ai_socktype = SOCK_STREAM;
56 hints.ai_flags = AI_PASSIVE;
57 hints.ai_protocol = 0;
59 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
62 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
63 addrinfo->ai_protocol);
67 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
68 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
72 if (listen(fd, 1) != 0) {
82 struct data *d = malloc(sizeof(*d));
83 struct addrinfo *addrinfo;
84 struct io_listener *l;
85 struct timer *expired;
88 /* This is how many tests you plan to run */
91 d->timeout_usec = 100000;
92 timers_init(&d->timers, time_mono());
93 timer_init(&d->timer);
94 fd = make_listen_fd(PORT, &addrinfo);
96 l = io_new_listener(NULL, fd, init_conn, d);
103 io_close_listener(l);
104 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
105 addrinfo->ai_protocol);
108 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
110 signal(SIGPIPE, SIG_IGN);
112 for (i = 0; i < strlen("hellothere"); i++) {
113 if (write(fd, "hellothere" + i, 1) != 1)
117 freeaddrinfo(addrinfo);
118 timers_cleanup(&d->timers);
122 ok1(io_loop(&d->timers, &expired) == NULL);
124 /* One element, d->timer. */
125 ok1(expired == &d->timer);
126 ok1(!timers_expire(&d->timers, time_mono()));
131 /* Finished will be called, d will be returned */
132 ok1(io_loop(&d->timers, &expired) == d);
133 ok1(expired == NULL);
136 /* It should have died. */
138 ok1(WIFEXITED(status));
139 ok1(WEXITSTATUS(status) < sizeof(d->buf));
141 /* This one shouldn't time out. */
143 d->timeout_usec = 500000;
149 io_close_listener(l);
150 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
151 addrinfo->ai_protocol);
154 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
156 signal(SIGPIPE, SIG_IGN);
158 for (i = 0; i < strlen("hellothere"); i++) {
159 if (write(fd, "hellothere" + i, 1) != 1)
163 freeaddrinfo(addrinfo);
164 timers_cleanup(&d->timers);
168 ok1(io_loop(&d->timers, &expired) == d);
170 ok1(expired == NULL);
172 ok1(WIFEXITED(status));
173 ok1(WEXITSTATUS(status) >= sizeof(d->buf));
175 io_close_listener(l);
176 freeaddrinfo(addrinfo);
177 timers_cleanup(&d->timers);
180 /* This exits depending on whether all tests passed */
181 return exit_status();