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>
10 #define NUM_ITERS 1000
14 struct io_conn *reader, *writer;
18 static struct io_plan poke_reader(struct io_conn *conn, struct buffer *buf);
20 static struct io_plan poke_writer(struct io_conn *conn, struct buffer *buf)
22 assert(conn == buf->reader);
24 if (buf->iters == NUM_ITERS)
25 return io_close(conn, NULL);
29 io_write(&buf->buf, sizeof(buf->buf), poke_reader, buf));
31 /* I'll wait until you wake me. */
35 static struct io_plan poke_reader(struct io_conn *conn, struct buffer *buf)
37 assert(conn == buf->writer);
40 io_read(&buf->buf, sizeof(buf->buf), poke_writer, buf));
42 if (++buf->iters == NUM_ITERS)
43 return io_close(conn, NULL);
45 /* I'll wait until you tell me to write. */
49 static struct buffer buf[NUM];
54 int fds[2], last_read, last_write;
62 for (i = 1; i < NUM; i++) {
65 memset(buf[i].buf, i, sizeof(buf[i].buf));
66 sprintf(buf[i].buf, "%i-%i", i, i);
68 /* Wait for writer to tell us to read. */
69 buf[i].reader = io_new_conn(last_read, io_idle(), NULL, &buf[i]);
72 buf[i].writer = io_new_conn(fds[1],
75 poke_reader, &buf[i]),
84 /* Last one completes the cirle. */
86 sprintf(buf[i].buf, "%i-%i", i, i);
87 buf[i].reader = io_new_conn(last_read, io_idle(), NULL, NULL);
89 buf[i].writer = io_new_conn(last_write,
90 io_write(&buf[i].buf, sizeof(buf[i].buf),
91 poke_reader, &buf[i]),
95 /* They should eventually exit */
96 ok1(io_loop() == NULL);
98 for (i = 0; i < NUM; i++) {
99 char b[sizeof(buf[0].buf)];
100 memset(b, i, sizeof(b));
101 sprintf(b, "%i-%i", i, i);
102 ok1(memcmp(b, buf[(i + NUM_ITERS) % NUM].buf, sizeof(b)) == 0);
105 /* This exits depending on whether all tests passed */
106 return exit_status();