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);
19 static struct io_plan *poke_writer(struct io_conn *conn, struct buffer *buf);
21 static struct io_plan *read_buf(struct io_conn *conn, struct buffer *buf)
23 return io_read(conn, &buf->buf, sizeof(buf->buf), poke_writer, buf);
26 static struct io_plan *poke_writer(struct io_conn *conn, struct buffer *buf)
28 assert(conn == buf->reader);
30 if (buf->iters == NUM_ITERS)
31 return io_close(conn);
34 io_wake(&buf->writer);
36 /* I'll wait until you wake me. */
37 return io_wait(conn, &buf->reader, read_buf, buf);
40 static struct io_plan *write_buf(struct io_conn *conn, struct buffer *buf)
42 return io_write(conn, &buf->buf, sizeof(buf->buf), poke_reader, buf);
45 static struct io_plan *poke_reader(struct io_conn *conn, struct buffer *buf)
47 assert(conn == buf->writer);
49 io_wake(&buf->reader);
51 if (++buf->iters == NUM_ITERS)
52 return io_close(conn);
54 /* I'll wait until you tell me to write. */
55 return io_wait(conn, &buf->writer, write_buf, buf);
58 static struct io_plan *setup_reader(struct io_conn *conn, struct buffer *buf)
60 return io_wait(conn, &buf->reader, read_buf, buf);
63 static struct buffer buf[NUM];
68 int fds[2], last_read, last_write;
76 for (i = 1; i < NUM; i++) {
79 memset(buf[i].buf, i, sizeof(buf[i].buf));
80 sprintf(buf[i].buf, "%i-%i", i, i);
82 /* Wait for writer to tell us to read. */
83 buf[i].reader = io_new_conn(NULL, last_read,
84 setup_reader, &buf[i]);
87 buf[i].writer = io_new_conn(NULL, fds[1], write_buf, &buf[i]);
95 /* Last one completes the cirle. */
97 sprintf(buf[i].buf, "%i-%i", i, i);
98 buf[i].reader = io_new_conn(NULL, last_read, setup_reader, &buf[i]);
100 buf[i].writer = io_new_conn(NULL, last_write, write_buf, &buf[i]);
103 /* They should eventually exit */
104 ok1(io_loop(NULL, NULL) == NULL);
106 for (i = 0; i < NUM; i++) {
107 char b[sizeof(buf[0].buf)];
108 memset(b, i, sizeof(b));
109 sprintf(b, "%i-%i", i, i);
110 ok1(memcmp(b, buf[(i + NUM_ITERS) % NUM].buf, sizeof(b)) == 0);
113 /* This exits depending on whether all tests passed */
114 return exit_status();