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)
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)
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());
72 buf[i].writer = io_new_conn(fds[1],
75 poke_reader, &buf[i]));
83 /* Last one completes the cirle. */
85 sprintf(buf[i].buf, "%i-%i", i, i);
86 buf[i].reader = io_new_conn(last_read, io_idle());
88 buf[i].writer = io_new_conn(last_write,
89 io_write(&buf[i].buf, sizeof(buf[i].buf),
90 poke_reader, &buf[i]));
93 /* They should eventually exit */
94 ok1(io_loop() == NULL);
96 for (i = 0; i < NUM; i++) {
97 char b[sizeof(buf[0].buf)];
98 memset(b, i, sizeof(b));
99 sprintf(b, "%i-%i", i, i);
100 ok1(memcmp(b, buf[(i + NUM_ITERS) % NUM].buf, sizeof(b)) == 0);
103 /* This exits depending on whether all tests passed */
104 return exit_status();