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