s3/docs: Fix typos.
[Samba/gebeck_regimport.git] / lib / tevent / testsuite.c
blobd964fb33d390fa495ff98a429ba206e4c63d1626
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/events/events.h"
28 #include "system/filesys.h"
29 #include "torture/torture.h"
31 static int fde_count;
33 static void fde_handler(struct tevent_context *ev_ctx, struct tevent_fd *f,
34 uint16_t flags, void *private_data)
36 int *fd = (int *)private_data;
37 char c;
38 #ifdef SA_SIGINFO
39 kill(getpid(), SIGUSR1);
40 #endif
41 kill(getpid(), SIGALRM);
42 read(fd[0], &c, 1);
43 write(fd[1], &c, 1);
44 fde_count++;
47 static void finished_handler(struct tevent_context *ev_ctx, struct tevent_timer *te,
48 struct timeval tval, void *private_data)
50 int *finished = (int *)private_data;
51 (*finished) = 1;
54 static void count_handler(struct tevent_context *ev_ctx, struct signal_event *te,
55 int signum, int count, void *info, void *private_data)
57 int *countp = (int *)private_data;
58 (*countp) += count;
61 static bool test_event_context(struct torture_context *test,
62 const void *test_data)
64 struct tevent_context *ev_ctx;
65 int fd[2] = { -1, -1 };
66 const char *backend = (const char *)test_data;
67 int alarm_count=0, info_count=0;
68 struct tevent_fd *fde;
69 struct signal_event *se1, *se2, *se3;
70 int finished=0;
71 struct timeval t;
72 char c = 0;
74 ev_ctx = event_context_init_byname(test, backend);
75 if (ev_ctx == NULL) {
76 torture_comment(test, "event backend '%s' not supported\n", backend);
77 return true;
80 torture_comment(test, "Testing event backend '%s'\n", backend);
82 /* reset globals */
83 fde_count = 0;
85 /* create a pipe */
86 pipe(fd);
88 fde = event_add_fd(ev_ctx, ev_ctx, fd[0], EVENT_FD_READ,
89 fde_handler, fd);
90 tevent_fd_set_auto_close(fde);
92 event_add_timed(ev_ctx, ev_ctx, timeval_current_ofs(2,0),
93 finished_handler, &finished);
95 se1 = event_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESTART, count_handler, &alarm_count);
96 se2 = event_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESETHAND, count_handler, &alarm_count);
97 #ifdef SA_SIGINFO
98 se3 = event_add_signal(ev_ctx, ev_ctx, SIGUSR1, SA_SIGINFO, count_handler, &info_count);
99 #endif
101 write(fd[1], &c, 1);
103 t = timeval_current();
104 while (!finished) {
105 errno = 0;
106 if (event_loop_once(ev_ctx) == -1) {
107 talloc_free(ev_ctx);
108 torture_fail(test, talloc_asprintf(test, "Failed event loop %s\n", strerror(errno)));
112 talloc_free(fde);
113 close(fd[1]);
115 while (alarm_count < fde_count+1) {
116 if (event_loop_once(ev_ctx) == -1) {
117 break;
121 torture_comment(test, "Got %.2f pipe events/sec\n", fde_count/timeval_elapsed(&t));
123 talloc_free(se1);
125 torture_assert_int_equal(test, alarm_count, 1+fde_count, "alarm count mismatch");
127 #ifdef SA_SIGINFO
128 talloc_free(se3);
129 torture_assert_int_equal(test, info_count, fde_count, "info count mismatch");
130 #endif
132 talloc_free(ev_ctx);
134 return true;
137 struct torture_suite *torture_local_event(TALLOC_CTX *mem_ctx)
139 struct torture_suite *suite = torture_suite_create(mem_ctx, "EVENT");
140 const char **list = event_backend_list(suite);
141 int i;
143 for (i=0;list && list[i];i++) {
144 torture_suite_add_simple_tcase_const(suite, list[i],
145 test_event_context,
146 (const void *)list[i]);
149 return suite;