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