TODO: Regression test to ensure that a tevent backend can cope with separate read...
authorJeremy Allison <jra@samba.org>
Thu, 14 Feb 2013 22:16:31 +0000 (14:16 -0800)
committerStefan Metzmacher <metze@samba.org>
Tue, 26 Feb 2013 15:44:02 +0000 (16:44 +0100)
This tests the multiplex fd changes to the epoll backend to
ensure they work correctly.

Signed-off-by: Jeremy Allison <jra@samba.org>
TODO: why (fde_wcount - fde_count < 256) ???
      if we get TEVENT_FD_WRITE we should not block
      we should also not read if we don't get TEVENT_FD_READ,
      because this will block

lib/tevent/testsuite.c

index 686e2ed9b3e96530e2d1d68b157c150c8b113766..e77124ec762f014a4f9e2dd809885464167b356d 100644 (file)
@@ -4,6 +4,7 @@
    testing of the events subsystem
 
    Copyright (C) Stefan Metzmacher 2006-2009
    testing of the events subsystem
 
    Copyright (C) Stefan Metzmacher 2006-2009
+   Copyright (C) Jeremy Allison    2013
 
      ** NOTE! The following LGPL license applies to the tevent
      ** library. This does NOT imply that all of Samba is released
 
      ** NOTE! The following LGPL license applies to the tevent
      ** library. This does NOT imply that all of Samba is released
 #endif
 
 static int fde_count;
 #endif
 
 static int fde_count;
+static int fde_wcount;
 
 
-static void fde_handler(struct tevent_context *ev_ctx, struct tevent_fd *f, 
+static void fde_handler_readwrite(struct tevent_context *ev_ctx, struct tevent_fd *f,
                        uint16_t flags, void *private_data)
 {
        int *fd = (int *)private_data;
                        uint16_t flags, void *private_data)
 {
        int *fd = (int *)private_data;
-       char c;
+       char c = 0;
 #ifdef SA_SIGINFO
        kill(getpid(), SIGUSR1);
 #endif
        kill(getpid(), SIGALRM);
 #ifdef SA_SIGINFO
        kill(getpid(), SIGUSR1);
 #endif
        kill(getpid(), SIGALRM);
-       read(fd[0], &c, 1);
-       write(fd[1], &c, 1);
-       fde_count++;
+
+       if ((flags & TEVENT_FD_WRITE) && (fde_wcount - fde_count < 256)) {
+               /* Don't fill the pipe and block... */
+               write(fd[1], &c, 1);
+               fde_wcount++;
+       } else {
+               read(fd[0], &c, 1);
+               fde_count++;
+       }
 }
 
 static void finished_handler(struct tevent_context *ev_ctx, struct tevent_timer *te,
 }
 
 static void finished_handler(struct tevent_context *ev_ctx, struct tevent_timer *te,
@@ -70,7 +78,10 @@ static bool test_event_context(struct torture_context *test,
        int fd[2] = { -1, -1 };
        const char *backend = (const char *)test_data;
        int alarm_count=0, info_count=0;
        int fd[2] = { -1, -1 };
        const char *backend = (const char *)test_data;
        int alarm_count=0, info_count=0;
-       struct tevent_fd *fde;
+       struct tevent_fd *fde_read;
+       struct tevent_fd *fde_read_1;
+       struct tevent_fd *fde_write;
+       struct tevent_fd *fde_write_1;
 #ifdef SA_RESTART
        struct tevent_signal *se1 = NULL;
 #endif
 #ifdef SA_RESTART
        struct tevent_signal *se1 = NULL;
 #endif
@@ -82,7 +93,6 @@ static bool test_event_context(struct torture_context *test,
 #endif
        int finished=0;
        struct timeval t;
 #endif
        int finished=0;
        struct timeval t;
-       char c = 0;
 
        ev_ctx = tevent_context_init_byname(test, backend);
        if (ev_ctx == NULL) {
 
        ev_ctx = tevent_context_init_byname(test, backend);
        if (ev_ctx == NULL) {
@@ -95,13 +105,23 @@ static bool test_event_context(struct torture_context *test,
 
        /* reset globals */
        fde_count = 0;
 
        /* reset globals */
        fde_count = 0;
+       fde_wcount = 0;
 
        /* create a pipe */
        pipe(fd);
 
 
        /* create a pipe */
        pipe(fd);
 
-       fde = tevent_add_fd(ev_ctx, ev_ctx, fd[0], TEVENT_FD_READ,
-                           fde_handler, fd);
-       tevent_fd_set_auto_close(fde);
+       fde_read = tevent_add_fd(ev_ctx, ev_ctx, fd[0], TEVENT_FD_READ,
+                           fde_handler_readwrite, fd);
+       fde_write_1 = tevent_add_fd(ev_ctx, ev_ctx, fd[0], TEVENT_FD_WRITE,
+                           fde_handler_readwrite, fd);
+
+       fde_write = tevent_add_fd(ev_ctx, ev_ctx, fd[1], TEVENT_FD_WRITE,
+                           fde_handler_readwrite, fd);
+       fde_read_1 = tevent_add_fd(ev_ctx, ev_ctx, fd[1], TEVENT_FD_READ,
+                           fde_handler_readwrite, fd);
+
+       tevent_fd_set_auto_close(fde_read);
+       tevent_fd_set_auto_close(fde_write);
 
        tevent_add_timer(ev_ctx, ev_ctx, timeval_current_ofs(2,0),
                         finished_handler, &finished);
 
        tevent_add_timer(ev_ctx, ev_ctx, timeval_current_ofs(2,0),
                         finished_handler, &finished);
@@ -116,8 +136,6 @@ static bool test_event_context(struct torture_context *test,
        se3 = tevent_add_signal(ev_ctx, ev_ctx, SIGUSR1, SA_SIGINFO, count_handler, &info_count);
 #endif
 
        se3 = tevent_add_signal(ev_ctx, ev_ctx, SIGUSR1, SA_SIGINFO, count_handler, &info_count);
 #endif
 
-       write(fd[1], &c, 1);
-
        t = timeval_current();
        while (!finished) {
                errno = 0;
        t = timeval_current();
        while (!finished) {
                errno = 0;
@@ -127,8 +145,10 @@ static bool test_event_context(struct torture_context *test,
                }
        }
 
                }
        }
 
-       talloc_free(fde);
-       close(fd[1]);
+       talloc_free(fde_read);
+       talloc_free(fde_write);
+       talloc_free(fde_read_1);
+       talloc_free(fde_write_1);
 
        while (alarm_count < fde_count+1) {
                if (tevent_loop_once(ev_ctx) == -1) {
 
        while (alarm_count < fde_count+1) {
                if (tevent_loop_once(ev_ctx) == -1) {
@@ -142,7 +162,7 @@ static bool test_event_context(struct torture_context *test,
        talloc_free(se1);
 #endif
 
        talloc_free(se1);
 #endif
 
-       torture_assert_int_equal(test, alarm_count, 1+fde_count, "alarm count mismatch");
+       torture_assert_int_equal(test, alarm_count, 1+fde_count+fde_wcount, "alarm count mismatch");
 
 #ifdef SA_RESETHAND
        talloc_free(se2);
 
 #ifdef SA_RESETHAND
        talloc_free(se2);
@@ -150,7 +170,7 @@ static bool test_event_context(struct torture_context *test,
 
 #ifdef SA_SIGINFO
        talloc_free(se3);
 
 #ifdef SA_SIGINFO
        talloc_free(se3);
-       torture_assert_int_equal(test, info_count, fde_count, "info count mismatch");
+       torture_assert_int_equal(test, info_count, fde_count+fde_wcount, "info count mismatch");
 #endif
 
        talloc_free(ev_ctx);
 #endif
 
        talloc_free(ev_ctx);