r19339: Merge my 4.0-unittest branch. This adds an API for more fine-grained
[Samba.git] / source / torture / local / event.c
blob6579b1b277a527aa165e903200f1152d5d9779b1
1 /*
2 Unix SMB/CIFS implementation.
4 testing of the events subsystem
6 Copyright (C) Stefan Metzmacher
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.
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.
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.
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "system/filesys.h"
26 #include "torture/torture.h"
28 const struct event_ops *event_standard_get_ops(void);
29 const struct event_ops *event_liboop_get_ops(void);
30 const struct event_ops *gtk_event_get_ops(void);
32 static int write_fd, read_fd;
33 static struct fd_event *fde;
34 static int te_count;
35 static int fde_count;
36 static struct torture_context *test;
38 static void fde_handler(struct event_context *ev_ctx, struct fd_event *f,
39 uint16_t flags, void *private)
41 int *fd = private;
43 torture_comment(test, "event[%d] fd[%d] events[0x%08X]%s%s\n",
44 fde_count, *fd, flags,
45 (flags & EVENT_FD_READ)?" EVENT_FD_READ":"",
46 (flags & EVENT_FD_WRITE)?" EVENT_FD_WRITE":"");
48 if (fde_count > 5) {
49 _torture_fail_ext(test, "got more than fde 5 events - bug!");
50 talloc_free(fde);
51 fde = NULL;
52 return;
55 event_set_fd_flags(fde, 0);
56 fde_count++;
59 static void timed_handler(struct event_context *ev_ctx, struct timed_event *te,
60 struct timeval tval, void *private)
62 torture_comment(test, "timed_handler called[%d]\n", te_count);
63 if (te_count > 2) {
64 close(write_fd);
65 write_fd = -1;
67 if (te_count > 5) {
68 torture_comment(test, "remove fd event!\n");
69 talloc_free(fde);
70 fde = NULL;
71 return;
73 te_count++;
74 event_add_timed(ev_ctx, ev_ctx, timeval_current_ofs(0,500), timed_handler, private);
77 static bool test_event_context(struct torture_context *torture_ctx,
78 const void *test_data)
80 struct event_context *ev_ctx;
81 int fd[2] = { -1, -1 };
82 BOOL try_epoll = (BOOL)test_data;
83 TALLOC_CTX *mem_ctx = torture_ctx;
85 ev_ctx = event_context_init_ops(mem_ctx,
86 event_standard_get_ops(),
87 &try_epoll);
89 test = torture_ctx;
91 /* reset globals */
92 write_fd = -1;
93 read_fd = -1;
94 fde = NULL;
95 te_count = 0;
96 fde_count = 0;
98 /* create a pipe */
99 pipe(fd);
100 read_fd = fd[0];
101 write_fd = fd[1];
103 fde = event_add_fd(ev_ctx, ev_ctx, read_fd, EVENT_FD_READ, fde_handler, &read_fd);
105 event_add_timed(ev_ctx, ev_ctx, timeval_current_ofs(0,500), timed_handler, fde);
107 event_loop_wait(ev_ctx);
109 close(read_fd);
110 close(write_fd);
112 talloc_free(ev_ctx);
113 return true;
116 struct torture_suite *torture_local_event(TALLOC_CTX *mem_ctx)
118 struct torture_suite *suite = torture_suite_create(mem_ctx, "EVENT");
120 torture_suite_add_simple_tcase(suite, "standard with select",
121 test_event_context,
122 (void *)False);
124 torture_suite_add_simple_tcase(suite, "standard try epoll (or select)",
125 test_event_context,
126 (void *)True);
128 return suite;