]> git.samba.org - obnox/samba/samba-obnox.git/blob - lib/tevent/testsuite.c
tevent: add test_event_fd1()
[obnox/samba/samba-obnox.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 "system/network.h"
32 #include "torture/torture.h"
33 #ifdef HAVE_PTHREAD
34 #include <pthread.h>
35 #include <assert.h>
36 #endif
37
38 static int fde_count;
39
40 static void fde_handler_read(struct tevent_context *ev_ctx, struct tevent_fd *f,
41                         uint16_t flags, void *private_data)
42 {
43         int *fd = (int *)private_data;
44         char c;
45 #ifdef SA_SIGINFO
46         kill(getpid(), SIGUSR1);
47 #endif
48         kill(getpid(), SIGALRM);
49
50         read(fd[0], &c, 1);
51         fde_count++;
52 }
53
54 static void fde_handler_write(struct tevent_context *ev_ctx, struct tevent_fd *f,
55                         uint16_t flags, void *private_data)
56 {
57         int *fd = (int *)private_data;
58         char c = 0;
59         write(fd[1], &c, 1);
60 }
61
62
63 /* These should never fire... */
64 static void fde_handler_read_1(struct tevent_context *ev_ctx, struct tevent_fd *f,
65                         uint16_t flags, void *private_data)
66 {
67         struct torture_context *test = (struct torture_context *)private_data;
68         torture_comment(test, "fde_handler_read_1 should never fire !\n");
69         abort();
70 }
71
72 /* These should never fire... */
73 static void fde_handler_write_1(struct tevent_context *ev_ctx, struct tevent_fd *f,
74                         uint16_t flags, void *private_data)
75 {
76         struct torture_context *test = (struct torture_context *)private_data;
77         torture_comment(test, "fde_handler_write_1 should never fire !\n");
78         abort();
79 }
80
81 static void finished_handler(struct tevent_context *ev_ctx, struct tevent_timer *te,
82                              struct timeval tval, void *private_data)
83 {
84         int *finished = (int *)private_data;
85         (*finished) = 1;
86 }
87
88 static void count_handler(struct tevent_context *ev_ctx, struct tevent_signal *te,
89                           int signum, int count, void *info, void *private_data)
90 {
91         int *countp = (int *)private_data;
92         (*countp) += count;
93 }
94
95 static bool test_event_context(struct torture_context *test,
96                                const void *test_data)
97 {
98         struct tevent_context *ev_ctx;
99         int fd[2] = { -1, -1 };
100         const char *backend = (const char *)test_data;
101         int alarm_count=0, info_count=0;
102         struct tevent_fd *fde_read;
103         struct tevent_fd *fde_read_1;
104         struct tevent_fd *fde_write;
105         struct tevent_fd *fde_write_1;
106 #ifdef SA_RESTART
107         struct tevent_signal *se1 = NULL;
108 #endif
109 #ifdef SA_RESETHAND
110         struct tevent_signal *se2 = NULL;
111 #endif
112 #ifdef SA_SIGINFO
113         struct tevent_signal *se3 = NULL;
114 #endif
115         int finished=0;
116         struct timeval t;
117
118         ev_ctx = tevent_context_init_byname(test, backend);
119         if (ev_ctx == NULL) {
120                 torture_comment(test, "event backend '%s' not supported\n", backend);
121                 return true;
122         }
123
124         torture_comment(test, "backend '%s' - %s\n",
125                         backend, __FUNCTION__);
126
127         /* reset globals */
128         fde_count = 0;
129
130         /* create a pipe */
131         pipe(fd);
132
133         fde_read = tevent_add_fd(ev_ctx, ev_ctx, fd[0], TEVENT_FD_READ,
134                             fde_handler_read, fd);
135         fde_write_1 = tevent_add_fd(ev_ctx, ev_ctx, fd[0], TEVENT_FD_WRITE,
136                             fde_handler_write_1, test);
137
138         fde_write = tevent_add_fd(ev_ctx, ev_ctx, fd[1], TEVENT_FD_WRITE,
139                             fde_handler_write, fd);
140         fde_read_1 = tevent_add_fd(ev_ctx, ev_ctx, fd[1], TEVENT_FD_READ,
141                             fde_handler_read_1, test);
142
143         tevent_fd_set_auto_close(fde_read);
144         tevent_fd_set_auto_close(fde_write);
145
146         tevent_add_timer(ev_ctx, ev_ctx, timeval_current_ofs(2,0),
147                          finished_handler, &finished);
148
149 #ifdef SA_RESTART
150         se1 = tevent_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESTART, count_handler, &alarm_count);
151         torture_assert(test, se1 != NULL, "failed to setup se1");
152 #endif
153 #ifdef SA_RESETHAND
154         se2 = tevent_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESETHAND, count_handler, &alarm_count);
155         torture_assert(test, se2 != NULL, "failed to setup se2");
156 #endif
157 #ifdef SA_SIGINFO
158         se3 = tevent_add_signal(ev_ctx, ev_ctx, SIGUSR1, SA_SIGINFO, count_handler, &info_count);
159         torture_assert(test, se3 != NULL, "failed to setup se3");
160 #endif
161
162         t = timeval_current();
163         while (!finished) {
164                 errno = 0;
165                 if (tevent_loop_once(ev_ctx) == -1) {
166                         talloc_free(ev_ctx);
167                         torture_fail(test, talloc_asprintf(test, "Failed event loop %s\n", strerror(errno)));
168                 }
169         }
170
171         talloc_free(fde_read);
172         talloc_free(fde_write);
173         talloc_free(fde_read_1);
174         talloc_free(fde_write_1);
175
176         while (alarm_count < fde_count+1) {
177                 if (tevent_loop_once(ev_ctx) == -1) {
178                         break;
179                 }
180         }
181
182         torture_comment(test, "Got %.2f pipe events/sec\n", fde_count/timeval_elapsed(&t));
183
184 #ifdef SA_RESTART
185         talloc_free(se1);
186 #endif
187
188         torture_assert_int_equal(test, alarm_count, 1+fde_count, "alarm count mismatch");
189
190 #ifdef SA_RESETHAND
191         /*
192          * we do not call talloc_free(se2)
193          * because it is already gone,
194          * after triggering the event handler.
195          */
196 #endif
197
198 #ifdef SA_SIGINFO
199         talloc_free(se3);
200         torture_assert_int_equal(test, info_count, fde_count, "info count mismatch");
201 #endif
202
203         talloc_free(ev_ctx);
204
205         return true;
206 }
207
208 struct test_event_fd1_state {
209         struct torture_context *tctx;
210         const char *backend;
211         struct tevent_context *ev;
212         int sock[2];
213         struct tevent_timer *te;
214         struct tevent_fd *fde0;
215         struct tevent_fd *fde1;
216         bool got_write;
217         bool got_read;
218         bool drain;
219         bool drain_done;
220         unsigned loop_count;
221         bool finished;
222         const char *error;
223 };
224
225 static void test_event_fd1_fde_handler(struct tevent_context *ev_ctx,
226                                        struct tevent_fd *fde,
227                                        uint16_t flags,
228                                        void *private_data)
229 {
230         struct test_event_fd1_state *state =
231                 (struct test_event_fd1_state *)private_data;
232
233         if (state->drain_done) {
234                 state->finished = true;
235                 state->error = __location__;
236                 return;
237         }
238
239         if (state->drain) {
240                 ssize_t ret;
241                 uint8_t c = 0;
242
243                 if (!(flags & TEVENT_FD_READ)) {
244                         state->finished = true;
245                         state->error = __location__;
246                         return;
247                 }
248
249                 ret = read(state->sock[0], &c, 1);
250                 if (ret == 1) {
251                         return;
252                 }
253
254                 /*
255                  * end of test...
256                  */
257                 tevent_fd_set_flags(fde, 0);
258                 state->drain_done = true;
259                 return;
260         }
261
262         if (!state->got_write) {
263                 uint8_t c = 0;
264
265                 if (flags != TEVENT_FD_WRITE) {
266                         state->finished = true;
267                         state->error = __location__;
268                         return;
269                 }
270                 state->got_write = true;
271
272                 /*
273                  * we write to the other socket...
274                  */
275                 write(state->sock[1], &c, 1);
276                 TEVENT_FD_NOT_WRITEABLE(fde);
277                 TEVENT_FD_READABLE(fde);
278                 return;
279         }
280
281         if (!state->got_read) {
282                 if (flags != TEVENT_FD_READ) {
283                         state->finished = true;
284                         state->error = __location__;
285                         return;
286                 }
287                 state->got_read = true;
288
289                 TEVENT_FD_NOT_READABLE(fde);
290                 return;
291         }
292
293         state->finished = true;
294         state->error = __location__;
295         return;
296 }
297
298 static void test_event_fd1_finished(struct tevent_context *ev_ctx,
299                                     struct tevent_timer *te,
300                                     struct timeval tval,
301                                     void *private_data)
302 {
303         struct test_event_fd1_state *state =
304                 (struct test_event_fd1_state *)private_data;
305
306         if (state->drain_done) {
307                 state->finished = true;
308                 return;
309         }
310
311         if (!state->got_write) {
312                 state->finished = true;
313                 state->error = __location__;
314                 return;
315         }
316
317         if (!state->got_read) {
318                 state->finished = true;
319                 state->error = __location__;
320                 return;
321         }
322
323         state->loop_count++;
324         if (state->loop_count > 3) {
325                 state->finished = true;
326                 state->error = __location__;
327                 return;
328         }
329
330         state->got_write = false;
331         state->got_read = false;
332
333         tevent_fd_set_flags(state->fde0, TEVENT_FD_WRITE);
334
335         if (state->loop_count > 2) {
336                 state->drain = true;
337                 TALLOC_FREE(state->fde1);
338                 TEVENT_FD_READABLE(state->fde0);
339         }
340
341         state->te = tevent_add_timer(state->ev, state->ev,
342                                     timeval_current_ofs(0,2000),
343                                     test_event_fd1_finished, state);
344 }
345
346 static bool test_event_fd1(struct torture_context *tctx,
347                            const void *test_data)
348 {
349         struct test_event_fd1_state state;
350
351         ZERO_STRUCT(state);
352         state.tctx = tctx;
353         state.backend = (const char *)test_data;
354
355         state.ev = tevent_context_init_byname(tctx, state.backend);
356         if (state.ev == NULL) {
357                 torture_skip(tctx, talloc_asprintf(tctx,
358                              "event backend '%s' not supported\n",
359                              state.backend));
360                 return true;
361         }
362
363         tevent_set_debug_stderr(state.ev);
364         torture_comment(tctx, "backend '%s' - %s\n",
365                         state.backend, __FUNCTION__);
366
367         /*
368          * This tests the following:
369          *
370          * It monitors the state of state.sock[0]
371          * with tevent_fd, but we never read/write on state.sock[0]
372          * while state.sock[1] * is only used to write a few bytes.
373          *
374          * We have a loop:
375          *   - we wait only for TEVENT_FD_WRITE on state.sock[0]
376          *   - we write 1 byte to state.sock[1]
377          *   - we wait only for TEVENT_FD_READ on state.sock[0]
378          *   - we disable events on state.sock[0]
379          *   - the timer event restarts the loop
380          * Then we close state.sock[1]
381          * We have a loop:
382          *   - we wait for TEVENT_FD_READ/WRITE on state.sock[0]
383          *   - we try to read 1 byte
384          *   - if the read gets an error of returns 0
385          *     we disable the event handler
386          *   - the timer finishes the test
387          */
388         state.sock[0] = -1;
389         state.sock[1] = -1;
390         socketpair(AF_UNIX, SOCK_STREAM, 0, state.sock);
391
392         state.te = tevent_add_timer(state.ev, state.ev,
393                                     timeval_current_ofs(0,1000),
394                                     test_event_fd1_finished, &state);
395         state.fde0 = tevent_add_fd(state.ev, state.ev,
396                                    state.sock[0], TEVENT_FD_WRITE,
397                                    test_event_fd1_fde_handler, &state);
398         /* state.fde1 is only used to auto close */
399         state.fde1 = tevent_add_fd(state.ev, state.ev,
400                                    state.sock[1], 0,
401                                    test_event_fd1_fde_handler, &state);
402
403         tevent_fd_set_auto_close(state.fde0);
404         tevent_fd_set_auto_close(state.fde1);
405
406         while (!state.finished) {
407                 errno = 0;
408                 if (tevent_loop_once(state.ev) == -1) {
409                         talloc_free(state.ev);
410                         torture_fail(tctx, talloc_asprintf(tctx,
411                                      "Failed event loop %s\n",
412                                      strerror(errno)));
413                 }
414         }
415
416         talloc_free(state.ev);
417
418         torture_assert(tctx, state.error == NULL, talloc_asprintf(tctx,
419                        "%s", state.error));
420
421         return true;
422 }
423
424 #ifdef HAVE_PTHREAD
425
426 static pthread_mutex_t threaded_mutex = PTHREAD_MUTEX_INITIALIZER;
427 static bool do_shutdown = false;
428
429 static void test_event_threaded_lock(void)
430 {
431         int ret;
432         ret = pthread_mutex_lock(&threaded_mutex);
433         assert(ret == 0);
434 }
435
436 static void test_event_threaded_unlock(void)
437 {
438         int ret;
439         ret = pthread_mutex_unlock(&threaded_mutex);
440         assert(ret == 0);
441 }
442
443 static void test_event_threaded_trace(enum tevent_trace_point point,
444                                       void *private_data)
445 {
446         switch (point) {
447         case TEVENT_TRACE_BEFORE_WAIT:
448                 test_event_threaded_unlock();
449                 break;
450         case TEVENT_TRACE_AFTER_WAIT:
451                 test_event_threaded_lock();
452                 break;
453         case TEVENT_TRACE_BEFORE_LOOP_ONCE:
454         case TEVENT_TRACE_AFTER_LOOP_ONCE:
455                 break;
456         }
457 }
458
459 static void test_event_threaded_timer(struct tevent_context *ev,
460                                       struct tevent_timer *te,
461                                       struct timeval current_time,
462                                       void *private_data)
463 {
464         return;
465 }
466
467 static void *test_event_poll_thread(void *private_data)
468 {
469         struct tevent_context *ev = (struct tevent_context *)private_data;
470
471         test_event_threaded_lock();
472
473         while (true) {
474                 int ret;
475                 ret = tevent_loop_once(ev);
476                 assert(ret == 0);
477                 if (do_shutdown) {
478                         test_event_threaded_unlock();
479                         return NULL;
480                 }
481         }
482
483 }
484
485 static void test_event_threaded_read_handler(struct tevent_context *ev,
486                                              struct tevent_fd *fde,
487                                              uint16_t flags,
488                                              void *private_data)
489 {
490         int *pfd = (int *)private_data;
491         char c;
492         ssize_t nread;
493
494         if ((flags & TEVENT_FD_READ) == 0) {
495                 return;
496         }
497
498         do {
499                 nread = read(*pfd, &c, 1);
500         } while ((nread == -1) && (errno == EINTR));
501
502         assert(nread == 1);
503 }
504
505 static bool test_event_context_threaded(struct torture_context *test,
506                                         const void *test_data)
507 {
508         struct tevent_context *ev;
509         struct tevent_timer *te;
510         struct tevent_fd *fde;
511         pthread_t poll_thread;
512         int fds[2];
513         int ret;
514         char c = 0;
515
516         ev = tevent_context_init_byname(test, "poll_mt");
517         torture_assert(test, ev != NULL, "poll_mt not supported");
518
519         tevent_set_trace_callback(ev, test_event_threaded_trace, NULL);
520
521         te = tevent_add_timer(ev, ev, timeval_current_ofs(5, 0),
522                               test_event_threaded_timer, NULL);
523         torture_assert(test, te != NULL, "Could not add timer");
524
525         ret = pthread_create(&poll_thread, NULL, test_event_poll_thread, ev);
526         torture_assert(test, ret == 0, "Could not create poll thread");
527
528         ret = pipe(fds);
529         torture_assert(test, ret == 0, "Could not create pipe");
530
531         poll(NULL, 0, 100);
532
533         test_event_threaded_lock();
534
535         fde = tevent_add_fd(ev, ev, fds[0], TEVENT_FD_READ,
536                             test_event_threaded_read_handler, &fds[0]);
537         torture_assert(test, fde != NULL, "Could not add fd event");
538
539         test_event_threaded_unlock();
540
541         poll(NULL, 0, 100);
542
543         write(fds[1], &c, 1);
544
545         poll(NULL, 0, 100);
546
547         test_event_threaded_lock();
548         do_shutdown = true;
549         test_event_threaded_unlock();
550
551         write(fds[1], &c, 1);
552
553         ret = pthread_join(poll_thread, NULL);
554         torture_assert(test, ret == 0, "pthread_join failed");
555
556         return true;
557 }
558
559 #endif
560
561 struct torture_suite *torture_local_event(TALLOC_CTX *mem_ctx)
562 {
563         struct torture_suite *suite = torture_suite_create(mem_ctx, "event");
564         const char **list = tevent_backend_list(suite);
565         int i;
566
567         for (i=0;list && list[i];i++) {
568                 struct torture_suite *backend_suite;
569
570                 backend_suite = torture_suite_create(mem_ctx, list[i]);
571
572                 torture_suite_add_simple_tcase_const(backend_suite,
573                                                "context",
574                                                test_event_context,
575                                                (const void *)list[i]);
576                 torture_suite_add_simple_tcase_const(backend_suite,
577                                                "fd1",
578                                                test_event_fd1,
579                                                (const void *)list[i]);
580
581                 torture_suite_add_suite(suite, backend_suite);
582         }
583
584 #ifdef HAVE_PTHREAD
585         torture_suite_add_simple_tcase_const(suite, "threaded_poll_mt",
586                                              test_event_context_threaded,
587                                              NULL);
588 #endif
589
590         return suite;
591 }