xref: /freebsd/contrib/ntp/sntp/libevent/test/regress_et.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert  * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
32b15cb3dSCy Schubert  *
42b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
52b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
62b15cb3dSCy Schubert  * are met:
72b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
82b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
92b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
102b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
112b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
122b15cb3dSCy Schubert  * 3. The name of the author may not be used to endorse or promote products
132b15cb3dSCy Schubert  *    derived from this software without specific prior written permission.
142b15cb3dSCy Schubert  *
152b15cb3dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162b15cb3dSCy Schubert  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172b15cb3dSCy Schubert  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
182b15cb3dSCy Schubert  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
192b15cb3dSCy Schubert  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202b15cb3dSCy Schubert  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212b15cb3dSCy Schubert  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
222b15cb3dSCy Schubert  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232b15cb3dSCy Schubert  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
242b15cb3dSCy Schubert  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
252b15cb3dSCy Schubert  */
262b15cb3dSCy Schubert #include "../util-internal.h"
272b15cb3dSCy Schubert #include "event2/event-config.h"
282b15cb3dSCy Schubert 
292b15cb3dSCy Schubert #ifdef _WIN32
302b15cb3dSCy Schubert #include <winsock2.h>
312b15cb3dSCy Schubert #endif
322b15cb3dSCy Schubert #include <sys/types.h>
332b15cb3dSCy Schubert #include <sys/stat.h>
342b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_SOCKET_H
352b15cb3dSCy Schubert #include <sys/socket.h>
362b15cb3dSCy Schubert #endif
372b15cb3dSCy Schubert #include <fcntl.h>
382b15cb3dSCy Schubert #include <stdlib.h>
392b15cb3dSCy Schubert #include <stdio.h>
402b15cb3dSCy Schubert #include <string.h>
412b15cb3dSCy Schubert #ifndef _WIN32
422b15cb3dSCy Schubert #include <sys/time.h>
432b15cb3dSCy Schubert #include <unistd.h>
442b15cb3dSCy Schubert #endif
452b15cb3dSCy Schubert #include <errno.h>
462b15cb3dSCy Schubert 
472b15cb3dSCy Schubert #include "event2/event.h"
482b15cb3dSCy Schubert #include "event2/util.h"
492b15cb3dSCy Schubert 
502b15cb3dSCy Schubert #include "regress.h"
512b15cb3dSCy Schubert 
522b15cb3dSCy Schubert static int was_et = 0;
532b15cb3dSCy Schubert 
base_supports_et(struct event_base * base)54*a466cc55SCy Schubert static int base_supports_et(struct event_base *base)
55*a466cc55SCy Schubert {
56*a466cc55SCy Schubert 	return
57*a466cc55SCy Schubert 		(!strcmp(event_base_get_method(base), "epoll") ||
58*a466cc55SCy Schubert 		!strcmp(event_base_get_method(base), "epoll (with changelist)") ||
59*a466cc55SCy Schubert 		!strcmp(event_base_get_method(base), "kqueue"));
60*a466cc55SCy Schubert }
61*a466cc55SCy Schubert 
622b15cb3dSCy Schubert static void
read_cb(evutil_socket_t fd,short event,void * arg)632b15cb3dSCy Schubert read_cb(evutil_socket_t fd, short event, void *arg)
642b15cb3dSCy Schubert {
652b15cb3dSCy Schubert 	char buf;
662b15cb3dSCy Schubert 	int len;
672b15cb3dSCy Schubert 
682b15cb3dSCy Schubert 	len = recv(fd, &buf, sizeof(buf), 0);
692b15cb3dSCy Schubert 
702b15cb3dSCy Schubert 	called++;
712b15cb3dSCy Schubert 	if (event & EV_ET)
722b15cb3dSCy Schubert 		was_et = 1;
732b15cb3dSCy Schubert 
742b15cb3dSCy Schubert 	if (!len)
752b15cb3dSCy Schubert 		event_del(arg);
762b15cb3dSCy Schubert }
772b15cb3dSCy Schubert 
782b15cb3dSCy Schubert static void
test_edgetriggered(void * data_)79*a466cc55SCy Schubert test_edgetriggered(void *data_)
802b15cb3dSCy Schubert {
81*a466cc55SCy Schubert 	struct basic_test_data *data = data_;
82*a466cc55SCy Schubert 	struct event_base *base = data->base;
83*a466cc55SCy Schubert 	evutil_socket_t *pair = data->pair;
842b15cb3dSCy Schubert 	struct event *ev = NULL;
852b15cb3dSCy Schubert 	const char *test = "test string";
862b15cb3dSCy Schubert 	int supports_et;
872b15cb3dSCy Schubert 
882b15cb3dSCy Schubert 	/* On Linux 3.2.1 (at least, as patched by Fedora and tested by Nick),
892b15cb3dSCy Schubert 	 * doing a "recv" on an AF_UNIX socket resets the readability of the
902b15cb3dSCy Schubert 	 * socket, even though there is no state change, so we don't actually
912b15cb3dSCy Schubert 	 * get edge-triggered behavior.  Yuck!  Linux 3.1.9 didn't have this
922b15cb3dSCy Schubert 	 * problem.
932b15cb3dSCy Schubert 	 */
942b15cb3dSCy Schubert 
952b15cb3dSCy Schubert 	called = was_et = 0;
962b15cb3dSCy Schubert 
972b15cb3dSCy Schubert 	tt_int_op(send(pair[0], test, (int)strlen(test)+1, 0), >, 0);
98*a466cc55SCy Schubert 	tt_int_op(shutdown(pair[0], EVUTIL_SHUT_WR), ==, 0);
992b15cb3dSCy Schubert 
100*a466cc55SCy Schubert 	supports_et = base_supports_et(base);
1012b15cb3dSCy Schubert 	TT_BLATHER(("Checking for edge-triggered events with %s, which should %s"
1022b15cb3dSCy Schubert 				"support edge-triggering", event_base_get_method(base),
1032b15cb3dSCy Schubert 				supports_et?"":"not "));
1042b15cb3dSCy Schubert 
105*a466cc55SCy Schubert 	/* Initialize one event */
1062b15cb3dSCy Schubert 	ev = event_new(base, pair[1], EV_READ|EV_ET|EV_PERSIST, read_cb, &ev);
107*a466cc55SCy Schubert 	tt_assert(ev != NULL);
108*a466cc55SCy Schubert 	tt_int_op(event_add(ev, NULL), ==, 0);
1092b15cb3dSCy Schubert 
1102b15cb3dSCy Schubert 	/* We're going to call the dispatch function twice.  The first invocation
1112b15cb3dSCy Schubert 	 * will read a single byte from pair[1] in either case.  If we're edge
1122b15cb3dSCy Schubert 	 * triggered, we'll only see the event once (since we only see transitions
1132b15cb3dSCy Schubert 	 * from no data to data), so the second invocation of event_base_loop will
1142b15cb3dSCy Schubert 	 * do nothing.  If we're level triggered, the second invocation of
1152b15cb3dSCy Schubert 	 * event_base_loop will also activate the event (because there's still
1162b15cb3dSCy Schubert 	 * data to read). */
117*a466cc55SCy Schubert 	tt_int_op(event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE), ==, 0);
118*a466cc55SCy Schubert 	tt_int_op(event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE), ==, 0);
1192b15cb3dSCy Schubert 
1202b15cb3dSCy Schubert 	if (supports_et) {
1212b15cb3dSCy Schubert 		tt_int_op(called, ==, 1);
1222b15cb3dSCy Schubert 		tt_assert(was_et);
1232b15cb3dSCy Schubert 	} else {
1242b15cb3dSCy Schubert 		tt_int_op(called, ==, 2);
1252b15cb3dSCy Schubert 		tt_assert(!was_et);
1262b15cb3dSCy Schubert 	}
1272b15cb3dSCy Schubert 
1282b15cb3dSCy Schubert end:
1292b15cb3dSCy Schubert 	if (ev) {
1302b15cb3dSCy Schubert 		event_del(ev);
1312b15cb3dSCy Schubert 		event_free(ev);
1322b15cb3dSCy Schubert 	}
1332b15cb3dSCy Schubert }
1342b15cb3dSCy Schubert 
1352b15cb3dSCy Schubert static void
test_edgetriggered_mix_error(void * data_)1362b15cb3dSCy Schubert test_edgetriggered_mix_error(void *data_)
1372b15cb3dSCy Schubert {
1382b15cb3dSCy Schubert 	struct basic_test_data *data = data_;
1392b15cb3dSCy Schubert 	struct event_base *base = NULL;
1402b15cb3dSCy Schubert 	struct event *ev_et=NULL, *ev_lt=NULL;
1412b15cb3dSCy Schubert 
1422b15cb3dSCy Schubert #ifdef EVENT__DISABLE_DEBUG_MODE
1432b15cb3dSCy Schubert 	if (1)
1442b15cb3dSCy Schubert 		tt_skip();
1452b15cb3dSCy Schubert #endif
1462b15cb3dSCy Schubert 
1472b15cb3dSCy Schubert 	if (!libevent_tests_running_in_debug_mode)
1482b15cb3dSCy Schubert 		event_enable_debug_mode();
1492b15cb3dSCy Schubert 
1502b15cb3dSCy Schubert 	base = event_base_new();
1512b15cb3dSCy Schubert 
1522b15cb3dSCy Schubert 	/* try mixing edge-triggered and level-triggered to make sure it fails*/
1532b15cb3dSCy Schubert 	ev_et = event_new(base, data->pair[0], EV_READ|EV_ET, read_cb, ev_et);
1542b15cb3dSCy Schubert 	tt_assert(ev_et);
1552b15cb3dSCy Schubert 	ev_lt = event_new(base, data->pair[0], EV_READ, read_cb, ev_lt);
1562b15cb3dSCy Schubert 	tt_assert(ev_lt);
1572b15cb3dSCy Schubert 
1582b15cb3dSCy Schubert 	/* Add edge-triggered, then level-triggered.  Get an error. */
1592b15cb3dSCy Schubert 	tt_int_op(0, ==, event_add(ev_et, NULL));
1602b15cb3dSCy Schubert 	tt_int_op(-1, ==, event_add(ev_lt, NULL));
1612b15cb3dSCy Schubert 	tt_int_op(EV_READ, ==, event_pending(ev_et, EV_READ, NULL));
1622b15cb3dSCy Schubert 	tt_int_op(0, ==, event_pending(ev_lt, EV_READ, NULL));
1632b15cb3dSCy Schubert 
1642b15cb3dSCy Schubert 	tt_int_op(0, ==, event_del(ev_et));
1652b15cb3dSCy Schubert 	/* Add level-triggered, then edge-triggered.  Get an error. */
1662b15cb3dSCy Schubert 	tt_int_op(0, ==, event_add(ev_lt, NULL));
1672b15cb3dSCy Schubert 	tt_int_op(-1, ==, event_add(ev_et, NULL));
1682b15cb3dSCy Schubert 	tt_int_op(EV_READ, ==, event_pending(ev_lt, EV_READ, NULL));
1692b15cb3dSCy Schubert 	tt_int_op(0, ==, event_pending(ev_et, EV_READ, NULL));
1702b15cb3dSCy Schubert 
1712b15cb3dSCy Schubert end:
1722b15cb3dSCy Schubert 	if (ev_et)
1732b15cb3dSCy Schubert 		event_free(ev_et);
1742b15cb3dSCy Schubert 	if (ev_lt)
1752b15cb3dSCy Schubert 		event_free(ev_lt);
1762b15cb3dSCy Schubert 	if (base)
1772b15cb3dSCy Schubert 		event_base_free(base);
1782b15cb3dSCy Schubert }
1792b15cb3dSCy Schubert 
180*a466cc55SCy Schubert static int read_notification_count;
181*a466cc55SCy Schubert static int last_read_notification_was_et;
182*a466cc55SCy Schubert static void
read_notification_cb(evutil_socket_t fd,short event,void * arg)183*a466cc55SCy Schubert read_notification_cb(evutil_socket_t fd, short event, void *arg)
184*a466cc55SCy Schubert {
185*a466cc55SCy Schubert 	read_notification_count++;
186*a466cc55SCy Schubert 	last_read_notification_was_et = (event & EV_ET);
187*a466cc55SCy Schubert }
188*a466cc55SCy Schubert 
189*a466cc55SCy Schubert static int write_notification_count;
190*a466cc55SCy Schubert static int last_write_notification_was_et;
191*a466cc55SCy Schubert static void
write_notification_cb(evutil_socket_t fd,short event,void * arg)192*a466cc55SCy Schubert write_notification_cb(evutil_socket_t fd, short event, void *arg)
193*a466cc55SCy Schubert {
194*a466cc55SCy Schubert 	write_notification_count++;
195*a466cc55SCy Schubert 	last_write_notification_was_et = (event & EV_ET);
196*a466cc55SCy Schubert }
197*a466cc55SCy Schubert 
198*a466cc55SCy Schubert /* After two or more events have been registered for the same
199*a466cc55SCy Schubert  * file descriptor using EV_ET, if one of the events is
200*a466cc55SCy Schubert  * deleted, then the epoll_ctl() call issued by libevent drops
201*a466cc55SCy Schubert  * the EPOLLET flag resulting in level triggered
202*a466cc55SCy Schubert  * notifications.
203*a466cc55SCy Schubert  */
204*a466cc55SCy Schubert static void
test_edge_triggered_multiple_events(void * data_)205*a466cc55SCy Schubert test_edge_triggered_multiple_events(void *data_)
206*a466cc55SCy Schubert {
207*a466cc55SCy Schubert 	struct basic_test_data *data = data_;
208*a466cc55SCy Schubert 	struct event *read_ev = NULL;
209*a466cc55SCy Schubert 	struct event *write_ev = NULL;
210*a466cc55SCy Schubert 	const char c = 'A';
211*a466cc55SCy Schubert 	struct event_base *base = data->base;
212*a466cc55SCy Schubert 	evutil_socket_t *pair = data->pair;
213*a466cc55SCy Schubert 
214*a466cc55SCy Schubert 	if (!base_supports_et(base)) {
215*a466cc55SCy Schubert 		tt_skip();
216*a466cc55SCy Schubert 		return;
217*a466cc55SCy Schubert 	}
218*a466cc55SCy Schubert 
219*a466cc55SCy Schubert 	read_notification_count = 0;
220*a466cc55SCy Schubert 	last_read_notification_was_et = 0;
221*a466cc55SCy Schubert 	write_notification_count = 0;
222*a466cc55SCy Schubert 	last_write_notification_was_et = 0;
223*a466cc55SCy Schubert 
224*a466cc55SCy Schubert 	/* Make pair[1] readable */
225*a466cc55SCy Schubert 	tt_int_op(send(pair[0], &c, 1, 0), >, 0);
226*a466cc55SCy Schubert 
227*a466cc55SCy Schubert 	read_ev = event_new(base, pair[1], EV_READ|EV_ET|EV_PERSIST,
228*a466cc55SCy Schubert 		read_notification_cb, NULL);
229*a466cc55SCy Schubert 	write_ev = event_new(base, pair[1], EV_WRITE|EV_ET|EV_PERSIST,
230*a466cc55SCy Schubert 		write_notification_cb, NULL);
231*a466cc55SCy Schubert 
232*a466cc55SCy Schubert 	event_add(read_ev, NULL);
233*a466cc55SCy Schubert 	event_add(write_ev, NULL);
234*a466cc55SCy Schubert 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
235*a466cc55SCy Schubert 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
236*a466cc55SCy Schubert 
237*a466cc55SCy Schubert 	tt_assert(last_read_notification_was_et);
238*a466cc55SCy Schubert 	tt_int_op(read_notification_count, ==, 1);
239*a466cc55SCy Schubert 	tt_assert(last_write_notification_was_et);
240*a466cc55SCy Schubert 	tt_int_op(write_notification_count, ==, 1);
241*a466cc55SCy Schubert 
242*a466cc55SCy Schubert 	event_del(read_ev);
243*a466cc55SCy Schubert 
244*a466cc55SCy Schubert 	/* trigger acitivity second time for the backend that can have multiple
245*a466cc55SCy Schubert 	 * events for one fd (like kqueue) */
246*a466cc55SCy Schubert 	close(pair[0]);
247*a466cc55SCy Schubert 	pair[0] = -1;
248*a466cc55SCy Schubert 
249*a466cc55SCy Schubert 	/* Verify that we are still edge-triggered for write notifications */
250*a466cc55SCy Schubert 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
251*a466cc55SCy Schubert 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
252*a466cc55SCy Schubert 	tt_assert(last_write_notification_was_et);
253*a466cc55SCy Schubert 	tt_int_op(write_notification_count, ==, 2);
254*a466cc55SCy Schubert 
255*a466cc55SCy Schubert end:
256*a466cc55SCy Schubert 	if (read_ev)
257*a466cc55SCy Schubert 		event_free(read_ev);
258*a466cc55SCy Schubert 	if (write_ev)
259*a466cc55SCy Schubert 		event_free(write_ev);
260*a466cc55SCy Schubert }
261*a466cc55SCy Schubert 
2622b15cb3dSCy Schubert struct testcase_t edgetriggered_testcases[] = {
263*a466cc55SCy Schubert 	{ "et", test_edgetriggered,
264*a466cc55SCy Schubert 	  TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR, &basic_setup, NULL },
2652b15cb3dSCy Schubert 	{ "et_mix_error", test_edgetriggered_mix_error,
2662b15cb3dSCy Schubert 	  TT_FORK|TT_NEED_SOCKETPAIR|TT_NO_LOGS, &basic_setup, NULL },
267*a466cc55SCy Schubert 	{ "et_multiple_events", test_edge_triggered_multiple_events,
268*a466cc55SCy Schubert 	  TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR, &basic_setup, NULL },
2692b15cb3dSCy Schubert 	END_OF_TESTCASES
2702b15cb3dSCy Schubert };
271