]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-15-timeout.c
ccan/io: implement timeouts.
[ccan] / ccan / io / test / run-15-timeout.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 <ccan/time/time.h>
7 #include <sys/wait.h>
8 #include <stdio.h>
9 #include <unistd.h>
10
11 #ifndef PORT
12 #define PORT "65015"
13 #endif
14
15 struct data {
16         struct timers timers;
17         int state;
18         struct io_conn *conn;
19         struct timer timer;
20         int timeout_usec;
21         char buf[4];
22 };
23
24 static void finish_ok(struct io_conn *conn, struct data *d)
25 {
26         d->state++;
27         io_break(d);
28 }
29
30 static struct io_plan *no_timeout(struct io_conn *conn, struct data *d)
31 {
32         ok1(d->state == 1);
33         d->state++;
34         return io_close(conn);
35 }
36
37 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
38 {
39         ok1(d->state == 0);
40         d->state++;
41
42         d->conn = conn;
43         io_set_finish(conn, finish_ok, d);
44
45         timer_add(&d->timers, &d->timer,
46                   timeabs_add(time_now(), time_from_usec(d->timeout_usec)));
47
48         return io_read(conn, d->buf, sizeof(d->buf), no_timeout, d);
49 }
50
51 static int make_listen_fd(const char *port, struct addrinfo **info)
52 {
53         int fd, on = 1;
54         struct addrinfo *addrinfo, hints;
55
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;
61
62         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
63                 return -1;
64
65         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
66                     addrinfo->ai_protocol);
67         if (fd < 0)
68                 return -1;
69
70         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
71         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
72                 close(fd);
73                 return -1;
74         }
75         if (listen(fd, 1) != 0) {
76                 close(fd);
77                 return -1;
78         }
79         *info = addrinfo;
80         return fd;
81 }
82
83 int main(void)
84 {
85         struct data *d = malloc(sizeof(*d));
86         struct addrinfo *addrinfo;
87         struct io_listener *l;
88         struct list_head expired;
89         int fd, status;
90
91         /* This is how many tests you plan to run */
92         plan_tests(21);
93         d->state = 0;
94         d->timeout_usec = 100000;
95         timers_init(&d->timers, time_now());
96         fd = make_listen_fd(PORT, &addrinfo);
97         ok1(fd >= 0);
98         l = io_new_listener(NULL, fd, init_conn, d);
99         ok1(l);
100         fflush(stdout);
101
102         if (!fork()) {
103                 int i;
104
105                 io_close_listener(l);
106                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
107                             addrinfo->ai_protocol);
108                 if (fd < 0)
109                         exit(1);
110                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
111                         exit(2);
112                 signal(SIGPIPE, SIG_IGN);
113                 usleep(500000);
114                 for (i = 0; i < strlen("hellothere"); i++) {
115                         if (write(fd, "hellothere" + i, 1) != 1)
116                                 break;
117                 }
118                 close(fd);
119                 freeaddrinfo(addrinfo);
120                 timers_cleanup(&d->timers);
121                 free(d);
122                 exit(i);
123         }
124         ok1(io_loop(&d->timers, &expired) == NULL);
125
126         /* One element, d->timer. */
127         ok1(list_pop(&expired, struct timer, list) == &d->timer);
128         ok1(list_empty(&expired));
129         ok1(d->state == 1);
130
131         io_close(d->conn);
132
133         /* Finished will be called, d will be returned */
134         ok1(io_loop(&d->timers, &expired) == d);
135         ok1(list_empty(&expired));
136         ok1(d->state == 2);
137
138         /* It should have died. */
139         ok1(wait(&status));
140         ok1(WIFEXITED(status));
141         ok1(WEXITSTATUS(status) < sizeof(d->buf));
142
143         /* This one shouldn't time out. */
144         d->state = 0;
145         d->timeout_usec = 500000;
146         fflush(stdout);
147
148         if (!fork()) {
149                 int i;
150
151                 io_close_listener(l);
152                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
153                             addrinfo->ai_protocol);
154                 if (fd < 0)
155                         exit(1);
156                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
157                         exit(2);
158                 signal(SIGPIPE, SIG_IGN);
159                 usleep(100000);
160                 for (i = 0; i < strlen("hellothere"); i++) {
161                         if (write(fd, "hellothere" + i, 1) != 1)
162                                 break;
163                 }
164                 close(fd);
165                 freeaddrinfo(addrinfo);
166                 timers_cleanup(&d->timers);
167                 free(d);
168                 exit(i);
169         }
170         ok1(io_loop(&d->timers, &expired) == d);
171         ok1(d->state == 3);
172         ok1(list_empty(&expired));
173         ok1(wait(&status));
174         ok1(WIFEXITED(status));
175         ok1(WEXITSTATUS(status) >= sizeof(d->buf));
176
177         io_close_listener(l);
178         freeaddrinfo(addrinfo);
179         timers_cleanup(&d->timers);
180         free(d);
181
182         /* This exits depending on whether all tests passed */
183         return exit_status();
184 }