]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-06-idle.c
ccan/io: go linear for debugging.
[ccan] / ccan / io / test / run-06-idle.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 <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11
12 #ifndef PORT
13 #define PORT "65006"
14 #endif
15
16 static struct io_conn *idler;
17
18 struct data {
19         int state;
20         char buf[4];
21 };
22
23 static struct io_plan read_done(struct io_conn *conn, struct data *d)
24 {
25         ok1(d->state == 2 || d->state == 3);
26         d->state++;
27         return io_close(conn, NULL);
28 }
29
30 static void finish_waker(struct io_conn *conn, struct data *d)
31 {
32         io_wake(idler, io_read(d->buf, sizeof(d->buf), read_done, d));
33         ok1(d->state == 1);
34         d->state++;
35 }
36
37 static void finish_idle(struct io_conn *conn, struct data *d)
38 {
39         ok1(d->state == 3);
40         d->state++;
41         io_break(d, io_idle());
42 }
43
44 static struct io_plan never(struct io_conn *conn, void *arg)
45 {
46         abort();
47 }
48
49 static void init_conn(int fd, struct data *d)
50 {
51         int fd2;
52
53         ok1(d->state == 0);
54         d->state++;
55         idler = io_new_conn(fd, io_idle(), finish_idle, d);
56
57         /* This will wake us up, as read will fail. */
58         fd2 = open("/dev/null", O_RDONLY);
59         ok1(fd2 >= 0);
60         ok1(io_new_conn(fd2, io_read(idler, 1, never, NULL), finish_waker, d));
61 }
62
63 static int make_listen_fd(const char *port, struct addrinfo **info)
64 {
65         int fd, on = 1;
66         struct addrinfo *addrinfo, hints;
67
68         memset(&hints, 0, sizeof(hints));
69         hints.ai_family = AF_UNSPEC;
70         hints.ai_socktype = SOCK_STREAM;
71         hints.ai_flags = AI_PASSIVE;
72         hints.ai_protocol = 0;
73
74         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
75                 return -1;
76
77         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
78                     addrinfo->ai_protocol);
79         if (fd < 0)
80                 return -1;
81
82         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
83         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
84                 close(fd);
85                 return -1;
86         }
87         if (listen(fd, 1) != 0) {
88                 close(fd);
89                 return -1;
90         }
91         *info = addrinfo;
92         return fd;
93 }
94
95 int main(void)
96 {
97         struct data *d = malloc(sizeof(*d));
98         struct addrinfo *addrinfo;
99         struct io_listener *l;
100         int fd, status;
101
102         /* This is how many tests you plan to run */
103         plan_tests(14);
104         d->state = 0;
105         fd = make_listen_fd(PORT, &addrinfo);
106         ok1(fd >= 0);
107         l = io_new_listener(fd, init_conn, d);
108         ok1(l);
109         fflush(stdout);
110         if (!fork()) {
111                 int i;
112
113                 io_close_listener(l);
114                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
115                             addrinfo->ai_protocol);
116                 if (fd < 0)
117                         exit(1);
118                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
119                         exit(2);
120                 signal(SIGPIPE, SIG_IGN);
121                 for (i = 0; i < strlen("hellothere"); i++) {
122                         if (write(fd, "hellothere" + i, 1) != 1)
123                                 break;
124                 }
125                 close(fd);
126                 freeaddrinfo(addrinfo);
127                 free(d);
128                 exit(0);
129         }
130         freeaddrinfo(addrinfo);
131
132         ok1(io_loop() == d);
133         ok1(d->state == 4);
134         ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
135         free(d);
136         io_close_listener(l);
137
138         ok1(wait(&status));
139         ok1(WIFEXITED(status));
140         ok1(WEXITSTATUS(status) == 0);
141
142         /* This exits depending on whether all tests passed */
143         return exit_status();
144 }