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,
357 static pthread_mutex_t threaded_mutex = PTHREAD_MUTEX_INITIALIZER;
358 static bool do_shutdown = false;
360 static void test_event_threaded_lock(void)
363 ret = pthread_mutex_lock(&threaded_mutex);
367 static void test_event_threaded_unlock(void)
370 ret = pthread_mutex_unlock(&threaded_mutex);
374 static void test_event_threaded_trace(enum tevent_trace_point point,
378 case TEVENT_TRACE_BEFORE_WAIT:
379 test_event_threaded_unlock();
381 case TEVENT_TRACE_AFTER_WAIT:
382 test_event_threaded_lock();
384 case TEVENT_TRACE_BEFORE_LOOP_ONCE:
385 case TEVENT_TRACE_AFTER_LOOP_ONCE:
390 static void test_event_threaded_timer(struct tevent_context *ev,
391 struct tevent_timer *te,
392 struct timeval current_time,
398 static void *test_event_poll_thread(void *private_data)
400 struct tevent_context *ev = (struct tevent_context *)private_data;
402 test_event_threaded_lock();
406 ret = tevent_loop_once(ev);
409 test_event_threaded_unlock();
416 static void test_event_threaded_read_handler(struct tevent_context *ev,
417 struct tevent_fd *fde,
421 int *pfd = (int *)private_data;
425 if ((flags & TEVENT_FD_READ) == 0) {
430 nread = read(*pfd, &c, 1);
431 } while ((nread == -1) && (errno == EINTR));
436 static bool test_event_context_threaded(struct torture_context *test,
437 const void *test_data)
439 struct tevent_context *ev;
440 struct tevent_timer *te;
441 struct tevent_fd *fde;
442 pthread_t poll_thread;
447 ev = tevent_context_init_byname(test, "poll_mt");
448 torture_assert(test, ev != NULL, "poll_mt not supported");
450 tevent_set_trace_callback(ev, test_event_threaded_trace, NULL);
452 te = tevent_add_timer(ev, ev, timeval_current_ofs(5, 0),
453 test_event_threaded_timer, NULL);
454 torture_assert(test, te != NULL, "Could not add timer");
456 ret = pthread_create(&poll_thread, NULL, test_event_poll_thread, ev);
457 torture_assert(test, ret == 0, "Could not create poll thread");
460 torture_assert(test, ret == 0, "Could not create pipe");
464 test_event_threaded_lock();
466 fde = tevent_add_fd(ev, ev, fds[0], TEVENT_FD_READ,
467 test_event_threaded_read_handler, &fds[0]);
468 torture_assert(test, fde != NULL, "Could not add fd event");
470 test_event_threaded_unlock();
474 write(fds[1], &c, 1);
478 test_event_threaded_lock();
480 test_event_threaded_unlock();
482 write(fds[1], &c, 1);
484 ret = pthread_join(poll_thread, NULL);
485 torture_assert(test, ret == 0, "pthread_join failed");
492 struct torture_suite *torture_local_event(TALLOC_CTX *mem_ctx)
494 struct torture_suite *suite = torture_suite_create(mem_ctx, "event");
495 const char **list = tevent_backend_list(suite);
498 for (i=0;list && list[i];i++) {
499 torture_suite_add_simple_tcase_const(suite, list[i],
501 (const void *)list[i]);
502 torture_suite_add_simple_tcase_const(suite, list[i],
504 (const void *)list[i]);
508 torture_suite_add_simple_tcase_const(suite, "poll_mt_threaded",
509 test_event_context_threaded,