tevent: fix some compiler warnings in testsuite.c
[Samba/gebeck_regimport.git] / lib / tevent / testsuite.c
blobe05368204747bcf229b941af2534c67ec5736451
1 /*
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
10 ** under the LGPL
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/>.
26 #include "includes.h"
27 #include "lib/tevent/tevent.h"
28 #include "system/filesys.h"
29 #include "system/select.h"
30 #include "torture/torture.h"
31 #ifdef HAVE_PTHREAD
32 #include <pthread.h>
33 #include <assert.h>
34 #endif
36 static int fde_count;
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;
42 char c;
43 #ifdef SA_SIGINFO
44 kill(getpid(), SIGUSR1);
45 #endif
46 kill(getpid(), SIGALRM);
47 read(fd[0], &c, 1);
48 write(fd[1], &c, 1);
49 fde_count++;
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;
56 (*finished) = 1;
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;
63 (*countp) += count;
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;
74 #ifdef SA_RESTART
75 struct tevent_signal *se1 = NULL;
76 #endif
77 #ifdef SA_RESETHAND
78 struct tevent_signal *se2 = NULL;
79 #endif
80 #ifdef SA_SIGINFO
81 struct tevent_signal *se3 = NULL;
82 #endif
83 int finished=0;
84 struct timeval t;
85 char c = 0;
87 ev_ctx = tevent_context_init_byname(test, backend);
88 if (ev_ctx == NULL) {
89 torture_comment(test, "event backend '%s' not supported\n", backend);
90 return true;
93 torture_comment(test, "backend '%s' - %s\n",
94 backend, __FUNCTION__);
96 /* reset globals */
97 fde_count = 0;
99 /* create a pipe */
100 pipe(fd);
102 fde = tevent_add_fd(ev_ctx, ev_ctx, fd[0], TEVENT_FD_READ,
103 fde_handler, fd);
104 tevent_fd_set_auto_close(fde);
106 tevent_add_timer(ev_ctx, ev_ctx, timeval_current_ofs(2,0),
107 finished_handler, &finished);
109 #ifdef SA_RESTART
110 se1 = tevent_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESTART, count_handler, &alarm_count);
111 torture_assert(test, se1 != NULL, "failed to setup se1");
112 #endif
113 #ifdef SA_RESETHAND
114 se2 = tevent_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESETHAND, count_handler, &alarm_count);
115 torture_assert(test, se2 != NULL, "failed to setup se2");
116 #endif
117 #ifdef SA_SIGINFO
118 se3 = tevent_add_signal(ev_ctx, ev_ctx, SIGUSR1, SA_SIGINFO, count_handler, &info_count);
119 torture_assert(test, se3 != NULL, "failed to setup se3");
120 #endif
122 write(fd[1], &c, 1);
124 t = timeval_current();
125 while (!finished) {
126 errno = 0;
127 if (tevent_loop_once(ev_ctx) == -1) {
128 talloc_free(ev_ctx);
129 torture_fail(test, talloc_asprintf(test, "Failed event loop %s\n", strerror(errno)));
133 talloc_free(fde);
134 close(fd[1]);
136 while (alarm_count < fde_count+1) {
137 if (tevent_loop_once(ev_ctx) == -1) {
138 break;
142 torture_comment(test, "Got %.2f pipe events/sec\n", fde_count/timeval_elapsed(&t));
144 #ifdef SA_RESTART
145 talloc_free(se1);
146 #endif
148 torture_assert_int_equal(test, alarm_count, 1+fde_count, "alarm count mismatch");
150 #ifdef SA_RESETHAND
152 * we do not call talloc_free(se2)
153 * because it is already gone,
154 * after triggering the event handler.
156 #endif
158 #ifdef SA_SIGINFO
159 talloc_free(se3);
160 torture_assert_int_equal(test, info_count, fde_count, "info count mismatch");
161 #endif
163 talloc_free(ev_ctx);
165 return true;
168 #ifdef HAVE_PTHREAD
170 static pthread_mutex_t threaded_mutex = PTHREAD_MUTEX_INITIALIZER;
171 static bool do_shutdown = false;
173 static void test_event_threaded_lock(void)
175 int ret;
176 ret = pthread_mutex_lock(&threaded_mutex);
177 assert(ret == 0);
180 static void test_event_threaded_unlock(void)
182 int ret;
183 ret = pthread_mutex_unlock(&threaded_mutex);
184 assert(ret == 0);
187 static void test_event_threaded_trace(enum tevent_trace_point point,
188 void *private_data)
190 switch (point) {
191 case TEVENT_TRACE_BEFORE_WAIT:
192 test_event_threaded_unlock();
193 break;
194 case TEVENT_TRACE_AFTER_WAIT:
195 test_event_threaded_lock();
196 break;
200 static void test_event_threaded_timer(struct tevent_context *ev,
201 struct tevent_timer *te,
202 struct timeval current_time,
203 void *private_data)
205 return;
208 static void *test_event_poll_thread(void *private_data)
210 struct tevent_context *ev = (struct tevent_context *)private_data;
212 test_event_threaded_lock();
214 while (true) {
215 int ret;
216 ret = tevent_loop_once(ev);
217 assert(ret == 0);
218 if (do_shutdown) {
219 test_event_threaded_unlock();
220 return NULL;
226 static void test_event_threaded_read_handler(struct tevent_context *ev,
227 struct tevent_fd *fde,
228 uint16_t flags,
229 void *private_data)
231 int *pfd = (int *)private_data;
232 char c;
233 ssize_t nread;
235 if ((flags & TEVENT_FD_READ) == 0) {
236 return;
239 do {
240 nread = read(*pfd, &c, 1);
241 } while ((nread == -1) && (errno == EINTR));
243 assert(nread == 1);
246 static bool test_event_context_threaded(struct torture_context *test,
247 const void *test_data)
249 struct tevent_context *ev;
250 struct tevent_timer *te;
251 struct tevent_fd *fde;
252 pthread_t poll_thread;
253 int fds[2];
254 int ret;
255 char c = 0;
257 ev = tevent_context_init_byname(test, "poll_mt");
258 torture_assert(test, ev != NULL, "poll_mt not supported");
260 tevent_set_trace_callback(ev, test_event_threaded_trace, NULL);
262 te = tevent_add_timer(ev, ev, timeval_current_ofs(5, 0),
263 test_event_threaded_timer, NULL);
264 torture_assert(test, te != NULL, "Could not add timer");
266 ret = pthread_create(&poll_thread, NULL, test_event_poll_thread, ev);
267 torture_assert(test, ret == 0, "Could not create poll thread");
269 ret = pipe(fds);
270 torture_assert(test, ret == 0, "Could not create pipe");
272 poll(NULL, 0, 100);
274 test_event_threaded_lock();
276 fde = tevent_add_fd(ev, ev, fds[0], TEVENT_FD_READ,
277 test_event_threaded_read_handler, &fds[0]);
278 torture_assert(test, fde != NULL, "Could not add fd event");
280 test_event_threaded_unlock();
282 poll(NULL, 0, 100);
284 write(fds[1], &c, 1);
286 poll(NULL, 0, 100);
288 test_event_threaded_lock();
289 do_shutdown = true;
290 test_event_threaded_unlock();
292 write(fds[1], &c, 1);
294 ret = pthread_join(poll_thread, NULL);
295 torture_assert(test, ret == 0, "pthread_join failed");
297 return true;
300 #endif
302 struct torture_suite *torture_local_event(TALLOC_CTX *mem_ctx)
304 struct torture_suite *suite = torture_suite_create(mem_ctx, "event");
305 const char **list = tevent_backend_list(suite);
306 int i;
308 for (i=0;list && list[i];i++) {
309 torture_suite_add_simple_tcase_const(suite, list[i],
310 test_event_context,
311 (const void *)list[i]);
314 #ifdef HAVE_PTHREAD
315 torture_suite_add_simple_tcase_const(suite, "poll_mt_threaded",
316 test_event_context_threaded,
317 NULL);
318 #endif
320 return suite;