io: change io_idle() to io_wait()
[ccan] / ccan / io / test / run-10-many.c
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>
6 #include <sys/wait.h>
7 #include <stdio.h>
8
9 #define NUM 100
10 #define NUM_ITERS 1000
11
12 struct buffer {
13         int iters;
14         struct io_conn *reader, *writer;
15         char buf[32];
16 };
17
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);
20
21 static struct io_plan read_buf(struct io_conn *conn, struct buffer *buf)
22 {
23         return io_read(&buf->buf, sizeof(buf->buf), poke_writer, buf);
24 }
25
26 static struct io_plan poke_writer(struct io_conn *conn, struct buffer *buf)
27 {
28         assert(conn == buf->reader);
29
30         if (buf->iters == NUM_ITERS)
31                 return io_close();
32
33         /* You write. */
34         io_wake(&buf->writer);
35
36         /* I'll wait until you wake me. */
37         return io_wait(&buf->reader, read_buf, buf);
38 }
39
40 static struct io_plan write_buf(struct io_conn *conn, struct buffer *buf)
41 {
42         return io_write(&buf->buf, sizeof(buf->buf), poke_reader, buf);
43 }
44
45 static struct io_plan poke_reader(struct io_conn *conn, struct buffer *buf)
46 {
47         assert(conn == buf->writer);
48         /* You read. */
49         io_wake(&buf->reader);
50
51         if (++buf->iters == NUM_ITERS)
52                 return io_close();
53
54         /* I'll wait until you tell me to write. */
55         return io_wait(&buf->writer, write_buf, buf);
56 }
57
58 static struct buffer buf[NUM];
59
60 int main(void)
61 {
62         unsigned int i;
63         int fds[2], last_read, last_write;
64
65         plan_tests(5 + NUM);
66
67         ok1(pipe(fds) == 0);
68         last_read = fds[0];
69         last_write = fds[1];
70
71         for (i = 1; i < NUM; i++) {
72                 if (pipe(fds) < 0)
73                         break;
74                 memset(buf[i].buf, i, sizeof(buf[i].buf));
75                 sprintf(buf[i].buf, "%i-%i", i, i);
76
77                 /* Wait for writer to tell us to read. */
78                 buf[i].reader = io_new_conn(last_read,
79                                             io_wait(&buf[i].reader, read_buf,
80                                                     &buf[i]));
81                 if (!buf[i].reader)
82                         break;
83                 buf[i].writer = io_new_conn(fds[1], write_buf(NULL, &buf[i]));
84                 if (!buf[i].writer)
85                         break;
86                 last_read = fds[0];
87         }
88         if (!ok1(i == NUM))
89                 exit(exit_status());
90
91         /* Last one completes the cirle. */
92         i = 0;
93         sprintf(buf[i].buf, "%i-%i", i, i);
94         buf[i].reader = io_new_conn(last_read,
95                                     io_wait(&buf[i].reader, read_buf, &buf[i]));
96         ok1(buf[i].reader);
97         buf[i].writer = io_new_conn(last_write, write_buf(NULL, &buf[i]));
98         ok1(buf[i].writer);
99
100         /* They should eventually exit */
101         ok1(io_loop() == NULL);
102
103         for (i = 0; i < NUM; i++) {
104                 char b[sizeof(buf[0].buf)];
105                 memset(b, i, sizeof(b));
106                 sprintf(b, "%i-%i", i, i);
107                 ok1(memcmp(b, buf[(i + NUM_ITERS) % NUM].buf, sizeof(b)) == 0);
108         }
109
110         /* This exits depending on whether all tests passed */
111         return exit_status();
112 }