void custom_reader() { struct poller *p = poller_new(1000); int counter = 13; char buf[1000]; struct buf b = buf_wrap((void *)buf, 1000); int pipefds[2]; if (pipe(pipefds) != 0) perrpanic("pipe"); if (fcntl(pipefds[0], F_SETFL, O_NONBLOCK) == -1) perrpanic("fcntl"); if (fcntl(pipefds[1], F_SETFL, O_NONBLOCK) == -1) perrpanic("fcntl"); if (write(pipefds[1], "z", 1) != 1) perrpanic("write asyn pipe"); /* make sure pipe never blocks */ copier_add(p, a_reader_make(&counter, pipefds[0]), buf_writer_make(&b, pipefds[1]), 1000); while (poller_poll(p, -1)); ok(strcmp(buf, "aaaaaaaaaaaaa") == 0, "a_writer produced %s", buf); poller_delete(p); }
static void simple_copier_test() { int src; int sink; struct poller *p = poller_new(1000); system("echo bobo >/tmp/test_copier"); src = open("/tmp/test_copier", O_RDONLY); if (src < 0) { perrpanic("open src"); } sink = open("/tmp/test_copier_out", O_CREAT|O_TRUNC|O_WRONLY, 0777); if (sink < 0) { perrpanic("open dst"); } copier_add(p, reader_wrap(src), writer_wrap(sink), 1000); while (poller_poll(p, -1)); ok1(system("[ \"`cat /tmp/test_copier_out`\" = 'bobo' ]") == 0); system("rm /tmp/test_copier /tmp/test_copier_out"); close(src); close(sink); poller_delete(p); }
static void emulate_full_buf() { int src; int sink; struct poller *p = poller_new(1000); system("echo 1234567890abcdefghijklmnopqrstuvwxyz >/tmp/test_copier_full_buf"); src = open("/tmp/test_copier_full_buf", O_RDONLY); if (src < 0) { perrpanic("open src"); } sink = open("/tmp/test_copier_full_buf_out", O_CREAT|O_TRUNC|O_WRONLY, 0777); if (sink < 0) { perrpanic("open dst"); } copier_add(p, reader_wrap(src), writer_wrap(sink), 1000); while (poller_poll(p, -1)); ok1(system("A=\"`cat /tmp/test_copier_full_buf_out`\";" " [ \"$A\" = '1234567890abcdefghijklmnopqrstuvwxyz' ] || (echo $A;false)") == 0); system("rm /tmp/test_copier_full_buf /tmp/test_copier_full_buf_out"); close(src); close(sink); poller_delete(p); }
static void poller_loop(void *d) { struct poller *p = d; while (!global_shutdown) poller_poll(p, 100); }