2 Unix SMB/CIFS implementation.
4 testing of the events subsystem
6 Copyright (C) Stefan Metzmacher 2006-2009
7 Copyright (C) Jeremy Allison 2013
9 ** NOTE! The following LGPL license applies to the tevent
10 ** library. This does NOT imply that all of Samba is released
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include "lib/tevent/tevent.h"
29 #include "system/filesys.h"
30 #include "system/select.h"
31 #include "system/network.h"
32 #include "torture/torture.h"
39 static int fde_wcount;
41 static void fde_handler_readwrite(struct tevent_context *ev_ctx, struct tevent_fd *f,
42 uint16_t flags, void *private_data)
44 int *fd = (int *)private_data;
47 kill(getpid(), SIGUSR1);
49 kill(getpid(), SIGALRM);
51 if ((flags & TEVENT_FD_WRITE) && (fde_wcount - fde_count < 256)) {
52 /* Don't fill the pipe and block... */
61 static void finished_handler(struct tevent_context *ev_ctx, struct tevent_timer *te,
62 struct timeval tval, void *private_data)
64 int *finished = (int *)private_data;
68 static void count_handler(struct tevent_context *ev_ctx, struct tevent_signal *te,
69 int signum, int count, void *info, void *private_data)
71 int *countp = (int *)private_data;
75 static bool test_event_context(struct torture_context *test,
76 const void *test_data)
78 struct tevent_context *ev_ctx;
79 int fd[2] = { -1, -1 };
80 const char *backend = (const char *)test_data;
81 int alarm_count=0, info_count=0;
82 struct tevent_fd *fde_read;
83 struct tevent_fd *fde_read_1;
84 struct tevent_fd *fde_write;
85 struct tevent_fd *fde_write_1;
87 struct tevent_signal *se1 = NULL;
90 struct tevent_signal *se2 = NULL;
93 struct tevent_signal *se3 = NULL;
98 ev_ctx = tevent_context_init_byname(test, backend);
100 torture_comment(test, "event backend '%s' not supported\n", backend);
104 torture_comment(test, "backend '%s' - %s\n",
105 backend, __FUNCTION__);
114 fde_read = tevent_add_fd(ev_ctx, ev_ctx, fd[0], TEVENT_FD_READ,
115 fde_handler_readwrite, fd);
116 fde_write_1 = tevent_add_fd(ev_ctx, ev_ctx, fd[0], TEVENT_FD_WRITE,
117 fde_handler_readwrite, fd);
119 fde_write = tevent_add_fd(ev_ctx, ev_ctx, fd[1], TEVENT_FD_WRITE,
120 fde_handler_readwrite, fd);
121 fde_read_1 = tevent_add_fd(ev_ctx, ev_ctx, fd[1], TEVENT_FD_READ,
122 fde_handler_readwrite, fd);
124 tevent_fd_set_auto_close(fde_read);
125 tevent_fd_set_auto_close(fde_write);
127 tevent_add_timer(ev_ctx, ev_ctx, timeval_current_ofs(2,0),
128 finished_handler, &finished);
131 se1 = tevent_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESTART, count_handler, &alarm_count);
132 torture_assert(test, se1 != NULL, "failed to setup se1");
135 se2 = tevent_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESETHAND, count_handler, &alarm_count);
136 torture_assert(test, se2 != NULL, "failed to setup se2");
139 se3 = tevent_add_signal(ev_ctx, ev_ctx, SIGUSR1, SA_SIGINFO, count_handler, &info_count);
140 torture_assert(test, se3 != NULL, "failed to setup se3");
143 t = timeval_current();
146 if (tevent_loop_once(ev_ctx) == -1) {
148 torture_fail(test, talloc_asprintf(test, "Failed event loop %s\n", strerror(errno)));
152 talloc_free(fde_read);
153 talloc_free(fde_write);
154 talloc_free(fde_read_1);
155 talloc_free(fde_write_1);
157 while (alarm_count < fde_count+1) {
158 if (tevent_loop_once(ev_ctx) == -1) {
163 torture_comment(test, "Got %.2f pipe events/sec\n", fde_count/timeval_elapsed(&t));
169 torture_assert_int_equal(test, alarm_count, 1+fde_count+fde_wcount, "alarm count mismatch");
173 * we do not call talloc_free(se2)
174 * because it is already gone,
175 * after triggering the event handler.
181 torture_assert_int_equal(test, info_count, fde_count+fde_wcount, "info count mismatch");
189 struct test_event_fd1_state {
190 struct torture_context *tctx;
192 struct tevent_context *ev;
194 struct tevent_timer *te;
195 struct tevent_fd *fde0;
196 struct tevent_fd *fde1;
204 static void test_event_fd1_fde_handler(struct tevent_context *ev_ctx,
205 struct tevent_fd *fde,
209 struct test_event_fd1_state *state =
210 (struct test_event_fd1_state *)private_data;
212 if (!state->got_write) {
215 if (flags != TEVENT_FD_WRITE) {
216 state->finished = true;
217 state->error = __location__;
220 state->got_write = true;
223 * we write to the other socket...
225 write(state->sock[1], &c, 1);
226 TEVENT_FD_NOT_WRITEABLE(fde);
227 TEVENT_FD_READABLE(fde);
231 if (!state->got_read) {
232 if (flags != TEVENT_FD_READ) {
233 state->finished = true;
234 state->error = __location__;
237 state->got_read = true;
239 TEVENT_FD_NOT_READABLE(fde);
243 state->finished = true;
244 state->error = __location__;
248 static void test_event_fd1_finished(struct tevent_context *ev_ctx,
249 struct tevent_timer *te,
253 struct test_event_fd1_state *state =
254 (struct test_event_fd1_state *)private_data;
256 if (!state->got_write) {
257 state->finished = true;
258 state->error = __location__;
262 if (!state->got_read) {
263 state->finished = true;
264 state->error = __location__;
269 if (state->loop_count > 2) {
270 state->finished = true;
274 state->got_write = false;
275 state->got_read = false;
277 tevent_fd_set_flags(state->fde0, TEVENT_FD_WRITE);
279 state->te = tevent_add_timer(state->ev, state->ev,
280 timeval_current_ofs(0,1000),
281 test_event_fd1_finished, state);
284 static bool test_event_fd1(struct torture_context *tctx,
285 const void *test_data)
287 struct test_event_fd1_state state;
291 state.backend = (const char *)test_data;
293 state.ev = tevent_context_init_byname(tctx, state.backend);
294 if (state.ev == NULL) {
295 torture_skip(tctx, talloc_asprintf(tctx,
296 "event backend '%s' not supported\n",
301 tevent_set_debug_stderr(state.ev);
302 torture_comment(tctx, "backend '%s' - %s\n",
303 state.backend, __FUNCTION__);
306 * This tests the following:
308 * It monitors the state of state.sock[0]
309 * with tevent_fd, but we never read/write on state.sock[0]
310 * while state.sock[1] * is only used to write a few bytes.
313 * - we wait only for TEVENT_FD_WRITE on state.sock[0]
314 * - we write 1 byte to state.sock[1]
315 * - we wait only for TEVENT_FD_READ on state.sock[0]
316 * - we disable events on state.sock[0]
317 * - the timer event restarts the loop
321 socketpair(AF_UNIX, SOCK_STREAM, 0, state.sock);
323 state.te = tevent_add_timer(state.ev, state.ev,
324 timeval_current_ofs(0,1000),
325 test_event_fd1_finished, &state);
326 state.fde0 = tevent_add_fd(state.ev, state.ev,
327 state.sock[0], TEVENT_FD_WRITE,
328 test_event_fd1_fde_handler, &state);
329 /* state.fde1 is only used to auto close */
330 state.fde1 = tevent_add_fd(state.ev, state.ev,
332 test_event_fd1_fde_handler, &state);
334 tevent_fd_set_auto_close(state.fde0);
335 tevent_fd_set_auto_close(state.fde1);
337 while (!state.finished) {
339 if (tevent_loop_once(state.ev) == -1) {
340 talloc_free(state.ev);
341 torture_fail(tctx, talloc_asprintf(tctx,
342 "Failed event loop %s\n",
347 talloc_free(state.ev);
349 torture_assert(tctx, state.error == NULL, talloc_asprintf(tctx,
355 struct test_event_fd2_state {
356 struct torture_context *tctx;
358 struct tevent_context *ev;
359 struct tevent_timer *te;
360 struct test_event_fd2_sock {
361 struct test_event_fd2_state *state;
363 struct tevent_fd *fde;
372 static void test_event_fd2_sock_handler(struct tevent_context *ev_ctx,
373 struct tevent_fd *fde,
377 struct test_event_fd2_sock *cur_sock =
378 (struct test_event_fd2_sock *)private_data;
379 struct test_event_fd2_state *state = cur_sock->state;
380 struct test_event_fd2_sock *oth_sock = NULL;
384 if (cur_sock == &state->sock0) {
385 oth_sock = &state->sock1;
387 oth_sock = &state->sock0;
390 if (oth_sock->num_written == 1) {
391 if (flags != (TEVENT_FD_READ | TEVENT_FD_WRITE)) {
392 state->finished = true;
393 state->error = __location__;
398 if (cur_sock->num_read == oth_sock->num_written) {
399 state->finished = true;
400 state->error = __location__;
404 if (!(flags & TEVENT_FD_READ)) {
405 state->finished = true;
406 state->error = __location__;
410 if (oth_sock->num_read > 0) {
412 * There should be room to write a byte again
414 if (!(flags & TEVENT_FD_WRITE)) {
415 state->finished = true;
416 state->error = __location__;
421 if ((flags & TEVENT_FD_WRITE) && !cur_sock->got_full) {
422 v = (uint8_t)cur_sock->num_written;
423 ret = write(cur_sock->fd, &v, 1);
425 state->finished = true;
426 state->error = __location__;
429 cur_sock->num_written++;
430 if (cur_sock->num_written > 0x80000000) {
431 state->finished = true;
432 state->error = __location__;
438 if (!cur_sock->got_full) {
439 cur_sock->got_full = true;
441 if (!oth_sock->got_full) {
444 * lets wait for oth_sock
447 tevent_fd_set_flags(cur_sock->fde, 0);
452 * oth_sock waited for cur_sock,
455 tevent_fd_set_flags(oth_sock->fde,
456 TEVENT_FD_READ|TEVENT_FD_WRITE);
459 ret = read(cur_sock->fd, &v, 1);
461 state->finished = true;
462 state->error = __location__;
465 c = (uint8_t)cur_sock->num_read;
467 state->finished = true;
468 state->error = __location__;
471 cur_sock->num_read++;
473 if (cur_sock->num_read < oth_sock->num_written) {
474 /* there is more to read */
478 * we read everything, we need to remove TEVENT_FD_WRITE
481 TEVENT_FD_NOT_WRITEABLE(cur_sock->fde);
483 if (oth_sock->num_read == cur_sock->num_written) {
485 * both directions are finished
487 state->finished = true;
493 static void test_event_fd2_finished(struct tevent_context *ev_ctx,
494 struct tevent_timer *te,
498 struct test_event_fd2_state *state =
499 (struct test_event_fd2_state *)private_data;
502 * this should never be triggered
504 state->finished = true;
505 state->error = __location__;
508 static bool test_event_fd2(struct torture_context *tctx,
509 const void *test_data)
511 struct test_event_fd2_state state;
517 state.backend = (const char *)test_data;
519 state.ev = tevent_context_init_byname(tctx, state.backend);
520 if (state.ev == NULL) {
521 torture_skip(tctx, talloc_asprintf(tctx,
522 "event backend '%s' not supported\n",
527 tevent_set_debug_stderr(state.ev);
528 torture_comment(tctx, "backend '%s' - %s\n",
529 state.backend, __FUNCTION__);
532 * This tests the following
534 * - We write 1 byte to each socket
535 * - We wait for TEVENT_FD_READ/WRITE on both sockets
536 * - When we get TEVENT_FD_WRITE we write 1 byte
540 socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
543 * the timer should never expire
545 state.te = tevent_add_timer(state.ev, state.ev,
546 timeval_current_ofs(600, 0),
547 test_event_fd2_finished, &state);
548 state.sock0.state = &state;
549 state.sock0.fd = sock[0];
550 state.sock0.fde = tevent_add_fd(state.ev, state.ev,
552 TEVENT_FD_READ | TEVENT_FD_WRITE,
553 test_event_fd2_sock_handler,
555 state.sock1.state = &state;
556 state.sock1.fd = sock[1];
557 state.sock1.fde = tevent_add_fd(state.ev, state.ev,
559 TEVENT_FD_READ | TEVENT_FD_WRITE,
560 test_event_fd2_sock_handler,
563 tevent_fd_set_auto_close(state.sock0.fde);
564 tevent_fd_set_auto_close(state.sock1.fde);
566 write(state.sock0.fd, &c, 1);
567 state.sock0.num_written++;
568 write(state.sock1.fd, &c, 1);
569 state.sock1.num_written++;
571 while (!state.finished) {
573 if (tevent_loop_once(state.ev) == -1) {
574 talloc_free(state.ev);
575 torture_fail(tctx, talloc_asprintf(tctx,
576 "Failed event loop %s\n",
581 talloc_free(state.ev);
583 torture_assert(tctx, state.error == NULL, talloc_asprintf(tctx,
591 static pthread_mutex_t threaded_mutex = PTHREAD_MUTEX_INITIALIZER;
592 static bool do_shutdown = false;
594 static void test_event_threaded_lock(void)
597 ret = pthread_mutex_lock(&threaded_mutex);
601 static void test_event_threaded_unlock(void)
604 ret = pthread_mutex_unlock(&threaded_mutex);
608 static void test_event_threaded_trace(enum tevent_trace_point point,
612 case TEVENT_TRACE_BEFORE_WAIT:
613 test_event_threaded_unlock();
615 case TEVENT_TRACE_AFTER_WAIT:
616 test_event_threaded_lock();
618 case TEVENT_TRACE_BEFORE_LOOP_ONCE:
619 case TEVENT_TRACE_AFTER_LOOP_ONCE:
624 static void test_event_threaded_timer(struct tevent_context *ev,
625 struct tevent_timer *te,
626 struct timeval current_time,
632 static void *test_event_poll_thread(void *private_data)
634 struct tevent_context *ev = (struct tevent_context *)private_data;
636 test_event_threaded_lock();
640 ret = tevent_loop_once(ev);
643 test_event_threaded_unlock();
650 static void test_event_threaded_read_handler(struct tevent_context *ev,
651 struct tevent_fd *fde,
655 int *pfd = (int *)private_data;
659 if ((flags & TEVENT_FD_READ) == 0) {
664 nread = read(*pfd, &c, 1);
665 } while ((nread == -1) && (errno == EINTR));
670 static bool test_event_context_threaded(struct torture_context *test,
671 const void *test_data)
673 struct tevent_context *ev;
674 struct tevent_timer *te;
675 struct tevent_fd *fde;
676 pthread_t poll_thread;
681 ev = tevent_context_init_byname(test, "poll_mt");
682 torture_assert(test, ev != NULL, "poll_mt not supported");
684 tevent_set_trace_callback(ev, test_event_threaded_trace, NULL);
686 te = tevent_add_timer(ev, ev, timeval_current_ofs(5, 0),
687 test_event_threaded_timer, NULL);
688 torture_assert(test, te != NULL, "Could not add timer");
690 ret = pthread_create(&poll_thread, NULL, test_event_poll_thread, ev);
691 torture_assert(test, ret == 0, "Could not create poll thread");
694 torture_assert(test, ret == 0, "Could not create pipe");
698 test_event_threaded_lock();
700 fde = tevent_add_fd(ev, ev, fds[0], TEVENT_FD_READ,
701 test_event_threaded_read_handler, &fds[0]);
702 torture_assert(test, fde != NULL, "Could not add fd event");
704 test_event_threaded_unlock();
708 write(fds[1], &c, 1);
712 test_event_threaded_lock();
714 test_event_threaded_unlock();
716 write(fds[1], &c, 1);
718 ret = pthread_join(poll_thread, NULL);
719 torture_assert(test, ret == 0, "pthread_join failed");
726 struct torture_suite *torture_local_event(TALLOC_CTX *mem_ctx)
728 struct torture_suite *suite = torture_suite_create(mem_ctx, "event");
729 const char **list = tevent_backend_list(suite);
732 for (i=0;list && list[i];i++) {
733 torture_suite_add_simple_tcase_const(suite,
734 talloc_asprintf(suite,
735 "%s.%s", list[i], "context"),
737 (const void *)list[i]);
738 torture_suite_add_simple_tcase_const(suite,
739 talloc_asprintf(suite,
740 "%s.%s", list[i], "fd1"),
742 (const void *)list[i]);
743 torture_suite_add_simple_tcase_const(suite,
744 talloc_asprintf(suite,
745 "%s.%s", list[i], "fd2"),
747 (const void *)list[i]);
751 torture_suite_add_simple_tcase_const(suite, "poll_mt_threaded",
752 test_event_context_threaded,