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>
9 static size_t bytes_written;
11 /* Should be called multiple times, since only writes 1 byte. */
12 static int do_controlled_write(int fd, struct io_plan_arg *arg)
16 ret = write(fd, arg->u1.cp, 1);
22 return arg->u2.s == 0;
25 static int do_error(int fd, struct io_plan_arg *arg)
31 static struct io_plan *conn_wait(struct io_conn *conn, void *unused)
33 return io_wait(conn, conn, io_never, NULL);
36 static struct io_plan *init_conn_writer(struct io_conn *conn, const char *str)
38 struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
40 arg->u1.const_vp = str;
41 arg->u2.s = strlen(str);
43 return io_set_plan(conn, IO_OUT, do_controlled_write, conn_wait, NULL);
46 static struct io_plan *init_conn_reader(struct io_conn *conn, void *dst)
48 /* Never actually succeeds. */
49 return io_read(conn, dst, 1000, io_never, NULL);
52 static struct io_plan *init_conn_error(struct io_conn *conn, void *unused)
54 io_plan_arg(conn, IO_OUT);
55 return io_set_plan(conn, IO_OUT, do_error, io_never, NULL);
60 int fd = open("/dev/null", O_RDWR);
61 const tal_t *ctx = tal(NULL, char);
64 /* This is how many tests you plan to run */
67 conn = io_new_conn(ctx, fd, init_conn_writer, "hello");
68 ok1(bytes_written == 0);
70 ok1(io_flush_sync(conn));
71 ok1(bytes_written == strlen("hello"));
73 /* This won't do anything */
74 ok1(io_flush_sync(conn));
75 ok1(bytes_written == strlen("hello"));
77 /* It's reading, this won't do anything. */
78 conn = io_new_conn(ctx, fd, init_conn_reader, ctx);
79 ok1(io_flush_sync(conn));
80 ok1(bytes_written == strlen("hello"));
82 /* Now test error state. */
83 conn = io_new_conn(ctx, fd, init_conn_error, ctx);
84 ok1(!io_flush_sync(conn));
89 /* This exits depending on whether all tests passed */