]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Add test for ioloop fd polling
authorAki Tuomi <aki.tuomi@dovecot.fi>
Mon, 24 Oct 2016 08:34:05 +0000 (11:34 +0300)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Mon, 24 Oct 2016 11:26:18 +0000 (14:26 +0300)
src/lib/test-ioloop.c

index 7dc09d170ef8e8cd7a7f19c9469ea4c5ede4ebd4..f8e32ef1954417c9ee9abe0e7f54194a217fe1c3 100644 (file)
@@ -8,6 +8,12 @@
 
 #include <unistd.h>
 
+struct test_ctx {
+       bool got_left;
+       bool got_right;
+       bool got_to;
+};
+
 static void timeout_callback(struct timeval *tv)
 {
        if (gettimeofday(tv, NULL) < 0)
@@ -15,6 +21,74 @@ static void timeout_callback(struct timeval *tv)
        io_loop_stop(current_ioloop);
 }
 
+static void test_ioloop_fd_cb_left(struct test_ctx *ctx)
+{
+       ctx->got_left = TRUE;
+       if (ctx->got_left && ctx->got_right)
+               io_loop_stop(current_ioloop);
+}
+
+static void test_ioloop_fd_cb_right(struct test_ctx *ctx)
+{
+       ctx->got_right = TRUE;
+       if (ctx->got_left && ctx->got_right)
+               io_loop_stop(current_ioloop);
+}
+
+static void test_ioloop_fd_to(struct test_ctx *ctx)
+{
+       ctx->got_to = TRUE;
+       io_loop_stop(current_ioloop);
+}
+
+static void test_ioloop_fd(void)
+{
+       test_begin("ioloop fd");
+
+       struct test_ctx test_ctx;
+       int fds[2];
+       int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
+
+       test_assert(ret == 0);
+       if (ret < 0) {
+               i_error("socketpair() failed: %m");
+               test_end();
+               return;
+       }
+
+       memset(&test_ctx, 0, sizeof(test_ctx));
+
+       struct ioloop *ioloop = io_loop_create();
+
+       struct io *io_left =
+                       io_add(fds[0], IO_READ,
+                              test_ioloop_fd_cb_left, &test_ctx);
+       struct io *io_right =
+                       io_add(fds[1], IO_READ,
+                               test_ioloop_fd_cb_right, &test_ctx);
+
+       struct timeout *to = timeout_add(2000, test_ioloop_fd_to, &test_ctx);
+
+       (void)write(fds[0], "ltr", 3);
+       (void)write(fds[1], "rtl", 3);
+
+       io_loop_run(ioloop);
+
+       timeout_remove(&to);
+       io_remove(&io_left);
+       io_remove(&io_right);
+
+       test_assert(test_ctx.got_to == FALSE);
+       test_assert(test_ctx.got_left == TRUE);
+       test_assert(test_ctx.got_right == TRUE);
+
+       io_loop_destroy(&ioloop);
+       i_close_fd(&fds[0]);
+       i_close_fd(&fds[1]);
+
+       test_end();
+}
+
 static void test_ioloop_timeout(void)
 {
        struct ioloop *ioloop, *ioloop2;
@@ -126,4 +200,5 @@ void test_ioloop(void)
        test_ioloop_timeout();
        test_ioloop_find_fd_conditions();
        test_ioloop_pending_io();
+       test_ioloop_fd();
 }