tevent: use better names for the subtests
[sfrench/samba-autobuild/.git] / lib / tevent / testsuite.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    testing of the events subsystem
5
6    Copyright (C) Stefan Metzmacher 2006-2009
7    Copyright (C) Jeremy Allison    2013
8
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
12
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.
17
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.
22
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/>.
25 */
26
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
36
37 static int fde_count;
38
39 static void fde_handler_read(struct tevent_context *ev_ctx, struct tevent_fd *f,
40                         uint16_t flags, void *private_data)
41 {
42         int *fd = (int *)private_data;
43         char c;
44 #ifdef SA_SIGINFO
45         kill(getpid(), SIGUSR1);
46 #endif
47         kill(getpid(), SIGALRM);
48
49         read(fd[0], &c, 1);
50         fde_count++;
51 }
52
53 static void fde_handler_write(struct tevent_context *ev_ctx, struct tevent_fd *f,
54                         uint16_t flags, void *private_data)
55 {
56         int *fd = (int *)private_data;
57         char c = 0;
58         write(fd[1], &c, 1);
59 }
60
61
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)
65 {
66         struct torture_context *test = (struct torture_context *)private_data;
67         torture_comment(test, "fde_handler_read_1 should never fire !\n");
68         abort();
69 }
70
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)
74 {
75         struct torture_context *test = (struct torture_context *)private_data;
76         torture_comment(test, "fde_handler_write_1 should never fire !\n");
77         abort();
78 }
79
80 static void finished_handler(struct tevent_context *ev_ctx, struct tevent_timer *te,
81                              struct timeval tval, void *private_data)
82 {
83         int *finished = (int *)private_data;
84         (*finished) = 1;
85 }
86
87 static void count_handler(struct tevent_context *ev_ctx, struct tevent_signal *te,
88                           int signum, int count, void *info, void *private_data)
89 {
90         int *countp = (int *)private_data;
91         (*countp) += count;
92 }
93
94 static bool test_event_context(struct torture_context *test,
95                                const void *test_data)
96 {
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;
116
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;
121         }
122
123         torture_comment(test, "backend '%s' - %s\n",
124                         backend, __FUNCTION__);
125
126         /* reset globals */
127         fde_count = 0;
128
129         /* create a pipe */
130         pipe(fd);
131
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);
136
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);
141
142         tevent_fd_set_auto_close(fde_read);
143         tevent_fd_set_auto_close(fde_write);
144
145         tevent_add_timer(ev_ctx, ev_ctx, timeval_current_ofs(2,0),
146                          finished_handler, &finished);
147
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
160
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)));
167                 }
168         }
169
170         talloc_free(fde_read);
171         talloc_free(fde_write);
172         talloc_free(fde_read_1);
173         talloc_free(fde_write_1);
174
175         while (alarm_count < fde_count+1) {
176                 if (tevent_loop_once(ev_ctx) == -1) {
177                         break;
178                 }
179         }
180
181         torture_comment(test, "Got %.2f pipe events/sec\n", fde_count/timeval_elapsed(&t));
182
183 #ifdef SA_RESTART
184         talloc_free(se1);
185 #endif
186
187         torture_assert_int_equal(test, alarm_count, 1+fde_count, "alarm count mismatch");
188
189 #ifdef SA_RESETHAND
190         /*
191          * we do not call talloc_free(se2)
192          * because it is already gone,
193          * after triggering the event handler.
194          */
195 #endif
196
197 #ifdef SA_SIGINFO
198         talloc_free(se3);
199         torture_assert_int_equal(test, info_count, fde_count, "info count mismatch");
200 #endif
201
202         talloc_free(ev_ctx);
203
204         return true;
205 }
206
207 #ifdef HAVE_PTHREAD
208
209 static pthread_mutex_t threaded_mutex = PTHREAD_MUTEX_INITIALIZER;
210 static bool do_shutdown = false;
211
212 static void test_event_threaded_lock(void)
213 {
214         int ret;
215         ret = pthread_mutex_lock(&threaded_mutex);
216         assert(ret == 0);
217 }
218
219 static void test_event_threaded_unlock(void)
220 {
221         int ret;
222         ret = pthread_mutex_unlock(&threaded_mutex);
223         assert(ret == 0);
224 }
225
226 static void test_event_threaded_trace(enum tevent_trace_point point,
227                                       void *private_data)
228 {
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;
239         }
240 }
241
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)
246 {
247         return;
248 }
249
250 static void *test_event_poll_thread(void *private_data)
251 {
252         struct tevent_context *ev = (struct tevent_context *)private_data;
253
254         test_event_threaded_lock();
255
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;
263                 }
264         }
265
266 }
267
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)
272 {
273         int *pfd = (int *)private_data;
274         char c;
275         ssize_t nread;
276
277         if ((flags & TEVENT_FD_READ) == 0) {
278                 return;
279         }
280
281         do {
282                 nread = read(*pfd, &c, 1);
283         } while ((nread == -1) && (errno == EINTR));
284
285         assert(nread == 1);
286 }
287
288 static bool test_event_context_threaded(struct torture_context *test,
289                                         const void *test_data)
290 {
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;
298
299         ev = tevent_context_init_byname(test, "poll_mt");
300         torture_assert(test, ev != NULL, "poll_mt not supported");
301
302         tevent_set_trace_callback(ev, test_event_threaded_trace, NULL);
303
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");
307
308         ret = pthread_create(&poll_thread, NULL, test_event_poll_thread, ev);
309         torture_assert(test, ret == 0, "Could not create poll thread");
310
311         ret = pipe(fds);
312         torture_assert(test, ret == 0, "Could not create pipe");
313
314         poll(NULL, 0, 100);
315
316         test_event_threaded_lock();
317
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");
321
322         test_event_threaded_unlock();
323
324         poll(NULL, 0, 100);
325
326         write(fds[1], &c, 1);
327
328         poll(NULL, 0, 100);
329
330         test_event_threaded_lock();
331         do_shutdown = true;
332         test_event_threaded_unlock();
333
334         write(fds[1], &c, 1);
335
336         ret = pthread_join(poll_thread, NULL);
337         torture_assert(test, ret == 0, "pthread_join failed");
338
339         return true;
340 }
341
342 #endif
343
344 struct torture_suite *torture_local_event(TALLOC_CTX *mem_ctx)
345 {
346         struct torture_suite *suite = torture_suite_create(mem_ctx, "event");
347         const char **list = tevent_backend_list(suite);
348         int i;
349
350         for (i=0;list && list[i];i++) {
351                 struct torture_suite *backend_suite;
352
353                 backend_suite = torture_suite_create(mem_ctx, list[i]);
354
355                 torture_suite_add_simple_tcase_const(backend_suite,
356                                                "context",
357                                                test_event_context,
358                                                (const void *)list[i]);
359
360                 torture_suite_add_suite(suite, backend_suite);
361         }
362
363 #ifdef HAVE_PTHREAD
364         torture_suite_add_simple_tcase_const(suite, "threaded_poll_mt",
365                                              test_event_context_threaded,
366                                              NULL);
367 #endif
368
369         return suite;
370 }