]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-15-timeout.c
ccan/io: use explicit IO callback functions, instead of io_state values.
[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 <sys/wait.h>
7 #include <stdio.h>
8 #include <unistd.h>
9
10 struct data {
11         int state;
12         int timeout_usec;
13         bool timed_out;
14         char buf[4];
15 };
16
17
18 static struct io_plan *no_timeout(struct io_conn *conn, struct data *d)
19 {
20         ok1(d->state == 1);
21         d->state++;
22         return io_close(conn, d);
23 }
24
25 static struct io_plan *timeout(struct io_conn *conn, struct data *d)
26 {
27         ok1(d->state == 1);
28         d->state++;
29         d->timed_out = true;
30         return io_close(conn, d);
31 }
32
33 static struct io_plan *start_ok(struct io_conn *conn, struct data *d)
34 {
35         ok1(d->state == 0);
36         d->state++;
37         io_timeout(conn, time_from_usec(d->timeout_usec), timeout, d);
38         return io_read(conn, d->buf, sizeof(d->buf), no_timeout, d);
39 }
40
41 static void finish_ok(struct io_conn *conn, struct data *d)
42 {
43         ok1(d->state == 2);
44         d->state++;
45         io_break(conn, d, NULL, NULL);
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         int fd, status;
86
87         /* This is how many tests you plan to run */
88         plan_tests(20);
89         d->state = 0;
90         d->timed_out = false;
91         d->timeout_usec = 100000;
92         fd = make_listen_fd("65002", &addrinfo);
93         ok1(fd >= 0);
94         l = io_new_listener(fd, start_ok, finish_ok, d);
95         ok1(l);
96         fflush(stdout);
97
98         if (!fork()) {
99                 int i;
100
101                 io_close_listener(l);
102                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
103                             addrinfo->ai_protocol);
104                 if (fd < 0)
105                         exit(1);
106                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
107                         exit(2);
108                 signal(SIGPIPE, SIG_IGN);
109                 usleep(500000);
110                 for (i = 0; i < strlen("hellothere"); i++) {
111                         if (write(fd, "hellothere" + i, 1) != 1)
112                                 break;
113                 }
114                 close(fd);
115                 freeaddrinfo(addrinfo);
116                 free(d);
117                 exit(i);
118         }
119         ok1(io_loop() == d);
120         ok1(d->state == 3);
121         ok1(d->timed_out == true);
122         ok1(wait(&status));
123         ok1(WIFEXITED(status));
124         ok1(WEXITSTATUS(status) < sizeof(d->buf));
125
126         /* This one shouldn't time out. */
127         d->state = 0;
128         d->timed_out = false;
129         d->timeout_usec = 500000;
130         fflush(stdout);
131
132         if (!fork()) {
133                 int i;
134
135                 io_close_listener(l);
136                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
137                             addrinfo->ai_protocol);
138                 if (fd < 0)
139                         exit(1);
140                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
141                         exit(2);
142                 signal(SIGPIPE, SIG_IGN);
143                 usleep(100000);
144                 for (i = 0; i < strlen("hellothere"); i++) {
145                         if (write(fd, "hellothere" + i, 1) != 1)
146                                 break;
147                 }
148                 close(fd);
149                 freeaddrinfo(addrinfo);
150                 free(d);
151                 exit(i);
152         }
153         ok1(io_loop() == d);
154         ok1(d->state == 3);
155         ok1(d->timed_out == false);
156         ok1(wait(&status));
157         ok1(WIFEXITED(status));
158         ok1(WEXITSTATUS(status) >= sizeof(d->buf));
159
160         io_close_listener(l);
161         freeaddrinfo(addrinfo);
162         free(d);
163
164         /* This exits depending on whether all tests passed */
165         return exit_status();
166 }