1c43e99fdSEd Maste /*
2c43e99fdSEd Maste * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
3c43e99fdSEd Maste * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4c43e99fdSEd Maste *
5c43e99fdSEd Maste * Redistribution and use in source and binary forms, with or without
6c43e99fdSEd Maste * modification, are permitted provided that the following conditions
7c43e99fdSEd Maste * are met:
8c43e99fdSEd Maste * 1. Redistributions of source code must retain the above copyright
9c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer.
10c43e99fdSEd Maste * 2. Redistributions in binary form must reproduce the above copyright
11c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer in the
12c43e99fdSEd Maste * documentation and/or other materials provided with the distribution.
13c43e99fdSEd Maste * 3. The name of the author may not be used to endorse or promote products
14c43e99fdSEd Maste * derived from this software without specific prior written permission.
15c43e99fdSEd Maste *
16c43e99fdSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17c43e99fdSEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18c43e99fdSEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19c43e99fdSEd Maste * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20c43e99fdSEd Maste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21c43e99fdSEd Maste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22c43e99fdSEd Maste * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23c43e99fdSEd Maste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24c43e99fdSEd Maste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25c43e99fdSEd Maste * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26c43e99fdSEd Maste */
27c43e99fdSEd Maste #include "util-internal.h"
28c43e99fdSEd Maste
29c43e99fdSEd Maste #ifdef _WIN32
30c43e99fdSEd Maste #include <winsock2.h>
31c43e99fdSEd Maste #include <windows.h>
32c43e99fdSEd Maste #endif
33c43e99fdSEd Maste
34c43e99fdSEd Maste #include "event2/event-config.h"
35c43e99fdSEd Maste
36c43e99fdSEd Maste #include <sys/types.h>
37c43e99fdSEd Maste #include <sys/stat.h>
38c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_TIME_H
39c43e99fdSEd Maste #include <sys/time.h>
40c43e99fdSEd Maste #endif
41c43e99fdSEd Maste #include <sys/queue.h>
42c43e99fdSEd Maste #ifndef _WIN32
43c43e99fdSEd Maste #include <sys/socket.h>
44c43e99fdSEd Maste #include <sys/wait.h>
45*b50261e2SCy Schubert #include <limits.h>
46c43e99fdSEd Maste #include <signal.h>
47c43e99fdSEd Maste #include <unistd.h>
48c43e99fdSEd Maste #include <netdb.h>
49c43e99fdSEd Maste #endif
50c43e99fdSEd Maste #include <fcntl.h>
51c43e99fdSEd Maste #include <signal.h>
52c43e99fdSEd Maste #include <stdlib.h>
53c43e99fdSEd Maste #include <stdio.h>
54c43e99fdSEd Maste #include <string.h>
55c43e99fdSEd Maste #include <errno.h>
56c43e99fdSEd Maste #include <assert.h>
57c43e99fdSEd Maste #include <ctype.h>
58c43e99fdSEd Maste
59c43e99fdSEd Maste #include "event2/event.h"
60c43e99fdSEd Maste #include "event2/event_struct.h"
61c43e99fdSEd Maste #include "event2/event_compat.h"
62c43e99fdSEd Maste #include "event2/tag.h"
63c43e99fdSEd Maste #include "event2/buffer.h"
64c43e99fdSEd Maste #include "event2/buffer_compat.h"
65c43e99fdSEd Maste #include "event2/util.h"
66c43e99fdSEd Maste #include "event-internal.h"
67c43e99fdSEd Maste #include "evthread-internal.h"
68c43e99fdSEd Maste #include "log-internal.h"
69c43e99fdSEd Maste #include "time-internal.h"
70c43e99fdSEd Maste
71c43e99fdSEd Maste #include "regress.h"
72*b50261e2SCy Schubert #include "regress_thread.h"
73c43e99fdSEd Maste
74c43e99fdSEd Maste #ifndef _WIN32
75c43e99fdSEd Maste #include "regress.gen.h"
76c43e99fdSEd Maste #endif
77c43e99fdSEd Maste
78c43e99fdSEd Maste evutil_socket_t pair[2];
79c43e99fdSEd Maste int test_ok;
80c43e99fdSEd Maste int called;
81c43e99fdSEd Maste struct event_base *global_base;
82c43e99fdSEd Maste
83c43e99fdSEd Maste static char wbuf[4096];
84c43e99fdSEd Maste static char rbuf[4096];
85c43e99fdSEd Maste static int woff;
86c43e99fdSEd Maste static int roff;
87c43e99fdSEd Maste static int usepersist;
88c43e99fdSEd Maste static struct timeval tset;
89c43e99fdSEd Maste static struct timeval tcalled;
90c43e99fdSEd Maste
91c43e99fdSEd Maste
92c43e99fdSEd Maste #define TEST1 "this is a test"
93c43e99fdSEd Maste
94c43e99fdSEd Maste #ifdef _WIN32
95c43e99fdSEd Maste #define write(fd,buf,len) send((fd),(buf),(int)(len),0)
96c43e99fdSEd Maste #define read(fd,buf,len) recv((fd),(buf),(int)(len),0)
97c43e99fdSEd Maste #endif
98c43e99fdSEd Maste
99c43e99fdSEd Maste struct basic_cb_args
100c43e99fdSEd Maste {
101c43e99fdSEd Maste struct event_base *eb;
102c43e99fdSEd Maste struct event *ev;
103c43e99fdSEd Maste unsigned int callcount;
104c43e99fdSEd Maste };
105c43e99fdSEd Maste
106c43e99fdSEd Maste static void
simple_read_cb(evutil_socket_t fd,short event,void * arg)107c43e99fdSEd Maste simple_read_cb(evutil_socket_t fd, short event, void *arg)
108c43e99fdSEd Maste {
109c43e99fdSEd Maste char buf[256];
110c43e99fdSEd Maste int len;
111c43e99fdSEd Maste
112c43e99fdSEd Maste len = read(fd, buf, sizeof(buf));
113c43e99fdSEd Maste
114c43e99fdSEd Maste if (len) {
115c43e99fdSEd Maste if (!called) {
116c43e99fdSEd Maste if (event_add(arg, NULL) == -1)
117c43e99fdSEd Maste exit(1);
118c43e99fdSEd Maste }
119c43e99fdSEd Maste } else if (called == 1)
120c43e99fdSEd Maste test_ok = 1;
121c43e99fdSEd Maste
122c43e99fdSEd Maste called++;
123c43e99fdSEd Maste }
124c43e99fdSEd Maste
125c43e99fdSEd Maste static void
basic_read_cb(evutil_socket_t fd,short event,void * data)126c43e99fdSEd Maste basic_read_cb(evutil_socket_t fd, short event, void *data)
127c43e99fdSEd Maste {
128c43e99fdSEd Maste char buf[256];
129c43e99fdSEd Maste int len;
130c43e99fdSEd Maste struct basic_cb_args *arg = data;
131c43e99fdSEd Maste
132c43e99fdSEd Maste len = read(fd, buf, sizeof(buf));
133c43e99fdSEd Maste
134c43e99fdSEd Maste if (len < 0) {
135c43e99fdSEd Maste tt_fail_perror("read (callback)");
136c43e99fdSEd Maste } else {
137c43e99fdSEd Maste switch (arg->callcount++) {
138c43e99fdSEd Maste case 0: /* first call: expect to read data; cycle */
139c43e99fdSEd Maste if (len > 0)
140c43e99fdSEd Maste return;
141c43e99fdSEd Maste
142c43e99fdSEd Maste tt_fail_msg("EOF before data read");
143c43e99fdSEd Maste break;
144c43e99fdSEd Maste
145c43e99fdSEd Maste case 1: /* second call: expect EOF; stop */
146c43e99fdSEd Maste if (len > 0)
147c43e99fdSEd Maste tt_fail_msg("not all data read on first cycle");
148c43e99fdSEd Maste break;
149c43e99fdSEd Maste
150c43e99fdSEd Maste default: /* third call: should not happen */
151c43e99fdSEd Maste tt_fail_msg("too many cycles");
152c43e99fdSEd Maste }
153c43e99fdSEd Maste }
154c43e99fdSEd Maste
155c43e99fdSEd Maste event_del(arg->ev);
156c43e99fdSEd Maste event_base_loopexit(arg->eb, NULL);
157c43e99fdSEd Maste }
158c43e99fdSEd Maste
159c43e99fdSEd Maste static void
dummy_read_cb(evutil_socket_t fd,short event,void * arg)160c43e99fdSEd Maste dummy_read_cb(evutil_socket_t fd, short event, void *arg)
161c43e99fdSEd Maste {
162c43e99fdSEd Maste }
163c43e99fdSEd Maste
164c43e99fdSEd Maste static void
simple_write_cb(evutil_socket_t fd,short event,void * arg)165c43e99fdSEd Maste simple_write_cb(evutil_socket_t fd, short event, void *arg)
166c43e99fdSEd Maste {
167c43e99fdSEd Maste int len;
168c43e99fdSEd Maste
169c43e99fdSEd Maste len = write(fd, TEST1, strlen(TEST1) + 1);
170c43e99fdSEd Maste if (len == -1)
171c43e99fdSEd Maste test_ok = 0;
172c43e99fdSEd Maste else
173c43e99fdSEd Maste test_ok = 1;
174c43e99fdSEd Maste }
175c43e99fdSEd Maste
176c43e99fdSEd Maste static void
multiple_write_cb(evutil_socket_t fd,short event,void * arg)177c43e99fdSEd Maste multiple_write_cb(evutil_socket_t fd, short event, void *arg)
178c43e99fdSEd Maste {
179c43e99fdSEd Maste struct event *ev = arg;
180c43e99fdSEd Maste int len;
181c43e99fdSEd Maste
182c43e99fdSEd Maste len = 128;
183c43e99fdSEd Maste if (woff + len >= (int)sizeof(wbuf))
184c43e99fdSEd Maste len = sizeof(wbuf) - woff;
185c43e99fdSEd Maste
186c43e99fdSEd Maste len = write(fd, wbuf + woff, len);
187c43e99fdSEd Maste if (len == -1) {
188c43e99fdSEd Maste fprintf(stderr, "%s: write\n", __func__);
189c43e99fdSEd Maste if (usepersist)
190c43e99fdSEd Maste event_del(ev);
191c43e99fdSEd Maste return;
192c43e99fdSEd Maste }
193c43e99fdSEd Maste
194c43e99fdSEd Maste woff += len;
195c43e99fdSEd Maste
196c43e99fdSEd Maste if (woff >= (int)sizeof(wbuf)) {
197c43e99fdSEd Maste shutdown(fd, EVUTIL_SHUT_WR);
198c43e99fdSEd Maste if (usepersist)
199c43e99fdSEd Maste event_del(ev);
200c43e99fdSEd Maste return;
201c43e99fdSEd Maste }
202c43e99fdSEd Maste
203c43e99fdSEd Maste if (!usepersist) {
204c43e99fdSEd Maste if (event_add(ev, NULL) == -1)
205c43e99fdSEd Maste exit(1);
206c43e99fdSEd Maste }
207c43e99fdSEd Maste }
208c43e99fdSEd Maste
209c43e99fdSEd Maste static void
multiple_read_cb(evutil_socket_t fd,short event,void * arg)210c43e99fdSEd Maste multiple_read_cb(evutil_socket_t fd, short event, void *arg)
211c43e99fdSEd Maste {
212c43e99fdSEd Maste struct event *ev = arg;
213c43e99fdSEd Maste int len;
214c43e99fdSEd Maste
215c43e99fdSEd Maste len = read(fd, rbuf + roff, sizeof(rbuf) - roff);
216c43e99fdSEd Maste if (len == -1)
217c43e99fdSEd Maste fprintf(stderr, "%s: read\n", __func__);
218c43e99fdSEd Maste if (len <= 0) {
219c43e99fdSEd Maste if (usepersist)
220c43e99fdSEd Maste event_del(ev);
221c43e99fdSEd Maste return;
222c43e99fdSEd Maste }
223c43e99fdSEd Maste
224c43e99fdSEd Maste roff += len;
225c43e99fdSEd Maste if (!usepersist) {
226c43e99fdSEd Maste if (event_add(ev, NULL) == -1)
227c43e99fdSEd Maste exit(1);
228c43e99fdSEd Maste }
229c43e99fdSEd Maste }
230c43e99fdSEd Maste
231c43e99fdSEd Maste static void
timeout_cb(evutil_socket_t fd,short event,void * arg)232c43e99fdSEd Maste timeout_cb(evutil_socket_t fd, short event, void *arg)
233c43e99fdSEd Maste {
234c43e99fdSEd Maste evutil_gettimeofday(&tcalled, NULL);
235c43e99fdSEd Maste }
236c43e99fdSEd Maste
237c43e99fdSEd Maste struct both {
238c43e99fdSEd Maste struct event ev;
239c43e99fdSEd Maste int nread;
240c43e99fdSEd Maste };
241c43e99fdSEd Maste
242c43e99fdSEd Maste static void
combined_read_cb(evutil_socket_t fd,short event,void * arg)243c43e99fdSEd Maste combined_read_cb(evutil_socket_t fd, short event, void *arg)
244c43e99fdSEd Maste {
245c43e99fdSEd Maste struct both *both = arg;
246c43e99fdSEd Maste char buf[128];
247c43e99fdSEd Maste int len;
248c43e99fdSEd Maste
249c43e99fdSEd Maste len = read(fd, buf, sizeof(buf));
250c43e99fdSEd Maste if (len == -1)
251c43e99fdSEd Maste fprintf(stderr, "%s: read\n", __func__);
252c43e99fdSEd Maste if (len <= 0)
253c43e99fdSEd Maste return;
254c43e99fdSEd Maste
255c43e99fdSEd Maste both->nread += len;
256c43e99fdSEd Maste if (event_add(&both->ev, NULL) == -1)
257c43e99fdSEd Maste exit(1);
258c43e99fdSEd Maste }
259c43e99fdSEd Maste
260c43e99fdSEd Maste static void
combined_write_cb(evutil_socket_t fd,short event,void * arg)261c43e99fdSEd Maste combined_write_cb(evutil_socket_t fd, short event, void *arg)
262c43e99fdSEd Maste {
263c43e99fdSEd Maste struct both *both = arg;
264c43e99fdSEd Maste char buf[128];
265c43e99fdSEd Maste int len;
266c43e99fdSEd Maste
267c43e99fdSEd Maste len = sizeof(buf);
268c43e99fdSEd Maste if (len > both->nread)
269c43e99fdSEd Maste len = both->nread;
270c43e99fdSEd Maste
271c43e99fdSEd Maste memset(buf, 'q', len);
272c43e99fdSEd Maste
273c43e99fdSEd Maste len = write(fd, buf, len);
274c43e99fdSEd Maste if (len == -1)
275c43e99fdSEd Maste fprintf(stderr, "%s: write\n", __func__);
276c43e99fdSEd Maste if (len <= 0) {
277c43e99fdSEd Maste shutdown(fd, EVUTIL_SHUT_WR);
278c43e99fdSEd Maste return;
279c43e99fdSEd Maste }
280c43e99fdSEd Maste
281c43e99fdSEd Maste both->nread -= len;
282c43e99fdSEd Maste if (event_add(&both->ev, NULL) == -1)
283c43e99fdSEd Maste exit(1);
284c43e99fdSEd Maste }
285c43e99fdSEd Maste
286c43e99fdSEd Maste /* These macros used to replicate the work of the legacy test wrapper code */
287c43e99fdSEd Maste #define setup_test(x) do { \
288c43e99fdSEd Maste if (!in_legacy_test_wrapper) { \
289c43e99fdSEd Maste TT_FAIL(("Legacy test %s not wrapped properly", x)); \
290c43e99fdSEd Maste return; \
291c43e99fdSEd Maste } \
292c43e99fdSEd Maste } while (0)
293c43e99fdSEd Maste #define cleanup_test() setup_test("cleanup")
294c43e99fdSEd Maste
295c43e99fdSEd Maste static void
test_simpleread(void)296c43e99fdSEd Maste test_simpleread(void)
297c43e99fdSEd Maste {
298c43e99fdSEd Maste struct event ev;
299c43e99fdSEd Maste
300c43e99fdSEd Maste /* Very simple read test */
301c43e99fdSEd Maste setup_test("Simple read: ");
302c43e99fdSEd Maste
303c43e99fdSEd Maste if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
304c43e99fdSEd Maste tt_fail_perror("write");
305c43e99fdSEd Maste }
306c43e99fdSEd Maste
307c43e99fdSEd Maste shutdown(pair[0], EVUTIL_SHUT_WR);
308c43e99fdSEd Maste
309c43e99fdSEd Maste event_set(&ev, pair[1], EV_READ, simple_read_cb, &ev);
310c43e99fdSEd Maste if (event_add(&ev, NULL) == -1)
311c43e99fdSEd Maste exit(1);
312c43e99fdSEd Maste event_dispatch();
313c43e99fdSEd Maste
314c43e99fdSEd Maste cleanup_test();
315c43e99fdSEd Maste }
316c43e99fdSEd Maste
317c43e99fdSEd Maste static void
test_simplewrite(void)318c43e99fdSEd Maste test_simplewrite(void)
319c43e99fdSEd Maste {
320c43e99fdSEd Maste struct event ev;
321c43e99fdSEd Maste
322c43e99fdSEd Maste /* Very simple write test */
323c43e99fdSEd Maste setup_test("Simple write: ");
324c43e99fdSEd Maste
325c43e99fdSEd Maste event_set(&ev, pair[0], EV_WRITE, simple_write_cb, &ev);
326c43e99fdSEd Maste if (event_add(&ev, NULL) == -1)
327c43e99fdSEd Maste exit(1);
328c43e99fdSEd Maste event_dispatch();
329c43e99fdSEd Maste
330c43e99fdSEd Maste cleanup_test();
331c43e99fdSEd Maste }
332c43e99fdSEd Maste
333c43e99fdSEd Maste static void
simpleread_multiple_cb(evutil_socket_t fd,short event,void * arg)334c43e99fdSEd Maste simpleread_multiple_cb(evutil_socket_t fd, short event, void *arg)
335c43e99fdSEd Maste {
336c43e99fdSEd Maste if (++called == 2)
337c43e99fdSEd Maste test_ok = 1;
338c43e99fdSEd Maste }
339c43e99fdSEd Maste
340c43e99fdSEd Maste static void
test_simpleread_multiple(void)341c43e99fdSEd Maste test_simpleread_multiple(void)
342c43e99fdSEd Maste {
343c43e99fdSEd Maste struct event one, two;
344c43e99fdSEd Maste
345c43e99fdSEd Maste /* Very simple read test */
346c43e99fdSEd Maste setup_test("Simple read to multiple evens: ");
347c43e99fdSEd Maste
348c43e99fdSEd Maste if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
349c43e99fdSEd Maste tt_fail_perror("write");
350c43e99fdSEd Maste }
351c43e99fdSEd Maste
352c43e99fdSEd Maste shutdown(pair[0], EVUTIL_SHUT_WR);
353c43e99fdSEd Maste
354c43e99fdSEd Maste event_set(&one, pair[1], EV_READ, simpleread_multiple_cb, NULL);
355c43e99fdSEd Maste if (event_add(&one, NULL) == -1)
356c43e99fdSEd Maste exit(1);
357c43e99fdSEd Maste event_set(&two, pair[1], EV_READ, simpleread_multiple_cb, NULL);
358c43e99fdSEd Maste if (event_add(&two, NULL) == -1)
359c43e99fdSEd Maste exit(1);
360c43e99fdSEd Maste event_dispatch();
361c43e99fdSEd Maste
362c43e99fdSEd Maste cleanup_test();
363c43e99fdSEd Maste }
364c43e99fdSEd Maste
365c43e99fdSEd Maste static int have_closed = 0;
366c43e99fdSEd Maste static int premature_event = 0;
367c43e99fdSEd Maste static void
simpleclose_close_fd_cb(evutil_socket_t s,short what,void * ptr)368c43e99fdSEd Maste simpleclose_close_fd_cb(evutil_socket_t s, short what, void *ptr)
369c43e99fdSEd Maste {
370c43e99fdSEd Maste evutil_socket_t **fds = ptr;
371c43e99fdSEd Maste TT_BLATHER(("Closing"));
372c43e99fdSEd Maste evutil_closesocket(*fds[0]);
373c43e99fdSEd Maste evutil_closesocket(*fds[1]);
374c43e99fdSEd Maste *fds[0] = -1;
375c43e99fdSEd Maste *fds[1] = -1;
376c43e99fdSEd Maste have_closed = 1;
377c43e99fdSEd Maste }
378c43e99fdSEd Maste
379c43e99fdSEd Maste static void
record_event_cb(evutil_socket_t s,short what,void * ptr)380c43e99fdSEd Maste record_event_cb(evutil_socket_t s, short what, void *ptr)
381c43e99fdSEd Maste {
382c43e99fdSEd Maste short *whatp = ptr;
383c43e99fdSEd Maste if (!have_closed)
384c43e99fdSEd Maste premature_event = 1;
385c43e99fdSEd Maste *whatp = what;
386c43e99fdSEd Maste TT_BLATHER(("Recorded %d on socket %d", (int)what, (int)s));
387c43e99fdSEd Maste }
388c43e99fdSEd Maste
389c43e99fdSEd Maste static void
test_simpleclose_rw(void * ptr)390*b50261e2SCy Schubert test_simpleclose_rw(void *ptr)
391c43e99fdSEd Maste {
392c43e99fdSEd Maste /* Test that a close of FD is detected as a read and as a write. */
393c43e99fdSEd Maste struct event_base *base = event_base_new();
394c43e99fdSEd Maste evutil_socket_t pair1[2]={-1,-1}, pair2[2] = {-1, -1};
395c43e99fdSEd Maste evutil_socket_t *to_close[2];
396c43e99fdSEd Maste struct event *rev=NULL, *wev=NULL, *closeev=NULL;
397c43e99fdSEd Maste struct timeval tv;
398c43e99fdSEd Maste short got_read_on_close = 0, got_write_on_close = 0;
399c43e99fdSEd Maste char buf[1024];
400c43e99fdSEd Maste memset(buf, 99, sizeof(buf));
401c43e99fdSEd Maste #ifdef _WIN32
402c43e99fdSEd Maste #define LOCAL_SOCKETPAIR_AF AF_INET
403c43e99fdSEd Maste #else
404c43e99fdSEd Maste #define LOCAL_SOCKETPAIR_AF AF_UNIX
405c43e99fdSEd Maste #endif
406c43e99fdSEd Maste if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, pair1)<0)
407c43e99fdSEd Maste TT_DIE(("socketpair: %s", strerror(errno)));
408c43e99fdSEd Maste if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, pair2)<0)
409c43e99fdSEd Maste TT_DIE(("socketpair: %s", strerror(errno)));
410c43e99fdSEd Maste if (evutil_make_socket_nonblocking(pair1[1]) < 0)
411c43e99fdSEd Maste TT_DIE(("make_socket_nonblocking"));
412c43e99fdSEd Maste if (evutil_make_socket_nonblocking(pair2[1]) < 0)
413c43e99fdSEd Maste TT_DIE(("make_socket_nonblocking"));
414c43e99fdSEd Maste
415c43e99fdSEd Maste /** Stuff pair2[1] full of data, until write fails */
416c43e99fdSEd Maste while (1) {
417c43e99fdSEd Maste int r = write(pair2[1], buf, sizeof(buf));
418c43e99fdSEd Maste if (r<0) {
419c43e99fdSEd Maste int err = evutil_socket_geterror(pair2[1]);
420c43e99fdSEd Maste if (! EVUTIL_ERR_RW_RETRIABLE(err))
421c43e99fdSEd Maste TT_DIE(("write failed strangely: %s",
422c43e99fdSEd Maste evutil_socket_error_to_string(err)));
423c43e99fdSEd Maste break;
424c43e99fdSEd Maste }
425c43e99fdSEd Maste }
426c43e99fdSEd Maste to_close[0] = &pair1[0];
427c43e99fdSEd Maste to_close[1] = &pair2[0];
428c43e99fdSEd Maste
429c43e99fdSEd Maste closeev = event_new(base, -1, EV_TIMEOUT, simpleclose_close_fd_cb,
430c43e99fdSEd Maste to_close);
431c43e99fdSEd Maste rev = event_new(base, pair1[1], EV_READ, record_event_cb,
432c43e99fdSEd Maste &got_read_on_close);
433c43e99fdSEd Maste TT_BLATHER(("Waiting for read on %d", (int)pair1[1]));
434c43e99fdSEd Maste wev = event_new(base, pair2[1], EV_WRITE, record_event_cb,
435c43e99fdSEd Maste &got_write_on_close);
436c43e99fdSEd Maste TT_BLATHER(("Waiting for write on %d", (int)pair2[1]));
437c43e99fdSEd Maste tv.tv_sec = 0;
438c43e99fdSEd Maste tv.tv_usec = 100*1000; /* Close pair1[0] after a little while, and make
439c43e99fdSEd Maste * sure we get a read event. */
440c43e99fdSEd Maste event_add(closeev, &tv);
441c43e99fdSEd Maste event_add(rev, NULL);
442c43e99fdSEd Maste event_add(wev, NULL);
443c43e99fdSEd Maste /* Don't let the test go on too long. */
444c43e99fdSEd Maste tv.tv_sec = 0;
445c43e99fdSEd Maste tv.tv_usec = 200*1000;
446c43e99fdSEd Maste event_base_loopexit(base, &tv);
447c43e99fdSEd Maste event_base_loop(base, 0);
448c43e99fdSEd Maste
449c43e99fdSEd Maste tt_int_op(got_read_on_close, ==, EV_READ);
450c43e99fdSEd Maste tt_int_op(got_write_on_close, ==, EV_WRITE);
451c43e99fdSEd Maste tt_int_op(premature_event, ==, 0);
452c43e99fdSEd Maste
453c43e99fdSEd Maste end:
454c43e99fdSEd Maste if (pair1[0] >= 0)
455c43e99fdSEd Maste evutil_closesocket(pair1[0]);
456c43e99fdSEd Maste if (pair1[1] >= 0)
457c43e99fdSEd Maste evutil_closesocket(pair1[1]);
458c43e99fdSEd Maste if (pair2[0] >= 0)
459c43e99fdSEd Maste evutil_closesocket(pair2[0]);
460c43e99fdSEd Maste if (pair2[1] >= 0)
461c43e99fdSEd Maste evutil_closesocket(pair2[1]);
462c43e99fdSEd Maste if (rev)
463c43e99fdSEd Maste event_free(rev);
464c43e99fdSEd Maste if (wev)
465c43e99fdSEd Maste event_free(wev);
466c43e99fdSEd Maste if (closeev)
467c43e99fdSEd Maste event_free(closeev);
468c43e99fdSEd Maste if (base)
469c43e99fdSEd Maste event_base_free(base);
470c43e99fdSEd Maste }
471c43e99fdSEd Maste
472*b50261e2SCy Schubert static void
test_simpleclose(void * ptr)473*b50261e2SCy Schubert test_simpleclose(void *ptr)
474*b50261e2SCy Schubert {
475*b50261e2SCy Schubert struct basic_test_data *data = ptr;
476*b50261e2SCy Schubert struct event_base *base = data->base;
477*b50261e2SCy Schubert evutil_socket_t *pair = data->pair;
478*b50261e2SCy Schubert const char *flags = (const char *)data->setup_data;
479*b50261e2SCy Schubert int et = !!strstr(flags, "ET");
480*b50261e2SCy Schubert int persist = !!strstr(flags, "persist");
481*b50261e2SCy Schubert short events = EV_CLOSED | (et ? EV_ET : 0) | (persist ? EV_PERSIST : 0);
482*b50261e2SCy Schubert struct event *ev = NULL;
483*b50261e2SCy Schubert short got_event;
484*b50261e2SCy Schubert
485*b50261e2SCy Schubert if (!(event_base_get_features(data->base) & EV_FEATURE_EARLY_CLOSE))
486*b50261e2SCy Schubert tt_skip();
487*b50261e2SCy Schubert
488*b50261e2SCy Schubert /* XXX: should this code moved to regress_et.c ? */
489*b50261e2SCy Schubert if (et && !(event_base_get_features(data->base) & EV_FEATURE_ET))
490*b50261e2SCy Schubert tt_skip();
491*b50261e2SCy Schubert
492*b50261e2SCy Schubert ev = event_new(base, pair[0], events, record_event_cb, &got_event);
493*b50261e2SCy Schubert tt_assert(ev);
494*b50261e2SCy Schubert tt_assert(!event_add(ev, NULL));
495*b50261e2SCy Schubert
496*b50261e2SCy Schubert got_event = 0;
497*b50261e2SCy Schubert if (strstr(flags, "close")) {
498*b50261e2SCy Schubert tt_assert(!evutil_closesocket(pair[1]));
499*b50261e2SCy Schubert /* avoid closing in setup routines */
500*b50261e2SCy Schubert pair[1] = -1;
501*b50261e2SCy Schubert } else if (strstr(flags, "shutdown")) {
502*b50261e2SCy Schubert tt_assert(!shutdown(pair[1], EVUTIL_SHUT_WR));
503*b50261e2SCy Schubert } else {
504*b50261e2SCy Schubert tt_abort_msg("unknown flags");
505*b50261e2SCy Schubert }
506*b50261e2SCy Schubert
507*b50261e2SCy Schubert /* w/o edge-triggerd but w/ persist it will not stop */
508*b50261e2SCy Schubert if (!et && persist) {
509*b50261e2SCy Schubert struct timeval tv;
510*b50261e2SCy Schubert tv.tv_sec = 0;
511*b50261e2SCy Schubert tv.tv_usec = 10000;
512*b50261e2SCy Schubert tt_assert(!event_base_loopexit(base, &tv));
513*b50261e2SCy Schubert }
514*b50261e2SCy Schubert
515*b50261e2SCy Schubert tt_int_op(event_base_loop(base, EVLOOP_NONBLOCK), ==, !persist);
516*b50261e2SCy Schubert tt_int_op(got_event, ==, (events & ~EV_PERSIST));
517*b50261e2SCy Schubert
518*b50261e2SCy Schubert end:
519*b50261e2SCy Schubert if (ev)
520*b50261e2SCy Schubert event_free(ev);
521*b50261e2SCy Schubert }
522c43e99fdSEd Maste
523c43e99fdSEd Maste static void
test_multiple(void)524c43e99fdSEd Maste test_multiple(void)
525c43e99fdSEd Maste {
526c43e99fdSEd Maste struct event ev, ev2;
527c43e99fdSEd Maste int i;
528c43e99fdSEd Maste
529c43e99fdSEd Maste /* Multiple read and write test */
530c43e99fdSEd Maste setup_test("Multiple read/write: ");
531c43e99fdSEd Maste memset(rbuf, 0, sizeof(rbuf));
532c43e99fdSEd Maste for (i = 0; i < (int)sizeof(wbuf); i++)
533c43e99fdSEd Maste wbuf[i] = i;
534c43e99fdSEd Maste
535c43e99fdSEd Maste roff = woff = 0;
536c43e99fdSEd Maste usepersist = 0;
537c43e99fdSEd Maste
538c43e99fdSEd Maste event_set(&ev, pair[0], EV_WRITE, multiple_write_cb, &ev);
539c43e99fdSEd Maste if (event_add(&ev, NULL) == -1)
540c43e99fdSEd Maste exit(1);
541c43e99fdSEd Maste event_set(&ev2, pair[1], EV_READ, multiple_read_cb, &ev2);
542c43e99fdSEd Maste if (event_add(&ev2, NULL) == -1)
543c43e99fdSEd Maste exit(1);
544c43e99fdSEd Maste event_dispatch();
545c43e99fdSEd Maste
546c43e99fdSEd Maste if (roff == woff)
547c43e99fdSEd Maste test_ok = memcmp(rbuf, wbuf, sizeof(wbuf)) == 0;
548c43e99fdSEd Maste
549c43e99fdSEd Maste cleanup_test();
550c43e99fdSEd Maste }
551c43e99fdSEd Maste
552c43e99fdSEd Maste static void
test_persistent(void)553c43e99fdSEd Maste test_persistent(void)
554c43e99fdSEd Maste {
555c43e99fdSEd Maste struct event ev, ev2;
556c43e99fdSEd Maste int i;
557c43e99fdSEd Maste
558c43e99fdSEd Maste /* Multiple read and write test with persist */
559c43e99fdSEd Maste setup_test("Persist read/write: ");
560c43e99fdSEd Maste memset(rbuf, 0, sizeof(rbuf));
561c43e99fdSEd Maste for (i = 0; i < (int)sizeof(wbuf); i++)
562c43e99fdSEd Maste wbuf[i] = i;
563c43e99fdSEd Maste
564c43e99fdSEd Maste roff = woff = 0;
565c43e99fdSEd Maste usepersist = 1;
566c43e99fdSEd Maste
567c43e99fdSEd Maste event_set(&ev, pair[0], EV_WRITE|EV_PERSIST, multiple_write_cb, &ev);
568c43e99fdSEd Maste if (event_add(&ev, NULL) == -1)
569c43e99fdSEd Maste exit(1);
570c43e99fdSEd Maste event_set(&ev2, pair[1], EV_READ|EV_PERSIST, multiple_read_cb, &ev2);
571c43e99fdSEd Maste if (event_add(&ev2, NULL) == -1)
572c43e99fdSEd Maste exit(1);
573c43e99fdSEd Maste event_dispatch();
574c43e99fdSEd Maste
575c43e99fdSEd Maste if (roff == woff)
576c43e99fdSEd Maste test_ok = memcmp(rbuf, wbuf, sizeof(wbuf)) == 0;
577c43e99fdSEd Maste
578c43e99fdSEd Maste cleanup_test();
579c43e99fdSEd Maste }
580c43e99fdSEd Maste
581c43e99fdSEd Maste static void
test_combined(void)582c43e99fdSEd Maste test_combined(void)
583c43e99fdSEd Maste {
584c43e99fdSEd Maste struct both r1, r2, w1, w2;
585c43e99fdSEd Maste
586c43e99fdSEd Maste setup_test("Combined read/write: ");
587c43e99fdSEd Maste memset(&r1, 0, sizeof(r1));
588c43e99fdSEd Maste memset(&r2, 0, sizeof(r2));
589c43e99fdSEd Maste memset(&w1, 0, sizeof(w1));
590c43e99fdSEd Maste memset(&w2, 0, sizeof(w2));
591c43e99fdSEd Maste
592c43e99fdSEd Maste w1.nread = 4096;
593c43e99fdSEd Maste w2.nread = 8192;
594c43e99fdSEd Maste
595c43e99fdSEd Maste event_set(&r1.ev, pair[0], EV_READ, combined_read_cb, &r1);
596c43e99fdSEd Maste event_set(&w1.ev, pair[0], EV_WRITE, combined_write_cb, &w1);
597c43e99fdSEd Maste event_set(&r2.ev, pair[1], EV_READ, combined_read_cb, &r2);
598c43e99fdSEd Maste event_set(&w2.ev, pair[1], EV_WRITE, combined_write_cb, &w2);
599c43e99fdSEd Maste tt_assert(event_add(&r1.ev, NULL) != -1);
600c43e99fdSEd Maste tt_assert(!event_add(&w1.ev, NULL));
601c43e99fdSEd Maste tt_assert(!event_add(&r2.ev, NULL));
602c43e99fdSEd Maste tt_assert(!event_add(&w2.ev, NULL));
603c43e99fdSEd Maste event_dispatch();
604c43e99fdSEd Maste
605c43e99fdSEd Maste if (r1.nread == 8192 && r2.nread == 4096)
606c43e99fdSEd Maste test_ok = 1;
607c43e99fdSEd Maste
608c43e99fdSEd Maste end:
609c43e99fdSEd Maste cleanup_test();
610c43e99fdSEd Maste }
611c43e99fdSEd Maste
612c43e99fdSEd Maste static void
test_simpletimeout(void)613c43e99fdSEd Maste test_simpletimeout(void)
614c43e99fdSEd Maste {
615c43e99fdSEd Maste struct timeval tv;
616c43e99fdSEd Maste struct event ev;
617c43e99fdSEd Maste
618c43e99fdSEd Maste setup_test("Simple timeout: ");
619c43e99fdSEd Maste
620c43e99fdSEd Maste tv.tv_usec = 200*1000;
621c43e99fdSEd Maste tv.tv_sec = 0;
622c43e99fdSEd Maste evutil_timerclear(&tcalled);
623c43e99fdSEd Maste evtimer_set(&ev, timeout_cb, NULL);
624c43e99fdSEd Maste evtimer_add(&ev, &tv);
625c43e99fdSEd Maste
626c43e99fdSEd Maste evutil_gettimeofday(&tset, NULL);
627c43e99fdSEd Maste event_dispatch();
628c43e99fdSEd Maste test_timeval_diff_eq(&tset, &tcalled, 200);
629c43e99fdSEd Maste
630c43e99fdSEd Maste test_ok = 1;
631c43e99fdSEd Maste end:
632c43e99fdSEd Maste cleanup_test();
633c43e99fdSEd Maste }
634c43e99fdSEd Maste
635c43e99fdSEd Maste static void
periodic_timeout_cb(evutil_socket_t fd,short event,void * arg)636c43e99fdSEd Maste periodic_timeout_cb(evutil_socket_t fd, short event, void *arg)
637c43e99fdSEd Maste {
638c43e99fdSEd Maste int *count = arg;
639c43e99fdSEd Maste
640c43e99fdSEd Maste (*count)++;
641c43e99fdSEd Maste if (*count == 6) {
642c43e99fdSEd Maste /* call loopexit only once - on slow machines(?), it is
643c43e99fdSEd Maste * apparently possible for this to get called twice. */
644c43e99fdSEd Maste test_ok = 1;
645c43e99fdSEd Maste event_base_loopexit(global_base, NULL);
646c43e99fdSEd Maste }
647c43e99fdSEd Maste }
648c43e99fdSEd Maste
649c43e99fdSEd Maste static void
test_persistent_timeout(void)650c43e99fdSEd Maste test_persistent_timeout(void)
651c43e99fdSEd Maste {
652c43e99fdSEd Maste struct timeval tv;
653c43e99fdSEd Maste struct event ev;
654c43e99fdSEd Maste int count = 0;
655c43e99fdSEd Maste
656c43e99fdSEd Maste evutil_timerclear(&tv);
657c43e99fdSEd Maste tv.tv_usec = 10000;
658c43e99fdSEd Maste
659c43e99fdSEd Maste event_assign(&ev, global_base, -1, EV_TIMEOUT|EV_PERSIST,
660c43e99fdSEd Maste periodic_timeout_cb, &count);
661c43e99fdSEd Maste event_add(&ev, &tv);
662c43e99fdSEd Maste
663c43e99fdSEd Maste event_dispatch();
664c43e99fdSEd Maste
665c43e99fdSEd Maste event_del(&ev);
666c43e99fdSEd Maste }
667c43e99fdSEd Maste
668c43e99fdSEd Maste static void
test_persistent_timeout_jump(void * ptr)669c43e99fdSEd Maste test_persistent_timeout_jump(void *ptr)
670c43e99fdSEd Maste {
671c43e99fdSEd Maste struct basic_test_data *data = ptr;
672c43e99fdSEd Maste struct event ev;
673c43e99fdSEd Maste int count = 0;
674c43e99fdSEd Maste struct timeval msec100 = { 0, 100 * 1000 };
675c43e99fdSEd Maste struct timeval msec50 = { 0, 50 * 1000 };
676c43e99fdSEd Maste struct timeval msec300 = { 0, 300 * 1000 };
677c43e99fdSEd Maste
678c43e99fdSEd Maste event_assign(&ev, data->base, -1, EV_PERSIST, periodic_timeout_cb, &count);
679c43e99fdSEd Maste event_add(&ev, &msec100);
680c43e99fdSEd Maste /* Wait for a bit */
681c43e99fdSEd Maste evutil_usleep_(&msec300);
682c43e99fdSEd Maste event_base_loopexit(data->base, &msec50);
683c43e99fdSEd Maste event_base_dispatch(data->base);
684c43e99fdSEd Maste tt_int_op(count, ==, 1);
685c43e99fdSEd Maste
686c43e99fdSEd Maste end:
687c43e99fdSEd Maste event_del(&ev);
688c43e99fdSEd Maste }
689c43e99fdSEd Maste
690c43e99fdSEd Maste struct persist_active_timeout_called {
691c43e99fdSEd Maste int n;
692c43e99fdSEd Maste short events[16];
693c43e99fdSEd Maste struct timeval tvs[16];
694c43e99fdSEd Maste };
695c43e99fdSEd Maste
696c43e99fdSEd Maste static void
activate_cb(evutil_socket_t fd,short event,void * arg)697c43e99fdSEd Maste activate_cb(evutil_socket_t fd, short event, void *arg)
698c43e99fdSEd Maste {
699c43e99fdSEd Maste struct event *ev = arg;
700c43e99fdSEd Maste event_active(ev, EV_READ, 1);
701c43e99fdSEd Maste }
702c43e99fdSEd Maste
703c43e99fdSEd Maste static void
persist_active_timeout_cb(evutil_socket_t fd,short event,void * arg)704c43e99fdSEd Maste persist_active_timeout_cb(evutil_socket_t fd, short event, void *arg)
705c43e99fdSEd Maste {
706c43e99fdSEd Maste struct persist_active_timeout_called *c = arg;
707c43e99fdSEd Maste if (c->n < 15) {
708c43e99fdSEd Maste c->events[c->n] = event;
709c43e99fdSEd Maste evutil_gettimeofday(&c->tvs[c->n], NULL);
710c43e99fdSEd Maste ++c->n;
711c43e99fdSEd Maste }
712c43e99fdSEd Maste }
713c43e99fdSEd Maste
714c43e99fdSEd Maste static void
test_persistent_active_timeout(void * ptr)715c43e99fdSEd Maste test_persistent_active_timeout(void *ptr)
716c43e99fdSEd Maste {
717c43e99fdSEd Maste struct timeval tv, tv2, tv_exit, start;
718c43e99fdSEd Maste struct event ev;
719c43e99fdSEd Maste struct persist_active_timeout_called res;
720c43e99fdSEd Maste
721c43e99fdSEd Maste struct basic_test_data *data = ptr;
722c43e99fdSEd Maste struct event_base *base = data->base;
723c43e99fdSEd Maste
724c43e99fdSEd Maste memset(&res, 0, sizeof(res));
725c43e99fdSEd Maste
726c43e99fdSEd Maste tv.tv_sec = 0;
727c43e99fdSEd Maste tv.tv_usec = 200 * 1000;
728c43e99fdSEd Maste event_assign(&ev, base, -1, EV_TIMEOUT|EV_PERSIST,
729c43e99fdSEd Maste persist_active_timeout_cb, &res);
730c43e99fdSEd Maste event_add(&ev, &tv);
731c43e99fdSEd Maste
732c43e99fdSEd Maste tv2.tv_sec = 0;
733c43e99fdSEd Maste tv2.tv_usec = 100 * 1000;
734c43e99fdSEd Maste event_base_once(base, -1, EV_TIMEOUT, activate_cb, &ev, &tv2);
735c43e99fdSEd Maste
736c43e99fdSEd Maste tv_exit.tv_sec = 0;
737c43e99fdSEd Maste tv_exit.tv_usec = 600 * 1000;
738c43e99fdSEd Maste event_base_loopexit(base, &tv_exit);
739c43e99fdSEd Maste
740c43e99fdSEd Maste event_base_assert_ok_(base);
741c43e99fdSEd Maste evutil_gettimeofday(&start, NULL);
742c43e99fdSEd Maste
743c43e99fdSEd Maste event_base_dispatch(base);
744c43e99fdSEd Maste event_base_assert_ok_(base);
745c43e99fdSEd Maste
746c43e99fdSEd Maste tt_int_op(res.n, ==, 3);
747c43e99fdSEd Maste tt_int_op(res.events[0], ==, EV_READ);
748c43e99fdSEd Maste tt_int_op(res.events[1], ==, EV_TIMEOUT);
749c43e99fdSEd Maste tt_int_op(res.events[2], ==, EV_TIMEOUT);
750c43e99fdSEd Maste test_timeval_diff_eq(&start, &res.tvs[0], 100);
751c43e99fdSEd Maste test_timeval_diff_eq(&start, &res.tvs[1], 300);
752c43e99fdSEd Maste test_timeval_diff_eq(&start, &res.tvs[2], 500);
753c43e99fdSEd Maste end:
754c43e99fdSEd Maste event_del(&ev);
755c43e99fdSEd Maste }
756c43e99fdSEd Maste
757c43e99fdSEd Maste struct common_timeout_info {
758c43e99fdSEd Maste struct event ev;
759c43e99fdSEd Maste struct timeval called_at;
760c43e99fdSEd Maste int which;
761c43e99fdSEd Maste int count;
762c43e99fdSEd Maste };
763c43e99fdSEd Maste
764c43e99fdSEd Maste static void
common_timeout_cb(evutil_socket_t fd,short event,void * arg)765c43e99fdSEd Maste common_timeout_cb(evutil_socket_t fd, short event, void *arg)
766c43e99fdSEd Maste {
767c43e99fdSEd Maste struct common_timeout_info *ti = arg;
768c43e99fdSEd Maste ++ti->count;
769c43e99fdSEd Maste evutil_gettimeofday(&ti->called_at, NULL);
770c43e99fdSEd Maste if (ti->count >= 4)
771c43e99fdSEd Maste event_del(&ti->ev);
772c43e99fdSEd Maste }
773c43e99fdSEd Maste
774c43e99fdSEd Maste static void
test_common_timeout(void * ptr)775c43e99fdSEd Maste test_common_timeout(void *ptr)
776c43e99fdSEd Maste {
777c43e99fdSEd Maste struct basic_test_data *data = ptr;
778c43e99fdSEd Maste
779c43e99fdSEd Maste struct event_base *base = data->base;
780c43e99fdSEd Maste int i;
781c43e99fdSEd Maste struct common_timeout_info info[100];
782c43e99fdSEd Maste
783c43e99fdSEd Maste struct timeval start;
784c43e99fdSEd Maste struct timeval tmp_100_ms = { 0, 100*1000 };
785c43e99fdSEd Maste struct timeval tmp_200_ms = { 0, 200*1000 };
786c43e99fdSEd Maste struct timeval tmp_5_sec = { 5, 0 };
787c43e99fdSEd Maste struct timeval tmp_5M_usec = { 0, 5*1000*1000 };
788c43e99fdSEd Maste
789c43e99fdSEd Maste const struct timeval *ms_100, *ms_200, *sec_5;
790c43e99fdSEd Maste
791c43e99fdSEd Maste ms_100 = event_base_init_common_timeout(base, &tmp_100_ms);
792c43e99fdSEd Maste ms_200 = event_base_init_common_timeout(base, &tmp_200_ms);
793c43e99fdSEd Maste sec_5 = event_base_init_common_timeout(base, &tmp_5_sec);
794c43e99fdSEd Maste tt_assert(ms_100);
795c43e99fdSEd Maste tt_assert(ms_200);
796c43e99fdSEd Maste tt_assert(sec_5);
797c43e99fdSEd Maste tt_ptr_op(event_base_init_common_timeout(base, &tmp_200_ms),
798c43e99fdSEd Maste ==, ms_200);
799c43e99fdSEd Maste tt_ptr_op(event_base_init_common_timeout(base, ms_200), ==, ms_200);
800c43e99fdSEd Maste tt_ptr_op(event_base_init_common_timeout(base, &tmp_5M_usec), ==, sec_5);
801c43e99fdSEd Maste tt_int_op(ms_100->tv_sec, ==, 0);
802c43e99fdSEd Maste tt_int_op(ms_200->tv_sec, ==, 0);
803c43e99fdSEd Maste tt_int_op(sec_5->tv_sec, ==, 5);
804c43e99fdSEd Maste tt_int_op(ms_100->tv_usec, ==, 100000|0x50000000);
805c43e99fdSEd Maste tt_int_op(ms_200->tv_usec, ==, 200000|0x50100000);
806c43e99fdSEd Maste tt_int_op(sec_5->tv_usec, ==, 0|0x50200000);
807c43e99fdSEd Maste
808c43e99fdSEd Maste memset(info, 0, sizeof(info));
809c43e99fdSEd Maste
810c43e99fdSEd Maste for (i=0; i<100; ++i) {
811c43e99fdSEd Maste info[i].which = i;
812c43e99fdSEd Maste event_assign(&info[i].ev, base, -1, EV_TIMEOUT|EV_PERSIST,
813c43e99fdSEd Maste common_timeout_cb, &info[i]);
814c43e99fdSEd Maste if (i % 2) {
815c43e99fdSEd Maste if ((i%20)==1) {
816c43e99fdSEd Maste /* Glass-box test: Make sure we survive the
817c43e99fdSEd Maste * transition to non-common timeouts. It's
818c43e99fdSEd Maste * a little tricky. */
819c43e99fdSEd Maste event_add(&info[i].ev, ms_200);
820c43e99fdSEd Maste event_add(&info[i].ev, &tmp_100_ms);
821c43e99fdSEd Maste } else if ((i%20)==3) {
822c43e99fdSEd Maste /* Check heap-to-common too. */
823c43e99fdSEd Maste event_add(&info[i].ev, &tmp_200_ms);
824c43e99fdSEd Maste event_add(&info[i].ev, ms_100);
825c43e99fdSEd Maste } else if ((i%20)==5) {
826c43e99fdSEd Maste /* Also check common-to-common. */
827c43e99fdSEd Maste event_add(&info[i].ev, ms_200);
828c43e99fdSEd Maste event_add(&info[i].ev, ms_100);
829c43e99fdSEd Maste } else {
830c43e99fdSEd Maste event_add(&info[i].ev, ms_100);
831c43e99fdSEd Maste }
832c43e99fdSEd Maste } else {
833c43e99fdSEd Maste event_add(&info[i].ev, ms_200);
834c43e99fdSEd Maste }
835c43e99fdSEd Maste }
836c43e99fdSEd Maste
837c43e99fdSEd Maste event_base_assert_ok_(base);
838c43e99fdSEd Maste evutil_gettimeofday(&start, NULL);
839c43e99fdSEd Maste event_base_dispatch(base);
840c43e99fdSEd Maste
841c43e99fdSEd Maste event_base_assert_ok_(base);
842c43e99fdSEd Maste
843c43e99fdSEd Maste for (i=0; i<10; ++i) {
844c43e99fdSEd Maste tt_int_op(info[i].count, ==, 4);
845c43e99fdSEd Maste if (i % 2) {
846c43e99fdSEd Maste test_timeval_diff_eq(&start, &info[i].called_at, 400);
847c43e99fdSEd Maste } else {
848c43e99fdSEd Maste test_timeval_diff_eq(&start, &info[i].called_at, 800);
849c43e99fdSEd Maste }
850c43e99fdSEd Maste }
851c43e99fdSEd Maste
852c43e99fdSEd Maste /* Make sure we can free the base with some events in. */
853c43e99fdSEd Maste for (i=0; i<100; ++i) {
854c43e99fdSEd Maste if (i % 2) {
855c43e99fdSEd Maste event_add(&info[i].ev, ms_100);
856c43e99fdSEd Maste } else {
857c43e99fdSEd Maste event_add(&info[i].ev, ms_200);
858c43e99fdSEd Maste }
859c43e99fdSEd Maste }
860c43e99fdSEd Maste
861c43e99fdSEd Maste end:
862c43e99fdSEd Maste event_base_free(data->base); /* need to do this here before info is
863c43e99fdSEd Maste * out-of-scope */
864c43e99fdSEd Maste data->base = NULL;
865c43e99fdSEd Maste }
866c43e99fdSEd Maste
867c43e99fdSEd Maste #ifndef _WIN32
868c43e99fdSEd Maste
869c43e99fdSEd Maste #define current_base event_global_current_base_
870c43e99fdSEd Maste extern struct event_base *current_base;
871c43e99fdSEd Maste
872c43e99fdSEd Maste static void
fork_signal_cb(evutil_socket_t fd,short events,void * arg)873c43e99fdSEd Maste fork_signal_cb(evutil_socket_t fd, short events, void *arg)
874c43e99fdSEd Maste {
875c43e99fdSEd Maste event_del(arg);
876c43e99fdSEd Maste }
877c43e99fdSEd Maste
878c43e99fdSEd Maste int child_pair[2] = { -1, -1 };
879c43e99fdSEd Maste static void
simple_child_read_cb(evutil_socket_t fd,short event,void * arg)880c43e99fdSEd Maste simple_child_read_cb(evutil_socket_t fd, short event, void *arg)
881c43e99fdSEd Maste {
882c43e99fdSEd Maste char buf[256];
883c43e99fdSEd Maste int len;
884c43e99fdSEd Maste
885c43e99fdSEd Maste len = read(fd, buf, sizeof(buf));
886c43e99fdSEd Maste if (write(child_pair[0], "", 1) < 0)
887c43e99fdSEd Maste tt_fail_perror("write");
888c43e99fdSEd Maste
889c43e99fdSEd Maste if (len) {
890c43e99fdSEd Maste if (!called) {
891c43e99fdSEd Maste if (event_add(arg, NULL) == -1)
892c43e99fdSEd Maste exit(1);
893c43e99fdSEd Maste }
894c43e99fdSEd Maste } else if (called == 1)
895c43e99fdSEd Maste test_ok = 1;
896c43e99fdSEd Maste
897c43e99fdSEd Maste called++;
898c43e99fdSEd Maste }
899*b50261e2SCy Schubert
900*b50261e2SCy Schubert #define TEST_FORK_EXIT_SUCCESS 76
fork_wait_check(int pid)901*b50261e2SCy Schubert static void fork_wait_check(int pid)
902*b50261e2SCy Schubert {
903*b50261e2SCy Schubert int status;
904*b50261e2SCy Schubert
905*b50261e2SCy Schubert TT_BLATHER(("Before waitpid"));
906*b50261e2SCy Schubert
907*b50261e2SCy Schubert #ifdef WNOWAIT
908*b50261e2SCy Schubert if ((waitpid(pid, &status, WNOWAIT) == -1 && errno == EINVAL) &&
909*b50261e2SCy Schubert #else
910*b50261e2SCy Schubert if (
911*b50261e2SCy Schubert #endif
912*b50261e2SCy Schubert waitpid(pid, &status, 0) == -1) {
913*b50261e2SCy Schubert perror("waitpid");
914*b50261e2SCy Schubert exit(1);
915*b50261e2SCy Schubert }
916*b50261e2SCy Schubert TT_BLATHER(("After waitpid"));
917*b50261e2SCy Schubert
918*b50261e2SCy Schubert if (WEXITSTATUS(status) != TEST_FORK_EXIT_SUCCESS) {
919*b50261e2SCy Schubert fprintf(stdout, "FAILED (exit): %d\n", WEXITSTATUS(status));
920*b50261e2SCy Schubert exit(1);
921*b50261e2SCy Schubert }
922*b50261e2SCy Schubert }
923c43e99fdSEd Maste static void
test_fork(void)924c43e99fdSEd Maste test_fork(void)
925c43e99fdSEd Maste {
926c43e99fdSEd Maste char c;
927c43e99fdSEd Maste struct event ev, sig_ev, usr_ev, existing_ev;
928c43e99fdSEd Maste pid_t pid;
929c43e99fdSEd Maste
930c43e99fdSEd Maste setup_test("After fork: ");
931c43e99fdSEd Maste
932c43e99fdSEd Maste {
933c43e99fdSEd Maste if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, child_pair) == -1) {
934c43e99fdSEd Maste fprintf(stderr, "%s: socketpair\n", __func__);
935c43e99fdSEd Maste exit(1);
936c43e99fdSEd Maste }
937c43e99fdSEd Maste
938c43e99fdSEd Maste if (evutil_make_socket_nonblocking(child_pair[0]) == -1) {
939c43e99fdSEd Maste fprintf(stderr, "fcntl(O_NONBLOCK)");
940c43e99fdSEd Maste exit(1);
941c43e99fdSEd Maste }
942c43e99fdSEd Maste }
943c43e99fdSEd Maste
944c43e99fdSEd Maste tt_assert(current_base);
945c43e99fdSEd Maste evthread_make_base_notifiable(current_base);
946c43e99fdSEd Maste
947c43e99fdSEd Maste if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
948c43e99fdSEd Maste tt_fail_perror("write");
949c43e99fdSEd Maste }
950c43e99fdSEd Maste
951c43e99fdSEd Maste event_set(&ev, pair[1], EV_READ, simple_child_read_cb, &ev);
952c43e99fdSEd Maste if (event_add(&ev, NULL) == -1)
953c43e99fdSEd Maste exit(1);
954c43e99fdSEd Maste
955c43e99fdSEd Maste evsignal_set(&sig_ev, SIGCHLD, fork_signal_cb, &sig_ev);
956c43e99fdSEd Maste evsignal_add(&sig_ev, NULL);
957c43e99fdSEd Maste
958c43e99fdSEd Maste evsignal_set(&existing_ev, SIGUSR2, fork_signal_cb, &existing_ev);
959c43e99fdSEd Maste evsignal_add(&existing_ev, NULL);
960c43e99fdSEd Maste
961c43e99fdSEd Maste event_base_assert_ok_(current_base);
962c43e99fdSEd Maste TT_BLATHER(("Before fork"));
963c43e99fdSEd Maste if ((pid = regress_fork()) == 0) {
964c43e99fdSEd Maste /* in the child */
965c43e99fdSEd Maste TT_BLATHER(("In child, before reinit"));
966c43e99fdSEd Maste event_base_assert_ok_(current_base);
967c43e99fdSEd Maste if (event_reinit(current_base) == -1) {
968c43e99fdSEd Maste fprintf(stdout, "FAILED (reinit)\n");
969c43e99fdSEd Maste exit(1);
970c43e99fdSEd Maste }
971c43e99fdSEd Maste TT_BLATHER(("After reinit"));
972c43e99fdSEd Maste event_base_assert_ok_(current_base);
973c43e99fdSEd Maste TT_BLATHER(("After assert-ok"));
974c43e99fdSEd Maste
975c43e99fdSEd Maste evsignal_del(&sig_ev);
976c43e99fdSEd Maste
977c43e99fdSEd Maste evsignal_set(&usr_ev, SIGUSR1, fork_signal_cb, &usr_ev);
978c43e99fdSEd Maste evsignal_add(&usr_ev, NULL);
979*b50261e2SCy Schubert kill(getpid(), SIGUSR1);
980*b50261e2SCy Schubert kill(getpid(), SIGUSR2);
981c43e99fdSEd Maste
982c43e99fdSEd Maste called = 0;
983c43e99fdSEd Maste
984c43e99fdSEd Maste event_dispatch();
985c43e99fdSEd Maste
986c43e99fdSEd Maste event_base_free(current_base);
987c43e99fdSEd Maste
988c43e99fdSEd Maste /* we do not send an EOF; simple_read_cb requires an EOF
989c43e99fdSEd Maste * to set test_ok. we just verify that the callback was
990c43e99fdSEd Maste * called. */
991*b50261e2SCy Schubert exit(test_ok != 0 || called != 2 ? -2 : TEST_FORK_EXIT_SUCCESS);
992c43e99fdSEd Maste }
993c43e99fdSEd Maste
994c43e99fdSEd Maste /** wait until client read first message */
995c43e99fdSEd Maste if (read(child_pair[1], &c, 1) < 0) {
996c43e99fdSEd Maste tt_fail_perror("read");
997c43e99fdSEd Maste }
998c43e99fdSEd Maste if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
999c43e99fdSEd Maste tt_fail_perror("write");
1000c43e99fdSEd Maste }
1001c43e99fdSEd Maste
1002*b50261e2SCy Schubert fork_wait_check(pid);
1003c43e99fdSEd Maste
1004c43e99fdSEd Maste /* test that the current event loop still works */
1005c43e99fdSEd Maste if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
1006c43e99fdSEd Maste fprintf(stderr, "%s: write\n", __func__);
1007c43e99fdSEd Maste }
1008c43e99fdSEd Maste
1009c43e99fdSEd Maste shutdown(pair[0], EVUTIL_SHUT_WR);
1010c43e99fdSEd Maste
1011c43e99fdSEd Maste evsignal_set(&usr_ev, SIGUSR1, fork_signal_cb, &usr_ev);
1012c43e99fdSEd Maste evsignal_add(&usr_ev, NULL);
1013*b50261e2SCy Schubert kill(getpid(), SIGUSR1);
1014*b50261e2SCy Schubert kill(getpid(), SIGUSR2);
1015c43e99fdSEd Maste
1016c43e99fdSEd Maste event_dispatch();
1017c43e99fdSEd Maste
1018c43e99fdSEd Maste evsignal_del(&sig_ev);
1019c43e99fdSEd Maste tt_int_op(test_ok, ==, 1);
1020c43e99fdSEd Maste
1021c43e99fdSEd Maste end:
1022c43e99fdSEd Maste cleanup_test();
1023c43e99fdSEd Maste if (child_pair[0] != -1)
1024c43e99fdSEd Maste evutil_closesocket(child_pair[0]);
1025c43e99fdSEd Maste if (child_pair[1] != -1)
1026c43e99fdSEd Maste evutil_closesocket(child_pair[1]);
1027c43e99fdSEd Maste }
1028c43e99fdSEd Maste
1029*b50261e2SCy Schubert #ifdef EVTHREAD_USE_PTHREADS_IMPLEMENTED
del_wait_thread(void * arg)1030c43e99fdSEd Maste static void* del_wait_thread(void *arg)
1031c43e99fdSEd Maste {
1032c43e99fdSEd Maste struct timeval tv_start, tv_end;
1033c43e99fdSEd Maste
1034c43e99fdSEd Maste evutil_gettimeofday(&tv_start, NULL);
1035c43e99fdSEd Maste event_dispatch();
1036c43e99fdSEd Maste evutil_gettimeofday(&tv_end, NULL);
1037c43e99fdSEd Maste
1038c43e99fdSEd Maste test_timeval_diff_eq(&tv_start, &tv_end, 300);
1039c43e99fdSEd Maste
1040c43e99fdSEd Maste end:
1041c43e99fdSEd Maste return NULL;
1042c43e99fdSEd Maste }
1043c43e99fdSEd Maste
1044c43e99fdSEd Maste static void
del_wait_cb(evutil_socket_t fd,short event,void * arg)1045c43e99fdSEd Maste del_wait_cb(evutil_socket_t fd, short event, void *arg)
1046c43e99fdSEd Maste {
1047c43e99fdSEd Maste struct timeval delay = { 0, 300*1000 };
1048*b50261e2SCy Schubert TT_BLATHER(("Sleeping: %i", test_ok));
1049c43e99fdSEd Maste evutil_usleep_(&delay);
1050*b50261e2SCy Schubert ++test_ok;
1051c43e99fdSEd Maste }
1052c43e99fdSEd Maste
1053c43e99fdSEd Maste static void
test_del_wait(void)1054c43e99fdSEd Maste test_del_wait(void)
1055c43e99fdSEd Maste {
1056c43e99fdSEd Maste struct event ev;
1057*b50261e2SCy Schubert THREAD_T thread;
1058c43e99fdSEd Maste
1059c43e99fdSEd Maste setup_test("event_del will wait: ");
1060c43e99fdSEd Maste
1061*b50261e2SCy Schubert event_set(&ev, pair[1], EV_READ|EV_PERSIST, del_wait_cb, &ev);
1062c43e99fdSEd Maste event_add(&ev, NULL);
1063c43e99fdSEd Maste
1064*b50261e2SCy Schubert THREAD_START(thread, del_wait_thread, NULL);
1065c43e99fdSEd Maste
1066c43e99fdSEd Maste if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
1067c43e99fdSEd Maste tt_fail_perror("write");
1068c43e99fdSEd Maste }
1069c43e99fdSEd Maste
1070c43e99fdSEd Maste {
1071c43e99fdSEd Maste struct timeval delay = { 0, 30*1000 };
1072c43e99fdSEd Maste evutil_usleep_(&delay);
1073c43e99fdSEd Maste }
1074c43e99fdSEd Maste
1075c43e99fdSEd Maste {
1076c43e99fdSEd Maste struct timeval tv_start, tv_end;
1077c43e99fdSEd Maste evutil_gettimeofday(&tv_start, NULL);
1078c43e99fdSEd Maste event_del(&ev);
1079c43e99fdSEd Maste evutil_gettimeofday(&tv_end, NULL);
1080c43e99fdSEd Maste test_timeval_diff_eq(&tv_start, &tv_end, 270);
1081c43e99fdSEd Maste }
1082c43e99fdSEd Maste
1083*b50261e2SCy Schubert THREAD_JOIN(thread);
1084*b50261e2SCy Schubert
1085*b50261e2SCy Schubert tt_int_op(test_ok, ==, 1);
1086c43e99fdSEd Maste
1087c43e99fdSEd Maste end:
1088c43e99fdSEd Maste ;
1089c43e99fdSEd Maste }
1090*b50261e2SCy Schubert
null_cb(evutil_socket_t fd,short what,void * arg)1091*b50261e2SCy Schubert static void null_cb(evutil_socket_t fd, short what, void *arg) {}
test_del_notify_thread(void * arg)1092*b50261e2SCy Schubert static void* test_del_notify_thread(void *arg)
1093*b50261e2SCy Schubert {
1094*b50261e2SCy Schubert event_dispatch();
1095*b50261e2SCy Schubert return NULL;
1096*b50261e2SCy Schubert }
1097*b50261e2SCy Schubert static void
test_del_notify(void)1098*b50261e2SCy Schubert test_del_notify(void)
1099*b50261e2SCy Schubert {
1100*b50261e2SCy Schubert struct event ev;
1101*b50261e2SCy Schubert THREAD_T thread;
1102*b50261e2SCy Schubert
1103*b50261e2SCy Schubert test_ok = 1;
1104*b50261e2SCy Schubert
1105*b50261e2SCy Schubert event_set(&ev, -1, EV_READ, null_cb, &ev);
1106*b50261e2SCy Schubert event_add(&ev, NULL);
1107*b50261e2SCy Schubert
1108*b50261e2SCy Schubert THREAD_START(thread, test_del_notify_thread, NULL);
1109*b50261e2SCy Schubert
1110*b50261e2SCy Schubert {
1111*b50261e2SCy Schubert struct timeval delay = { 0, 1000 };
1112*b50261e2SCy Schubert evutil_usleep_(&delay);
1113*b50261e2SCy Schubert }
1114*b50261e2SCy Schubert
1115*b50261e2SCy Schubert event_del(&ev);
1116*b50261e2SCy Schubert THREAD_JOIN(thread);
1117*b50261e2SCy Schubert }
1118c43e99fdSEd Maste #endif
1119c43e99fdSEd Maste
1120c43e99fdSEd Maste static void
signal_cb_sa(int sig)1121c43e99fdSEd Maste signal_cb_sa(int sig)
1122c43e99fdSEd Maste {
1123c43e99fdSEd Maste test_ok = 2;
1124c43e99fdSEd Maste }
1125c43e99fdSEd Maste
1126c43e99fdSEd Maste static void
signal_cb(evutil_socket_t fd,short event,void * arg)1127c43e99fdSEd Maste signal_cb(evutil_socket_t fd, short event, void *arg)
1128c43e99fdSEd Maste {
1129c43e99fdSEd Maste struct event *ev = arg;
1130c43e99fdSEd Maste
1131c43e99fdSEd Maste evsignal_del(ev);
1132c43e99fdSEd Maste test_ok = 1;
1133c43e99fdSEd Maste }
1134c43e99fdSEd Maste
1135c43e99fdSEd Maste static void
test_simplesignal_impl(int find_reorder)1136c43e99fdSEd Maste test_simplesignal_impl(int find_reorder)
1137c43e99fdSEd Maste {
1138c43e99fdSEd Maste struct event ev;
1139c43e99fdSEd Maste struct itimerval itv;
1140c43e99fdSEd Maste
1141c43e99fdSEd Maste evsignal_set(&ev, SIGALRM, signal_cb, &ev);
1142c43e99fdSEd Maste evsignal_add(&ev, NULL);
1143c43e99fdSEd Maste /* find bugs in which operations are re-ordered */
1144c43e99fdSEd Maste if (find_reorder) {
1145c43e99fdSEd Maste evsignal_del(&ev);
1146c43e99fdSEd Maste evsignal_add(&ev, NULL);
1147c43e99fdSEd Maste }
1148c43e99fdSEd Maste
1149c43e99fdSEd Maste memset(&itv, 0, sizeof(itv));
1150c43e99fdSEd Maste itv.it_value.tv_sec = 0;
1151c43e99fdSEd Maste itv.it_value.tv_usec = 100000;
1152c43e99fdSEd Maste if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
1153c43e99fdSEd Maste goto skip_simplesignal;
1154c43e99fdSEd Maste
1155c43e99fdSEd Maste event_dispatch();
1156c43e99fdSEd Maste skip_simplesignal:
1157c43e99fdSEd Maste if (evsignal_del(&ev) == -1)
1158c43e99fdSEd Maste test_ok = 0;
1159c43e99fdSEd Maste
1160c43e99fdSEd Maste cleanup_test();
1161c43e99fdSEd Maste }
1162c43e99fdSEd Maste
1163c43e99fdSEd Maste static void
test_simplestsignal(void)1164c43e99fdSEd Maste test_simplestsignal(void)
1165c43e99fdSEd Maste {
1166c43e99fdSEd Maste setup_test("Simplest one signal: ");
1167c43e99fdSEd Maste test_simplesignal_impl(0);
1168c43e99fdSEd Maste }
1169c43e99fdSEd Maste
1170c43e99fdSEd Maste static void
test_simplesignal(void)1171c43e99fdSEd Maste test_simplesignal(void)
1172c43e99fdSEd Maste {
1173c43e99fdSEd Maste setup_test("Simple signal: ");
1174c43e99fdSEd Maste test_simplesignal_impl(1);
1175c43e99fdSEd Maste }
1176c43e99fdSEd Maste
1177c43e99fdSEd Maste static void
test_multiplesignal(void)1178c43e99fdSEd Maste test_multiplesignal(void)
1179c43e99fdSEd Maste {
1180c43e99fdSEd Maste struct event ev_one, ev_two;
1181c43e99fdSEd Maste struct itimerval itv;
1182c43e99fdSEd Maste
1183c43e99fdSEd Maste setup_test("Multiple signal: ");
1184c43e99fdSEd Maste
1185c43e99fdSEd Maste evsignal_set(&ev_one, SIGALRM, signal_cb, &ev_one);
1186c43e99fdSEd Maste evsignal_add(&ev_one, NULL);
1187c43e99fdSEd Maste
1188c43e99fdSEd Maste evsignal_set(&ev_two, SIGALRM, signal_cb, &ev_two);
1189c43e99fdSEd Maste evsignal_add(&ev_two, NULL);
1190c43e99fdSEd Maste
1191c43e99fdSEd Maste memset(&itv, 0, sizeof(itv));
1192c43e99fdSEd Maste itv.it_value.tv_sec = 0;
1193c43e99fdSEd Maste itv.it_value.tv_usec = 100000;
1194c43e99fdSEd Maste if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
1195c43e99fdSEd Maste goto skip_simplesignal;
1196c43e99fdSEd Maste
1197c43e99fdSEd Maste event_dispatch();
1198c43e99fdSEd Maste
1199c43e99fdSEd Maste skip_simplesignal:
1200c43e99fdSEd Maste if (evsignal_del(&ev_one) == -1)
1201c43e99fdSEd Maste test_ok = 0;
1202c43e99fdSEd Maste if (evsignal_del(&ev_two) == -1)
1203c43e99fdSEd Maste test_ok = 0;
1204c43e99fdSEd Maste
1205c43e99fdSEd Maste cleanup_test();
1206c43e99fdSEd Maste }
1207c43e99fdSEd Maste
1208c43e99fdSEd Maste static void
test_immediatesignal(void)1209c43e99fdSEd Maste test_immediatesignal(void)
1210c43e99fdSEd Maste {
1211c43e99fdSEd Maste struct event ev;
1212c43e99fdSEd Maste
1213c43e99fdSEd Maste test_ok = 0;
1214c43e99fdSEd Maste evsignal_set(&ev, SIGUSR1, signal_cb, &ev);
1215c43e99fdSEd Maste evsignal_add(&ev, NULL);
1216*b50261e2SCy Schubert kill(getpid(), SIGUSR1);
1217c43e99fdSEd Maste event_loop(EVLOOP_NONBLOCK);
1218c43e99fdSEd Maste evsignal_del(&ev);
1219c43e99fdSEd Maste cleanup_test();
1220c43e99fdSEd Maste }
1221c43e99fdSEd Maste
1222c43e99fdSEd Maste static void
test_signal_dealloc(void)1223c43e99fdSEd Maste test_signal_dealloc(void)
1224c43e99fdSEd Maste {
1225c43e99fdSEd Maste /* make sure that evsignal_event is event_del'ed and pipe closed */
1226c43e99fdSEd Maste struct event ev;
1227c43e99fdSEd Maste struct event_base *base = event_init();
1228c43e99fdSEd Maste evsignal_set(&ev, SIGUSR1, signal_cb, &ev);
1229c43e99fdSEd Maste evsignal_add(&ev, NULL);
1230c43e99fdSEd Maste evsignal_del(&ev);
1231c43e99fdSEd Maste event_base_free(base);
1232c43e99fdSEd Maste /* If we got here without asserting, we're fine. */
1233c43e99fdSEd Maste test_ok = 1;
1234c43e99fdSEd Maste cleanup_test();
1235c43e99fdSEd Maste }
1236c43e99fdSEd Maste
1237c43e99fdSEd Maste static void
test_signal_pipeloss(void)1238c43e99fdSEd Maste test_signal_pipeloss(void)
1239c43e99fdSEd Maste {
1240c43e99fdSEd Maste /* make sure that the base1 pipe is closed correctly. */
1241c43e99fdSEd Maste struct event_base *base1, *base2;
1242c43e99fdSEd Maste int pipe1;
1243c43e99fdSEd Maste test_ok = 0;
1244c43e99fdSEd Maste base1 = event_init();
1245c43e99fdSEd Maste pipe1 = base1->sig.ev_signal_pair[0];
1246c43e99fdSEd Maste base2 = event_init();
1247c43e99fdSEd Maste event_base_free(base2);
1248c43e99fdSEd Maste event_base_free(base1);
1249c43e99fdSEd Maste if (close(pipe1) != -1 || errno!=EBADF) {
1250c43e99fdSEd Maste /* fd must be closed, so second close gives -1, EBADF */
1251c43e99fdSEd Maste printf("signal pipe not closed. ");
1252c43e99fdSEd Maste test_ok = 0;
1253c43e99fdSEd Maste } else {
1254c43e99fdSEd Maste test_ok = 1;
1255c43e99fdSEd Maste }
1256c43e99fdSEd Maste cleanup_test();
1257c43e99fdSEd Maste }
1258c43e99fdSEd Maste
1259c43e99fdSEd Maste /*
1260c43e99fdSEd Maste * make two bases to catch signals, use both of them. this only works
1261c43e99fdSEd Maste * for event mechanisms that use our signal pipe trick. kqueue handles
1262c43e99fdSEd Maste * signals internally, and all interested kqueues get all the signals.
1263c43e99fdSEd Maste */
1264c43e99fdSEd Maste static void
test_signal_switchbase(void)1265c43e99fdSEd Maste test_signal_switchbase(void)
1266c43e99fdSEd Maste {
1267c43e99fdSEd Maste struct event ev1, ev2;
1268c43e99fdSEd Maste struct event_base *base1, *base2;
1269c43e99fdSEd Maste int is_kqueue;
1270c43e99fdSEd Maste test_ok = 0;
1271c43e99fdSEd Maste base1 = event_init();
1272c43e99fdSEd Maste base2 = event_init();
1273c43e99fdSEd Maste is_kqueue = !strcmp(event_get_method(),"kqueue");
1274c43e99fdSEd Maste evsignal_set(&ev1, SIGUSR1, signal_cb, &ev1);
1275c43e99fdSEd Maste evsignal_set(&ev2, SIGUSR1, signal_cb, &ev2);
1276c43e99fdSEd Maste if (event_base_set(base1, &ev1) ||
1277c43e99fdSEd Maste event_base_set(base2, &ev2) ||
1278c43e99fdSEd Maste event_add(&ev1, NULL) ||
1279c43e99fdSEd Maste event_add(&ev2, NULL)) {
1280c43e99fdSEd Maste fprintf(stderr, "%s: cannot set base, add\n", __func__);
1281c43e99fdSEd Maste exit(1);
1282c43e99fdSEd Maste }
1283c43e99fdSEd Maste
1284c43e99fdSEd Maste tt_ptr_op(event_get_base(&ev1), ==, base1);
1285c43e99fdSEd Maste tt_ptr_op(event_get_base(&ev2), ==, base2);
1286c43e99fdSEd Maste
1287c43e99fdSEd Maste test_ok = 0;
1288c43e99fdSEd Maste /* can handle signal before loop is called */
1289*b50261e2SCy Schubert kill(getpid(), SIGUSR1);
1290c43e99fdSEd Maste event_base_loop(base2, EVLOOP_NONBLOCK);
1291c43e99fdSEd Maste if (is_kqueue) {
1292c43e99fdSEd Maste if (!test_ok)
1293c43e99fdSEd Maste goto end;
1294c43e99fdSEd Maste test_ok = 0;
1295c43e99fdSEd Maste }
1296c43e99fdSEd Maste event_base_loop(base1, EVLOOP_NONBLOCK);
1297c43e99fdSEd Maste if (test_ok && !is_kqueue) {
1298c43e99fdSEd Maste test_ok = 0;
1299c43e99fdSEd Maste
1300c43e99fdSEd Maste /* set base1 to handle signals */
1301c43e99fdSEd Maste event_base_loop(base1, EVLOOP_NONBLOCK);
1302*b50261e2SCy Schubert kill(getpid(), SIGUSR1);
1303c43e99fdSEd Maste event_base_loop(base1, EVLOOP_NONBLOCK);
1304c43e99fdSEd Maste event_base_loop(base2, EVLOOP_NONBLOCK);
1305c43e99fdSEd Maste }
1306c43e99fdSEd Maste end:
1307c43e99fdSEd Maste event_base_free(base1);
1308c43e99fdSEd Maste event_base_free(base2);
1309c43e99fdSEd Maste cleanup_test();
1310c43e99fdSEd Maste }
1311c43e99fdSEd Maste
1312c43e99fdSEd Maste /*
1313c43e99fdSEd Maste * assert that a signal event removed from the event queue really is
1314c43e99fdSEd Maste * removed - with no possibility of it's parent handler being fired.
1315c43e99fdSEd Maste */
1316c43e99fdSEd Maste static void
test_signal_assert(void)1317c43e99fdSEd Maste test_signal_assert(void)
1318c43e99fdSEd Maste {
1319c43e99fdSEd Maste struct event ev;
1320c43e99fdSEd Maste struct event_base *base = event_init();
1321c43e99fdSEd Maste test_ok = 0;
1322c43e99fdSEd Maste /* use SIGCONT so we don't kill ourselves when we signal to nowhere */
1323c43e99fdSEd Maste evsignal_set(&ev, SIGCONT, signal_cb, &ev);
1324c43e99fdSEd Maste evsignal_add(&ev, NULL);
1325c43e99fdSEd Maste /*
1326c43e99fdSEd Maste * if evsignal_del() fails to reset the handler, it's current handler
1327c43e99fdSEd Maste * will still point to evsig_handler().
1328c43e99fdSEd Maste */
1329c43e99fdSEd Maste evsignal_del(&ev);
1330c43e99fdSEd Maste
1331*b50261e2SCy Schubert kill(getpid(), SIGCONT);
1332c43e99fdSEd Maste #if 0
1333c43e99fdSEd Maste /* only way to verify we were in evsig_handler() */
1334c43e99fdSEd Maste /* XXXX Now there's no longer a good way. */
1335c43e99fdSEd Maste if (base->sig.evsig_caught)
1336c43e99fdSEd Maste test_ok = 0;
1337c43e99fdSEd Maste else
1338c43e99fdSEd Maste test_ok = 1;
1339c43e99fdSEd Maste #else
1340c43e99fdSEd Maste test_ok = 1;
1341c43e99fdSEd Maste #endif
1342c43e99fdSEd Maste
1343c43e99fdSEd Maste event_base_free(base);
1344c43e99fdSEd Maste cleanup_test();
1345c43e99fdSEd Maste return;
1346c43e99fdSEd Maste }
1347c43e99fdSEd Maste
1348c43e99fdSEd Maste /*
1349c43e99fdSEd Maste * assert that we restore our previous signal handler properly.
1350c43e99fdSEd Maste */
1351c43e99fdSEd Maste static void
test_signal_restore(void)1352c43e99fdSEd Maste test_signal_restore(void)
1353c43e99fdSEd Maste {
1354c43e99fdSEd Maste struct event ev;
1355c43e99fdSEd Maste struct event_base *base = event_init();
1356c43e99fdSEd Maste #ifdef EVENT__HAVE_SIGACTION
1357c43e99fdSEd Maste struct sigaction sa;
1358c43e99fdSEd Maste #endif
1359c43e99fdSEd Maste
1360c43e99fdSEd Maste test_ok = 0;
1361c43e99fdSEd Maste #ifdef EVENT__HAVE_SIGACTION
1362c43e99fdSEd Maste sa.sa_handler = signal_cb_sa;
1363c43e99fdSEd Maste sa.sa_flags = 0x0;
1364c43e99fdSEd Maste sigemptyset(&sa.sa_mask);
1365c43e99fdSEd Maste if (sigaction(SIGUSR1, &sa, NULL) == -1)
1366c43e99fdSEd Maste goto out;
1367c43e99fdSEd Maste #else
1368c43e99fdSEd Maste if (signal(SIGUSR1, signal_cb_sa) == SIG_ERR)
1369c43e99fdSEd Maste goto out;
1370c43e99fdSEd Maste #endif
1371c43e99fdSEd Maste evsignal_set(&ev, SIGUSR1, signal_cb, &ev);
1372c43e99fdSEd Maste evsignal_add(&ev, NULL);
1373c43e99fdSEd Maste evsignal_del(&ev);
1374c43e99fdSEd Maste
1375*b50261e2SCy Schubert kill(getpid(), SIGUSR1);
1376c43e99fdSEd Maste /* 1 == signal_cb, 2 == signal_cb_sa, we want our previous handler */
1377c43e99fdSEd Maste if (test_ok != 2)
1378c43e99fdSEd Maste test_ok = 0;
1379c43e99fdSEd Maste out:
1380c43e99fdSEd Maste event_base_free(base);
1381c43e99fdSEd Maste cleanup_test();
1382c43e99fdSEd Maste return;
1383c43e99fdSEd Maste }
1384c43e99fdSEd Maste
1385c43e99fdSEd Maste static void
signal_cb_swp(int sig,short event,void * arg)1386c43e99fdSEd Maste signal_cb_swp(int sig, short event, void *arg)
1387c43e99fdSEd Maste {
1388c43e99fdSEd Maste called++;
1389c43e99fdSEd Maste if (called < 5)
1390*b50261e2SCy Schubert kill(getpid(), sig);
1391c43e99fdSEd Maste else
1392c43e99fdSEd Maste event_loopexit(NULL);
1393c43e99fdSEd Maste }
1394c43e99fdSEd Maste static void
timeout_cb_swp(evutil_socket_t fd,short event,void * arg)1395c43e99fdSEd Maste timeout_cb_swp(evutil_socket_t fd, short event, void *arg)
1396c43e99fdSEd Maste {
1397c43e99fdSEd Maste if (called == -1) {
1398c43e99fdSEd Maste struct timeval tv = {5, 0};
1399c43e99fdSEd Maste
1400c43e99fdSEd Maste called = 0;
1401c43e99fdSEd Maste evtimer_add((struct event *)arg, &tv);
1402*b50261e2SCy Schubert kill(getpid(), SIGUSR1);
1403c43e99fdSEd Maste return;
1404c43e99fdSEd Maste }
1405c43e99fdSEd Maste test_ok = 0;
1406c43e99fdSEd Maste event_loopexit(NULL);
1407c43e99fdSEd Maste }
1408c43e99fdSEd Maste
1409c43e99fdSEd Maste static void
test_signal_while_processing(void)1410c43e99fdSEd Maste test_signal_while_processing(void)
1411c43e99fdSEd Maste {
1412c43e99fdSEd Maste struct event_base *base = event_init();
1413c43e99fdSEd Maste struct event ev, ev_timer;
1414c43e99fdSEd Maste struct timeval tv = {0, 0};
1415c43e99fdSEd Maste
1416c43e99fdSEd Maste setup_test("Receiving a signal while processing other signal: ");
1417c43e99fdSEd Maste
1418c43e99fdSEd Maste called = -1;
1419c43e99fdSEd Maste test_ok = 1;
1420c43e99fdSEd Maste signal_set(&ev, SIGUSR1, signal_cb_swp, NULL);
1421c43e99fdSEd Maste signal_add(&ev, NULL);
1422c43e99fdSEd Maste evtimer_set(&ev_timer, timeout_cb_swp, &ev_timer);
1423c43e99fdSEd Maste evtimer_add(&ev_timer, &tv);
1424c43e99fdSEd Maste event_dispatch();
1425c43e99fdSEd Maste
1426c43e99fdSEd Maste event_base_free(base);
1427c43e99fdSEd Maste cleanup_test();
1428c43e99fdSEd Maste return;
1429c43e99fdSEd Maste }
1430c43e99fdSEd Maste #endif
1431c43e99fdSEd Maste
1432c43e99fdSEd Maste static void
test_free_active_base(void * ptr)1433c43e99fdSEd Maste test_free_active_base(void *ptr)
1434c43e99fdSEd Maste {
1435c43e99fdSEd Maste struct basic_test_data *data = ptr;
1436c43e99fdSEd Maste struct event_base *base1;
1437c43e99fdSEd Maste struct event ev1;
1438c43e99fdSEd Maste
1439c43e99fdSEd Maste base1 = event_init();
1440*b50261e2SCy Schubert tt_assert(base1);
1441*b50261e2SCy Schubert event_assign(&ev1, base1, data->pair[1], EV_READ, dummy_read_cb, NULL);
1442c43e99fdSEd Maste event_add(&ev1, NULL);
1443c43e99fdSEd Maste event_base_free(base1); /* should not crash */
1444c43e99fdSEd Maste
1445c43e99fdSEd Maste base1 = event_init();
1446c43e99fdSEd Maste tt_assert(base1);
1447*b50261e2SCy Schubert event_assign(&ev1, base1, data->pair[0], 0, dummy_read_cb, NULL);
1448c43e99fdSEd Maste event_active(&ev1, EV_READ, 1);
1449c43e99fdSEd Maste event_base_free(base1);
1450c43e99fdSEd Maste end:
1451c43e99fdSEd Maste ;
1452c43e99fdSEd Maste }
1453c43e99fdSEd Maste
1454c43e99fdSEd Maste static void
test_manipulate_active_events(void * ptr)1455c43e99fdSEd Maste test_manipulate_active_events(void *ptr)
1456c43e99fdSEd Maste {
1457c43e99fdSEd Maste struct basic_test_data *data = ptr;
1458c43e99fdSEd Maste struct event_base *base = data->base;
1459c43e99fdSEd Maste struct event ev1;
1460c43e99fdSEd Maste
1461c43e99fdSEd Maste event_assign(&ev1, base, -1, EV_TIMEOUT, dummy_read_cb, NULL);
1462c43e99fdSEd Maste
1463c43e99fdSEd Maste /* Make sure an active event is pending. */
1464c43e99fdSEd Maste event_active(&ev1, EV_READ, 1);
1465c43e99fdSEd Maste tt_int_op(event_pending(&ev1, EV_READ|EV_TIMEOUT|EV_WRITE, NULL),
1466c43e99fdSEd Maste ==, EV_READ);
1467c43e99fdSEd Maste
1468c43e99fdSEd Maste /* Make sure that activating an event twice works. */
1469c43e99fdSEd Maste event_active(&ev1, EV_WRITE, 1);
1470c43e99fdSEd Maste tt_int_op(event_pending(&ev1, EV_READ|EV_TIMEOUT|EV_WRITE, NULL),
1471c43e99fdSEd Maste ==, EV_READ|EV_WRITE);
1472c43e99fdSEd Maste
1473c43e99fdSEd Maste end:
1474c43e99fdSEd Maste event_del(&ev1);
1475c43e99fdSEd Maste }
1476c43e99fdSEd Maste
1477c43e99fdSEd Maste static void
event_selfarg_cb(evutil_socket_t fd,short event,void * arg)1478c43e99fdSEd Maste event_selfarg_cb(evutil_socket_t fd, short event, void *arg)
1479c43e99fdSEd Maste {
1480c43e99fdSEd Maste struct event *ev = arg;
1481c43e99fdSEd Maste struct event_base *base = event_get_base(ev);
1482c43e99fdSEd Maste event_base_assert_ok_(base);
1483c43e99fdSEd Maste event_base_loopexit(base, NULL);
1484c43e99fdSEd Maste tt_want(ev == event_base_get_running_event(base));
1485c43e99fdSEd Maste }
1486c43e99fdSEd Maste
1487c43e99fdSEd Maste static void
test_event_new_selfarg(void * ptr)1488c43e99fdSEd Maste test_event_new_selfarg(void *ptr)
1489c43e99fdSEd Maste {
1490c43e99fdSEd Maste struct basic_test_data *data = ptr;
1491c43e99fdSEd Maste struct event_base *base = data->base;
1492c43e99fdSEd Maste struct event *ev = event_new(base, -1, EV_READ, event_selfarg_cb,
1493c43e99fdSEd Maste event_self_cbarg());
1494c43e99fdSEd Maste
1495c43e99fdSEd Maste event_active(ev, EV_READ, 1);
1496c43e99fdSEd Maste event_base_dispatch(base);
1497c43e99fdSEd Maste
1498c43e99fdSEd Maste event_free(ev);
1499c43e99fdSEd Maste }
1500c43e99fdSEd Maste
1501c43e99fdSEd Maste static void
test_event_assign_selfarg(void * ptr)1502c43e99fdSEd Maste test_event_assign_selfarg(void *ptr)
1503c43e99fdSEd Maste {
1504c43e99fdSEd Maste struct basic_test_data *data = ptr;
1505c43e99fdSEd Maste struct event_base *base = data->base;
1506c43e99fdSEd Maste struct event ev;
1507c43e99fdSEd Maste
1508c43e99fdSEd Maste event_assign(&ev, base, -1, EV_READ, event_selfarg_cb,
1509c43e99fdSEd Maste event_self_cbarg());
1510c43e99fdSEd Maste event_active(&ev, EV_READ, 1);
1511c43e99fdSEd Maste event_base_dispatch(base);
1512c43e99fdSEd Maste }
1513c43e99fdSEd Maste
1514c43e99fdSEd Maste static void
test_event_base_get_num_events(void * ptr)1515c43e99fdSEd Maste test_event_base_get_num_events(void *ptr)
1516c43e99fdSEd Maste {
1517c43e99fdSEd Maste struct basic_test_data *data = ptr;
1518c43e99fdSEd Maste struct event_base *base = data->base;
1519c43e99fdSEd Maste struct event ev;
1520c43e99fdSEd Maste int event_count_active;
1521c43e99fdSEd Maste int event_count_virtual;
1522c43e99fdSEd Maste int event_count_added;
1523c43e99fdSEd Maste int event_count_active_virtual;
1524c43e99fdSEd Maste int event_count_active_added;
1525c43e99fdSEd Maste int event_count_virtual_added;
1526c43e99fdSEd Maste int event_count_active_added_virtual;
1527c43e99fdSEd Maste
1528c43e99fdSEd Maste struct timeval qsec = {0, 100000};
1529c43e99fdSEd Maste
1530c43e99fdSEd Maste event_assign(&ev, base, -1, EV_READ, event_selfarg_cb,
1531c43e99fdSEd Maste event_self_cbarg());
1532c43e99fdSEd Maste
1533c43e99fdSEd Maste event_add(&ev, &qsec);
1534c43e99fdSEd Maste event_count_active = event_base_get_num_events(base,
1535c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE);
1536c43e99fdSEd Maste event_count_virtual = event_base_get_num_events(base,
1537c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL);
1538c43e99fdSEd Maste event_count_added = event_base_get_num_events(base,
1539c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED);
1540c43e99fdSEd Maste event_count_active_virtual = event_base_get_num_events(base,
1541c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_VIRTUAL);
1542c43e99fdSEd Maste event_count_active_added = event_base_get_num_events(base,
1543c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_ADDED);
1544c43e99fdSEd Maste event_count_virtual_added = event_base_get_num_events(base,
1545c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL|EVENT_BASE_COUNT_ADDED);
1546c43e99fdSEd Maste event_count_active_added_virtual = event_base_get_num_events(base,
1547c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE|
1548c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED|
1549c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL);
1550c43e99fdSEd Maste tt_int_op(event_count_active, ==, 0);
1551c43e99fdSEd Maste tt_int_op(event_count_virtual, ==, 0);
1552c43e99fdSEd Maste /* libevent itself adds a timeout event, so the event_count is 2 here */
1553c43e99fdSEd Maste tt_int_op(event_count_added, ==, 2);
1554c43e99fdSEd Maste tt_int_op(event_count_active_virtual, ==, 0);
1555c43e99fdSEd Maste tt_int_op(event_count_active_added, ==, 2);
1556c43e99fdSEd Maste tt_int_op(event_count_virtual_added, ==, 2);
1557c43e99fdSEd Maste tt_int_op(event_count_active_added_virtual, ==, 2);
1558c43e99fdSEd Maste
1559c43e99fdSEd Maste event_active(&ev, EV_READ, 1);
1560c43e99fdSEd Maste event_count_active = event_base_get_num_events(base,
1561c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE);
1562c43e99fdSEd Maste event_count_virtual = event_base_get_num_events(base,
1563c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL);
1564c43e99fdSEd Maste event_count_added = event_base_get_num_events(base,
1565c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED);
1566c43e99fdSEd Maste event_count_active_virtual = event_base_get_num_events(base,
1567c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_VIRTUAL);
1568c43e99fdSEd Maste event_count_active_added = event_base_get_num_events(base,
1569c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_ADDED);
1570c43e99fdSEd Maste event_count_virtual_added = event_base_get_num_events(base,
1571c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL|EVENT_BASE_COUNT_ADDED);
1572c43e99fdSEd Maste event_count_active_added_virtual = event_base_get_num_events(base,
1573c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE|
1574c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED|
1575c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL);
1576c43e99fdSEd Maste tt_int_op(event_count_active, ==, 1);
1577c43e99fdSEd Maste tt_int_op(event_count_virtual, ==, 0);
1578c43e99fdSEd Maste tt_int_op(event_count_added, ==, 3);
1579c43e99fdSEd Maste tt_int_op(event_count_active_virtual, ==, 1);
1580c43e99fdSEd Maste tt_int_op(event_count_active_added, ==, 4);
1581c43e99fdSEd Maste tt_int_op(event_count_virtual_added, ==, 3);
1582c43e99fdSEd Maste tt_int_op(event_count_active_added_virtual, ==, 4);
1583c43e99fdSEd Maste
1584c43e99fdSEd Maste event_base_loop(base, 0);
1585c43e99fdSEd Maste event_count_active = event_base_get_num_events(base,
1586c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE);
1587c43e99fdSEd Maste event_count_virtual = event_base_get_num_events(base,
1588c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL);
1589c43e99fdSEd Maste event_count_added = event_base_get_num_events(base,
1590c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED);
1591c43e99fdSEd Maste event_count_active_virtual = event_base_get_num_events(base,
1592c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_VIRTUAL);
1593c43e99fdSEd Maste event_count_active_added = event_base_get_num_events(base,
1594c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_ADDED);
1595c43e99fdSEd Maste event_count_virtual_added = event_base_get_num_events(base,
1596c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL|EVENT_BASE_COUNT_ADDED);
1597c43e99fdSEd Maste event_count_active_added_virtual = event_base_get_num_events(base,
1598c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE|
1599c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED|
1600c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL);
1601c43e99fdSEd Maste tt_int_op(event_count_active, ==, 0);
1602c43e99fdSEd Maste tt_int_op(event_count_virtual, ==, 0);
1603c43e99fdSEd Maste tt_int_op(event_count_added, ==, 0);
1604c43e99fdSEd Maste tt_int_op(event_count_active_virtual, ==, 0);
1605c43e99fdSEd Maste tt_int_op(event_count_active_added, ==, 0);
1606c43e99fdSEd Maste tt_int_op(event_count_virtual_added, ==, 0);
1607c43e99fdSEd Maste tt_int_op(event_count_active_added_virtual, ==, 0);
1608c43e99fdSEd Maste
1609c43e99fdSEd Maste event_base_add_virtual_(base);
1610c43e99fdSEd Maste event_count_active = event_base_get_num_events(base,
1611c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE);
1612c43e99fdSEd Maste event_count_virtual = event_base_get_num_events(base,
1613c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL);
1614c43e99fdSEd Maste event_count_added = event_base_get_num_events(base,
1615c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED);
1616c43e99fdSEd Maste event_count_active_virtual = event_base_get_num_events(base,
1617c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_VIRTUAL);
1618c43e99fdSEd Maste event_count_active_added = event_base_get_num_events(base,
1619c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_ADDED);
1620c43e99fdSEd Maste event_count_virtual_added = event_base_get_num_events(base,
1621c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL|EVENT_BASE_COUNT_ADDED);
1622c43e99fdSEd Maste event_count_active_added_virtual = event_base_get_num_events(base,
1623c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE|
1624c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED|
1625c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL);
1626c43e99fdSEd Maste tt_int_op(event_count_active, ==, 0);
1627c43e99fdSEd Maste tt_int_op(event_count_virtual, ==, 1);
1628c43e99fdSEd Maste tt_int_op(event_count_added, ==, 0);
1629c43e99fdSEd Maste tt_int_op(event_count_active_virtual, ==, 1);
1630c43e99fdSEd Maste tt_int_op(event_count_active_added, ==, 0);
1631c43e99fdSEd Maste tt_int_op(event_count_virtual_added, ==, 1);
1632c43e99fdSEd Maste tt_int_op(event_count_active_added_virtual, ==, 1);
1633c43e99fdSEd Maste
1634c43e99fdSEd Maste end:
1635c43e99fdSEd Maste ;
1636c43e99fdSEd Maste }
1637c43e99fdSEd Maste
1638c43e99fdSEd Maste static void
test_event_base_get_max_events(void * ptr)1639c43e99fdSEd Maste test_event_base_get_max_events(void *ptr)
1640c43e99fdSEd Maste {
1641c43e99fdSEd Maste struct basic_test_data *data = ptr;
1642c43e99fdSEd Maste struct event_base *base = data->base;
1643c43e99fdSEd Maste struct event ev;
1644c43e99fdSEd Maste struct event ev2;
1645c43e99fdSEd Maste int event_count_active;
1646c43e99fdSEd Maste int event_count_virtual;
1647c43e99fdSEd Maste int event_count_added;
1648c43e99fdSEd Maste int event_count_active_virtual;
1649c43e99fdSEd Maste int event_count_active_added;
1650c43e99fdSEd Maste int event_count_virtual_added;
1651c43e99fdSEd Maste int event_count_active_added_virtual;
1652c43e99fdSEd Maste
1653c43e99fdSEd Maste struct timeval qsec = {0, 100000};
1654c43e99fdSEd Maste
1655c43e99fdSEd Maste event_assign(&ev, base, -1, EV_READ, event_selfarg_cb,
1656c43e99fdSEd Maste event_self_cbarg());
1657c43e99fdSEd Maste event_assign(&ev2, base, -1, EV_READ, event_selfarg_cb,
1658c43e99fdSEd Maste event_self_cbarg());
1659c43e99fdSEd Maste
1660c43e99fdSEd Maste event_add(&ev, &qsec);
1661c43e99fdSEd Maste event_add(&ev2, &qsec);
1662c43e99fdSEd Maste event_del(&ev2);
1663c43e99fdSEd Maste
1664c43e99fdSEd Maste event_count_active = event_base_get_max_events(base,
1665c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE, 0);
1666c43e99fdSEd Maste event_count_virtual = event_base_get_max_events(base,
1667c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL, 0);
1668c43e99fdSEd Maste event_count_added = event_base_get_max_events(base,
1669c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED, 0);
1670c43e99fdSEd Maste event_count_active_virtual = event_base_get_max_events(base,
1671c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_VIRTUAL, 0);
1672c43e99fdSEd Maste event_count_active_added = event_base_get_max_events(base,
1673c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_ADDED, 0);
1674c43e99fdSEd Maste event_count_virtual_added = event_base_get_max_events(base,
1675c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL | EVENT_BASE_COUNT_ADDED, 0);
1676c43e99fdSEd Maste event_count_active_added_virtual = event_base_get_max_events(base,
1677c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE |
1678c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED |
1679c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL, 0);
1680c43e99fdSEd Maste
1681c43e99fdSEd Maste tt_int_op(event_count_active, ==, 0);
1682c43e99fdSEd Maste tt_int_op(event_count_virtual, ==, 0);
1683c43e99fdSEd Maste /* libevent itself adds a timeout event, so the event_count is 4 here */
1684c43e99fdSEd Maste tt_int_op(event_count_added, ==, 4);
1685c43e99fdSEd Maste tt_int_op(event_count_active_virtual, ==, 0);
1686c43e99fdSEd Maste tt_int_op(event_count_active_added, ==, 4);
1687c43e99fdSEd Maste tt_int_op(event_count_virtual_added, ==, 4);
1688c43e99fdSEd Maste tt_int_op(event_count_active_added_virtual, ==, 4);
1689c43e99fdSEd Maste
1690c43e99fdSEd Maste event_active(&ev, EV_READ, 1);
1691c43e99fdSEd Maste event_count_active = event_base_get_max_events(base,
1692c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE, 0);
1693c43e99fdSEd Maste event_count_virtual = event_base_get_max_events(base,
1694c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL, 0);
1695c43e99fdSEd Maste event_count_added = event_base_get_max_events(base,
1696c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED, 0);
1697c43e99fdSEd Maste event_count_active_virtual = event_base_get_max_events(base,
1698c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_VIRTUAL, 0);
1699c43e99fdSEd Maste event_count_active_added = event_base_get_max_events(base,
1700c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_ADDED, 0);
1701c43e99fdSEd Maste event_count_virtual_added = event_base_get_max_events(base,
1702c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL | EVENT_BASE_COUNT_ADDED, 0);
1703c43e99fdSEd Maste event_count_active_added_virtual = event_base_get_max_events(base,
1704c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE |
1705c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED |
1706c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL, 0);
1707c43e99fdSEd Maste
1708c43e99fdSEd Maste tt_int_op(event_count_active, ==, 1);
1709c43e99fdSEd Maste tt_int_op(event_count_virtual, ==, 0);
1710c43e99fdSEd Maste tt_int_op(event_count_added, ==, 4);
1711c43e99fdSEd Maste tt_int_op(event_count_active_virtual, ==, 1);
1712c43e99fdSEd Maste tt_int_op(event_count_active_added, ==, 5);
1713c43e99fdSEd Maste tt_int_op(event_count_virtual_added, ==, 4);
1714c43e99fdSEd Maste tt_int_op(event_count_active_added_virtual, ==, 5);
1715c43e99fdSEd Maste
1716c43e99fdSEd Maste event_base_loop(base, 0);
1717c43e99fdSEd Maste event_count_active = event_base_get_max_events(base,
1718c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE, 1);
1719c43e99fdSEd Maste event_count_virtual = event_base_get_max_events(base,
1720c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL, 1);
1721c43e99fdSEd Maste event_count_added = event_base_get_max_events(base,
1722c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED, 1);
1723c43e99fdSEd Maste event_count_active_virtual = event_base_get_max_events(base,
1724c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_VIRTUAL, 0);
1725c43e99fdSEd Maste event_count_active_added = event_base_get_max_events(base,
1726c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_ADDED, 0);
1727c43e99fdSEd Maste event_count_virtual_added = event_base_get_max_events(base,
1728c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL | EVENT_BASE_COUNT_ADDED, 0);
1729c43e99fdSEd Maste event_count_active_added_virtual = event_base_get_max_events(base,
1730c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE |
1731c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED |
1732c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL, 1);
1733c43e99fdSEd Maste
1734c43e99fdSEd Maste tt_int_op(event_count_active, ==, 1);
1735c43e99fdSEd Maste tt_int_op(event_count_virtual, ==, 0);
1736c43e99fdSEd Maste tt_int_op(event_count_added, ==, 4);
1737c43e99fdSEd Maste tt_int_op(event_count_active_virtual, ==, 0);
1738c43e99fdSEd Maste tt_int_op(event_count_active_added, ==, 0);
1739c43e99fdSEd Maste tt_int_op(event_count_virtual_added, ==, 0);
1740c43e99fdSEd Maste tt_int_op(event_count_active_added_virtual, ==, 0);
1741c43e99fdSEd Maste
1742c43e99fdSEd Maste event_count_active = event_base_get_max_events(base,
1743c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE, 0);
1744c43e99fdSEd Maste event_count_virtual = event_base_get_max_events(base,
1745c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL, 0);
1746c43e99fdSEd Maste event_count_added = event_base_get_max_events(base,
1747c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED, 0);
1748c43e99fdSEd Maste tt_int_op(event_count_active, ==, 0);
1749c43e99fdSEd Maste tt_int_op(event_count_virtual, ==, 0);
1750c43e99fdSEd Maste tt_int_op(event_count_added, ==, 0);
1751c43e99fdSEd Maste
1752c43e99fdSEd Maste event_base_add_virtual_(base);
1753c43e99fdSEd Maste event_count_active = event_base_get_max_events(base,
1754c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE, 0);
1755c43e99fdSEd Maste event_count_virtual = event_base_get_max_events(base,
1756c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL, 0);
1757c43e99fdSEd Maste event_count_added = event_base_get_max_events(base,
1758c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED, 0);
1759c43e99fdSEd Maste event_count_active_virtual = event_base_get_max_events(base,
1760c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_VIRTUAL, 0);
1761c43e99fdSEd Maste event_count_active_added = event_base_get_max_events(base,
1762c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_ADDED, 0);
1763c43e99fdSEd Maste event_count_virtual_added = event_base_get_max_events(base,
1764c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL | EVENT_BASE_COUNT_ADDED, 0);
1765c43e99fdSEd Maste event_count_active_added_virtual = event_base_get_max_events(base,
1766c43e99fdSEd Maste EVENT_BASE_COUNT_ACTIVE |
1767c43e99fdSEd Maste EVENT_BASE_COUNT_ADDED |
1768c43e99fdSEd Maste EVENT_BASE_COUNT_VIRTUAL, 0);
1769c43e99fdSEd Maste
1770c43e99fdSEd Maste tt_int_op(event_count_active, ==, 0);
1771c43e99fdSEd Maste tt_int_op(event_count_virtual, ==, 1);
1772c43e99fdSEd Maste tt_int_op(event_count_added, ==, 0);
1773c43e99fdSEd Maste tt_int_op(event_count_active_virtual, ==, 1);
1774c43e99fdSEd Maste tt_int_op(event_count_active_added, ==, 0);
1775c43e99fdSEd Maste tt_int_op(event_count_virtual_added, ==, 1);
1776c43e99fdSEd Maste tt_int_op(event_count_active_added_virtual, ==, 1);
1777c43e99fdSEd Maste
1778c43e99fdSEd Maste end:
1779c43e99fdSEd Maste ;
1780c43e99fdSEd Maste }
1781c43e99fdSEd Maste
1782c43e99fdSEd Maste static void
test_bad_assign(void * ptr)1783c43e99fdSEd Maste test_bad_assign(void *ptr)
1784c43e99fdSEd Maste {
1785c43e99fdSEd Maste struct event ev;
1786c43e99fdSEd Maste int r;
1787c43e99fdSEd Maste /* READ|SIGNAL is not allowed */
1788c43e99fdSEd Maste r = event_assign(&ev, NULL, -1, EV_SIGNAL|EV_READ, dummy_read_cb, NULL);
1789c43e99fdSEd Maste tt_int_op(r,==,-1);
1790c43e99fdSEd Maste
1791c43e99fdSEd Maste end:
1792c43e99fdSEd Maste ;
1793c43e99fdSEd Maste }
1794c43e99fdSEd Maste
1795c43e99fdSEd Maste static int reentrant_cb_run = 0;
1796c43e99fdSEd Maste
1797c43e99fdSEd Maste static void
bad_reentrant_run_loop_cb(evutil_socket_t fd,short what,void * ptr)1798c43e99fdSEd Maste bad_reentrant_run_loop_cb(evutil_socket_t fd, short what, void *ptr)
1799c43e99fdSEd Maste {
1800c43e99fdSEd Maste struct event_base *base = ptr;
1801c43e99fdSEd Maste int r;
1802c43e99fdSEd Maste reentrant_cb_run = 1;
1803c43e99fdSEd Maste /* This reentrant call to event_base_loop should be detected and
1804c43e99fdSEd Maste * should fail */
1805c43e99fdSEd Maste r = event_base_loop(base, 0);
1806c43e99fdSEd Maste tt_int_op(r, ==, -1);
1807c43e99fdSEd Maste end:
1808c43e99fdSEd Maste ;
1809c43e99fdSEd Maste }
1810c43e99fdSEd Maste
1811c43e99fdSEd Maste static void
test_bad_reentrant(void * ptr)1812c43e99fdSEd Maste test_bad_reentrant(void *ptr)
1813c43e99fdSEd Maste {
1814c43e99fdSEd Maste struct basic_test_data *data = ptr;
1815c43e99fdSEd Maste struct event_base *base = data->base;
1816c43e99fdSEd Maste struct event ev;
1817c43e99fdSEd Maste int r;
1818c43e99fdSEd Maste event_assign(&ev, base, -1,
1819c43e99fdSEd Maste 0, bad_reentrant_run_loop_cb, base);
1820c43e99fdSEd Maste
1821c43e99fdSEd Maste event_active(&ev, EV_WRITE, 1);
1822c43e99fdSEd Maste r = event_base_loop(base, 0);
1823c43e99fdSEd Maste tt_int_op(r, ==, 1);
1824c43e99fdSEd Maste tt_int_op(reentrant_cb_run, ==, 1);
1825c43e99fdSEd Maste end:
1826c43e99fdSEd Maste ;
1827c43e99fdSEd Maste }
1828c43e99fdSEd Maste
1829c43e99fdSEd Maste static int n_write_a_byte_cb=0;
1830c43e99fdSEd Maste static int n_read_and_drain_cb=0;
1831c43e99fdSEd Maste static int n_activate_other_event_cb=0;
1832c43e99fdSEd Maste static void
write_a_byte_cb(evutil_socket_t fd,short what,void * arg)1833c43e99fdSEd Maste write_a_byte_cb(evutil_socket_t fd, short what, void *arg)
1834c43e99fdSEd Maste {
1835c43e99fdSEd Maste char buf[] = "x";
1836c43e99fdSEd Maste if (write(fd, buf, 1) == 1)
1837c43e99fdSEd Maste ++n_write_a_byte_cb;
1838c43e99fdSEd Maste }
1839c43e99fdSEd Maste static void
read_and_drain_cb(evutil_socket_t fd,short what,void * arg)1840c43e99fdSEd Maste read_and_drain_cb(evutil_socket_t fd, short what, void *arg)
1841c43e99fdSEd Maste {
1842c43e99fdSEd Maste char buf[128];
1843c43e99fdSEd Maste int n;
1844c43e99fdSEd Maste ++n_read_and_drain_cb;
1845c43e99fdSEd Maste while ((n = read(fd, buf, sizeof(buf))) > 0)
1846c43e99fdSEd Maste ;
1847c43e99fdSEd Maste }
1848c43e99fdSEd Maste
1849c43e99fdSEd Maste static void
activate_other_event_cb(evutil_socket_t fd,short what,void * other_)1850c43e99fdSEd Maste activate_other_event_cb(evutil_socket_t fd, short what, void *other_)
1851c43e99fdSEd Maste {
1852c43e99fdSEd Maste struct event *ev_activate = other_;
1853c43e99fdSEd Maste ++n_activate_other_event_cb;
1854c43e99fdSEd Maste event_active_later_(ev_activate, EV_READ);
1855c43e99fdSEd Maste }
1856c43e99fdSEd Maste
1857c43e99fdSEd Maste static void
test_active_later(void * ptr)1858c43e99fdSEd Maste test_active_later(void *ptr)
1859c43e99fdSEd Maste {
1860c43e99fdSEd Maste struct basic_test_data *data = ptr;
1861c43e99fdSEd Maste struct event *ev1 = NULL, *ev2 = NULL;
1862c43e99fdSEd Maste struct event ev3, ev4;
1863c43e99fdSEd Maste struct timeval qsec = {0, 100000};
1864c43e99fdSEd Maste ev1 = event_new(data->base, data->pair[0], EV_READ|EV_PERSIST, read_and_drain_cb, NULL);
1865c43e99fdSEd Maste ev2 = event_new(data->base, data->pair[1], EV_WRITE|EV_PERSIST, write_a_byte_cb, NULL);
1866c43e99fdSEd Maste event_assign(&ev3, data->base, -1, 0, activate_other_event_cb, &ev4);
1867c43e99fdSEd Maste event_assign(&ev4, data->base, -1, 0, activate_other_event_cb, &ev3);
1868c43e99fdSEd Maste event_add(ev1, NULL);
1869c43e99fdSEd Maste event_add(ev2, NULL);
1870c43e99fdSEd Maste event_active_later_(&ev3, EV_READ);
1871c43e99fdSEd Maste
1872c43e99fdSEd Maste event_base_loopexit(data->base, &qsec);
1873c43e99fdSEd Maste
1874c43e99fdSEd Maste event_base_loop(data->base, 0);
1875c43e99fdSEd Maste
1876c43e99fdSEd Maste TT_BLATHER(("%d write calls, %d read calls, %d activate-other calls.",
1877c43e99fdSEd Maste n_write_a_byte_cb, n_read_and_drain_cb, n_activate_other_event_cb));
1878c43e99fdSEd Maste event_del(&ev3);
1879c43e99fdSEd Maste event_del(&ev4);
1880c43e99fdSEd Maste
1881c43e99fdSEd Maste tt_int_op(n_write_a_byte_cb, ==, n_activate_other_event_cb);
1882c43e99fdSEd Maste tt_int_op(n_write_a_byte_cb, >, 100);
1883c43e99fdSEd Maste tt_int_op(n_read_and_drain_cb, >, 100);
1884c43e99fdSEd Maste tt_int_op(n_activate_other_event_cb, >, 100);
1885c43e99fdSEd Maste
1886c43e99fdSEd Maste event_active_later_(&ev4, EV_READ);
1887c43e99fdSEd Maste event_active(&ev4, EV_READ, 1); /* This should make the event
1888c43e99fdSEd Maste active immediately. */
1889c43e99fdSEd Maste tt_assert((ev4.ev_flags & EVLIST_ACTIVE) != 0);
1890c43e99fdSEd Maste tt_assert((ev4.ev_flags & EVLIST_ACTIVE_LATER) == 0);
1891c43e99fdSEd Maste
1892c43e99fdSEd Maste /* Now leave this one around, so that event_free sees it and removes
1893c43e99fdSEd Maste * it. */
1894c43e99fdSEd Maste event_active_later_(&ev3, EV_READ);
1895c43e99fdSEd Maste event_base_assert_ok_(data->base);
1896c43e99fdSEd Maste
1897c43e99fdSEd Maste end:
1898c43e99fdSEd Maste if (ev1)
1899c43e99fdSEd Maste event_free(ev1);
1900c43e99fdSEd Maste if (ev2)
1901c43e99fdSEd Maste event_free(ev2);
1902c43e99fdSEd Maste
1903c43e99fdSEd Maste event_base_free(data->base);
1904c43e99fdSEd Maste data->base = NULL;
1905c43e99fdSEd Maste }
1906c43e99fdSEd Maste
1907c43e99fdSEd Maste
incr_arg_cb(evutil_socket_t fd,short what,void * arg)1908c43e99fdSEd Maste static void incr_arg_cb(evutil_socket_t fd, short what, void *arg)
1909c43e99fdSEd Maste {
1910c43e99fdSEd Maste int *intptr = arg;
1911c43e99fdSEd Maste (void) fd; (void) what;
1912c43e99fdSEd Maste ++*intptr;
1913c43e99fdSEd Maste }
remove_timers_cb(evutil_socket_t fd,short what,void * arg)1914c43e99fdSEd Maste static void remove_timers_cb(evutil_socket_t fd, short what, void *arg)
1915c43e99fdSEd Maste {
1916c43e99fdSEd Maste struct event **ep = arg;
1917c43e99fdSEd Maste (void) fd; (void) what;
1918c43e99fdSEd Maste event_remove_timer(ep[0]);
1919c43e99fdSEd Maste event_remove_timer(ep[1]);
1920c43e99fdSEd Maste }
send_a_byte_cb(evutil_socket_t fd,short what,void * arg)1921c43e99fdSEd Maste static void send_a_byte_cb(evutil_socket_t fd, short what, void *arg)
1922c43e99fdSEd Maste {
1923c43e99fdSEd Maste evutil_socket_t *sockp = arg;
1924c43e99fdSEd Maste (void) fd; (void) what;
1925*b50261e2SCy Schubert if (write(*sockp, "A", 1) < 0)
1926*b50261e2SCy Schubert tt_fail_perror("write");
1927c43e99fdSEd Maste }
1928c43e99fdSEd Maste struct read_not_timeout_param
1929c43e99fdSEd Maste {
1930c43e99fdSEd Maste struct event **ev;
1931c43e99fdSEd Maste int events;
1932c43e99fdSEd Maste int count;
1933c43e99fdSEd Maste };
read_not_timeout_cb(evutil_socket_t fd,short what,void * arg)1934c43e99fdSEd Maste static void read_not_timeout_cb(evutil_socket_t fd, short what, void *arg)
1935c43e99fdSEd Maste {
1936c43e99fdSEd Maste struct read_not_timeout_param *rntp = arg;
1937c43e99fdSEd Maste char c;
1938c43e99fdSEd Maste ev_ssize_t n;
1939c43e99fdSEd Maste (void) fd; (void) what;
1940c43e99fdSEd Maste n = read(fd, &c, 1);
1941c43e99fdSEd Maste tt_int_op(n, ==, 1);
1942c43e99fdSEd Maste rntp->events |= what;
1943c43e99fdSEd Maste ++rntp->count;
1944c43e99fdSEd Maste if(2 == rntp->count) event_del(rntp->ev[0]);
1945c43e99fdSEd Maste end:
1946c43e99fdSEd Maste ;
1947c43e99fdSEd Maste }
1948c43e99fdSEd Maste
1949c43e99fdSEd Maste static void
test_event_remove_timeout(void * ptr)1950c43e99fdSEd Maste test_event_remove_timeout(void *ptr)
1951c43e99fdSEd Maste {
1952c43e99fdSEd Maste struct basic_test_data *data = ptr;
1953c43e99fdSEd Maste struct event_base *base = data->base;
1954c43e99fdSEd Maste struct event *ev[5];
1955c43e99fdSEd Maste int ev1_fired=0;
1956c43e99fdSEd Maste struct timeval ms25 = { 0, 25*1000 },
1957c43e99fdSEd Maste ms40 = { 0, 40*1000 },
1958c43e99fdSEd Maste ms75 = { 0, 75*1000 },
1959c43e99fdSEd Maste ms125 = { 0, 125*1000 };
1960c43e99fdSEd Maste struct read_not_timeout_param rntp = { ev, 0, 0 };
1961c43e99fdSEd Maste
1962c43e99fdSEd Maste event_base_assert_ok_(base);
1963c43e99fdSEd Maste
1964c43e99fdSEd Maste ev[0] = event_new(base, data->pair[0], EV_READ|EV_PERSIST,
1965c43e99fdSEd Maste read_not_timeout_cb, &rntp);
1966c43e99fdSEd Maste ev[1] = evtimer_new(base, incr_arg_cb, &ev1_fired);
1967c43e99fdSEd Maste ev[2] = evtimer_new(base, remove_timers_cb, ev);
1968c43e99fdSEd Maste ev[3] = evtimer_new(base, send_a_byte_cb, &data->pair[1]);
1969c43e99fdSEd Maste ev[4] = evtimer_new(base, send_a_byte_cb, &data->pair[1]);
1970c43e99fdSEd Maste tt_assert(base);
1971c43e99fdSEd Maste event_add(ev[2], &ms25); /* remove timers */
1972c43e99fdSEd Maste event_add(ev[4], &ms40); /* write to test if timer re-activates */
1973c43e99fdSEd Maste event_add(ev[0], &ms75); /* read */
1974c43e99fdSEd Maste event_add(ev[1], &ms75); /* timer */
1975c43e99fdSEd Maste event_add(ev[3], &ms125); /* timeout. */
1976c43e99fdSEd Maste event_base_assert_ok_(base);
1977c43e99fdSEd Maste
1978c43e99fdSEd Maste event_base_dispatch(base);
1979c43e99fdSEd Maste
1980c43e99fdSEd Maste tt_int_op(ev1_fired, ==, 0);
1981c43e99fdSEd Maste tt_int_op(rntp.events, ==, EV_READ);
1982c43e99fdSEd Maste
1983c43e99fdSEd Maste event_base_assert_ok_(base);
1984c43e99fdSEd Maste end:
1985c43e99fdSEd Maste event_free(ev[0]);
1986c43e99fdSEd Maste event_free(ev[1]);
1987c43e99fdSEd Maste event_free(ev[2]);
1988c43e99fdSEd Maste event_free(ev[3]);
1989c43e99fdSEd Maste event_free(ev[4]);
1990c43e99fdSEd Maste }
1991c43e99fdSEd Maste
1992c43e99fdSEd Maste static void
test_event_base_new(void * ptr)1993c43e99fdSEd Maste test_event_base_new(void *ptr)
1994c43e99fdSEd Maste {
1995c43e99fdSEd Maste struct basic_test_data *data = ptr;
1996c43e99fdSEd Maste struct event_base *base = 0;
1997c43e99fdSEd Maste struct event ev1;
1998c43e99fdSEd Maste struct basic_cb_args args;
1999c43e99fdSEd Maste
2000c43e99fdSEd Maste int towrite = (int)strlen(TEST1)+1;
2001c43e99fdSEd Maste int len = write(data->pair[0], TEST1, towrite);
2002c43e99fdSEd Maste
2003c43e99fdSEd Maste if (len < 0)
2004c43e99fdSEd Maste tt_abort_perror("initial write");
2005c43e99fdSEd Maste else if (len != towrite)
2006c43e99fdSEd Maste tt_abort_printf(("initial write fell short (%d of %d bytes)",
2007c43e99fdSEd Maste len, towrite));
2008c43e99fdSEd Maste
2009c43e99fdSEd Maste if (shutdown(data->pair[0], EVUTIL_SHUT_WR))
2010c43e99fdSEd Maste tt_abort_perror("initial write shutdown");
2011c43e99fdSEd Maste
2012c43e99fdSEd Maste base = event_base_new();
2013c43e99fdSEd Maste if (!base)
2014c43e99fdSEd Maste tt_abort_msg("failed to create event base");
2015c43e99fdSEd Maste
2016c43e99fdSEd Maste args.eb = base;
2017c43e99fdSEd Maste args.ev = &ev1;
2018c43e99fdSEd Maste args.callcount = 0;
2019c43e99fdSEd Maste event_assign(&ev1, base, data->pair[1],
2020c43e99fdSEd Maste EV_READ|EV_PERSIST, basic_read_cb, &args);
2021c43e99fdSEd Maste
2022c43e99fdSEd Maste if (event_add(&ev1, NULL))
2023c43e99fdSEd Maste tt_abort_perror("initial event_add");
2024c43e99fdSEd Maste
2025c43e99fdSEd Maste if (event_base_loop(base, 0))
2026c43e99fdSEd Maste tt_abort_msg("unsuccessful exit from event loop");
2027c43e99fdSEd Maste
2028c43e99fdSEd Maste end:
2029c43e99fdSEd Maste if (base)
2030c43e99fdSEd Maste event_base_free(base);
2031c43e99fdSEd Maste }
2032c43e99fdSEd Maste
2033c43e99fdSEd Maste static void
test_loopexit(void)2034c43e99fdSEd Maste test_loopexit(void)
2035c43e99fdSEd Maste {
2036c43e99fdSEd Maste struct timeval tv, tv_start, tv_end;
2037c43e99fdSEd Maste struct event ev;
2038c43e99fdSEd Maste
2039c43e99fdSEd Maste setup_test("Loop exit: ");
2040c43e99fdSEd Maste
2041c43e99fdSEd Maste tv.tv_usec = 0;
2042c43e99fdSEd Maste tv.tv_sec = 60*60*24;
2043c43e99fdSEd Maste evtimer_set(&ev, timeout_cb, NULL);
2044c43e99fdSEd Maste evtimer_add(&ev, &tv);
2045c43e99fdSEd Maste
2046c43e99fdSEd Maste tv.tv_usec = 300*1000;
2047c43e99fdSEd Maste tv.tv_sec = 0;
2048c43e99fdSEd Maste event_loopexit(&tv);
2049c43e99fdSEd Maste
2050c43e99fdSEd Maste evutil_gettimeofday(&tv_start, NULL);
2051c43e99fdSEd Maste event_dispatch();
2052c43e99fdSEd Maste evutil_gettimeofday(&tv_end, NULL);
2053c43e99fdSEd Maste
2054c43e99fdSEd Maste evtimer_del(&ev);
2055c43e99fdSEd Maste
2056c43e99fdSEd Maste tt_assert(event_base_got_exit(global_base));
2057c43e99fdSEd Maste tt_assert(!event_base_got_break(global_base));
2058c43e99fdSEd Maste
2059c43e99fdSEd Maste test_timeval_diff_eq(&tv_start, &tv_end, 300);
2060c43e99fdSEd Maste
2061c43e99fdSEd Maste test_ok = 1;
2062c43e99fdSEd Maste end:
2063c43e99fdSEd Maste cleanup_test();
2064c43e99fdSEd Maste }
2065c43e99fdSEd Maste
2066c43e99fdSEd Maste static void
test_loopexit_multiple(void)2067c43e99fdSEd Maste test_loopexit_multiple(void)
2068c43e99fdSEd Maste {
2069c43e99fdSEd Maste struct timeval tv, tv_start, tv_end;
2070c43e99fdSEd Maste struct event_base *base;
2071c43e99fdSEd Maste
2072c43e99fdSEd Maste setup_test("Loop Multiple exit: ");
2073c43e99fdSEd Maste
2074c43e99fdSEd Maste base = event_base_new();
2075c43e99fdSEd Maste
2076c43e99fdSEd Maste tv.tv_usec = 200*1000;
2077c43e99fdSEd Maste tv.tv_sec = 0;
2078c43e99fdSEd Maste event_base_loopexit(base, &tv);
2079c43e99fdSEd Maste
2080c43e99fdSEd Maste tv.tv_usec = 0;
2081c43e99fdSEd Maste tv.tv_sec = 3;
2082c43e99fdSEd Maste event_base_loopexit(base, &tv);
2083c43e99fdSEd Maste
2084c43e99fdSEd Maste evutil_gettimeofday(&tv_start, NULL);
2085c43e99fdSEd Maste event_base_dispatch(base);
2086c43e99fdSEd Maste evutil_gettimeofday(&tv_end, NULL);
2087c43e99fdSEd Maste
2088c43e99fdSEd Maste tt_assert(event_base_got_exit(base));
2089c43e99fdSEd Maste tt_assert(!event_base_got_break(base));
2090c43e99fdSEd Maste
2091c43e99fdSEd Maste event_base_free(base);
2092c43e99fdSEd Maste
2093c43e99fdSEd Maste test_timeval_diff_eq(&tv_start, &tv_end, 200);
2094c43e99fdSEd Maste
2095c43e99fdSEd Maste test_ok = 1;
2096c43e99fdSEd Maste
2097c43e99fdSEd Maste end:
2098c43e99fdSEd Maste cleanup_test();
2099c43e99fdSEd Maste }
2100c43e99fdSEd Maste
2101c43e99fdSEd Maste static void
break_cb(evutil_socket_t fd,short events,void * arg)2102c43e99fdSEd Maste break_cb(evutil_socket_t fd, short events, void *arg)
2103c43e99fdSEd Maste {
2104c43e99fdSEd Maste test_ok = 1;
2105c43e99fdSEd Maste event_loopbreak();
2106c43e99fdSEd Maste }
2107c43e99fdSEd Maste
2108c43e99fdSEd Maste static void
fail_cb(evutil_socket_t fd,short events,void * arg)2109c43e99fdSEd Maste fail_cb(evutil_socket_t fd, short events, void *arg)
2110c43e99fdSEd Maste {
2111c43e99fdSEd Maste test_ok = 0;
2112c43e99fdSEd Maste }
2113c43e99fdSEd Maste
2114c43e99fdSEd Maste static void
test_loopbreak(void)2115c43e99fdSEd Maste test_loopbreak(void)
2116c43e99fdSEd Maste {
2117c43e99fdSEd Maste struct event ev1, ev2;
2118c43e99fdSEd Maste struct timeval tv;
2119c43e99fdSEd Maste
2120c43e99fdSEd Maste setup_test("Loop break: ");
2121c43e99fdSEd Maste
2122c43e99fdSEd Maste tv.tv_sec = 0;
2123c43e99fdSEd Maste tv.tv_usec = 0;
2124c43e99fdSEd Maste evtimer_set(&ev1, break_cb, NULL);
2125c43e99fdSEd Maste evtimer_add(&ev1, &tv);
2126c43e99fdSEd Maste evtimer_set(&ev2, fail_cb, NULL);
2127c43e99fdSEd Maste evtimer_add(&ev2, &tv);
2128c43e99fdSEd Maste
2129c43e99fdSEd Maste event_dispatch();
2130c43e99fdSEd Maste
2131c43e99fdSEd Maste tt_assert(!event_base_got_exit(global_base));
2132c43e99fdSEd Maste tt_assert(event_base_got_break(global_base));
2133c43e99fdSEd Maste
2134c43e99fdSEd Maste evtimer_del(&ev1);
2135c43e99fdSEd Maste evtimer_del(&ev2);
2136c43e99fdSEd Maste
2137c43e99fdSEd Maste end:
2138c43e99fdSEd Maste cleanup_test();
2139c43e99fdSEd Maste }
2140c43e99fdSEd Maste
2141c43e99fdSEd Maste static struct event *readd_test_event_last_added = NULL;
2142c43e99fdSEd Maste static void
re_add_read_cb(evutil_socket_t fd,short event,void * arg)2143c43e99fdSEd Maste re_add_read_cb(evutil_socket_t fd, short event, void *arg)
2144c43e99fdSEd Maste {
2145c43e99fdSEd Maste char buf[256];
2146c43e99fdSEd Maste struct event *ev_other = arg;
2147c43e99fdSEd Maste ev_ssize_t n_read;
2148c43e99fdSEd Maste
2149c43e99fdSEd Maste readd_test_event_last_added = ev_other;
2150c43e99fdSEd Maste
2151c43e99fdSEd Maste n_read = read(fd, buf, sizeof(buf));
2152c43e99fdSEd Maste
2153c43e99fdSEd Maste if (n_read < 0) {
2154c43e99fdSEd Maste tt_fail_perror("read");
2155c43e99fdSEd Maste event_base_loopbreak(event_get_base(ev_other));
2156c43e99fdSEd Maste } else {
2157c43e99fdSEd Maste event_add(ev_other, NULL);
2158c43e99fdSEd Maste ++test_ok;
2159c43e99fdSEd Maste }
2160c43e99fdSEd Maste }
2161c43e99fdSEd Maste static void
test_nonpersist_readd(void * _data)2162*b50261e2SCy Schubert test_nonpersist_readd(void *_data)
2163c43e99fdSEd Maste {
2164c43e99fdSEd Maste struct event ev1, ev2;
2165*b50261e2SCy Schubert struct basic_test_data *data = _data;
2166c43e99fdSEd Maste
2167*b50261e2SCy Schubert memset(&ev1, 0, sizeof(ev1));
2168*b50261e2SCy Schubert memset(&ev2, 0, sizeof(ev2));
2169c43e99fdSEd Maste
2170*b50261e2SCy Schubert tt_assert(!event_assign(&ev1, data->base, data->pair[0], EV_READ, re_add_read_cb, &ev2));
2171*b50261e2SCy Schubert tt_assert(!event_assign(&ev2, data->base, data->pair[1], EV_READ, re_add_read_cb, &ev1));
2172c43e99fdSEd Maste
2173*b50261e2SCy Schubert tt_int_op(write(data->pair[0], "Hello", 5), ==, 5);
2174*b50261e2SCy Schubert tt_int_op(write(data->pair[1], "Hello", 5), ==, 5);
2175c43e99fdSEd Maste
2176*b50261e2SCy Schubert tt_int_op(event_add(&ev1, NULL), ==, 0);
2177*b50261e2SCy Schubert tt_int_op(event_add(&ev2, NULL), ==, 0);
2178*b50261e2SCy Schubert tt_int_op(event_base_loop(data->base, EVLOOP_ONCE), ==, 0);
2179*b50261e2SCy Schubert tt_int_op(test_ok, ==, 2);
2180*b50261e2SCy Schubert
2181c43e99fdSEd Maste /* At this point, we executed both callbacks. Whichever one got
2182c43e99fdSEd Maste * called first added the second, but the second then immediately got
2183c43e99fdSEd Maste * deleted before its callback was called. At this point, though, it
2184c43e99fdSEd Maste * re-added the first.
2185c43e99fdSEd Maste */
2186*b50261e2SCy Schubert tt_assert(readd_test_event_last_added);
2187*b50261e2SCy Schubert if (readd_test_event_last_added == &ev1) {
2188*b50261e2SCy Schubert tt_assert(event_pending(&ev1, EV_READ, NULL) && !event_pending(&ev2, EV_READ, NULL));
2189c43e99fdSEd Maste } else {
2190*b50261e2SCy Schubert tt_assert(event_pending(&ev2, EV_READ, NULL) && !event_pending(&ev1, EV_READ, NULL));
2191c43e99fdSEd Maste }
2192c43e99fdSEd Maste
2193*b50261e2SCy Schubert end:
2194*b50261e2SCy Schubert if (event_initialized(&ev1))
2195c43e99fdSEd Maste event_del(&ev1);
2196*b50261e2SCy Schubert if (event_initialized(&ev2))
2197c43e99fdSEd Maste event_del(&ev2);
2198c43e99fdSEd Maste }
2199c43e99fdSEd Maste
2200c43e99fdSEd Maste struct test_pri_event {
2201c43e99fdSEd Maste struct event ev;
2202c43e99fdSEd Maste int count;
2203c43e99fdSEd Maste };
2204c43e99fdSEd Maste
2205c43e99fdSEd Maste static void
test_priorities_cb(evutil_socket_t fd,short what,void * arg)2206c43e99fdSEd Maste test_priorities_cb(evutil_socket_t fd, short what, void *arg)
2207c43e99fdSEd Maste {
2208c43e99fdSEd Maste struct test_pri_event *pri = arg;
2209c43e99fdSEd Maste struct timeval tv;
2210c43e99fdSEd Maste
2211c43e99fdSEd Maste if (pri->count == 3) {
2212c43e99fdSEd Maste event_loopexit(NULL);
2213c43e99fdSEd Maste return;
2214c43e99fdSEd Maste }
2215c43e99fdSEd Maste
2216c43e99fdSEd Maste pri->count++;
2217c43e99fdSEd Maste
2218c43e99fdSEd Maste evutil_timerclear(&tv);
2219c43e99fdSEd Maste event_add(&pri->ev, &tv);
2220c43e99fdSEd Maste }
2221c43e99fdSEd Maste
2222c43e99fdSEd Maste static void
test_priorities_impl(int npriorities)2223c43e99fdSEd Maste test_priorities_impl(int npriorities)
2224c43e99fdSEd Maste {
2225c43e99fdSEd Maste struct test_pri_event one, two;
2226c43e99fdSEd Maste struct timeval tv;
2227c43e99fdSEd Maste
2228c43e99fdSEd Maste TT_BLATHER(("Testing Priorities %d: ", npriorities));
2229c43e99fdSEd Maste
2230c43e99fdSEd Maste event_base_priority_init(global_base, npriorities);
2231c43e99fdSEd Maste
2232c43e99fdSEd Maste memset(&one, 0, sizeof(one));
2233c43e99fdSEd Maste memset(&two, 0, sizeof(two));
2234c43e99fdSEd Maste
2235c43e99fdSEd Maste timeout_set(&one.ev, test_priorities_cb, &one);
2236c43e99fdSEd Maste if (event_priority_set(&one.ev, 0) == -1) {
2237c43e99fdSEd Maste fprintf(stderr, "%s: failed to set priority", __func__);
2238c43e99fdSEd Maste exit(1);
2239c43e99fdSEd Maste }
2240c43e99fdSEd Maste
2241c43e99fdSEd Maste timeout_set(&two.ev, test_priorities_cb, &two);
2242c43e99fdSEd Maste if (event_priority_set(&two.ev, npriorities - 1) == -1) {
2243c43e99fdSEd Maste fprintf(stderr, "%s: failed to set priority", __func__);
2244c43e99fdSEd Maste exit(1);
2245c43e99fdSEd Maste }
2246c43e99fdSEd Maste
2247c43e99fdSEd Maste evutil_timerclear(&tv);
2248c43e99fdSEd Maste
2249c43e99fdSEd Maste if (event_add(&one.ev, &tv) == -1)
2250c43e99fdSEd Maste exit(1);
2251c43e99fdSEd Maste if (event_add(&two.ev, &tv) == -1)
2252c43e99fdSEd Maste exit(1);
2253c43e99fdSEd Maste
2254c43e99fdSEd Maste event_dispatch();
2255c43e99fdSEd Maste
2256c43e99fdSEd Maste event_del(&one.ev);
2257c43e99fdSEd Maste event_del(&two.ev);
2258c43e99fdSEd Maste
2259c43e99fdSEd Maste if (npriorities == 1) {
2260c43e99fdSEd Maste if (one.count == 3 && two.count == 3)
2261c43e99fdSEd Maste test_ok = 1;
2262c43e99fdSEd Maste } else if (npriorities == 2) {
2263c43e99fdSEd Maste /* Two is called once because event_loopexit is priority 1 */
2264c43e99fdSEd Maste if (one.count == 3 && two.count == 1)
2265c43e99fdSEd Maste test_ok = 1;
2266c43e99fdSEd Maste } else {
2267c43e99fdSEd Maste if (one.count == 3 && two.count == 0)
2268c43e99fdSEd Maste test_ok = 1;
2269c43e99fdSEd Maste }
2270c43e99fdSEd Maste }
2271c43e99fdSEd Maste
2272c43e99fdSEd Maste static void
test_priorities(void)2273c43e99fdSEd Maste test_priorities(void)
2274c43e99fdSEd Maste {
2275c43e99fdSEd Maste test_priorities_impl(1);
2276c43e99fdSEd Maste if (test_ok)
2277c43e99fdSEd Maste test_priorities_impl(2);
2278c43e99fdSEd Maste if (test_ok)
2279c43e99fdSEd Maste test_priorities_impl(3);
2280c43e99fdSEd Maste }
2281c43e99fdSEd Maste
2282c43e99fdSEd Maste /* priority-active-inversion: activate a higher-priority event, and make sure
2283c43e99fdSEd Maste * it keeps us from running a lower-priority event first. */
2284c43e99fdSEd Maste static int n_pai_calls = 0;
2285c43e99fdSEd Maste static struct event pai_events[3];
2286c43e99fdSEd Maste
2287c43e99fdSEd Maste static void
prio_active_inversion_cb(evutil_socket_t fd,short what,void * arg)2288c43e99fdSEd Maste prio_active_inversion_cb(evutil_socket_t fd, short what, void *arg)
2289c43e99fdSEd Maste {
2290c43e99fdSEd Maste int *call_order = arg;
2291c43e99fdSEd Maste *call_order = n_pai_calls++;
2292c43e99fdSEd Maste if (n_pai_calls == 1) {
2293c43e99fdSEd Maste /* This should activate later, even though it shares a
2294c43e99fdSEd Maste priority with us. */
2295c43e99fdSEd Maste event_active(&pai_events[1], EV_READ, 1);
2296c43e99fdSEd Maste /* This should activate next, since its priority is higher,
2297c43e99fdSEd Maste even though we activated it second. */
2298c43e99fdSEd Maste event_active(&pai_events[2], EV_TIMEOUT, 1);
2299c43e99fdSEd Maste }
2300c43e99fdSEd Maste }
2301c43e99fdSEd Maste
2302c43e99fdSEd Maste static void
test_priority_active_inversion(void * data_)2303c43e99fdSEd Maste test_priority_active_inversion(void *data_)
2304c43e99fdSEd Maste {
2305c43e99fdSEd Maste struct basic_test_data *data = data_;
2306c43e99fdSEd Maste struct event_base *base = data->base;
2307c43e99fdSEd Maste int call_order[3];
2308c43e99fdSEd Maste int i;
2309c43e99fdSEd Maste tt_int_op(event_base_priority_init(base, 8), ==, 0);
2310c43e99fdSEd Maste
2311c43e99fdSEd Maste n_pai_calls = 0;
2312c43e99fdSEd Maste memset(call_order, 0, sizeof(call_order));
2313c43e99fdSEd Maste
2314c43e99fdSEd Maste for (i=0;i<3;++i) {
2315c43e99fdSEd Maste event_assign(&pai_events[i], data->base, -1, 0,
2316c43e99fdSEd Maste prio_active_inversion_cb, &call_order[i]);
2317c43e99fdSEd Maste }
2318c43e99fdSEd Maste
2319c43e99fdSEd Maste event_priority_set(&pai_events[0], 4);
2320c43e99fdSEd Maste event_priority_set(&pai_events[1], 4);
2321c43e99fdSEd Maste event_priority_set(&pai_events[2], 0);
2322c43e99fdSEd Maste
2323c43e99fdSEd Maste event_active(&pai_events[0], EV_WRITE, 1);
2324c43e99fdSEd Maste
2325c43e99fdSEd Maste event_base_dispatch(base);
2326c43e99fdSEd Maste tt_int_op(n_pai_calls, ==, 3);
2327c43e99fdSEd Maste tt_int_op(call_order[0], ==, 0);
2328c43e99fdSEd Maste tt_int_op(call_order[1], ==, 2);
2329c43e99fdSEd Maste tt_int_op(call_order[2], ==, 1);
2330c43e99fdSEd Maste end:
2331c43e99fdSEd Maste ;
2332c43e99fdSEd Maste }
2333c43e99fdSEd Maste
2334c43e99fdSEd Maste
2335c43e99fdSEd Maste static void
test_multiple_cb(evutil_socket_t fd,short event,void * arg)2336c43e99fdSEd Maste test_multiple_cb(evutil_socket_t fd, short event, void *arg)
2337c43e99fdSEd Maste {
2338c43e99fdSEd Maste if (event & EV_READ)
2339c43e99fdSEd Maste test_ok |= 1;
2340c43e99fdSEd Maste else if (event & EV_WRITE)
2341c43e99fdSEd Maste test_ok |= 2;
2342c43e99fdSEd Maste }
2343c43e99fdSEd Maste
2344c43e99fdSEd Maste static void
test_multiple_events_for_same_fd(void)2345c43e99fdSEd Maste test_multiple_events_for_same_fd(void)
2346c43e99fdSEd Maste {
2347c43e99fdSEd Maste struct event e1, e2;
2348c43e99fdSEd Maste
2349c43e99fdSEd Maste setup_test("Multiple events for same fd: ");
2350c43e99fdSEd Maste
2351c43e99fdSEd Maste event_set(&e1, pair[0], EV_READ, test_multiple_cb, NULL);
2352c43e99fdSEd Maste event_add(&e1, NULL);
2353c43e99fdSEd Maste event_set(&e2, pair[0], EV_WRITE, test_multiple_cb, NULL);
2354c43e99fdSEd Maste event_add(&e2, NULL);
2355c43e99fdSEd Maste event_loop(EVLOOP_ONCE);
2356c43e99fdSEd Maste event_del(&e2);
2357c43e99fdSEd Maste
2358c43e99fdSEd Maste if (write(pair[1], TEST1, strlen(TEST1)+1) < 0) {
2359c43e99fdSEd Maste tt_fail_perror("write");
2360c43e99fdSEd Maste }
2361c43e99fdSEd Maste
2362c43e99fdSEd Maste event_loop(EVLOOP_ONCE);
2363c43e99fdSEd Maste event_del(&e1);
2364c43e99fdSEd Maste
2365c43e99fdSEd Maste if (test_ok != 3)
2366c43e99fdSEd Maste test_ok = 0;
2367c43e99fdSEd Maste
2368c43e99fdSEd Maste cleanup_test();
2369c43e99fdSEd Maste }
2370c43e99fdSEd Maste
2371c43e99fdSEd Maste int evtag_decode_int(ev_uint32_t *pnumber, struct evbuffer *evbuf);
2372c43e99fdSEd Maste int evtag_decode_int64(ev_uint64_t *pnumber, struct evbuffer *evbuf);
2373c43e99fdSEd Maste int evtag_encode_tag(struct evbuffer *evbuf, ev_uint32_t number);
2374c43e99fdSEd Maste int evtag_decode_tag(ev_uint32_t *pnumber, struct evbuffer *evbuf);
2375c43e99fdSEd Maste
2376c43e99fdSEd Maste static void
read_once_cb(evutil_socket_t fd,short event,void * arg)2377c43e99fdSEd Maste read_once_cb(evutil_socket_t fd, short event, void *arg)
2378c43e99fdSEd Maste {
2379c43e99fdSEd Maste char buf[256];
2380c43e99fdSEd Maste int len;
2381c43e99fdSEd Maste
2382c43e99fdSEd Maste len = read(fd, buf, sizeof(buf));
2383c43e99fdSEd Maste
2384c43e99fdSEd Maste if (called) {
2385c43e99fdSEd Maste test_ok = 0;
2386c43e99fdSEd Maste } else if (len) {
2387c43e99fdSEd Maste /* Assumes global pair[0] can be used for writing */
2388c43e99fdSEd Maste if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
2389c43e99fdSEd Maste tt_fail_perror("write");
2390c43e99fdSEd Maste test_ok = 0;
2391c43e99fdSEd Maste } else {
2392c43e99fdSEd Maste test_ok = 1;
2393c43e99fdSEd Maste }
2394c43e99fdSEd Maste }
2395c43e99fdSEd Maste
2396c43e99fdSEd Maste called++;
2397c43e99fdSEd Maste }
2398c43e99fdSEd Maste
2399c43e99fdSEd Maste static void
test_want_only_once(void)2400c43e99fdSEd Maste test_want_only_once(void)
2401c43e99fdSEd Maste {
2402c43e99fdSEd Maste struct event ev;
2403c43e99fdSEd Maste struct timeval tv;
2404c43e99fdSEd Maste
2405c43e99fdSEd Maste /* Very simple read test */
2406c43e99fdSEd Maste setup_test("Want read only once: ");
2407c43e99fdSEd Maste
2408c43e99fdSEd Maste if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
2409c43e99fdSEd Maste tt_fail_perror("write");
2410c43e99fdSEd Maste }
2411c43e99fdSEd Maste
2412c43e99fdSEd Maste /* Setup the loop termination */
2413c43e99fdSEd Maste evutil_timerclear(&tv);
2414c43e99fdSEd Maste tv.tv_usec = 300*1000;
2415c43e99fdSEd Maste event_loopexit(&tv);
2416c43e99fdSEd Maste
2417c43e99fdSEd Maste event_set(&ev, pair[1], EV_READ, read_once_cb, &ev);
2418c43e99fdSEd Maste if (event_add(&ev, NULL) == -1)
2419c43e99fdSEd Maste exit(1);
2420c43e99fdSEd Maste event_dispatch();
2421c43e99fdSEd Maste
2422c43e99fdSEd Maste cleanup_test();
2423c43e99fdSEd Maste }
2424c43e99fdSEd Maste
2425c43e99fdSEd Maste #define TEST_MAX_INT 6
2426c43e99fdSEd Maste
2427c43e99fdSEd Maste static void
evtag_int_test(void * ptr)2428c43e99fdSEd Maste evtag_int_test(void *ptr)
2429c43e99fdSEd Maste {
2430c43e99fdSEd Maste struct evbuffer *tmp = evbuffer_new();
2431c43e99fdSEd Maste ev_uint32_t integers[TEST_MAX_INT] = {
2432c43e99fdSEd Maste 0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
2433c43e99fdSEd Maste };
2434c43e99fdSEd Maste ev_uint32_t integer;
2435c43e99fdSEd Maste ev_uint64_t big_int;
2436c43e99fdSEd Maste int i;
2437c43e99fdSEd Maste
2438c43e99fdSEd Maste evtag_init();
2439c43e99fdSEd Maste
2440c43e99fdSEd Maste for (i = 0; i < TEST_MAX_INT; i++) {
2441c43e99fdSEd Maste int oldlen, newlen;
2442c43e99fdSEd Maste oldlen = (int)EVBUFFER_LENGTH(tmp);
2443c43e99fdSEd Maste evtag_encode_int(tmp, integers[i]);
2444c43e99fdSEd Maste newlen = (int)EVBUFFER_LENGTH(tmp);
2445c43e99fdSEd Maste TT_BLATHER(("encoded 0x%08x with %d bytes",
2446c43e99fdSEd Maste (unsigned)integers[i], newlen - oldlen));
2447c43e99fdSEd Maste big_int = integers[i];
2448c43e99fdSEd Maste big_int *= 1000000000; /* 1 billion */
2449c43e99fdSEd Maste evtag_encode_int64(tmp, big_int);
2450c43e99fdSEd Maste }
2451c43e99fdSEd Maste
2452c43e99fdSEd Maste for (i = 0; i < TEST_MAX_INT; i++) {
2453c43e99fdSEd Maste tt_int_op(evtag_decode_int(&integer, tmp), !=, -1);
2454c43e99fdSEd Maste tt_uint_op(integer, ==, integers[i]);
2455c43e99fdSEd Maste tt_int_op(evtag_decode_int64(&big_int, tmp), !=, -1);
2456c43e99fdSEd Maste tt_assert((big_int / 1000000000) == integers[i]);
2457c43e99fdSEd Maste }
2458c43e99fdSEd Maste
2459c43e99fdSEd Maste tt_uint_op(EVBUFFER_LENGTH(tmp), ==, 0);
2460c43e99fdSEd Maste end:
2461c43e99fdSEd Maste evbuffer_free(tmp);
2462c43e99fdSEd Maste }
2463c43e99fdSEd Maste
2464c43e99fdSEd Maste static void
evtag_fuzz(void * ptr)2465c43e99fdSEd Maste evtag_fuzz(void *ptr)
2466c43e99fdSEd Maste {
2467c43e99fdSEd Maste unsigned char buffer[4096];
2468c43e99fdSEd Maste struct evbuffer *tmp = evbuffer_new();
2469c43e99fdSEd Maste struct timeval tv;
2470c43e99fdSEd Maste int i, j;
2471c43e99fdSEd Maste
2472c43e99fdSEd Maste int not_failed = 0;
2473c43e99fdSEd Maste
2474c43e99fdSEd Maste evtag_init();
2475c43e99fdSEd Maste
2476c43e99fdSEd Maste for (j = 0; j < 100; j++) {
2477c43e99fdSEd Maste for (i = 0; i < (int)sizeof(buffer); i++)
2478c43e99fdSEd Maste buffer[i] = test_weakrand();
2479c43e99fdSEd Maste evbuffer_drain(tmp, -1);
2480c43e99fdSEd Maste evbuffer_add(tmp, buffer, sizeof(buffer));
2481c43e99fdSEd Maste
2482c43e99fdSEd Maste if (evtag_unmarshal_timeval(tmp, 0, &tv) != -1)
2483c43e99fdSEd Maste not_failed++;
2484c43e99fdSEd Maste }
2485c43e99fdSEd Maste
2486c43e99fdSEd Maste /* The majority of decodes should fail */
2487c43e99fdSEd Maste tt_int_op(not_failed, <, 10);
2488c43e99fdSEd Maste
2489c43e99fdSEd Maste /* Now insert some corruption into the tag length field */
2490c43e99fdSEd Maste evbuffer_drain(tmp, -1);
2491c43e99fdSEd Maste evutil_timerclear(&tv);
2492c43e99fdSEd Maste tv.tv_sec = 1;
2493c43e99fdSEd Maste evtag_marshal_timeval(tmp, 0, &tv);
2494c43e99fdSEd Maste evbuffer_add(tmp, buffer, sizeof(buffer));
2495c43e99fdSEd Maste
2496c43e99fdSEd Maste ((char *)EVBUFFER_DATA(tmp))[1] = '\xff';
2497c43e99fdSEd Maste if (evtag_unmarshal_timeval(tmp, 0, &tv) != -1) {
2498c43e99fdSEd Maste tt_abort_msg("evtag_unmarshal_timeval should have failed");
2499c43e99fdSEd Maste }
2500c43e99fdSEd Maste
2501c43e99fdSEd Maste end:
2502c43e99fdSEd Maste evbuffer_free(tmp);
2503c43e99fdSEd Maste }
2504c43e99fdSEd Maste
2505c43e99fdSEd Maste static void
evtag_tag_encoding(void * ptr)2506c43e99fdSEd Maste evtag_tag_encoding(void *ptr)
2507c43e99fdSEd Maste {
2508c43e99fdSEd Maste struct evbuffer *tmp = evbuffer_new();
2509c43e99fdSEd Maste ev_uint32_t integers[TEST_MAX_INT] = {
2510c43e99fdSEd Maste 0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
2511c43e99fdSEd Maste };
2512c43e99fdSEd Maste ev_uint32_t integer;
2513c43e99fdSEd Maste int i;
2514c43e99fdSEd Maste
2515c43e99fdSEd Maste evtag_init();
2516c43e99fdSEd Maste
2517c43e99fdSEd Maste for (i = 0; i < TEST_MAX_INT; i++) {
2518c43e99fdSEd Maste int oldlen, newlen;
2519c43e99fdSEd Maste oldlen = (int)EVBUFFER_LENGTH(tmp);
2520c43e99fdSEd Maste evtag_encode_tag(tmp, integers[i]);
2521c43e99fdSEd Maste newlen = (int)EVBUFFER_LENGTH(tmp);
2522c43e99fdSEd Maste TT_BLATHER(("encoded 0x%08x with %d bytes",
2523c43e99fdSEd Maste (unsigned)integers[i], newlen - oldlen));
2524c43e99fdSEd Maste }
2525c43e99fdSEd Maste
2526c43e99fdSEd Maste for (i = 0; i < TEST_MAX_INT; i++) {
2527c43e99fdSEd Maste tt_int_op(evtag_decode_tag(&integer, tmp), !=, -1);
2528c43e99fdSEd Maste tt_uint_op(integer, ==, integers[i]);
2529c43e99fdSEd Maste }
2530c43e99fdSEd Maste
2531c43e99fdSEd Maste tt_uint_op(EVBUFFER_LENGTH(tmp), ==, 0);
2532c43e99fdSEd Maste
2533c43e99fdSEd Maste end:
2534c43e99fdSEd Maste evbuffer_free(tmp);
2535c43e99fdSEd Maste }
2536c43e99fdSEd Maste
2537c43e99fdSEd Maste static void
evtag_test_peek(void * ptr)2538c43e99fdSEd Maste evtag_test_peek(void *ptr)
2539c43e99fdSEd Maste {
2540c43e99fdSEd Maste struct evbuffer *tmp = evbuffer_new();
2541c43e99fdSEd Maste ev_uint32_t u32;
2542c43e99fdSEd Maste
2543c43e99fdSEd Maste evtag_marshal_int(tmp, 30, 0);
2544c43e99fdSEd Maste evtag_marshal_string(tmp, 40, "Hello world");
2545c43e99fdSEd Maste
2546c43e99fdSEd Maste tt_int_op(evtag_peek(tmp, &u32), ==, 1);
2547c43e99fdSEd Maste tt_int_op(u32, ==, 30);
2548c43e99fdSEd Maste tt_int_op(evtag_peek_length(tmp, &u32), ==, 0);
2549c43e99fdSEd Maste tt_int_op(u32, ==, 1+1+1);
2550c43e99fdSEd Maste tt_int_op(evtag_consume(tmp), ==, 0);
2551c43e99fdSEd Maste
2552c43e99fdSEd Maste tt_int_op(evtag_peek(tmp, &u32), ==, 1);
2553c43e99fdSEd Maste tt_int_op(u32, ==, 40);
2554c43e99fdSEd Maste tt_int_op(evtag_peek_length(tmp, &u32), ==, 0);
2555c43e99fdSEd Maste tt_int_op(u32, ==, 1+1+11);
2556c43e99fdSEd Maste tt_int_op(evtag_payload_length(tmp, &u32), ==, 0);
2557c43e99fdSEd Maste tt_int_op(u32, ==, 11);
2558c43e99fdSEd Maste
2559c43e99fdSEd Maste end:
2560c43e99fdSEd Maste evbuffer_free(tmp);
2561c43e99fdSEd Maste }
2562c43e99fdSEd Maste
2563c43e99fdSEd Maste
2564c43e99fdSEd Maste static void
test_methods(void * ptr)2565c43e99fdSEd Maste test_methods(void *ptr)
2566c43e99fdSEd Maste {
2567c43e99fdSEd Maste const char **methods = event_get_supported_methods();
2568c43e99fdSEd Maste struct event_config *cfg = NULL;
2569c43e99fdSEd Maste struct event_base *base = NULL;
2570c43e99fdSEd Maste const char *backend;
2571c43e99fdSEd Maste int n_methods = 0;
2572c43e99fdSEd Maste
2573c43e99fdSEd Maste tt_assert(methods);
2574c43e99fdSEd Maste
2575c43e99fdSEd Maste backend = methods[0];
2576c43e99fdSEd Maste while (*methods != NULL) {
2577c43e99fdSEd Maste TT_BLATHER(("Support method: %s", *methods));
2578c43e99fdSEd Maste ++methods;
2579c43e99fdSEd Maste ++n_methods;
2580c43e99fdSEd Maste }
2581c43e99fdSEd Maste
2582c43e99fdSEd Maste cfg = event_config_new();
2583c43e99fdSEd Maste assert(cfg != NULL);
2584c43e99fdSEd Maste
2585c43e99fdSEd Maste tt_int_op(event_config_avoid_method(cfg, backend), ==, 0);
2586c43e99fdSEd Maste event_config_set_flag(cfg, EVENT_BASE_FLAG_IGNORE_ENV);
2587c43e99fdSEd Maste
2588c43e99fdSEd Maste base = event_base_new_with_config(cfg);
2589c43e99fdSEd Maste if (n_methods > 1) {
2590c43e99fdSEd Maste tt_assert(base);
2591c43e99fdSEd Maste tt_str_op(backend, !=, event_base_get_method(base));
2592c43e99fdSEd Maste } else {
2593c43e99fdSEd Maste tt_assert(base == NULL);
2594c43e99fdSEd Maste }
2595c43e99fdSEd Maste
2596c43e99fdSEd Maste end:
2597c43e99fdSEd Maste if (base)
2598c43e99fdSEd Maste event_base_free(base);
2599c43e99fdSEd Maste if (cfg)
2600c43e99fdSEd Maste event_config_free(cfg);
2601c43e99fdSEd Maste }
2602c43e99fdSEd Maste
2603c43e99fdSEd Maste static void
test_version(void * arg)2604c43e99fdSEd Maste test_version(void *arg)
2605c43e99fdSEd Maste {
2606c43e99fdSEd Maste const char *vstr;
2607c43e99fdSEd Maste ev_uint32_t vint;
2608c43e99fdSEd Maste int major, minor, patch, n;
2609c43e99fdSEd Maste
2610c43e99fdSEd Maste vstr = event_get_version();
2611c43e99fdSEd Maste vint = event_get_version_number();
2612c43e99fdSEd Maste
2613c43e99fdSEd Maste tt_assert(vstr);
2614c43e99fdSEd Maste tt_assert(vint);
2615c43e99fdSEd Maste
2616c43e99fdSEd Maste tt_str_op(vstr, ==, LIBEVENT_VERSION);
2617c43e99fdSEd Maste tt_int_op(vint, ==, LIBEVENT_VERSION_NUMBER);
2618c43e99fdSEd Maste
2619c43e99fdSEd Maste n = sscanf(vstr, "%d.%d.%d", &major, &minor, &patch);
2620c43e99fdSEd Maste tt_assert(3 == n);
2621c43e99fdSEd Maste tt_int_op((vint&0xffffff00), ==, ((major<<24)|(minor<<16)|(patch<<8)));
2622c43e99fdSEd Maste end:
2623c43e99fdSEd Maste ;
2624c43e99fdSEd Maste }
2625c43e99fdSEd Maste
2626c43e99fdSEd Maste static void
test_base_features(void * arg)2627c43e99fdSEd Maste test_base_features(void *arg)
2628c43e99fdSEd Maste {
2629c43e99fdSEd Maste struct event_base *base = NULL;
2630c43e99fdSEd Maste struct event_config *cfg = NULL;
2631c43e99fdSEd Maste
2632c43e99fdSEd Maste cfg = event_config_new();
2633c43e99fdSEd Maste
2634c43e99fdSEd Maste tt_assert(0 == event_config_require_features(cfg, EV_FEATURE_ET));
2635c43e99fdSEd Maste
2636c43e99fdSEd Maste base = event_base_new_with_config(cfg);
2637c43e99fdSEd Maste if (base) {
2638c43e99fdSEd Maste tt_int_op(EV_FEATURE_ET, ==,
2639c43e99fdSEd Maste event_base_get_features(base) & EV_FEATURE_ET);
2640c43e99fdSEd Maste } else {
2641c43e99fdSEd Maste base = event_base_new();
2642c43e99fdSEd Maste tt_int_op(0, ==, event_base_get_features(base) & EV_FEATURE_ET);
2643c43e99fdSEd Maste }
2644c43e99fdSEd Maste
2645c43e99fdSEd Maste end:
2646c43e99fdSEd Maste if (base)
2647c43e99fdSEd Maste event_base_free(base);
2648c43e99fdSEd Maste if (cfg)
2649c43e99fdSEd Maste event_config_free(cfg);
2650c43e99fdSEd Maste }
2651c43e99fdSEd Maste
2652c43e99fdSEd Maste #ifdef EVENT__HAVE_SETENV
2653c43e99fdSEd Maste #define SETENV_OK
2654c43e99fdSEd Maste #elif !defined(EVENT__HAVE_SETENV) && defined(EVENT__HAVE_PUTENV)
setenv(const char * k,const char * v,int o_)2655c43e99fdSEd Maste static void setenv(const char *k, const char *v, int o_)
2656c43e99fdSEd Maste {
2657c43e99fdSEd Maste char b[256];
2658c43e99fdSEd Maste evutil_snprintf(b, sizeof(b), "%s=%s",k,v);
2659c43e99fdSEd Maste putenv(b);
2660c43e99fdSEd Maste }
2661c43e99fdSEd Maste #define SETENV_OK
2662c43e99fdSEd Maste #endif
2663c43e99fdSEd Maste
2664c43e99fdSEd Maste #ifdef EVENT__HAVE_UNSETENV
2665c43e99fdSEd Maste #define UNSETENV_OK
2666c43e99fdSEd Maste #elif !defined(EVENT__HAVE_UNSETENV) && defined(EVENT__HAVE_PUTENV)
unsetenv(const char * k)2667c43e99fdSEd Maste static void unsetenv(const char *k)
2668c43e99fdSEd Maste {
2669c43e99fdSEd Maste char b[256];
2670c43e99fdSEd Maste evutil_snprintf(b, sizeof(b), "%s=",k);
2671c43e99fdSEd Maste putenv(b);
2672c43e99fdSEd Maste }
2673c43e99fdSEd Maste #define UNSETENV_OK
2674c43e99fdSEd Maste #endif
2675c43e99fdSEd Maste
2676c43e99fdSEd Maste #if defined(SETENV_OK) && defined(UNSETENV_OK)
2677c43e99fdSEd Maste static void
methodname_to_envvar(const char * mname,char * buf,size_t buflen)2678c43e99fdSEd Maste methodname_to_envvar(const char *mname, char *buf, size_t buflen)
2679c43e99fdSEd Maste {
2680c43e99fdSEd Maste char *cp;
2681c43e99fdSEd Maste evutil_snprintf(buf, buflen, "EVENT_NO%s", mname);
2682c43e99fdSEd Maste for (cp = buf; *cp; ++cp) {
2683c43e99fdSEd Maste *cp = EVUTIL_TOUPPER_(*cp);
2684c43e99fdSEd Maste }
2685c43e99fdSEd Maste }
2686c43e99fdSEd Maste #endif
2687c43e99fdSEd Maste
2688c43e99fdSEd Maste static void
test_base_environ(void * arg)2689c43e99fdSEd Maste test_base_environ(void *arg)
2690c43e99fdSEd Maste {
2691c43e99fdSEd Maste struct event_base *base = NULL;
2692c43e99fdSEd Maste struct event_config *cfg = NULL;
2693c43e99fdSEd Maste
2694c43e99fdSEd Maste #if defined(SETENV_OK) && defined(UNSETENV_OK)
2695c43e99fdSEd Maste const char **basenames;
2696c43e99fdSEd Maste int i, n_methods=0;
2697c43e99fdSEd Maste char varbuf[128];
2698c43e99fdSEd Maste const char *defaultname, *ignoreenvname;
2699c43e99fdSEd Maste
2700c43e99fdSEd Maste /* See if unsetenv works before we rely on it. */
2701c43e99fdSEd Maste setenv("EVENT_NOWAFFLES", "1", 1);
2702c43e99fdSEd Maste unsetenv("EVENT_NOWAFFLES");
2703c43e99fdSEd Maste if (getenv("EVENT_NOWAFFLES") != NULL) {
2704c43e99fdSEd Maste #ifndef EVENT__HAVE_UNSETENV
2705c43e99fdSEd Maste TT_DECLARE("NOTE", ("Can't fake unsetenv; skipping test"));
2706c43e99fdSEd Maste #else
2707c43e99fdSEd Maste TT_DECLARE("NOTE", ("unsetenv doesn't work; skipping test"));
2708c43e99fdSEd Maste #endif
2709c43e99fdSEd Maste tt_skip();
2710c43e99fdSEd Maste }
2711c43e99fdSEd Maste
2712c43e99fdSEd Maste basenames = event_get_supported_methods();
2713c43e99fdSEd Maste for (i = 0; basenames[i]; ++i) {
2714c43e99fdSEd Maste methodname_to_envvar(basenames[i], varbuf, sizeof(varbuf));
2715c43e99fdSEd Maste unsetenv(varbuf);
2716c43e99fdSEd Maste ++n_methods;
2717c43e99fdSEd Maste }
2718c43e99fdSEd Maste
2719c43e99fdSEd Maste base = event_base_new();
2720c43e99fdSEd Maste tt_assert(base);
2721c43e99fdSEd Maste
2722c43e99fdSEd Maste defaultname = event_base_get_method(base);
2723c43e99fdSEd Maste TT_BLATHER(("default is <%s>", defaultname));
2724c43e99fdSEd Maste event_base_free(base);
2725c43e99fdSEd Maste base = NULL;
2726c43e99fdSEd Maste
2727c43e99fdSEd Maste /* Can we disable the method with EVENT_NOfoo ? */
2728c43e99fdSEd Maste if (!strcmp(defaultname, "epoll (with changelist)")) {
2729c43e99fdSEd Maste setenv("EVENT_NOEPOLL", "1", 1);
2730c43e99fdSEd Maste ignoreenvname = "epoll";
2731c43e99fdSEd Maste } else {
2732c43e99fdSEd Maste methodname_to_envvar(defaultname, varbuf, sizeof(varbuf));
2733c43e99fdSEd Maste setenv(varbuf, "1", 1);
2734c43e99fdSEd Maste ignoreenvname = defaultname;
2735c43e99fdSEd Maste }
2736c43e99fdSEd Maste
2737c43e99fdSEd Maste /* Use an empty cfg rather than NULL so a failure doesn't exit() */
2738c43e99fdSEd Maste cfg = event_config_new();
2739c43e99fdSEd Maste base = event_base_new_with_config(cfg);
2740c43e99fdSEd Maste event_config_free(cfg);
2741c43e99fdSEd Maste cfg = NULL;
2742c43e99fdSEd Maste if (n_methods == 1) {
2743c43e99fdSEd Maste tt_assert(!base);
2744c43e99fdSEd Maste } else {
2745c43e99fdSEd Maste tt_assert(base);
2746c43e99fdSEd Maste tt_str_op(defaultname, !=, event_base_get_method(base));
2747c43e99fdSEd Maste event_base_free(base);
2748c43e99fdSEd Maste base = NULL;
2749c43e99fdSEd Maste }
2750c43e99fdSEd Maste
2751c43e99fdSEd Maste /* Can we disable looking at the environment with IGNORE_ENV ? */
2752c43e99fdSEd Maste cfg = event_config_new();
2753c43e99fdSEd Maste event_config_set_flag(cfg, EVENT_BASE_FLAG_IGNORE_ENV);
2754c43e99fdSEd Maste base = event_base_new_with_config(cfg);
2755c43e99fdSEd Maste tt_assert(base);
2756c43e99fdSEd Maste tt_str_op(ignoreenvname, ==, event_base_get_method(base));
2757c43e99fdSEd Maste #else
2758c43e99fdSEd Maste tt_skip();
2759c43e99fdSEd Maste #endif
2760c43e99fdSEd Maste
2761c43e99fdSEd Maste end:
2762c43e99fdSEd Maste if (base)
2763c43e99fdSEd Maste event_base_free(base);
2764c43e99fdSEd Maste if (cfg)
2765c43e99fdSEd Maste event_config_free(cfg);
2766c43e99fdSEd Maste }
2767c43e99fdSEd Maste
2768c43e99fdSEd Maste static void
read_called_once_cb(evutil_socket_t fd,short event,void * arg)2769c43e99fdSEd Maste read_called_once_cb(evutil_socket_t fd, short event, void *arg)
2770c43e99fdSEd Maste {
2771c43e99fdSEd Maste tt_int_op(event, ==, EV_READ);
2772c43e99fdSEd Maste called += 1;
2773c43e99fdSEd Maste end:
2774c43e99fdSEd Maste ;
2775c43e99fdSEd Maste }
2776c43e99fdSEd Maste
2777c43e99fdSEd Maste static void
timeout_called_once_cb(evutil_socket_t fd,short event,void * arg)2778c43e99fdSEd Maste timeout_called_once_cb(evutil_socket_t fd, short event, void *arg)
2779c43e99fdSEd Maste {
2780c43e99fdSEd Maste tt_int_op(event, ==, EV_TIMEOUT);
2781c43e99fdSEd Maste called += 100;
2782c43e99fdSEd Maste end:
2783c43e99fdSEd Maste ;
2784c43e99fdSEd Maste }
2785c43e99fdSEd Maste
2786c43e99fdSEd Maste static void
immediate_called_twice_cb(evutil_socket_t fd,short event,void * arg)2787c43e99fdSEd Maste immediate_called_twice_cb(evutil_socket_t fd, short event, void *arg)
2788c43e99fdSEd Maste {
2789c43e99fdSEd Maste tt_int_op(event, ==, EV_TIMEOUT);
2790c43e99fdSEd Maste called += 1000;
2791c43e99fdSEd Maste end:
2792c43e99fdSEd Maste ;
2793c43e99fdSEd Maste }
2794c43e99fdSEd Maste
2795c43e99fdSEd Maste static void
test_event_once(void * ptr)2796c43e99fdSEd Maste test_event_once(void *ptr)
2797c43e99fdSEd Maste {
2798c43e99fdSEd Maste struct basic_test_data *data = ptr;
2799c43e99fdSEd Maste struct timeval tv;
2800c43e99fdSEd Maste int r;
2801c43e99fdSEd Maste
2802c43e99fdSEd Maste tv.tv_sec = 0;
2803c43e99fdSEd Maste tv.tv_usec = 50*1000;
2804c43e99fdSEd Maste called = 0;
2805c43e99fdSEd Maste r = event_base_once(data->base, data->pair[0], EV_READ,
2806c43e99fdSEd Maste read_called_once_cb, NULL, NULL);
2807c43e99fdSEd Maste tt_int_op(r, ==, 0);
2808c43e99fdSEd Maste r = event_base_once(data->base, -1, EV_TIMEOUT,
2809c43e99fdSEd Maste timeout_called_once_cb, NULL, &tv);
2810c43e99fdSEd Maste tt_int_op(r, ==, 0);
2811c43e99fdSEd Maste r = event_base_once(data->base, -1, 0, NULL, NULL, NULL);
2812c43e99fdSEd Maste tt_int_op(r, <, 0);
2813c43e99fdSEd Maste r = event_base_once(data->base, -1, EV_TIMEOUT,
2814c43e99fdSEd Maste immediate_called_twice_cb, NULL, NULL);
2815c43e99fdSEd Maste tt_int_op(r, ==, 0);
2816c43e99fdSEd Maste tv.tv_sec = 0;
2817c43e99fdSEd Maste tv.tv_usec = 0;
2818c43e99fdSEd Maste r = event_base_once(data->base, -1, EV_TIMEOUT,
2819c43e99fdSEd Maste immediate_called_twice_cb, NULL, &tv);
2820c43e99fdSEd Maste tt_int_op(r, ==, 0);
2821c43e99fdSEd Maste
2822c43e99fdSEd Maste if (write(data->pair[1], TEST1, strlen(TEST1)+1) < 0) {
2823c43e99fdSEd Maste tt_fail_perror("write");
2824c43e99fdSEd Maste }
2825c43e99fdSEd Maste
2826c43e99fdSEd Maste shutdown(data->pair[1], EVUTIL_SHUT_WR);
2827c43e99fdSEd Maste
2828c43e99fdSEd Maste event_base_dispatch(data->base);
2829c43e99fdSEd Maste
2830c43e99fdSEd Maste tt_int_op(called, ==, 2101);
2831c43e99fdSEd Maste end:
2832c43e99fdSEd Maste ;
2833c43e99fdSEd Maste }
2834c43e99fdSEd Maste
2835c43e99fdSEd Maste static void
test_event_once_never(void * ptr)2836c43e99fdSEd Maste test_event_once_never(void *ptr)
2837c43e99fdSEd Maste {
2838c43e99fdSEd Maste struct basic_test_data *data = ptr;
2839c43e99fdSEd Maste struct timeval tv;
2840c43e99fdSEd Maste
2841c43e99fdSEd Maste /* Have one trigger in 10 seconds (don't worry, because) */
2842c43e99fdSEd Maste tv.tv_sec = 10;
2843c43e99fdSEd Maste tv.tv_usec = 0;
2844c43e99fdSEd Maste called = 0;
2845c43e99fdSEd Maste event_base_once(data->base, -1, EV_TIMEOUT,
2846c43e99fdSEd Maste timeout_called_once_cb, NULL, &tv);
2847c43e99fdSEd Maste
2848c43e99fdSEd Maste /* But shut down the base in 75 msec. */
2849c43e99fdSEd Maste tv.tv_sec = 0;
2850c43e99fdSEd Maste tv.tv_usec = 75*1000;
2851c43e99fdSEd Maste event_base_loopexit(data->base, &tv);
2852c43e99fdSEd Maste
2853c43e99fdSEd Maste event_base_dispatch(data->base);
2854c43e99fdSEd Maste
2855c43e99fdSEd Maste tt_int_op(called, ==, 0);
2856c43e99fdSEd Maste end:
2857c43e99fdSEd Maste ;
2858c43e99fdSEd Maste }
2859c43e99fdSEd Maste
2860c43e99fdSEd Maste static void
test_event_pending(void * ptr)2861c43e99fdSEd Maste test_event_pending(void *ptr)
2862c43e99fdSEd Maste {
2863c43e99fdSEd Maste struct basic_test_data *data = ptr;
2864c43e99fdSEd Maste struct event *r=NULL, *w=NULL, *t=NULL;
2865c43e99fdSEd Maste struct timeval tv, now, tv2;
2866c43e99fdSEd Maste
2867c43e99fdSEd Maste tv.tv_sec = 0;
2868c43e99fdSEd Maste tv.tv_usec = 500 * 1000;
2869c43e99fdSEd Maste r = event_new(data->base, data->pair[0], EV_READ, simple_read_cb,
2870c43e99fdSEd Maste NULL);
2871c43e99fdSEd Maste w = event_new(data->base, data->pair[1], EV_WRITE, simple_write_cb,
2872c43e99fdSEd Maste NULL);
2873c43e99fdSEd Maste t = evtimer_new(data->base, timeout_cb, NULL);
2874c43e99fdSEd Maste
2875c43e99fdSEd Maste tt_assert(r);
2876c43e99fdSEd Maste tt_assert(w);
2877c43e99fdSEd Maste tt_assert(t);
2878c43e99fdSEd Maste
2879c43e99fdSEd Maste evutil_gettimeofday(&now, NULL);
2880c43e99fdSEd Maste event_add(r, NULL);
2881c43e99fdSEd Maste event_add(t, &tv);
2882c43e99fdSEd Maste
2883c43e99fdSEd Maste tt_assert( event_pending(r, EV_READ, NULL));
2884c43e99fdSEd Maste tt_assert(!event_pending(w, EV_WRITE, NULL));
2885c43e99fdSEd Maste tt_assert(!event_pending(r, EV_WRITE, NULL));
2886c43e99fdSEd Maste tt_assert( event_pending(r, EV_READ|EV_WRITE, NULL));
2887c43e99fdSEd Maste tt_assert(!event_pending(r, EV_TIMEOUT, NULL));
2888c43e99fdSEd Maste tt_assert( event_pending(t, EV_TIMEOUT, NULL));
2889c43e99fdSEd Maste tt_assert( event_pending(t, EV_TIMEOUT, &tv2));
2890c43e99fdSEd Maste
2891c43e99fdSEd Maste tt_assert(evutil_timercmp(&tv2, &now, >));
2892c43e99fdSEd Maste
2893c43e99fdSEd Maste test_timeval_diff_eq(&now, &tv2, 500);
2894c43e99fdSEd Maste
2895c43e99fdSEd Maste end:
2896c43e99fdSEd Maste if (r) {
2897c43e99fdSEd Maste event_del(r);
2898c43e99fdSEd Maste event_free(r);
2899c43e99fdSEd Maste }
2900c43e99fdSEd Maste if (w) {
2901c43e99fdSEd Maste event_del(w);
2902c43e99fdSEd Maste event_free(w);
2903c43e99fdSEd Maste }
2904c43e99fdSEd Maste if (t) {
2905c43e99fdSEd Maste event_del(t);
2906c43e99fdSEd Maste event_free(t);
2907c43e99fdSEd Maste }
2908c43e99fdSEd Maste }
2909c43e99fdSEd Maste
2910c43e99fdSEd Maste static void
dfd_cb(evutil_socket_t fd,short e,void * data)2911c43e99fdSEd Maste dfd_cb(evutil_socket_t fd, short e, void *data)
2912c43e99fdSEd Maste {
2913c43e99fdSEd Maste *(int*)data = (int)e;
2914c43e99fdSEd Maste }
2915c43e99fdSEd Maste
2916c43e99fdSEd Maste static void
test_event_closed_fd_poll(void * arg)2917c43e99fdSEd Maste test_event_closed_fd_poll(void *arg)
2918c43e99fdSEd Maste {
2919c43e99fdSEd Maste struct timeval tv;
2920c43e99fdSEd Maste struct event *e;
2921c43e99fdSEd Maste struct basic_test_data *data = (struct basic_test_data *)arg;
2922c43e99fdSEd Maste int i = 0;
2923c43e99fdSEd Maste
2924c43e99fdSEd Maste if (strcmp(event_base_get_method(data->base), "poll")) {
2925c43e99fdSEd Maste tinytest_set_test_skipped_();
2926c43e99fdSEd Maste return;
2927c43e99fdSEd Maste }
2928c43e99fdSEd Maste
2929c43e99fdSEd Maste e = event_new(data->base, data->pair[0], EV_READ, dfd_cb, &i);
2930c43e99fdSEd Maste tt_assert(e);
2931c43e99fdSEd Maste
2932c43e99fdSEd Maste tv.tv_sec = 0;
2933c43e99fdSEd Maste tv.tv_usec = 500 * 1000;
2934c43e99fdSEd Maste event_add(e, &tv);
2935c43e99fdSEd Maste tt_assert(event_pending(e, EV_READ, NULL));
2936c43e99fdSEd Maste close(data->pair[0]);
2937c43e99fdSEd Maste data->pair[0] = -1; /** avoids double-close */
2938c43e99fdSEd Maste event_base_loop(data->base, EVLOOP_ONCE);
2939c43e99fdSEd Maste tt_int_op(i, ==, EV_READ);
2940c43e99fdSEd Maste
2941c43e99fdSEd Maste end:
2942c43e99fdSEd Maste if (e) {
2943c43e99fdSEd Maste event_del(e);
2944c43e99fdSEd Maste event_free(e);
2945c43e99fdSEd Maste }
2946c43e99fdSEd Maste }
2947c43e99fdSEd Maste
2948c43e99fdSEd Maste #ifndef _WIN32
2949c43e99fdSEd Maste /* You can't do this test on windows, since dup2 doesn't work on sockets */
2950c43e99fdSEd Maste
2951c43e99fdSEd Maste /* Regression test for our workaround for a fun epoll/linux related bug
2952c43e99fdSEd Maste * where fd2 = dup(fd1); add(fd2); close(fd2); dup2(fd1,fd2); add(fd2)
2953c43e99fdSEd Maste * will get you an EEXIST */
2954c43e99fdSEd Maste static void
test_dup_fd(void * arg)2955c43e99fdSEd Maste test_dup_fd(void *arg)
2956c43e99fdSEd Maste {
2957c43e99fdSEd Maste struct basic_test_data *data = arg;
2958c43e99fdSEd Maste struct event_base *base = data->base;
2959c43e99fdSEd Maste struct event *ev1=NULL, *ev2=NULL;
2960c43e99fdSEd Maste int fd, dfd=-1;
2961c43e99fdSEd Maste int ev1_got, ev2_got;
2962c43e99fdSEd Maste
2963c43e99fdSEd Maste tt_int_op(write(data->pair[0], "Hello world",
2964c43e99fdSEd Maste strlen("Hello world")), >, 0);
2965c43e99fdSEd Maste fd = data->pair[1];
2966c43e99fdSEd Maste
2967c43e99fdSEd Maste dfd = dup(fd);
2968c43e99fdSEd Maste tt_int_op(dfd, >=, 0);
2969c43e99fdSEd Maste
2970c43e99fdSEd Maste ev1 = event_new(base, fd, EV_READ|EV_PERSIST, dfd_cb, &ev1_got);
2971c43e99fdSEd Maste ev2 = event_new(base, dfd, EV_READ|EV_PERSIST, dfd_cb, &ev2_got);
2972c43e99fdSEd Maste ev1_got = ev2_got = 0;
2973c43e99fdSEd Maste event_add(ev1, NULL);
2974c43e99fdSEd Maste event_add(ev2, NULL);
2975c43e99fdSEd Maste event_base_loop(base, EVLOOP_ONCE);
2976c43e99fdSEd Maste tt_int_op(ev1_got, ==, EV_READ);
2977c43e99fdSEd Maste tt_int_op(ev2_got, ==, EV_READ);
2978c43e99fdSEd Maste
2979c43e99fdSEd Maste /* Now close and delete dfd then dispatch. We need to do the
2980c43e99fdSEd Maste * dispatch here so that when we add it later, we think there
2981c43e99fdSEd Maste * was an intermediate delete. */
2982c43e99fdSEd Maste close(dfd);
2983c43e99fdSEd Maste event_del(ev2);
2984c43e99fdSEd Maste ev1_got = ev2_got = 0;
2985c43e99fdSEd Maste event_base_loop(base, EVLOOP_ONCE);
2986c43e99fdSEd Maste tt_want_int_op(ev1_got, ==, EV_READ);
2987c43e99fdSEd Maste tt_int_op(ev2_got, ==, 0);
2988c43e99fdSEd Maste
2989c43e99fdSEd Maste /* Re-duplicate the fd. We need to get the same duplicated
2990c43e99fdSEd Maste * value that we closed to provoke the epoll quirk. Also, we
2991c43e99fdSEd Maste * need to change the events to write, or else the old lingering
2992c43e99fdSEd Maste * read event will make the test pass whether the change was
2993c43e99fdSEd Maste * successful or not. */
2994c43e99fdSEd Maste tt_int_op(dup2(fd, dfd), ==, dfd);
2995c43e99fdSEd Maste event_free(ev2);
2996c43e99fdSEd Maste ev2 = event_new(base, dfd, EV_WRITE|EV_PERSIST, dfd_cb, &ev2_got);
2997c43e99fdSEd Maste event_add(ev2, NULL);
2998c43e99fdSEd Maste ev1_got = ev2_got = 0;
2999c43e99fdSEd Maste event_base_loop(base, EVLOOP_ONCE);
3000c43e99fdSEd Maste tt_want_int_op(ev1_got, ==, EV_READ);
3001c43e99fdSEd Maste tt_int_op(ev2_got, ==, EV_WRITE);
3002c43e99fdSEd Maste
3003c43e99fdSEd Maste end:
3004c43e99fdSEd Maste if (ev1)
3005c43e99fdSEd Maste event_free(ev1);
3006c43e99fdSEd Maste if (ev2)
3007c43e99fdSEd Maste event_free(ev2);
3008c43e99fdSEd Maste if (dfd >= 0)
3009c43e99fdSEd Maste close(dfd);
3010c43e99fdSEd Maste }
3011c43e99fdSEd Maste #endif
3012c43e99fdSEd Maste
3013c43e99fdSEd Maste #ifdef EVENT__DISABLE_MM_REPLACEMENT
3014c43e99fdSEd Maste static void
test_mm_functions(void * arg)3015c43e99fdSEd Maste test_mm_functions(void *arg)
3016c43e99fdSEd Maste {
3017c43e99fdSEd Maste tinytest_set_test_skipped_();
3018c43e99fdSEd Maste }
3019c43e99fdSEd Maste #else
3020c43e99fdSEd Maste static int
check_dummy_mem_ok(void * mem_)3021c43e99fdSEd Maste check_dummy_mem_ok(void *mem_)
3022c43e99fdSEd Maste {
3023c43e99fdSEd Maste char *mem = mem_;
3024c43e99fdSEd Maste mem -= 16;
3025c43e99fdSEd Maste return !memcmp(mem, "{[<guardedram>]}", 16);
3026c43e99fdSEd Maste }
3027c43e99fdSEd Maste
3028c43e99fdSEd Maste static void *
dummy_malloc(size_t len)3029c43e99fdSEd Maste dummy_malloc(size_t len)
3030c43e99fdSEd Maste {
3031c43e99fdSEd Maste char *mem = malloc(len+16);
3032c43e99fdSEd Maste memcpy(mem, "{[<guardedram>]}", 16);
3033c43e99fdSEd Maste return mem+16;
3034c43e99fdSEd Maste }
3035c43e99fdSEd Maste
3036c43e99fdSEd Maste static void *
dummy_realloc(void * mem_,size_t len)3037c43e99fdSEd Maste dummy_realloc(void *mem_, size_t len)
3038c43e99fdSEd Maste {
3039c43e99fdSEd Maste char *mem = mem_;
3040c43e99fdSEd Maste if (!mem)
3041c43e99fdSEd Maste return dummy_malloc(len);
3042c43e99fdSEd Maste tt_want(check_dummy_mem_ok(mem_));
3043c43e99fdSEd Maste mem -= 16;
3044c43e99fdSEd Maste mem = realloc(mem, len+16);
3045c43e99fdSEd Maste return mem+16;
3046c43e99fdSEd Maste }
3047c43e99fdSEd Maste
3048c43e99fdSEd Maste static void
dummy_free(void * mem_)3049c43e99fdSEd Maste dummy_free(void *mem_)
3050c43e99fdSEd Maste {
3051c43e99fdSEd Maste char *mem = mem_;
3052c43e99fdSEd Maste tt_want(check_dummy_mem_ok(mem_));
3053c43e99fdSEd Maste mem -= 16;
3054c43e99fdSEd Maste free(mem);
3055c43e99fdSEd Maste }
3056c43e99fdSEd Maste
3057c43e99fdSEd Maste static void
test_mm_functions(void * arg)3058c43e99fdSEd Maste test_mm_functions(void *arg)
3059c43e99fdSEd Maste {
3060c43e99fdSEd Maste struct event_base *b = NULL;
3061c43e99fdSEd Maste struct event_config *cfg = NULL;
3062c43e99fdSEd Maste event_set_mem_functions(dummy_malloc, dummy_realloc, dummy_free);
3063c43e99fdSEd Maste cfg = event_config_new();
3064c43e99fdSEd Maste event_config_avoid_method(cfg, "Nonesuch");
3065c43e99fdSEd Maste b = event_base_new_with_config(cfg);
3066c43e99fdSEd Maste tt_assert(b);
3067c43e99fdSEd Maste tt_assert(check_dummy_mem_ok(b));
3068c43e99fdSEd Maste end:
3069c43e99fdSEd Maste if (cfg)
3070c43e99fdSEd Maste event_config_free(cfg);
3071c43e99fdSEd Maste if (b)
3072c43e99fdSEd Maste event_base_free(b);
3073c43e99fdSEd Maste }
3074c43e99fdSEd Maste #endif
3075c43e99fdSEd Maste
3076c43e99fdSEd Maste static void
many_event_cb(evutil_socket_t fd,short event,void * arg)3077c43e99fdSEd Maste many_event_cb(evutil_socket_t fd, short event, void *arg)
3078c43e99fdSEd Maste {
3079c43e99fdSEd Maste int *calledp = arg;
3080c43e99fdSEd Maste *calledp += 1;
3081c43e99fdSEd Maste }
3082c43e99fdSEd Maste
3083c43e99fdSEd Maste static void
test_many_events(void * arg)3084c43e99fdSEd Maste test_many_events(void *arg)
3085c43e99fdSEd Maste {
3086c43e99fdSEd Maste /* Try 70 events that should all be ready at once. This will
3087c43e99fdSEd Maste * exercise the "resize" code on most of the backends, and will make
3088c43e99fdSEd Maste * sure that we can get past the 64-handle limit of some windows
3089c43e99fdSEd Maste * functions. */
3090c43e99fdSEd Maste #define MANY 70
3091c43e99fdSEd Maste
3092c43e99fdSEd Maste struct basic_test_data *data = arg;
3093c43e99fdSEd Maste struct event_base *base = data->base;
3094c43e99fdSEd Maste int one_at_a_time = data->setup_data != NULL;
3095c43e99fdSEd Maste evutil_socket_t sock[MANY];
3096c43e99fdSEd Maste struct event *ev[MANY];
3097c43e99fdSEd Maste int called[MANY];
3098c43e99fdSEd Maste int i;
3099c43e99fdSEd Maste int loopflags = EVLOOP_NONBLOCK, evflags=0;
3100c43e99fdSEd Maste if (one_at_a_time) {
3101c43e99fdSEd Maste loopflags |= EVLOOP_ONCE;
3102c43e99fdSEd Maste evflags = EV_PERSIST;
3103c43e99fdSEd Maste }
3104c43e99fdSEd Maste
3105c43e99fdSEd Maste memset(sock, 0xff, sizeof(sock));
3106c43e99fdSEd Maste memset(ev, 0, sizeof(ev));
3107c43e99fdSEd Maste memset(called, 0, sizeof(called));
3108c43e99fdSEd Maste
3109c43e99fdSEd Maste for (i = 0; i < MANY; ++i) {
3110c43e99fdSEd Maste /* We need an event that will hit the backend, and that will
3111c43e99fdSEd Maste * be ready immediately. "Send a datagram" is an easy
3112c43e99fdSEd Maste * instance of that. */
3113c43e99fdSEd Maste sock[i] = socket(AF_INET, SOCK_DGRAM, 0);
3114c43e99fdSEd Maste tt_assert(sock[i] >= 0);
3115*b50261e2SCy Schubert tt_assert(!evutil_make_socket_nonblocking(sock[i]));
3116c43e99fdSEd Maste called[i] = 0;
3117c43e99fdSEd Maste ev[i] = event_new(base, sock[i], EV_WRITE|evflags,
3118c43e99fdSEd Maste many_event_cb, &called[i]);
3119c43e99fdSEd Maste event_add(ev[i], NULL);
3120c43e99fdSEd Maste if (one_at_a_time)
3121c43e99fdSEd Maste event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
3122c43e99fdSEd Maste }
3123c43e99fdSEd Maste
3124c43e99fdSEd Maste event_base_loop(base, loopflags);
3125c43e99fdSEd Maste
3126c43e99fdSEd Maste for (i = 0; i < MANY; ++i) {
3127c43e99fdSEd Maste if (one_at_a_time)
3128c43e99fdSEd Maste tt_int_op(called[i], ==, MANY - i + 1);
3129c43e99fdSEd Maste else
3130c43e99fdSEd Maste tt_int_op(called[i], ==, 1);
3131c43e99fdSEd Maste }
3132c43e99fdSEd Maste
3133c43e99fdSEd Maste end:
3134c43e99fdSEd Maste for (i = 0; i < MANY; ++i) {
3135c43e99fdSEd Maste if (ev[i])
3136c43e99fdSEd Maste event_free(ev[i]);
3137c43e99fdSEd Maste if (sock[i] >= 0)
3138c43e99fdSEd Maste evutil_closesocket(sock[i]);
3139c43e99fdSEd Maste }
3140c43e99fdSEd Maste #undef MANY
3141c43e99fdSEd Maste }
3142c43e99fdSEd Maste
3143c43e99fdSEd Maste static void
test_struct_event_size(void * arg)3144c43e99fdSEd Maste test_struct_event_size(void *arg)
3145c43e99fdSEd Maste {
3146c43e99fdSEd Maste tt_int_op(event_get_struct_event_size(), <=, sizeof(struct event));
3147c43e99fdSEd Maste end:
3148c43e99fdSEd Maste ;
3149c43e99fdSEd Maste }
3150c43e99fdSEd Maste
3151c43e99fdSEd Maste static void
test_get_assignment(void * arg)3152c43e99fdSEd Maste test_get_assignment(void *arg)
3153c43e99fdSEd Maste {
3154c43e99fdSEd Maste struct basic_test_data *data = arg;
3155c43e99fdSEd Maste struct event_base *base = data->base;
3156c43e99fdSEd Maste struct event *ev1 = NULL;
3157c43e99fdSEd Maste const char *str = "foo";
3158c43e99fdSEd Maste
3159c43e99fdSEd Maste struct event_base *b;
3160c43e99fdSEd Maste evutil_socket_t s;
3161c43e99fdSEd Maste short what;
3162c43e99fdSEd Maste event_callback_fn cb;
3163c43e99fdSEd Maste void *cb_arg;
3164c43e99fdSEd Maste
3165c43e99fdSEd Maste ev1 = event_new(base, data->pair[1], EV_READ, dummy_read_cb, (void*)str);
3166c43e99fdSEd Maste event_get_assignment(ev1, &b, &s, &what, &cb, &cb_arg);
3167c43e99fdSEd Maste
3168c43e99fdSEd Maste tt_ptr_op(b, ==, base);
3169*b50261e2SCy Schubert tt_fd_op(s, ==, data->pair[1]);
3170c43e99fdSEd Maste tt_int_op(what, ==, EV_READ);
3171c43e99fdSEd Maste tt_ptr_op(cb, ==, dummy_read_cb);
3172c43e99fdSEd Maste tt_ptr_op(cb_arg, ==, str);
3173c43e99fdSEd Maste
3174c43e99fdSEd Maste /* Now make sure this doesn't crash. */
3175c43e99fdSEd Maste event_get_assignment(ev1, NULL, NULL, NULL, NULL, NULL);
3176c43e99fdSEd Maste
3177c43e99fdSEd Maste end:
3178c43e99fdSEd Maste if (ev1)
3179c43e99fdSEd Maste event_free(ev1);
3180c43e99fdSEd Maste }
3181c43e99fdSEd Maste
3182c43e99fdSEd Maste struct foreach_helper {
3183c43e99fdSEd Maste int count;
3184c43e99fdSEd Maste const struct event *ev;
3185c43e99fdSEd Maste };
3186c43e99fdSEd Maste
3187c43e99fdSEd Maste static int
foreach_count_cb(const struct event_base * base,const struct event * ev,void * arg)3188c43e99fdSEd Maste foreach_count_cb(const struct event_base *base, const struct event *ev, void *arg)
3189c43e99fdSEd Maste {
3190c43e99fdSEd Maste struct foreach_helper *h = event_get_callback_arg(ev);
3191c43e99fdSEd Maste struct timeval *tv = arg;
3192c43e99fdSEd Maste if (event_get_callback(ev) != timeout_cb)
3193c43e99fdSEd Maste return 0;
3194c43e99fdSEd Maste tt_ptr_op(event_get_base(ev), ==, base);
3195c43e99fdSEd Maste tt_int_op(tv->tv_sec, ==, 10);
3196c43e99fdSEd Maste h->ev = ev;
3197c43e99fdSEd Maste h->count++;
3198c43e99fdSEd Maste return 0;
3199c43e99fdSEd Maste end:
3200c43e99fdSEd Maste return -1;
3201c43e99fdSEd Maste }
3202c43e99fdSEd Maste
3203c43e99fdSEd Maste static int
foreach_find_cb(const struct event_base * base,const struct event * ev,void * arg)3204c43e99fdSEd Maste foreach_find_cb(const struct event_base *base, const struct event *ev, void *arg)
3205c43e99fdSEd Maste {
3206c43e99fdSEd Maste const struct event **ev_out = arg;
3207c43e99fdSEd Maste struct foreach_helper *h = event_get_callback_arg(ev);
3208c43e99fdSEd Maste if (event_get_callback(ev) != timeout_cb)
3209c43e99fdSEd Maste return 0;
3210c43e99fdSEd Maste if (h->count == 99) {
3211c43e99fdSEd Maste *ev_out = ev;
3212c43e99fdSEd Maste return 101;
3213c43e99fdSEd Maste }
3214c43e99fdSEd Maste return 0;
3215c43e99fdSEd Maste }
3216c43e99fdSEd Maste
3217c43e99fdSEd Maste static void
test_event_foreach(void * arg)3218c43e99fdSEd Maste test_event_foreach(void *arg)
3219c43e99fdSEd Maste {
3220c43e99fdSEd Maste struct basic_test_data *data = arg;
3221c43e99fdSEd Maste struct event_base *base = data->base;
3222c43e99fdSEd Maste struct event *ev[5];
3223c43e99fdSEd Maste struct foreach_helper visited[5];
3224c43e99fdSEd Maste int i;
3225c43e99fdSEd Maste struct timeval ten_sec = {10,0};
3226c43e99fdSEd Maste const struct event *ev_found = NULL;
3227c43e99fdSEd Maste
3228c43e99fdSEd Maste for (i = 0; i < 5; ++i) {
3229c43e99fdSEd Maste visited[i].count = 0;
3230c43e99fdSEd Maste visited[i].ev = NULL;
3231c43e99fdSEd Maste ev[i] = event_new(base, -1, 0, timeout_cb, &visited[i]);
3232c43e99fdSEd Maste }
3233c43e99fdSEd Maste
3234c43e99fdSEd Maste tt_int_op(-1, ==, event_base_foreach_event(NULL, foreach_count_cb, NULL));
3235c43e99fdSEd Maste tt_int_op(-1, ==, event_base_foreach_event(base, NULL, NULL));
3236c43e99fdSEd Maste
3237c43e99fdSEd Maste event_add(ev[0], &ten_sec);
3238c43e99fdSEd Maste event_add(ev[1], &ten_sec);
3239c43e99fdSEd Maste event_active(ev[1], EV_TIMEOUT, 1);
3240c43e99fdSEd Maste event_active(ev[2], EV_TIMEOUT, 1);
3241c43e99fdSEd Maste event_add(ev[3], &ten_sec);
3242c43e99fdSEd Maste /* Don't touch ev[4]. */
3243c43e99fdSEd Maste
3244c43e99fdSEd Maste tt_int_op(0, ==, event_base_foreach_event(base, foreach_count_cb,
3245c43e99fdSEd Maste &ten_sec));
3246c43e99fdSEd Maste tt_int_op(1, ==, visited[0].count);
3247c43e99fdSEd Maste tt_int_op(1, ==, visited[1].count);
3248c43e99fdSEd Maste tt_int_op(1, ==, visited[2].count);
3249c43e99fdSEd Maste tt_int_op(1, ==, visited[3].count);
3250c43e99fdSEd Maste tt_ptr_op(ev[0], ==, visited[0].ev);
3251c43e99fdSEd Maste tt_ptr_op(ev[1], ==, visited[1].ev);
3252c43e99fdSEd Maste tt_ptr_op(ev[2], ==, visited[2].ev);
3253c43e99fdSEd Maste tt_ptr_op(ev[3], ==, visited[3].ev);
3254c43e99fdSEd Maste
3255c43e99fdSEd Maste visited[2].count = 99;
3256c43e99fdSEd Maste tt_int_op(101, ==, event_base_foreach_event(base, foreach_find_cb,
3257c43e99fdSEd Maste &ev_found));
3258c43e99fdSEd Maste tt_ptr_op(ev_found, ==, ev[2]);
3259c43e99fdSEd Maste
3260c43e99fdSEd Maste end:
3261c43e99fdSEd Maste for (i=0; i<5; ++i) {
3262c43e99fdSEd Maste event_free(ev[i]);
3263c43e99fdSEd Maste }
3264c43e99fdSEd Maste }
3265c43e99fdSEd Maste
3266c43e99fdSEd Maste static struct event_base *cached_time_base = NULL;
3267c43e99fdSEd Maste static int cached_time_reset = 0;
3268c43e99fdSEd Maste static int cached_time_sleep = 0;
3269c43e99fdSEd Maste static void
cache_time_cb(evutil_socket_t fd,short what,void * arg)3270c43e99fdSEd Maste cache_time_cb(evutil_socket_t fd, short what, void *arg)
3271c43e99fdSEd Maste {
3272c43e99fdSEd Maste struct timeval *tv = arg;
3273c43e99fdSEd Maste tt_int_op(0, ==, event_base_gettimeofday_cached(cached_time_base, tv));
3274c43e99fdSEd Maste if (cached_time_sleep) {
3275c43e99fdSEd Maste struct timeval delay = { 0, 30*1000 };
3276c43e99fdSEd Maste evutil_usleep_(&delay);
3277c43e99fdSEd Maste }
3278c43e99fdSEd Maste if (cached_time_reset) {
3279c43e99fdSEd Maste event_base_update_cache_time(cached_time_base);
3280c43e99fdSEd Maste }
3281c43e99fdSEd Maste end:
3282c43e99fdSEd Maste ;
3283c43e99fdSEd Maste }
3284c43e99fdSEd Maste
3285c43e99fdSEd Maste static void
test_gettimeofday_cached(void * arg)3286c43e99fdSEd Maste test_gettimeofday_cached(void *arg)
3287c43e99fdSEd Maste {
3288c43e99fdSEd Maste struct basic_test_data *data = arg;
3289c43e99fdSEd Maste struct event_config *cfg = NULL;
3290c43e99fdSEd Maste struct event_base *base = NULL;
3291c43e99fdSEd Maste struct timeval tv1, tv2, tv3, now;
3292c43e99fdSEd Maste struct event *ev1=NULL, *ev2=NULL, *ev3=NULL;
3293c43e99fdSEd Maste int cached_time_disable = strstr(data->setup_data, "disable") != NULL;
3294c43e99fdSEd Maste
3295c43e99fdSEd Maste cfg = event_config_new();
3296c43e99fdSEd Maste if (cached_time_disable) {
3297c43e99fdSEd Maste event_config_set_flag(cfg, EVENT_BASE_FLAG_NO_CACHE_TIME);
3298c43e99fdSEd Maste }
3299c43e99fdSEd Maste cached_time_base = base = event_base_new_with_config(cfg);
3300c43e99fdSEd Maste tt_assert(base);
3301c43e99fdSEd Maste
3302c43e99fdSEd Maste /* Try gettimeofday_cached outside of an event loop. */
3303c43e99fdSEd Maste evutil_gettimeofday(&now, NULL);
3304c43e99fdSEd Maste tt_int_op(0, ==, event_base_gettimeofday_cached(NULL, &tv1));
3305c43e99fdSEd Maste tt_int_op(0, ==, event_base_gettimeofday_cached(base, &tv2));
3306c43e99fdSEd Maste tt_int_op(timeval_msec_diff(&tv1, &tv2), <, 10);
3307c43e99fdSEd Maste tt_int_op(timeval_msec_diff(&tv1, &now), <, 10);
3308c43e99fdSEd Maste
3309c43e99fdSEd Maste cached_time_reset = strstr(data->setup_data, "reset") != NULL;
3310c43e99fdSEd Maste cached_time_sleep = strstr(data->setup_data, "sleep") != NULL;
3311c43e99fdSEd Maste
3312c43e99fdSEd Maste ev1 = event_new(base, -1, 0, cache_time_cb, &tv1);
3313c43e99fdSEd Maste ev2 = event_new(base, -1, 0, cache_time_cb, &tv2);
3314c43e99fdSEd Maste ev3 = event_new(base, -1, 0, cache_time_cb, &tv3);
3315c43e99fdSEd Maste
3316c43e99fdSEd Maste event_active(ev1, EV_TIMEOUT, 1);
3317c43e99fdSEd Maste event_active(ev2, EV_TIMEOUT, 1);
3318c43e99fdSEd Maste event_active(ev3, EV_TIMEOUT, 1);
3319c43e99fdSEd Maste
3320c43e99fdSEd Maste event_base_dispatch(base);
3321c43e99fdSEd Maste
3322c43e99fdSEd Maste if (cached_time_reset && cached_time_sleep) {
3323c43e99fdSEd Maste tt_int_op(labs(timeval_msec_diff(&tv1,&tv2)), >, 10);
3324c43e99fdSEd Maste tt_int_op(labs(timeval_msec_diff(&tv2,&tv3)), >, 10);
3325c43e99fdSEd Maste } else if (cached_time_disable && cached_time_sleep) {
3326c43e99fdSEd Maste tt_int_op(labs(timeval_msec_diff(&tv1,&tv2)), >, 10);
3327c43e99fdSEd Maste tt_int_op(labs(timeval_msec_diff(&tv2,&tv3)), >, 10);
3328c43e99fdSEd Maste } else if (! cached_time_disable) {
3329c43e99fdSEd Maste tt_assert(evutil_timercmp(&tv1, &tv2, ==));
3330c43e99fdSEd Maste tt_assert(evutil_timercmp(&tv2, &tv3, ==));
3331c43e99fdSEd Maste }
3332c43e99fdSEd Maste
3333c43e99fdSEd Maste end:
3334c43e99fdSEd Maste if (ev1)
3335c43e99fdSEd Maste event_free(ev1);
3336c43e99fdSEd Maste if (ev2)
3337c43e99fdSEd Maste event_free(ev2);
3338c43e99fdSEd Maste if (ev3)
3339c43e99fdSEd Maste event_free(ev3);
3340c43e99fdSEd Maste if (base)
3341c43e99fdSEd Maste event_base_free(base);
3342c43e99fdSEd Maste if (cfg)
3343c43e99fdSEd Maste event_config_free(cfg);
3344c43e99fdSEd Maste }
3345c43e99fdSEd Maste
3346c43e99fdSEd Maste static void
tabf_cb(evutil_socket_t fd,short what,void * arg)3347c43e99fdSEd Maste tabf_cb(evutil_socket_t fd, short what, void *arg)
3348c43e99fdSEd Maste {
3349c43e99fdSEd Maste int *ptr = arg;
3350c43e99fdSEd Maste *ptr = what;
3351c43e99fdSEd Maste *ptr += 0x10000;
3352c43e99fdSEd Maste }
3353c43e99fdSEd Maste
3354c43e99fdSEd Maste static void
test_evmap_invalid_slots(void * arg)3355*b50261e2SCy Schubert test_evmap_invalid_slots(void *arg)
3356*b50261e2SCy Schubert {
3357*b50261e2SCy Schubert struct basic_test_data *data = arg;
3358*b50261e2SCy Schubert struct event_base *base = data->base;
3359*b50261e2SCy Schubert struct event *ev1 = NULL, *ev2 = NULL;
3360*b50261e2SCy Schubert int e1, e2;
3361*b50261e2SCy Schubert #ifndef _WIN32
3362*b50261e2SCy Schubert struct event *ev3 = NULL, *ev4 = NULL;
3363*b50261e2SCy Schubert int e3, e4;
3364*b50261e2SCy Schubert #endif
3365*b50261e2SCy Schubert
3366*b50261e2SCy Schubert ev1 = evsignal_new(base, -1, dummy_read_cb, (void *)base);
3367*b50261e2SCy Schubert ev2 = evsignal_new(base, NSIG, dummy_read_cb, (void *)base);
3368*b50261e2SCy Schubert tt_assert(ev1);
3369*b50261e2SCy Schubert tt_assert(ev2);
3370*b50261e2SCy Schubert e1 = event_add(ev1, NULL);
3371*b50261e2SCy Schubert e2 = event_add(ev2, NULL);
3372*b50261e2SCy Schubert tt_int_op(e1, !=, 0);
3373*b50261e2SCy Schubert tt_int_op(e2, !=, 0);
3374*b50261e2SCy Schubert #ifndef _WIN32
3375*b50261e2SCy Schubert ev3 = event_new(base, INT_MAX, EV_READ, dummy_read_cb, (void *)base);
3376*b50261e2SCy Schubert ev4 = event_new(base, INT_MAX / 2, EV_READ, dummy_read_cb, (void *)base);
3377*b50261e2SCy Schubert tt_assert(ev3);
3378*b50261e2SCy Schubert tt_assert(ev4);
3379*b50261e2SCy Schubert e3 = event_add(ev3, NULL);
3380*b50261e2SCy Schubert e4 = event_add(ev4, NULL);
3381*b50261e2SCy Schubert tt_int_op(e3, !=, 0);
3382*b50261e2SCy Schubert tt_int_op(e4, !=, 0);
3383*b50261e2SCy Schubert #endif
3384*b50261e2SCy Schubert
3385*b50261e2SCy Schubert end:
3386*b50261e2SCy Schubert event_free(ev1);
3387*b50261e2SCy Schubert event_free(ev2);
3388*b50261e2SCy Schubert #ifndef _WIN32
3389*b50261e2SCy Schubert event_free(ev3);
3390*b50261e2SCy Schubert event_free(ev4);
3391*b50261e2SCy Schubert #endif
3392*b50261e2SCy Schubert }
3393*b50261e2SCy Schubert
3394*b50261e2SCy Schubert static void
test_active_by_fd(void * arg)3395c43e99fdSEd Maste test_active_by_fd(void *arg)
3396c43e99fdSEd Maste {
3397c43e99fdSEd Maste struct basic_test_data *data = arg;
3398c43e99fdSEd Maste struct event_base *base = data->base;
3399c43e99fdSEd Maste struct event *ev1 = NULL, *ev2 = NULL, *ev3 = NULL, *ev4 = NULL;
3400c43e99fdSEd Maste int e1,e2,e3,e4;
3401c43e99fdSEd Maste #ifndef _WIN32
3402c43e99fdSEd Maste struct event *evsig = NULL;
3403c43e99fdSEd Maste int es;
3404c43e99fdSEd Maste #endif
3405c43e99fdSEd Maste struct timeval tenmin = { 600, 0 };
3406c43e99fdSEd Maste
3407c43e99fdSEd Maste /* Ensure no crash on nonexistent FD. */
3408c43e99fdSEd Maste event_base_active_by_fd(base, 1000, EV_READ);
3409c43e99fdSEd Maste
3410c43e99fdSEd Maste /* Ensure no crash on bogus FD. */
3411c43e99fdSEd Maste event_base_active_by_fd(base, -1, EV_READ);
3412c43e99fdSEd Maste
3413c43e99fdSEd Maste /* Ensure no crash on nonexistent/bogus signal. */
3414c43e99fdSEd Maste event_base_active_by_signal(base, 1000);
3415c43e99fdSEd Maste event_base_active_by_signal(base, -1);
3416c43e99fdSEd Maste
3417c43e99fdSEd Maste event_base_assert_ok_(base);
3418c43e99fdSEd Maste
3419c43e99fdSEd Maste e1 = e2 = e3 = e4 = 0;
3420c43e99fdSEd Maste ev1 = event_new(base, data->pair[0], EV_READ, tabf_cb, &e1);
3421c43e99fdSEd Maste ev2 = event_new(base, data->pair[0], EV_WRITE, tabf_cb, &e2);
3422c43e99fdSEd Maste ev3 = event_new(base, data->pair[1], EV_READ, tabf_cb, &e3);
3423c43e99fdSEd Maste ev4 = event_new(base, data->pair[1], EV_READ, tabf_cb, &e4);
3424c43e99fdSEd Maste tt_assert(ev1);
3425c43e99fdSEd Maste tt_assert(ev2);
3426c43e99fdSEd Maste tt_assert(ev3);
3427c43e99fdSEd Maste tt_assert(ev4);
3428c43e99fdSEd Maste #ifndef _WIN32
3429c43e99fdSEd Maste evsig = event_new(base, SIGHUP, EV_SIGNAL, tabf_cb, &es);
3430c43e99fdSEd Maste tt_assert(evsig);
3431c43e99fdSEd Maste event_add(evsig, &tenmin);
3432c43e99fdSEd Maste #endif
3433c43e99fdSEd Maste
3434c43e99fdSEd Maste event_add(ev1, &tenmin);
3435c43e99fdSEd Maste event_add(ev2, NULL);
3436c43e99fdSEd Maste event_add(ev3, NULL);
3437c43e99fdSEd Maste event_add(ev4, &tenmin);
3438c43e99fdSEd Maste
3439c43e99fdSEd Maste
3440c43e99fdSEd Maste event_base_assert_ok_(base);
3441c43e99fdSEd Maste
3442c43e99fdSEd Maste /* Trigger 2, 3, 4 */
3443c43e99fdSEd Maste event_base_active_by_fd(base, data->pair[0], EV_WRITE);
3444c43e99fdSEd Maste event_base_active_by_fd(base, data->pair[1], EV_READ);
3445*b50261e2SCy Schubert event_base_active_by_fd(base, data->pair[1], EV_TIMEOUT);
3446c43e99fdSEd Maste #ifndef _WIN32
3447c43e99fdSEd Maste event_base_active_by_signal(base, SIGHUP);
3448c43e99fdSEd Maste #endif
3449c43e99fdSEd Maste
3450c43e99fdSEd Maste event_base_assert_ok_(base);
3451c43e99fdSEd Maste
3452c43e99fdSEd Maste event_base_loop(base, EVLOOP_ONCE);
3453c43e99fdSEd Maste
3454c43e99fdSEd Maste tt_int_op(e1, ==, 0);
3455c43e99fdSEd Maste tt_int_op(e2, ==, EV_WRITE | 0x10000);
3456c43e99fdSEd Maste tt_int_op(e3, ==, EV_READ | 0x10000);
3457c43e99fdSEd Maste /* Mask out EV_WRITE here, since it could be genuinely writeable. */
3458*b50261e2SCy Schubert tt_int_op((e4 & ~EV_WRITE), ==, EV_READ | EV_TIMEOUT | 0x10000);
3459c43e99fdSEd Maste #ifndef _WIN32
3460c43e99fdSEd Maste tt_int_op(es, ==, EV_SIGNAL | 0x10000);
3461c43e99fdSEd Maste #endif
3462c43e99fdSEd Maste
3463c43e99fdSEd Maste end:
3464c43e99fdSEd Maste if (ev1)
3465c43e99fdSEd Maste event_free(ev1);
3466c43e99fdSEd Maste if (ev2)
3467c43e99fdSEd Maste event_free(ev2);
3468c43e99fdSEd Maste if (ev3)
3469c43e99fdSEd Maste event_free(ev3);
3470c43e99fdSEd Maste if (ev4)
3471c43e99fdSEd Maste event_free(ev4);
3472c43e99fdSEd Maste #ifndef _WIN32
3473c43e99fdSEd Maste if (evsig)
3474c43e99fdSEd Maste event_free(evsig);
3475c43e99fdSEd Maste #endif
3476c43e99fdSEd Maste }
3477c43e99fdSEd Maste
3478c43e99fdSEd Maste struct testcase_t main_testcases[] = {
3479c43e99fdSEd Maste /* Some converted-over tests */
3480c43e99fdSEd Maste { "methods", test_methods, TT_FORK, NULL, NULL },
3481c43e99fdSEd Maste { "version", test_version, 0, NULL, NULL },
3482c43e99fdSEd Maste BASIC(base_features, TT_FORK|TT_NO_LOGS),
3483c43e99fdSEd Maste { "base_environ", test_base_environ, TT_FORK, NULL, NULL },
3484c43e99fdSEd Maste
3485c43e99fdSEd Maste BASIC(event_base_new, TT_FORK|TT_NEED_SOCKETPAIR),
3486c43e99fdSEd Maste BASIC(free_active_base, TT_FORK|TT_NEED_SOCKETPAIR),
3487c43e99fdSEd Maste
3488c43e99fdSEd Maste BASIC(manipulate_active_events, TT_FORK|TT_NEED_BASE),
3489c43e99fdSEd Maste BASIC(event_new_selfarg, TT_FORK|TT_NEED_BASE),
3490c43e99fdSEd Maste BASIC(event_assign_selfarg, TT_FORK|TT_NEED_BASE),
3491c43e99fdSEd Maste BASIC(event_base_get_num_events, TT_FORK|TT_NEED_BASE),
3492c43e99fdSEd Maste BASIC(event_base_get_max_events, TT_FORK|TT_NEED_BASE),
3493*b50261e2SCy Schubert BASIC(evmap_invalid_slots, TT_FORK|TT_NEED_BASE),
3494c43e99fdSEd Maste
3495c43e99fdSEd Maste BASIC(bad_assign, TT_FORK|TT_NEED_BASE|TT_NO_LOGS),
3496c43e99fdSEd Maste BASIC(bad_reentrant, TT_FORK|TT_NEED_BASE|TT_NO_LOGS),
3497*b50261e2SCy Schubert BASIC(active_later, TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR|TT_RETRIABLE),
3498c43e99fdSEd Maste BASIC(event_remove_timeout, TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR),
3499c43e99fdSEd Maste
3500c43e99fdSEd Maste /* These are still using the old API */
3501c43e99fdSEd Maste LEGACY(persistent_timeout, TT_FORK|TT_NEED_BASE),
3502c43e99fdSEd Maste { "persistent_timeout_jump", test_persistent_timeout_jump, TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
3503c43e99fdSEd Maste { "persistent_active_timeout", test_persistent_active_timeout,
3504*b50261e2SCy Schubert TT_FORK|TT_NEED_BASE|TT_RETRIABLE, &basic_setup, NULL },
3505c43e99fdSEd Maste LEGACY(priorities, TT_FORK|TT_NEED_BASE),
3506c43e99fdSEd Maste BASIC(priority_active_inversion, TT_FORK|TT_NEED_BASE),
3507c43e99fdSEd Maste { "common_timeout", test_common_timeout, TT_FORK|TT_NEED_BASE,
3508c43e99fdSEd Maste &basic_setup, NULL },
3509c43e99fdSEd Maste
3510c43e99fdSEd Maste /* These legacy tests may not all need all of these flags. */
3511c43e99fdSEd Maste LEGACY(simpleread, TT_ISOLATED),
3512c43e99fdSEd Maste LEGACY(simpleread_multiple, TT_ISOLATED),
3513c43e99fdSEd Maste LEGACY(simplewrite, TT_ISOLATED),
3514*b50261e2SCy Schubert { "simpleclose_rw", test_simpleclose_rw, TT_FORK, &basic_setup, NULL },
3515*b50261e2SCy Schubert /* simpleclose */
3516*b50261e2SCy Schubert { "simpleclose_close", test_simpleclose,
3517*b50261e2SCy Schubert TT_FORK|TT_NEED_SOCKETPAIR|TT_NEED_BASE,
3518*b50261e2SCy Schubert &basic_setup, (void *)"close" },
3519*b50261e2SCy Schubert { "simpleclose_shutdown", test_simpleclose,
3520*b50261e2SCy Schubert TT_FORK|TT_NEED_SOCKETPAIR|TT_NEED_BASE,
3521*b50261e2SCy Schubert &basic_setup, (void *)"shutdown" },
3522*b50261e2SCy Schubert /* simpleclose_*_persist */
3523*b50261e2SCy Schubert { "simpleclose_close_persist", test_simpleclose,
3524*b50261e2SCy Schubert TT_FORK|TT_NEED_SOCKETPAIR|TT_NEED_BASE,
3525*b50261e2SCy Schubert &basic_setup, (void *)"close_persist" },
3526*b50261e2SCy Schubert { "simpleclose_shutdown_persist", test_simpleclose,
3527*b50261e2SCy Schubert TT_FORK|TT_NEED_SOCKETPAIR|TT_NEED_BASE,
3528*b50261e2SCy Schubert &basic_setup, (void *)"shutdown_persist" },
3529*b50261e2SCy Schubert /* simpleclose_*_et */
3530*b50261e2SCy Schubert { "simpleclose_close_et", test_simpleclose,
3531*b50261e2SCy Schubert TT_FORK|TT_NEED_SOCKETPAIR|TT_NEED_BASE,
3532*b50261e2SCy Schubert &basic_setup, (void *)"close_ET" },
3533*b50261e2SCy Schubert { "simpleclose_shutdown_et", test_simpleclose,
3534*b50261e2SCy Schubert TT_FORK|TT_NEED_SOCKETPAIR|TT_NEED_BASE,
3535*b50261e2SCy Schubert &basic_setup, (void *)"shutdown_ET" },
3536*b50261e2SCy Schubert /* simpleclose_*_persist_et */
3537*b50261e2SCy Schubert { "simpleclose_close_persist_et", test_simpleclose,
3538*b50261e2SCy Schubert TT_FORK|TT_NEED_SOCKETPAIR|TT_NEED_BASE,
3539*b50261e2SCy Schubert &basic_setup, (void *)"close_persist_ET" },
3540*b50261e2SCy Schubert { "simpleclose_shutdown_persist_et", test_simpleclose,
3541*b50261e2SCy Schubert TT_FORK|TT_NEED_SOCKETPAIR|TT_NEED_BASE,
3542*b50261e2SCy Schubert &basic_setup, (void *)"shutdown_persist_ET" },
3543c43e99fdSEd Maste LEGACY(multiple, TT_ISOLATED),
3544c43e99fdSEd Maste LEGACY(persistent, TT_ISOLATED),
3545c43e99fdSEd Maste LEGACY(combined, TT_ISOLATED),
3546c43e99fdSEd Maste LEGACY(simpletimeout, TT_ISOLATED),
3547c43e99fdSEd Maste LEGACY(loopbreak, TT_ISOLATED),
3548c43e99fdSEd Maste LEGACY(loopexit, TT_ISOLATED),
3549c43e99fdSEd Maste LEGACY(loopexit_multiple, TT_ISOLATED),
3550*b50261e2SCy Schubert { "nonpersist_readd", test_nonpersist_readd, TT_FORK|TT_NEED_SOCKETPAIR|TT_NEED_BASE, &basic_setup, NULL },
3551c43e99fdSEd Maste LEGACY(multiple_events_for_same_fd, TT_ISOLATED),
3552c43e99fdSEd Maste LEGACY(want_only_once, TT_ISOLATED),
3553c43e99fdSEd Maste { "event_once", test_event_once, TT_ISOLATED, &basic_setup, NULL },
3554c43e99fdSEd Maste { "event_once_never", test_event_once_never, TT_ISOLATED, &basic_setup, NULL },
3555c43e99fdSEd Maste { "event_pending", test_event_pending, TT_ISOLATED, &basic_setup,
3556c43e99fdSEd Maste NULL },
3557c43e99fdSEd Maste { "event_closed_fd_poll", test_event_closed_fd_poll, TT_ISOLATED, &basic_setup,
3558c43e99fdSEd Maste NULL },
3559c43e99fdSEd Maste
3560c43e99fdSEd Maste #ifndef _WIN32
3561c43e99fdSEd Maste { "dup_fd", test_dup_fd, TT_ISOLATED, &basic_setup, NULL },
3562c43e99fdSEd Maste #endif
3563c43e99fdSEd Maste { "mm_functions", test_mm_functions, TT_FORK, NULL, NULL },
3564c43e99fdSEd Maste { "many_events", test_many_events, TT_ISOLATED, &basic_setup, NULL },
3565c43e99fdSEd Maste { "many_events_slow_add", test_many_events, TT_ISOLATED, &basic_setup, (void*)1 },
3566c43e99fdSEd Maste
3567c43e99fdSEd Maste { "struct_event_size", test_struct_event_size, 0, NULL, NULL },
3568c43e99fdSEd Maste BASIC(get_assignment, TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR),
3569c43e99fdSEd Maste
3570c43e99fdSEd Maste BASIC(event_foreach, TT_FORK|TT_NEED_BASE),
3571c43e99fdSEd Maste { "gettimeofday_cached", test_gettimeofday_cached, TT_FORK, &basic_setup, (void*)"" },
3572c43e99fdSEd Maste { "gettimeofday_cached_sleep", test_gettimeofday_cached, TT_FORK, &basic_setup, (void*)"sleep" },
3573c43e99fdSEd Maste { "gettimeofday_cached_reset", test_gettimeofday_cached, TT_FORK, &basic_setup, (void*)"sleep reset" },
3574c43e99fdSEd Maste { "gettimeofday_cached_disabled", test_gettimeofday_cached, TT_FORK, &basic_setup, (void*)"sleep disable" },
3575c43e99fdSEd Maste { "gettimeofday_cached_disabled_nosleep", test_gettimeofday_cached, TT_FORK, &basic_setup, (void*)"disable" },
3576c43e99fdSEd Maste
3577c43e99fdSEd Maste BASIC(active_by_fd, TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR),
3578c43e99fdSEd Maste
3579c43e99fdSEd Maste #ifndef _WIN32
3580c43e99fdSEd Maste LEGACY(fork, TT_ISOLATED),
3581c43e99fdSEd Maste #endif
3582*b50261e2SCy Schubert
3583*b50261e2SCy Schubert #ifdef EVTHREAD_USE_PTHREADS_IMPLEMENTED
3584*b50261e2SCy Schubert LEGACY(del_wait, TT_ISOLATED|TT_NEED_THREADS|TT_RETRIABLE),
3585*b50261e2SCy Schubert LEGACY(del_notify, TT_ISOLATED|TT_NEED_THREADS),
3586c43e99fdSEd Maste #endif
3587c43e99fdSEd Maste
3588c43e99fdSEd Maste END_OF_TESTCASES
3589c43e99fdSEd Maste };
3590c43e99fdSEd Maste
3591c43e99fdSEd Maste struct testcase_t evtag_testcases[] = {
3592c43e99fdSEd Maste { "int", evtag_int_test, TT_FORK, NULL, NULL },
3593c43e99fdSEd Maste { "fuzz", evtag_fuzz, TT_FORK, NULL, NULL },
3594c43e99fdSEd Maste { "encoding", evtag_tag_encoding, TT_FORK, NULL, NULL },
3595c43e99fdSEd Maste { "peek", evtag_test_peek, 0, NULL, NULL },
3596c43e99fdSEd Maste
3597c43e99fdSEd Maste END_OF_TESTCASES
3598c43e99fdSEd Maste };
3599c43e99fdSEd Maste
3600c43e99fdSEd Maste struct testcase_t signal_testcases[] = {
3601c43e99fdSEd Maste #ifndef _WIN32
3602c43e99fdSEd Maste LEGACY(simplestsignal, TT_ISOLATED),
3603c43e99fdSEd Maste LEGACY(simplesignal, TT_ISOLATED),
3604c43e99fdSEd Maste LEGACY(multiplesignal, TT_ISOLATED),
3605c43e99fdSEd Maste LEGACY(immediatesignal, TT_ISOLATED),
3606c43e99fdSEd Maste LEGACY(signal_dealloc, TT_ISOLATED),
3607c43e99fdSEd Maste LEGACY(signal_pipeloss, TT_ISOLATED),
3608c43e99fdSEd Maste LEGACY(signal_switchbase, TT_ISOLATED|TT_NO_LOGS),
3609c43e99fdSEd Maste LEGACY(signal_restore, TT_ISOLATED),
3610c43e99fdSEd Maste LEGACY(signal_assert, TT_ISOLATED),
3611c43e99fdSEd Maste LEGACY(signal_while_processing, TT_ISOLATED),
3612c43e99fdSEd Maste #endif
3613c43e99fdSEd Maste END_OF_TESTCASES
3614c43e99fdSEd Maste };
3615c43e99fdSEd Maste
3616