2 Unix SMB/CIFS implementation.
4 testing of the events subsystem
6 Copyright (C) Stefan Metzmacher 2006-2009
8 ** NOTE! The following LGPL license applies to the tevent
9 ** library. This does NOT imply that all of Samba is released
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 3 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 #include "lib/tevent/tevent.h"
28 #include "system/filesys.h"
29 #include "system/select.h"
30 #include "torture/torture.h"
38 static void fde_handler(struct tevent_context *ev_ctx, struct tevent_fd *f,
39 uint16_t flags, void *private_data)
41 int *fd = (int *)private_data;
44 kill(getpid(), SIGUSR1);
46 kill(getpid(), SIGALRM);
52 static void finished_handler(struct tevent_context *ev_ctx, struct tevent_timer *te,
53 struct timeval tval, void *private_data)
55 int *finished = (int *)private_data;
59 static void count_handler(struct tevent_context *ev_ctx, struct tevent_signal *te,
60 int signum, int count, void *info, void *private_data)
62 int *countp = (int *)private_data;
66 static bool test_event_context(struct torture_context *test,
67 const void *test_data)
69 struct tevent_context *ev_ctx;
70 int fd[2] = { -1, -1 };
71 const char *backend = (const char *)test_data;
72 int alarm_count=0, info_count=0;
73 struct tevent_fd *fde;
75 struct tevent_signal *se1 = NULL;
78 struct tevent_signal *se2 = NULL;
81 struct tevent_signal *se3 = NULL;
87 ev_ctx = tevent_context_init_byname(test, backend);
89 torture_comment(test, "event backend '%s' not supported\n", backend);
93 torture_comment(test, "backend '%s' - %s\n",
94 backend, __FUNCTION__);
102 fde = tevent_add_fd(ev_ctx, ev_ctx, fd[0], TEVENT_FD_READ,
104 tevent_fd_set_auto_close(fde);
106 tevent_add_timer(ev_ctx, ev_ctx, timeval_current_ofs(2,0),
107 finished_handler, &finished);
110 se1 = tevent_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESTART, count_handler, &alarm_count);
113 se2 = tevent_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESETHAND, count_handler, &alarm_count);
116 se3 = tevent_add_signal(ev_ctx, ev_ctx, SIGUSR1, SA_SIGINFO, count_handler, &info_count);
121 t = timeval_current();
124 if (tevent_loop_once(ev_ctx) == -1) {
126 torture_fail(test, talloc_asprintf(test, "Failed event loop %s\n", strerror(errno)));
133 while (alarm_count < fde_count+1) {
134 if (tevent_loop_once(ev_ctx) == -1) {
139 torture_comment(test, "Got %.2f pipe events/sec\n", fde_count/timeval_elapsed(&t));
145 torture_assert_int_equal(test, alarm_count, 1+fde_count, "alarm count mismatch");
153 torture_assert_int_equal(test, info_count, fde_count, "info count mismatch");
163 static pthread_mutex_t threaded_mutex = PTHREAD_MUTEX_INITIALIZER;
164 static bool do_shutdown = false;
166 static void test_event_threaded_lock(void)
169 ret = pthread_mutex_lock(&threaded_mutex);
173 static void test_event_threaded_unlock(void)
176 ret = pthread_mutex_unlock(&threaded_mutex);
180 static void test_event_threaded_trace(enum tevent_trace_point point,
184 case TEVENT_TRACE_BEFORE_WAIT:
185 test_event_threaded_unlock();
187 case TEVENT_TRACE_AFTER_WAIT:
188 test_event_threaded_lock();
193 static void test_event_threaded_timer(struct tevent_context *ev,
194 struct tevent_timer *te,
195 struct timeval current_time,
201 static void *test_event_poll_thread(void *private_data)
203 struct tevent_context *ev = (struct tevent_context *)private_data;
205 test_event_threaded_lock();
209 ret = tevent_loop_once(ev);
212 test_event_threaded_unlock();
219 static void test_event_threaded_read_handler(struct tevent_context *ev,
220 struct tevent_fd *fde,
224 int *pfd = (int *)private_data;
228 if ((flags & TEVENT_FD_READ) == 0) {
233 nread = read(*pfd, &c, 1);
234 } while ((nread == -1) && (errno == EINTR));
239 static bool test_event_context_threaded(struct torture_context *test,
240 const void *test_data)
242 struct tevent_context *ev;
243 struct tevent_timer *te;
244 struct tevent_fd *fde;
245 pthread_t poll_thread;
250 ev = tevent_context_init_byname(test, "poll_mt");
251 torture_assert(test, ev != NULL, "poll_mt not supported");
253 tevent_set_trace_callback(ev, test_event_threaded_trace, NULL);
255 te = tevent_add_timer(ev, ev, timeval_current_ofs(5, 0),
256 test_event_threaded_timer, NULL);
257 torture_assert(test, te != NULL, "Could not add timer");
259 ret = pthread_create(&poll_thread, NULL, test_event_poll_thread, ev);
260 torture_assert(test, ret == 0, "Could not create poll thread");
263 torture_assert(test, ret == 0, "Could not create pipe");
267 test_event_threaded_lock();
269 fde = tevent_add_fd(ev, ev, fds[0], TEVENT_FD_READ,
270 test_event_threaded_read_handler, &fds[0]);
271 torture_assert(test, fde != NULL, "Could not add fd event");
273 test_event_threaded_unlock();
277 write(fds[1], &c, 1);
281 test_event_threaded_lock();
283 test_event_threaded_unlock();
285 write(fds[1], &c, 1);
287 ret = pthread_join(poll_thread, NULL);
288 torture_assert(test, ret == 0, "pthread_join failed");
295 struct torture_suite *torture_local_event(TALLOC_CTX *mem_ctx)
297 struct torture_suite *suite = torture_suite_create(mem_ctx, "event");
298 const char **list = tevent_backend_list(suite);
301 for (i=0;list && list[i];i++) {
302 torture_suite_add_simple_tcase_const(suite, list[i],
304 (const void *)list[i]);
308 torture_suite_add_simple_tcase_const(suite, "poll_mt_threaded",
309 test_event_context_threaded,