]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-15-timeout.c
a4ab23add0abed3b0a3bcc5d536983db458ce8b5
[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_addrel(&d->timers, &d->timer, time_from_usec(d->timeout_usec));
51
52         return io_read(conn, d->buf, sizeof(d->buf), no_timeout, d);
53 }
54
55 static int make_listen_fd(const char *port, struct addrinfo **info)
56 {
57         int fd, on = 1;
58         struct addrinfo *addrinfo, hints;
59
60         memset(&hints, 0, sizeof(hints));
61         hints.ai_family = AF_UNSPEC;
62         hints.ai_socktype = SOCK_STREAM;
63         hints.ai_flags = AI_PASSIVE;
64         hints.ai_protocol = 0;
65
66         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
67                 return -1;
68
69         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
70                     addrinfo->ai_protocol);
71         if (fd < 0)
72                 return -1;
73
74         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
75         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
76                 close(fd);
77                 return -1;
78         }
79         if (listen(fd, 1) != 0) {
80                 close(fd);
81                 return -1;
82         }
83         *info = addrinfo;
84         return fd;
85 }
86
87 int main(void)
88 {
89         struct data *d = malloc(sizeof(*d));
90         struct addrinfo *addrinfo;
91         struct io_listener *l;
92         struct timer *expired;
93         int fd, status;
94
95         /* This is how many tests you plan to run */
96         plan_tests(21);
97         d->state = 0;
98         d->timeout_usec = 100000;
99         timers_init(&d->timers, time_mono());
100         timer_init(&d->timer);
101         fd = make_listen_fd(PORT, &addrinfo);
102         ok1(fd >= 0);
103         l = io_new_listener(NULL, fd, init_conn, d);
104         ok1(l);
105         fflush(stdout);
106
107         if (!fork()) {
108                 int i;
109
110                 io_close_listener(l);
111                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
112                             addrinfo->ai_protocol);
113                 if (fd < 0)
114                         exit(1);
115                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
116                         exit(2);
117                 signal(SIGPIPE, SIG_IGN);
118                 usleep(500000);
119                 for (i = 0; i < strlen("hellothere"); i++) {
120                         if (write(fd, "hellothere" + i, 1) != 1)
121                                 break;
122                 }
123                 close(fd);
124                 freeaddrinfo(addrinfo);
125                 timers_cleanup(&d->timers);
126                 free(d);
127                 exit(i);
128         }
129         ok1(io_loop(&d->timers, &expired) == NULL);
130
131         /* One element, d->timer. */
132         ok1(expired == &d->timer);
133         ok1(!timers_expire(&d->timers, time_mono()));
134         ok1(d->state == 1);
135
136         io_close(d->conn);
137
138         /* Finished will be called, d will be returned */
139         ok1(io_loop(&d->timers, &expired) == d);
140         ok1(expired == NULL);
141         ok1(d->state == 2);
142
143         /* It should have died. */
144         ok1(wait(&status));
145         ok1(WIFEXITED(status));
146         ok1(WEXITSTATUS(status) < sizeof(d->buf));
147
148         /* This one shouldn't time out. */
149         d->state = 0;
150         d->timeout_usec = 500000;
151         fflush(stdout);
152
153         if (!fork()) {
154                 int i;
155
156                 io_close_listener(l);
157                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
158                             addrinfo->ai_protocol);
159                 if (fd < 0)
160                         exit(1);
161                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
162                         exit(2);
163                 signal(SIGPIPE, SIG_IGN);
164                 usleep(100000);
165                 for (i = 0; i < strlen("hellothere"); i++) {
166                         if (write(fd, "hellothere" + i, 1) != 1)
167                                 break;
168                 }
169                 close(fd);
170                 freeaddrinfo(addrinfo);
171                 timers_cleanup(&d->timers);
172                 free(d);
173                 exit(i);
174         }
175         ok1(io_loop(&d->timers, &expired) == d);
176         ok1(d->state == 3);
177         ok1(expired == NULL);
178         ok1(wait(&status));
179         ok1(WIFEXITED(status));
180         ok1(WEXITSTATUS(status) >= sizeof(d->buf));
181
182         io_close_listener(l);
183         freeaddrinfo(addrinfo);
184         timers_cleanup(&d->timers);
185         free(d);
186
187         /* This exits depending on whether all tests passed */
188         return exit_status();
189 }