]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-41-io_poll_override.c
0a62e2d3bf162c108949e387e787f8a1c6d49401
[ccan] / ccan / io / test / run-41-io_poll_override.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 PORT "65020"
10
11 /* Should be looking to read from one fd. */
12 static int mypoll(struct pollfd *fds, nfds_t nfds, int timeout)
13 {
14         ok1(nfds == 1);
15         ok1(fds[0].fd >= 0);
16         ok1(fds[0].events & POLLIN);
17         ok1(!(fds[0].events & POLLOUT));
18
19         /* Pretend it's readable. */
20         fds[0].revents = POLLIN;
21         return 1;
22 }
23
24 static int check_cant_read(int fd, struct io_plan_arg *arg)
25 {
26         char c;
27         ssize_t ret = read(fd, &c, 1);
28
29         ok1(errno == EAGAIN || errno == EWOULDBLOCK);
30         ok1(ret == -1);
31
32         return 1;
33 }
34
35 static struct io_plan *setup_conn(struct io_conn *conn, void *unused)
36 {
37         /* Need to get this to mark it IO_POLLING */
38         io_plan_arg(conn, IO_IN);
39         return io_set_plan(conn, IO_IN, check_cant_read, io_close_cb, NULL);
40 }
41
42 int main(void)
43 {
44         int fds[2];
45
46         plan_tests(8);
47
48         pipe(fds);
49         ok1(io_poll_override(mypoll) == poll);
50
51         io_new_conn(NULL, fds[0], setup_conn, NULL);
52         ok1(io_loop(NULL, NULL) == NULL);
53         close(fds[1]);
54
55         /* This exits depending on whether all tests passed */
56         return exit_status();
57 }