r20647: add cluster code
[ira/wip.git] / source / torture / local / event.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    testing of the events subsystem
5    
6    Copyright (C) Stefan Metzmacher
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "system/filesys.h"
26 #include "torture/torture.h"
27
28 static int write_fd, read_fd;
29 static struct fd_event *fde;
30 static int te_count;
31 static int fde_count;
32 static struct torture_context *test;
33
34 static void fde_handler(struct event_context *ev_ctx, struct fd_event *f, 
35                         uint16_t flags, void *private)
36 {
37         int *fd = private;
38
39         torture_comment(test, "event[%d] fd[%d] events[0x%08X]%s%s\n", 
40                                                 fde_count, *fd, flags, 
41                                         (flags & EVENT_FD_READ)?" EVENT_FD_READ":"", 
42                                         (flags & EVENT_FD_WRITE)?" EVENT_FD_WRITE":"");
43
44         if (fde_count > 5) {
45                 torture_result(test, TORTURE_FAIL, 
46                                            __location__": got more than fde 5 events - bug!");
47                 talloc_free(fde);
48                 fde = NULL;
49                 return;
50         }
51
52         event_set_fd_flags(fde, 0);
53         fde_count++;
54 }
55
56 static void timed_handler(struct event_context *ev_ctx, struct timed_event *te,
57                           struct timeval tval, void *private)
58 {
59         torture_comment(test, "timed_handler called[%d]\n", te_count);
60         if (te_count > 2) {
61                 close(write_fd);
62                 write_fd = -1;
63         }
64         if (te_count > 5) {
65                 torture_comment(test, "remove fd event!\n");
66                 talloc_free(fde);
67                 fde = NULL;
68                 return;
69         }
70         te_count++;
71         event_add_timed(ev_ctx, ev_ctx, timeval_current_ofs(0,500), timed_handler, private);
72 }
73
74 static bool test_event_context(struct torture_context *torture_ctx,
75                                                            const void *test_data)
76 {
77         struct event_context *ev_ctx;
78         int fd[2] = { -1, -1 };
79         const char *backend = (const char *)test_data;
80         TALLOC_CTX *mem_ctx = torture_ctx;
81
82         test = torture_ctx;
83
84         ev_ctx = event_context_init_byname(mem_ctx, backend);
85         if (ev_ctx == NULL) {
86                 torture_comment(test, "event backend '%s' not supported\n", backend);
87                 return true;
88         }
89
90         torture_comment(test, "Testing event backend '%s'\n", backend);
91
92         /* reset globals */
93         write_fd = -1;
94         read_fd = -1;
95         fde = NULL;
96         te_count = 0;
97         fde_count = 0;
98
99         /* create a pipe */
100         pipe(fd);
101         read_fd = fd[0];
102         write_fd = fd[1];
103
104         fde = event_add_fd(ev_ctx, ev_ctx, read_fd, EVENT_FD_READ, 
105                            fde_handler, &read_fd);
106
107         event_add_timed(ev_ctx, ev_ctx, timeval_current_ofs(0,500), 
108                         timed_handler, fde);
109
110         while (fde) {
111                 event_loop_once(ev_ctx);
112         }
113
114         close(read_fd);
115         close(write_fd);
116         
117         talloc_free(ev_ctx);
118         return true;
119 }
120
121 struct torture_suite *torture_local_event(TALLOC_CTX *mem_ctx)
122 {
123         struct torture_suite *suite = torture_suite_create(mem_ctx, "EVENT");
124         const char **list = event_backend_list(suite);
125         int i;
126
127         for (i=0;list && list[i];i++) {
128                 torture_suite_add_simple_tcase(suite, list[i],
129                                                test_event_context,
130                                                (const void *)list[i]);
131         }
132
133         return suite;
134 }