2 * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "../util-internal.h"
27 #include "event2/event-config.h"
32 #include <sys/types.h>
34 #ifdef _EVENT_HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
47 #include "event2/event.h"
48 #include "event2/util.h"
52 static int was_et
= 0;
55 read_cb(evutil_socket_t fd
, short event
, void *arg
)
60 len
= recv(fd
, &buf
, sizeof(buf
), 0);
75 #define LOCAL_SOCKETPAIR_AF AF_INET
77 #define LOCAL_SOCKETPAIR_AF AF_UNIX
81 test_edgetriggered(void *et
)
83 struct event
*ev
= NULL
;
84 struct event_base
*base
= NULL
;
85 const char *test
= "test string";
86 evutil_socket_t pair
[2] = {-1,-1};
89 /* On Linux 3.2.1 (at least, as patched by Fedora and tested by Nick),
90 * doing a "recv" on an AF_UNIX socket resets the readability of the
91 * socket, even though there is no state change, so we don't actually
92 * get edge-triggered behavior. Yuck! Linux 3.1.9 didn't have this
96 if (evutil_ersatz_socketpair(AF_INET
, SOCK_STREAM
, 0, pair
) == -1) {
97 tt_abort_perror("socketpair");
100 if (evutil_socketpair(LOCAL_SOCKETPAIR_AF
, SOCK_STREAM
, 0, pair
) == -1) {
101 tt_abort_perror("socketpair");
107 tt_int_op(send(pair
[0], test
, (int)strlen(test
)+1, 0), >, 0);
108 shutdown(pair
[0], SHUT_WR
);
110 /* Initalize the event library */
111 base
= event_base_new();
113 if (!strcmp(event_base_get_method(base
), "epoll") ||
114 !strcmp(event_base_get_method(base
), "epoll (with changelist)") ||
115 !strcmp(event_base_get_method(base
), "kqueue"))
120 TT_BLATHER(("Checking for edge-triggered events with %s, which should %s"
121 "support edge-triggering", event_base_get_method(base
),
122 supports_et
?"":"not "));
124 /* Initalize one event */
125 ev
= event_new(base
, pair
[1], EV_READ
|EV_ET
|EV_PERSIST
, read_cb
, &ev
);
129 /* We're going to call the dispatch function twice. The first invocation
130 * will read a single byte from pair[1] in either case. If we're edge
131 * triggered, we'll only see the event once (since we only see transitions
132 * from no data to data), so the second invocation of event_base_loop will
133 * do nothing. If we're level triggered, the second invocation of
134 * event_base_loop will also activate the event (because there's still
136 event_base_loop(base
,EVLOOP_NONBLOCK
|EVLOOP_ONCE
);
137 event_base_loop(base
,EVLOOP_NONBLOCK
|EVLOOP_ONCE
);
140 tt_int_op(called
, ==, 1);
143 tt_int_op(called
, ==, 2);
153 event_base_free(base
);
154 evutil_closesocket(pair
[0]);
155 evutil_closesocket(pair
[1]);
159 test_edgetriggered_mix_error(void *data_
)
161 struct basic_test_data
*data
= data_
;
162 struct event_base
*base
= NULL
;
163 struct event
*ev_et
=NULL
, *ev_lt
=NULL
;
165 #ifdef _EVENT_DISABLE_DEBUG_MODE
170 event_enable_debug_mode();
172 base
= event_base_new();
174 /* try mixing edge-triggered and level-triggered to make sure it fails*/
175 ev_et
= event_new(base
, data
->pair
[0], EV_READ
|EV_ET
, read_cb
, ev_et
);
177 ev_lt
= event_new(base
, data
->pair
[0], EV_READ
, read_cb
, ev_lt
);
180 /* Add edge-triggered, then level-triggered. Get an error. */
181 tt_int_op(0, ==, event_add(ev_et
, NULL
));
182 tt_int_op(-1, ==, event_add(ev_lt
, NULL
));
183 tt_int_op(EV_READ
, ==, event_pending(ev_et
, EV_READ
, NULL
));
184 tt_int_op(0, ==, event_pending(ev_lt
, EV_READ
, NULL
));
186 tt_int_op(0, ==, event_del(ev_et
));
187 /* Add level-triggered, then edge-triggered. Get an error. */
188 tt_int_op(0, ==, event_add(ev_lt
, NULL
));
189 tt_int_op(-1, ==, event_add(ev_et
, NULL
));
190 tt_int_op(EV_READ
, ==, event_pending(ev_lt
, EV_READ
, NULL
));
191 tt_int_op(0, ==, event_pending(ev_et
, EV_READ
, NULL
));
199 event_base_free(base
);
202 struct testcase_t edgetriggered_testcases
[] = {
203 { "et", test_edgetriggered
, TT_FORK
, NULL
, NULL
},
204 { "et_mix_error", test_edgetriggered_mix_error
,
205 TT_FORK
|TT_NEED_SOCKETPAIR
|TT_NO_LOGS
, &basic_setup
, NULL
},