]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-15-timeout.c
ccan/io: generic init function for listening connections.
[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(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(d, NULL, NULL);
46 }
47
48 static void init_conn(int fd, struct data *d)
49 {
50         if (!io_new_conn(fd, start_ok, finish_ok, d))
51                 abort();
52 }
53
54 static int make_listen_fd(const char *port, struct addrinfo **info)
55 {
56         int fd, on = 1;
57         struct addrinfo *addrinfo, hints;
58
59         memset(&hints, 0, sizeof(hints));
60         hints.ai_family = AF_UNSPEC;
61         hints.ai_socktype = SOCK_STREAM;
62         hints.ai_flags = AI_PASSIVE;
63         hints.ai_protocol = 0;
64
65         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
66                 return -1;
67
68         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
69                     addrinfo->ai_protocol);
70         if (fd < 0)
71                 return -1;
72
73         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
74         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
75                 close(fd);
76                 return -1;
77         }
78         if (listen(fd, 1) != 0) {
79                 close(fd);
80                 return -1;
81         }
82         *info = addrinfo;
83         return fd;
84 }
85
86 int main(void)
87 {
88         struct data *d = malloc(sizeof(*d));
89         struct addrinfo *addrinfo;
90         struct io_listener *l;
91         int fd, status;
92
93         /* This is how many tests you plan to run */
94         plan_tests(20);
95         d->state = 0;
96         d->timed_out = false;
97         d->timeout_usec = 100000;
98         fd = make_listen_fd("65002", &addrinfo);
99         ok1(fd >= 0);
100         l = io_new_listener(fd, init_conn, d);
101         ok1(l);
102         fflush(stdout);
103
104         if (!fork()) {
105                 int i;
106
107                 io_close_listener(l);
108                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
109                             addrinfo->ai_protocol);
110                 if (fd < 0)
111                         exit(1);
112                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
113                         exit(2);
114                 signal(SIGPIPE, SIG_IGN);
115                 usleep(500000);
116                 for (i = 0; i < strlen("hellothere"); i++) {
117                         if (write(fd, "hellothere" + i, 1) != 1)
118                                 break;
119                 }
120                 close(fd);
121                 freeaddrinfo(addrinfo);
122                 free(d);
123                 exit(i);
124         }
125         ok1(io_loop() == d);
126         ok1(d->state == 3);
127         ok1(d->timed_out == true);
128         ok1(wait(&status));
129         ok1(WIFEXITED(status));
130         ok1(WEXITSTATUS(status) < sizeof(d->buf));
131
132         /* This one shouldn't time out. */
133         d->state = 0;
134         d->timed_out = false;
135         d->timeout_usec = 500000;
136         fflush(stdout);
137
138         if (!fork()) {
139                 int i;
140
141                 io_close_listener(l);
142                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
143                             addrinfo->ai_protocol);
144                 if (fd < 0)
145                         exit(1);
146                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
147                         exit(2);
148                 signal(SIGPIPE, SIG_IGN);
149                 usleep(100000);
150                 for (i = 0; i < strlen("hellothere"); i++) {
151                         if (write(fd, "hellothere" + i, 1) != 1)
152                                 break;
153                 }
154                 close(fd);
155                 freeaddrinfo(addrinfo);
156                 free(d);
157                 exit(i);
158         }
159         ok1(io_loop() == d);
160         ok1(d->state == 3);
161         ok1(d->timed_out == false);
162         ok1(wait(&status));
163         ok1(WIFEXITED(status));
164         ok1(WEXITSTATUS(status) >= sizeof(d->buf));
165
166         io_close_listener(l);
167         freeaddrinfo(addrinfo);
168         free(d);
169
170         /* This exits depending on whether all tests passed */
171         return exit_status();
172 }