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>
12 struct io_listener *l;
18 static struct io_plan *read_more(struct io_conn *conn, struct data *d);
19 static struct io_plan *write_more(struct io_conn *conn, struct data *d);
21 static struct io_plan *read_done(struct io_conn *conn, struct data *d)
23 tal_resize(&d->pattern, tal_count(d->pattern) + 1 + strlen(d->buf));
24 strcat(d->pattern, "<");
25 strcat(d->pattern, d->buf);
26 return read_more(conn, d);
29 static struct io_plan *read_more(struct io_conn *conn, struct data *d)
31 memset(d->buf, 0, sizeof(d->buf));
32 return io_read_partial(conn, d->buf, sizeof(d->buf), &d->buflen,
36 static struct io_plan *write_done(struct io_conn *conn, struct data *d)
38 tal_resize(&d->pattern, tal_count(d->pattern) + 1);
39 strcat(d->pattern, ">");
40 return write_more(conn, d);
43 static struct io_plan *write_more(struct io_conn *conn, struct data *d)
45 return io_write_partial(conn, d->buf, 1, &d->buflen,
49 static struct io_plan *read_priority_init(struct io_conn *conn, struct data *d)
51 /* This should suppress the write */
52 ok1(io_conn_exclusive(conn, true));
53 return read_more(conn, d);
56 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
58 /* Free listener so when conns close we exit io_loop */
59 io_close_listener(d->l);
61 return io_duplex(conn, read_priority_init(conn, d), write_more(conn, d));
65 static int make_listen_fd(const char *port, struct addrinfo **info)
68 struct addrinfo *addrinfo, hints;
70 memset(&hints, 0, sizeof(hints));
71 hints.ai_family = AF_UNSPEC;
72 hints.ai_socktype = SOCK_STREAM;
73 hints.ai_flags = AI_PASSIVE;
74 hints.ai_protocol = 0;
76 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
79 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
80 addrinfo->ai_protocol);
84 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
85 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
89 if (listen(fd, 1) != 0) {
99 struct addrinfo *addrinfo = NULL;
103 /* This is how many tests you plan to run */
105 fd = make_listen_fd(PORT, &addrinfo);
107 d.l = io_new_listener(NULL, fd, init_conn, &d);
112 io_close_listener(d.l);
113 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
114 addrinfo->ai_protocol);
117 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
119 signal(SIGPIPE, SIG_IGN);
121 if (write(fd, "1hellothere", strlen("1hellothere")) != strlen("1hellothere"))
124 if (write(fd, "1helloagain", strlen("1helloagain")) != strlen("1helloagain"))
127 freeaddrinfo(addrinfo);
130 freeaddrinfo(addrinfo);
132 d.pattern = tal_arrz(NULL, char, 1);
133 ok1(io_loop(NULL, NULL) == NULL);
134 /* No trace of writes */
135 ok1(strcmp(d.pattern, "<1hellothere<1helloagain") == 0);
139 ok1(WIFEXITED(status));
140 ok1(WEXITSTATUS(status) == 0);
142 /* This exits depending on whether all tests passed */
143 return exit_status();