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