xref: /freebsd/contrib/libevent/test/regress_et.c (revision b50261e21f39a6c7249a49e7b60aa878c98512a8)
1c43e99fdSEd Maste /*
2c43e99fdSEd Maste  * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
3c43e99fdSEd Maste  *
4c43e99fdSEd Maste  * Redistribution and use in source and binary forms, with or without
5c43e99fdSEd Maste  * modification, are permitted provided that the following conditions
6c43e99fdSEd Maste  * are met:
7c43e99fdSEd Maste  * 1. Redistributions of source code must retain the above copyright
8c43e99fdSEd Maste  *    notice, this list of conditions and the following disclaimer.
9c43e99fdSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
10c43e99fdSEd Maste  *    notice, this list of conditions and the following disclaimer in the
11c43e99fdSEd Maste  *    documentation and/or other materials provided with the distribution.
12c43e99fdSEd Maste  * 3. The name of the author may not be used to endorse or promote products
13c43e99fdSEd Maste  *    derived from this software without specific prior written permission.
14c43e99fdSEd Maste  *
15c43e99fdSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16c43e99fdSEd Maste  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17c43e99fdSEd Maste  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18c43e99fdSEd Maste  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19c43e99fdSEd Maste  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20c43e99fdSEd Maste  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21c43e99fdSEd Maste  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22c43e99fdSEd Maste  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23c43e99fdSEd Maste  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24c43e99fdSEd Maste  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25c43e99fdSEd Maste  */
26c43e99fdSEd Maste #include "../util-internal.h"
27c43e99fdSEd Maste #include "event2/event-config.h"
28c43e99fdSEd Maste 
29c43e99fdSEd Maste #ifdef _WIN32
30c43e99fdSEd Maste #include <winsock2.h>
31c43e99fdSEd Maste #endif
32c43e99fdSEd Maste #include <sys/types.h>
33c43e99fdSEd Maste #include <sys/stat.h>
34c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_SOCKET_H
35c43e99fdSEd Maste #include <sys/socket.h>
36c43e99fdSEd Maste #endif
37c43e99fdSEd Maste #include <fcntl.h>
38c43e99fdSEd Maste #include <stdlib.h>
39c43e99fdSEd Maste #include <stdio.h>
40c43e99fdSEd Maste #include <string.h>
41c43e99fdSEd Maste #ifndef _WIN32
42c43e99fdSEd Maste #include <sys/time.h>
43c43e99fdSEd Maste #include <unistd.h>
44c43e99fdSEd Maste #endif
45c43e99fdSEd Maste #include <errno.h>
46c43e99fdSEd Maste 
47c43e99fdSEd Maste #include "event2/event.h"
48c43e99fdSEd Maste #include "event2/util.h"
49c43e99fdSEd Maste 
50c43e99fdSEd Maste #include "regress.h"
51c43e99fdSEd Maste 
52c43e99fdSEd Maste static int was_et = 0;
53c43e99fdSEd Maste 
base_supports_et(struct event_base * base)54*b50261e2SCy Schubert static int base_supports_et(struct event_base *base)
55*b50261e2SCy Schubert {
56*b50261e2SCy Schubert 	return
57*b50261e2SCy Schubert 		(!strcmp(event_base_get_method(base), "epoll") ||
58*b50261e2SCy Schubert 		!strcmp(event_base_get_method(base), "epoll (with changelist)") ||
59*b50261e2SCy Schubert 		!strcmp(event_base_get_method(base), "kqueue"));
60*b50261e2SCy Schubert }
61*b50261e2SCy Schubert 
62c43e99fdSEd Maste static void
read_cb(evutil_socket_t fd,short event,void * arg)63c43e99fdSEd Maste read_cb(evutil_socket_t fd, short event, void *arg)
64c43e99fdSEd Maste {
65c43e99fdSEd Maste 	char buf;
66c43e99fdSEd Maste 	int len;
67c43e99fdSEd Maste 
68c43e99fdSEd Maste 	len = recv(fd, &buf, sizeof(buf), 0);
69c43e99fdSEd Maste 
70c43e99fdSEd Maste 	called++;
71c43e99fdSEd Maste 	if (event & EV_ET)
72c43e99fdSEd Maste 		was_et = 1;
73c43e99fdSEd Maste 
74c43e99fdSEd Maste 	if (!len)
75c43e99fdSEd Maste 		event_del(arg);
76c43e99fdSEd Maste }
77c43e99fdSEd Maste 
78c43e99fdSEd Maste static void
test_edgetriggered(void * data_)79*b50261e2SCy Schubert test_edgetriggered(void *data_)
80c43e99fdSEd Maste {
81*b50261e2SCy Schubert 	struct basic_test_data *data = data_;
82*b50261e2SCy Schubert 	struct event_base *base = data->base;
83*b50261e2SCy Schubert 	evutil_socket_t *pair = data->pair;
84c43e99fdSEd Maste 	struct event *ev = NULL;
85c43e99fdSEd Maste 	const char *test = "test string";
86c43e99fdSEd Maste 	int supports_et;
87c43e99fdSEd Maste 
88c43e99fdSEd Maste 	/* On Linux 3.2.1 (at least, as patched by Fedora and tested by Nick),
89c43e99fdSEd Maste 	 * doing a "recv" on an AF_UNIX socket resets the readability of the
90c43e99fdSEd Maste 	 * socket, even though there is no state change, so we don't actually
91c43e99fdSEd Maste 	 * get edge-triggered behavior.  Yuck!  Linux 3.1.9 didn't have this
92c43e99fdSEd Maste 	 * problem.
93c43e99fdSEd Maste 	 */
94c43e99fdSEd Maste 
95c43e99fdSEd Maste 	called = was_et = 0;
96c43e99fdSEd Maste 
97c43e99fdSEd Maste 	tt_int_op(send(pair[0], test, (int)strlen(test)+1, 0), >, 0);
98*b50261e2SCy Schubert 	tt_int_op(shutdown(pair[0], EVUTIL_SHUT_WR), ==, 0);
99c43e99fdSEd Maste 
100*b50261e2SCy Schubert 	supports_et = base_supports_et(base);
101c43e99fdSEd Maste 	TT_BLATHER(("Checking for edge-triggered events with %s, which should %s"
102c43e99fdSEd Maste 				"support edge-triggering", event_base_get_method(base),
103c43e99fdSEd Maste 				supports_et?"":"not "));
104c43e99fdSEd Maste 
105*b50261e2SCy Schubert 	/* Initialize one event */
106c43e99fdSEd Maste 	ev = event_new(base, pair[1], EV_READ|EV_ET|EV_PERSIST, read_cb, &ev);
107*b50261e2SCy Schubert 	tt_assert(ev != NULL);
108*b50261e2SCy Schubert 	tt_int_op(event_add(ev, NULL), ==, 0);
109c43e99fdSEd Maste 
110c43e99fdSEd Maste 	/* We're going to call the dispatch function twice.  The first invocation
111c43e99fdSEd Maste 	 * will read a single byte from pair[1] in either case.  If we're edge
112c43e99fdSEd Maste 	 * triggered, we'll only see the event once (since we only see transitions
113c43e99fdSEd Maste 	 * from no data to data), so the second invocation of event_base_loop will
114c43e99fdSEd Maste 	 * do nothing.  If we're level triggered, the second invocation of
115c43e99fdSEd Maste 	 * event_base_loop will also activate the event (because there's still
116c43e99fdSEd Maste 	 * data to read). */
117*b50261e2SCy Schubert 	tt_int_op(event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE), ==, 0);
118*b50261e2SCy Schubert 	tt_int_op(event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE), ==, 0);
119c43e99fdSEd Maste 
120c43e99fdSEd Maste 	if (supports_et) {
121c43e99fdSEd Maste 		tt_int_op(called, ==, 1);
122c43e99fdSEd Maste 		tt_assert(was_et);
123c43e99fdSEd Maste 	} else {
124c43e99fdSEd Maste 		tt_int_op(called, ==, 2);
125c43e99fdSEd Maste 		tt_assert(!was_et);
126c43e99fdSEd Maste 	}
127c43e99fdSEd Maste 
128c43e99fdSEd Maste end:
129c43e99fdSEd Maste 	if (ev) {
130c43e99fdSEd Maste 		event_del(ev);
131c43e99fdSEd Maste 		event_free(ev);
132c43e99fdSEd Maste 	}
133c43e99fdSEd Maste }
134c43e99fdSEd Maste 
135c43e99fdSEd Maste static void
test_edgetriggered_mix_error(void * data_)136c43e99fdSEd Maste test_edgetriggered_mix_error(void *data_)
137c43e99fdSEd Maste {
138c43e99fdSEd Maste 	struct basic_test_data *data = data_;
139c43e99fdSEd Maste 	struct event_base *base = NULL;
140c43e99fdSEd Maste 	struct event *ev_et=NULL, *ev_lt=NULL;
141c43e99fdSEd Maste 
142c43e99fdSEd Maste #ifdef EVENT__DISABLE_DEBUG_MODE
143c43e99fdSEd Maste 	if (1)
144c43e99fdSEd Maste 		tt_skip();
145c43e99fdSEd Maste #endif
146c43e99fdSEd Maste 
147c43e99fdSEd Maste 	if (!libevent_tests_running_in_debug_mode)
148c43e99fdSEd Maste 		event_enable_debug_mode();
149c43e99fdSEd Maste 
150c43e99fdSEd Maste 	base = event_base_new();
151c43e99fdSEd Maste 
152c43e99fdSEd Maste 	/* try mixing edge-triggered and level-triggered to make sure it fails*/
153c43e99fdSEd Maste 	ev_et = event_new(base, data->pair[0], EV_READ|EV_ET, read_cb, ev_et);
154c43e99fdSEd Maste 	tt_assert(ev_et);
155c43e99fdSEd Maste 	ev_lt = event_new(base, data->pair[0], EV_READ, read_cb, ev_lt);
156c43e99fdSEd Maste 	tt_assert(ev_lt);
157c43e99fdSEd Maste 
158c43e99fdSEd Maste 	/* Add edge-triggered, then level-triggered.  Get an error. */
159c43e99fdSEd Maste 	tt_int_op(0, ==, event_add(ev_et, NULL));
160c43e99fdSEd Maste 	tt_int_op(-1, ==, event_add(ev_lt, NULL));
161c43e99fdSEd Maste 	tt_int_op(EV_READ, ==, event_pending(ev_et, EV_READ, NULL));
162c43e99fdSEd Maste 	tt_int_op(0, ==, event_pending(ev_lt, EV_READ, NULL));
163c43e99fdSEd Maste 
164c43e99fdSEd Maste 	tt_int_op(0, ==, event_del(ev_et));
165c43e99fdSEd Maste 	/* Add level-triggered, then edge-triggered.  Get an error. */
166c43e99fdSEd Maste 	tt_int_op(0, ==, event_add(ev_lt, NULL));
167c43e99fdSEd Maste 	tt_int_op(-1, ==, event_add(ev_et, NULL));
168c43e99fdSEd Maste 	tt_int_op(EV_READ, ==, event_pending(ev_lt, EV_READ, NULL));
169c43e99fdSEd Maste 	tt_int_op(0, ==, event_pending(ev_et, EV_READ, NULL));
170c43e99fdSEd Maste 
171c43e99fdSEd Maste end:
172c43e99fdSEd Maste 	if (ev_et)
173c43e99fdSEd Maste 		event_free(ev_et);
174c43e99fdSEd Maste 	if (ev_lt)
175c43e99fdSEd Maste 		event_free(ev_lt);
176c43e99fdSEd Maste 	if (base)
177c43e99fdSEd Maste 		event_base_free(base);
178c43e99fdSEd Maste }
179c43e99fdSEd Maste 
180*b50261e2SCy Schubert static int read_notification_count;
181*b50261e2SCy Schubert static int last_read_notification_was_et;
182*b50261e2SCy Schubert static void
read_notification_cb(evutil_socket_t fd,short event,void * arg)183*b50261e2SCy Schubert read_notification_cb(evutil_socket_t fd, short event, void *arg)
184*b50261e2SCy Schubert {
185*b50261e2SCy Schubert 	read_notification_count++;
186*b50261e2SCy Schubert 	last_read_notification_was_et = (event & EV_ET);
187*b50261e2SCy Schubert }
188*b50261e2SCy Schubert 
189*b50261e2SCy Schubert static int write_notification_count;
190*b50261e2SCy Schubert static int last_write_notification_was_et;
191*b50261e2SCy Schubert static void
write_notification_cb(evutil_socket_t fd,short event,void * arg)192*b50261e2SCy Schubert write_notification_cb(evutil_socket_t fd, short event, void *arg)
193*b50261e2SCy Schubert {
194*b50261e2SCy Schubert 	write_notification_count++;
195*b50261e2SCy Schubert 	last_write_notification_was_et = (event & EV_ET);
196*b50261e2SCy Schubert }
197*b50261e2SCy Schubert 
198*b50261e2SCy Schubert /* After two or more events have been registered for the same
199*b50261e2SCy Schubert  * file descriptor using EV_ET, if one of the events is
200*b50261e2SCy Schubert  * deleted, then the epoll_ctl() call issued by libevent drops
201*b50261e2SCy Schubert  * the EPOLLET flag resulting in level triggered
202*b50261e2SCy Schubert  * notifications.
203*b50261e2SCy Schubert  */
204*b50261e2SCy Schubert static void
test_edge_triggered_multiple_events(void * data_)205*b50261e2SCy Schubert test_edge_triggered_multiple_events(void *data_)
206*b50261e2SCy Schubert {
207*b50261e2SCy Schubert 	struct basic_test_data *data = data_;
208*b50261e2SCy Schubert 	struct event *read_ev = NULL;
209*b50261e2SCy Schubert 	struct event *write_ev = NULL;
210*b50261e2SCy Schubert 	const char c = 'A';
211*b50261e2SCy Schubert 	struct event_base *base = data->base;
212*b50261e2SCy Schubert 	evutil_socket_t *pair = data->pair;
213*b50261e2SCy Schubert 
214*b50261e2SCy Schubert 	if (!base_supports_et(base)) {
215*b50261e2SCy Schubert 		tt_skip();
216*b50261e2SCy Schubert 		return;
217*b50261e2SCy Schubert 	}
218*b50261e2SCy Schubert 
219*b50261e2SCy Schubert 	read_notification_count = 0;
220*b50261e2SCy Schubert 	last_read_notification_was_et = 0;
221*b50261e2SCy Schubert 	write_notification_count = 0;
222*b50261e2SCy Schubert 	last_write_notification_was_et = 0;
223*b50261e2SCy Schubert 
224*b50261e2SCy Schubert 	/* Make pair[1] readable */
225*b50261e2SCy Schubert 	tt_int_op(send(pair[0], &c, 1, 0), >, 0);
226*b50261e2SCy Schubert 
227*b50261e2SCy Schubert 	read_ev = event_new(base, pair[1], EV_READ|EV_ET|EV_PERSIST,
228*b50261e2SCy Schubert 		read_notification_cb, NULL);
229*b50261e2SCy Schubert 	write_ev = event_new(base, pair[1], EV_WRITE|EV_ET|EV_PERSIST,
230*b50261e2SCy Schubert 		write_notification_cb, NULL);
231*b50261e2SCy Schubert 
232*b50261e2SCy Schubert 	event_add(read_ev, NULL);
233*b50261e2SCy Schubert 	event_add(write_ev, NULL);
234*b50261e2SCy Schubert 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
235*b50261e2SCy Schubert 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
236*b50261e2SCy Schubert 
237*b50261e2SCy Schubert 	tt_assert(last_read_notification_was_et);
238*b50261e2SCy Schubert 	tt_int_op(read_notification_count, ==, 1);
239*b50261e2SCy Schubert 	tt_assert(last_write_notification_was_et);
240*b50261e2SCy Schubert 	tt_int_op(write_notification_count, ==, 1);
241*b50261e2SCy Schubert 
242*b50261e2SCy Schubert 	event_del(read_ev);
243*b50261e2SCy Schubert 
244*b50261e2SCy Schubert 	/* trigger acitivity second time for the backend that can have multiple
245*b50261e2SCy Schubert 	 * events for one fd (like kqueue) */
246*b50261e2SCy Schubert 	close(pair[0]);
247*b50261e2SCy Schubert 	pair[0] = -1;
248*b50261e2SCy Schubert 
249*b50261e2SCy Schubert 	/* Verify that we are still edge-triggered for write notifications */
250*b50261e2SCy Schubert 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
251*b50261e2SCy Schubert 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
252*b50261e2SCy Schubert 	tt_assert(last_write_notification_was_et);
253*b50261e2SCy Schubert 	tt_int_op(write_notification_count, ==, 2);
254*b50261e2SCy Schubert 
255*b50261e2SCy Schubert end:
256*b50261e2SCy Schubert 	if (read_ev)
257*b50261e2SCy Schubert 		event_free(read_ev);
258*b50261e2SCy Schubert 	if (write_ev)
259*b50261e2SCy Schubert 		event_free(write_ev);
260*b50261e2SCy Schubert }
261*b50261e2SCy Schubert 
262c43e99fdSEd Maste struct testcase_t edgetriggered_testcases[] = {
263*b50261e2SCy Schubert 	{ "et", test_edgetriggered,
264*b50261e2SCy Schubert 	  TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR, &basic_setup, NULL },
265c43e99fdSEd Maste 	{ "et_mix_error", test_edgetriggered_mix_error,
266c43e99fdSEd Maste 	  TT_FORK|TT_NEED_SOCKETPAIR|TT_NO_LOGS, &basic_setup, NULL },
267*b50261e2SCy Schubert 	{ "et_multiple_events", test_edge_triggered_multiple_events,
268*b50261e2SCy Schubert 	  TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR, &basic_setup, NULL },
269c43e99fdSEd Maste 	END_OF_TESTCASES
270c43e99fdSEd Maste };
271