1 /* Check a bug where we have just completed a read, then set up a duplex
2 * which tries to do a read. */
3 #include <ccan/io/io.h>
4 /* Include the C files directly. */
5 #include <ccan/io/poll.c>
6 #include <ccan/io/io.c>
7 #include <ccan/tap/tap.h>
16 struct io_listener *l;
18 struct io_conn *c1, *c2;
23 static void finish_ok(struct io_conn *conn, struct data *d)
28 static struct io_plan end(struct io_conn *conn, struct data *d)
32 /* last one out closes. */
33 if (conn == d->c1 && io_is_idle(d->c2))
36 /* last one out closes. */
37 if (conn == d->c2 && io_is_idle(d->c1))
43 static struct io_plan make_duplex(struct io_conn *conn, struct data *d)
45 /* Have duplex read the rest of the buffer. */
46 d->c2 = io_duplex(conn, io_read(d->buf+1, sizeof(d->buf)-1, end, d));
48 io_set_finish(d->c2, finish_ok, d);
50 return io_write(d->wbuf, sizeof(d->wbuf), end, d);
53 static void init_conn(int fd, struct data *d)
58 io_close_listener(d->l);
60 memset(d->wbuf, 7, sizeof(d->wbuf));
62 d->c1 = io_new_conn(fd, io_read(d->buf, 1, make_duplex, d));
63 io_set_finish(d->c1, finish_ok, d);
66 static int make_listen_fd(const char *port, struct addrinfo **info)
69 struct addrinfo *addrinfo, hints;
71 memset(&hints, 0, sizeof(hints));
72 hints.ai_family = AF_UNSPEC;
73 hints.ai_socktype = SOCK_STREAM;
74 hints.ai_flags = AI_PASSIVE;
75 hints.ai_protocol = 0;
77 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
80 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
81 addrinfo->ai_protocol);
85 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
86 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
90 if (listen(fd, 1) != 0) {
100 struct data *d = malloc(sizeof(*d));
101 struct addrinfo *addrinfo;
104 /* This is how many tests you plan to run */
107 fd = make_listen_fd(PORT, &addrinfo);
109 d->l = io_new_listener(fd, init_conn, d);
116 io_close_listener(d->l);
118 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
119 addrinfo->ai_protocol);
122 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
124 signal(SIGPIPE, SIG_IGN);
125 for (i = 0; i < strlen("hellothere"); i++) {
126 if (write(fd, "hellothere" + i, 1) != 1)
129 for (i = 0; i < 32; i++) {
130 if (read(fd, buf+i, 1) != 1)
134 freeaddrinfo(addrinfo);
137 freeaddrinfo(addrinfo);
138 ok1(io_loop() == NULL);
140 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
144 ok1(WIFEXITED(status));
145 ok1(WEXITSTATUS(status) == 0);
147 /* This exits depending on whether all tests passed */
148 return exit_status();