tevent: use better names for the subtests
[Samba/gebeck_regimport.git] / lib / tevent / testsuite.c
blob5c2f695f27fa80c5999019b170e82c8064e2a14c
1 /*
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
11 ** under the LGPL
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/>.
27 #include "includes.h"
28 #include "lib/tevent/tevent.h"
29 #include "system/filesys.h"
30 #include "system/select.h"
31 #include "torture/torture.h"
32 #ifdef HAVE_PTHREAD
33 #include <pthread.h>
34 #include <assert.h>
35 #endif
37 static int fde_count;
39 static void fde_handler_read(struct tevent_context *ev_ctx, struct tevent_fd *f,
40 uint16_t flags, void *private_data)
42 int *fd = (int *)private_data;
43 char c;
44 #ifdef SA_SIGINFO
45 kill(getpid(), SIGUSR1);
46 #endif
47 kill(getpid(), SIGALRM);
49 read(fd[0], &c, 1);
50 fde_count++;
53 static void fde_handler_write(struct tevent_context *ev_ctx, struct tevent_fd *f,
54 uint16_t flags, void *private_data)
56 int *fd = (int *)private_data;
57 char c = 0;
58 write(fd[1], &c, 1);
62 /* These should never fire... */
63 static void fde_handler_read_1(struct tevent_context *ev_ctx, struct tevent_fd *f,
64 uint16_t flags, void *private_data)
66 struct torture_context *test = (struct torture_context *)private_data;
67 torture_comment(test, "fde_handler_read_1 should never fire !\n");
68 abort();
71 /* These should never fire... */
72 static void fde_handler_write_1(struct tevent_context *ev_ctx, struct tevent_fd *f,
73 uint16_t flags, void *private_data)
75 struct torture_context *test = (struct torture_context *)private_data;
76 torture_comment(test, "fde_handler_write_1 should never fire !\n");
77 abort();
80 static void finished_handler(struct tevent_context *ev_ctx, struct tevent_timer *te,
81 struct timeval tval, void *private_data)
83 int *finished = (int *)private_data;
84 (*finished) = 1;
87 static void count_handler(struct tevent_context *ev_ctx, struct tevent_signal *te,
88 int signum, int count, void *info, void *private_data)
90 int *countp = (int *)private_data;
91 (*countp) += count;
94 static bool test_event_context(struct torture_context *test,
95 const void *test_data)
97 struct tevent_context *ev_ctx;
98 int fd[2] = { -1, -1 };
99 const char *backend = (const char *)test_data;
100 int alarm_count=0, info_count=0;
101 struct tevent_fd *fde_read;
102 struct tevent_fd *fde_read_1;
103 struct tevent_fd *fde_write;
104 struct tevent_fd *fde_write_1;
105 #ifdef SA_RESTART
106 struct tevent_signal *se1 = NULL;
107 #endif
108 #ifdef SA_RESETHAND
109 struct tevent_signal *se2 = NULL;
110 #endif
111 #ifdef SA_SIGINFO
112 struct tevent_signal *se3 = NULL;
113 #endif
114 int finished=0;
115 struct timeval t;
117 ev_ctx = tevent_context_init_byname(test, backend);
118 if (ev_ctx == NULL) {
119 torture_comment(test, "event backend '%s' not supported\n", backend);
120 return true;
123 torture_comment(test, "backend '%s' - %s\n",
124 backend, __FUNCTION__);
126 /* reset globals */
127 fde_count = 0;
129 /* create a pipe */
130 pipe(fd);
132 fde_read = tevent_add_fd(ev_ctx, ev_ctx, fd[0], TEVENT_FD_READ,
133 fde_handler_read, fd);
134 fde_write_1 = tevent_add_fd(ev_ctx, ev_ctx, fd[0], TEVENT_FD_WRITE,
135 fde_handler_write_1, test);
137 fde_write = tevent_add_fd(ev_ctx, ev_ctx, fd[1], TEVENT_FD_WRITE,
138 fde_handler_write, fd);
139 fde_read_1 = tevent_add_fd(ev_ctx, ev_ctx, fd[1], TEVENT_FD_READ,
140 fde_handler_read_1, test);
142 tevent_fd_set_auto_close(fde_read);
143 tevent_fd_set_auto_close(fde_write);
145 tevent_add_timer(ev_ctx, ev_ctx, timeval_current_ofs(2,0),
146 finished_handler, &finished);
148 #ifdef SA_RESTART
149 se1 = tevent_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESTART, count_handler, &alarm_count);
150 torture_assert(test, se1 != NULL, "failed to setup se1");
151 #endif
152 #ifdef SA_RESETHAND
153 se2 = tevent_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESETHAND, count_handler, &alarm_count);
154 torture_assert(test, se2 != NULL, "failed to setup se2");
155 #endif
156 #ifdef SA_SIGINFO
157 se3 = tevent_add_signal(ev_ctx, ev_ctx, SIGUSR1, SA_SIGINFO, count_handler, &info_count);
158 torture_assert(test, se3 != NULL, "failed to setup se3");
159 #endif
161 t = timeval_current();
162 while (!finished) {
163 errno = 0;
164 if (tevent_loop_once(ev_ctx) == -1) {
165 talloc_free(ev_ctx);
166 torture_fail(test, talloc_asprintf(test, "Failed event loop %s\n", strerror(errno)));
170 talloc_free(fde_read);
171 talloc_free(fde_write);
172 talloc_free(fde_read_1);
173 talloc_free(fde_write_1);
175 while (alarm_count < fde_count+1) {
176 if (tevent_loop_once(ev_ctx) == -1) {
177 break;
181 torture_comment(test, "Got %.2f pipe events/sec\n", fde_count/timeval_elapsed(&t));
183 #ifdef SA_RESTART
184 talloc_free(se1);
185 #endif
187 torture_assert_int_equal(test, alarm_count, 1+fde_count, "alarm count mismatch");
189 #ifdef SA_RESETHAND
191 * we do not call talloc_free(se2)
192 * because it is already gone,
193 * after triggering the event handler.
195 #endif
197 #ifdef SA_SIGINFO
198 talloc_free(se3);
199 torture_assert_int_equal(test, info_count, fde_count, "info count mismatch");
200 #endif
202 talloc_free(ev_ctx);
204 return true;
207 #ifdef HAVE_PTHREAD
209 static pthread_mutex_t threaded_mutex = PTHREAD_MUTEX_INITIALIZER;
210 static bool do_shutdown = false;
212 static void test_event_threaded_lock(void)
214 int ret;
215 ret = pthread_mutex_lock(&threaded_mutex);
216 assert(ret == 0);
219 static void test_event_threaded_unlock(void)
221 int ret;
222 ret = pthread_mutex_unlock(&threaded_mutex);
223 assert(ret == 0);
226 static void test_event_threaded_trace(enum tevent_trace_point point,
227 void *private_data)
229 switch (point) {
230 case TEVENT_TRACE_BEFORE_WAIT:
231 test_event_threaded_unlock();
232 break;
233 case TEVENT_TRACE_AFTER_WAIT:
234 test_event_threaded_lock();
235 break;
236 case TEVENT_TRACE_BEFORE_LOOP_ONCE:
237 case TEVENT_TRACE_AFTER_LOOP_ONCE:
238 break;
242 static void test_event_threaded_timer(struct tevent_context *ev,
243 struct tevent_timer *te,
244 struct timeval current_time,
245 void *private_data)
247 return;
250 static void *test_event_poll_thread(void *private_data)
252 struct tevent_context *ev = (struct tevent_context *)private_data;
254 test_event_threaded_lock();
256 while (true) {
257 int ret;
258 ret = tevent_loop_once(ev);
259 assert(ret == 0);
260 if (do_shutdown) {
261 test_event_threaded_unlock();
262 return NULL;
268 static void test_event_threaded_read_handler(struct tevent_context *ev,
269 struct tevent_fd *fde,
270 uint16_t flags,
271 void *private_data)
273 int *pfd = (int *)private_data;
274 char c;
275 ssize_t nread;
277 if ((flags & TEVENT_FD_READ) == 0) {
278 return;
281 do {
282 nread = read(*pfd, &c, 1);
283 } while ((nread == -1) && (errno == EINTR));
285 assert(nread == 1);
288 static bool test_event_context_threaded(struct torture_context *test,
289 const void *test_data)
291 struct tevent_context *ev;
292 struct tevent_timer *te;
293 struct tevent_fd *fde;
294 pthread_t poll_thread;
295 int fds[2];
296 int ret;
297 char c = 0;
299 ev = tevent_context_init_byname(test, "poll_mt");
300 torture_assert(test, ev != NULL, "poll_mt not supported");
302 tevent_set_trace_callback(ev, test_event_threaded_trace, NULL);
304 te = tevent_add_timer(ev, ev, timeval_current_ofs(5, 0),
305 test_event_threaded_timer, NULL);
306 torture_assert(test, te != NULL, "Could not add timer");
308 ret = pthread_create(&poll_thread, NULL, test_event_poll_thread, ev);
309 torture_assert(test, ret == 0, "Could not create poll thread");
311 ret = pipe(fds);
312 torture_assert(test, ret == 0, "Could not create pipe");
314 poll(NULL, 0, 100);
316 test_event_threaded_lock();
318 fde = tevent_add_fd(ev, ev, fds[0], TEVENT_FD_READ,
319 test_event_threaded_read_handler, &fds[0]);
320 torture_assert(test, fde != NULL, "Could not add fd event");
322 test_event_threaded_unlock();
324 poll(NULL, 0, 100);
326 write(fds[1], &c, 1);
328 poll(NULL, 0, 100);
330 test_event_threaded_lock();
331 do_shutdown = true;
332 test_event_threaded_unlock();
334 write(fds[1], &c, 1);
336 ret = pthread_join(poll_thread, NULL);
337 torture_assert(test, ret == 0, "pthread_join failed");
339 return true;
342 #endif
344 struct torture_suite *torture_local_event(TALLOC_CTX *mem_ctx)
346 struct torture_suite *suite = torture_suite_create(mem_ctx, "event");
347 const char **list = tevent_backend_list(suite);
348 int i;
350 for (i=0;list && list[i];i++) {
351 struct torture_suite *backend_suite;
353 backend_suite = torture_suite_create(mem_ctx, list[i]);
355 torture_suite_add_simple_tcase_const(backend_suite,
356 "context",
357 test_event_context,
358 (const void *)list[i]);
360 torture_suite_add_suite(suite, backend_suite);
363 #ifdef HAVE_PTHREAD
364 torture_suite_add_simple_tcase_const(suite, "threaded_poll_mt",
365 test_event_context_threaded,
366 NULL);
367 #endif
369 return suite;