MiniDLNA update: 1.0.19.1 to 1.0.20
[tomato.git] / release / src / router / libevent / test / regress_et.c
blob9da53fdb1b30f5ea9c7659f0bc235833a4bf71d3
1 /*
2 * Copyright (c) 2009-2010 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
6 * are met:
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.
27 #include "event2/event-config.h"
29 #ifdef WIN32
30 #include <winsock2.h>
31 #endif
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #ifdef _EVENT_HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
36 #endif
37 #include <fcntl.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #ifndef WIN32
42 #include <sys/time.h>
43 #include <unistd.h>
44 #endif
45 #include <errno.h>
47 #include "event2/event.h"
48 #include "event2/util.h"
50 #include "regress.h"
52 static int was_et = 0;
54 static void
55 read_cb(evutil_socket_t fd, short event, void *arg)
57 char buf;
58 int len;
60 len = recv(fd, &buf, sizeof(buf), 0);
62 /*printf("%s: %s %d%s\n", __func__, event & EV_ET ? "etread" : "read",
63 len, len ? "" : " - means EOF");
66 called++;
67 if (event & EV_ET)
68 was_et = 1;
70 if (!len)
71 event_del(arg);
74 #ifndef SHUT_WR
75 #define SHUT_WR 1
76 #endif
78 #ifdef WIN32
79 #define LOCAL_SOCKETPAIR_AF AF_INET
80 #else
81 #define LOCAL_SOCKETPAIR_AF AF_UNIX
82 #endif
84 static void
85 test_edgetriggered(void *et)
87 struct event *ev = NULL;
88 struct event_base *base = NULL;
89 const char *test = "test string";
90 evutil_socket_t pair[2] = {-1,-1};
91 int supports_et;
93 if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, pair) == -1) {
94 tt_abort_perror("socketpair");
97 called = was_et = 0;
99 send(pair[0], test, (int)strlen(test)+1, 0);
100 shutdown(pair[0], SHUT_WR);
102 /* Initalize the event library */
103 base = event_base_new();
105 if (!strcmp(event_base_get_method(base), "epoll") ||
106 !strcmp(event_base_get_method(base), "epoll (with changelist)") ||
107 !strcmp(event_base_get_method(base), "kqueue"))
108 supports_et = 1;
109 else
110 supports_et = 0;
112 TT_BLATHER(("Checking for edge-triggered events with %s, which should %s"
113 "support edge-triggering", event_base_get_method(base),
114 supports_et?"":"not "));
116 /* Initalize one event */
117 ev = event_new(base, pair[1], EV_READ|EV_ET|EV_PERSIST, read_cb, &ev);
119 event_add(ev, NULL);
121 /* We're going to call the dispatch function twice. The first invocation
122 * will read a single byte from pair[1] in either case. If we're edge
123 * triggered, we'll only see the event once (since we only see transitions
124 * from no data to data), so the second invocation of event_base_loop will
125 * do nothing. If we're level triggered, the second invocation of
126 * event_base_loop will also activate the event (because there's still
127 * data to read). */
128 event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE);
129 event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE);
131 if (supports_et) {
132 tt_int_op(called, ==, 1);
133 tt_assert(was_et);
134 } else {
135 tt_int_op(called, ==, 2);
136 tt_assert(!was_et);
139 end:
140 if (ev) {
141 event_del(ev);
142 event_free(ev);
144 if (base)
145 event_base_free(base);
146 evutil_closesocket(pair[0]);
147 evutil_closesocket(pair[1]);
150 static void
151 test_edgetriggered_mix_error(void *data_)
153 struct basic_test_data *data = data_;
154 struct event_base *base = NULL;
155 struct event *ev_et=NULL, *ev_lt=NULL;
157 #ifdef _EVENT_DISABLE_DEBUG_MODE
158 if (1)
159 tt_skip();
160 #endif
162 event_enable_debug_mode();
164 base = event_base_new();
166 /* try mixing edge-triggered and level-triggered to make sure it fails*/
167 ev_et = event_new(base, data->pair[0], EV_READ|EV_ET, read_cb, ev_et);
168 ev_lt = event_new(base, data->pair[0], EV_READ, read_cb, ev_lt);
170 /* Add edge-triggered, then level-triggered. Get an error. */
171 tt_int_op(0, ==, event_add(ev_et, NULL));
172 tt_int_op(-1, ==, event_add(ev_lt, NULL));
173 tt_int_op(EV_READ, ==, event_pending(ev_et, EV_READ, NULL));
174 tt_int_op(0, ==, event_pending(ev_lt, EV_READ, NULL));
176 tt_int_op(0, ==, event_del(ev_et));
177 /* Add level-triggered, then edge-triggered. Get an error. */
178 tt_int_op(0, ==, event_add(ev_lt, NULL));
179 tt_int_op(-1, ==, event_add(ev_et, NULL));
180 tt_int_op(EV_READ, ==, event_pending(ev_lt, EV_READ, NULL));
181 tt_int_op(0, ==, event_pending(ev_et, EV_READ, NULL));
183 end:
184 if (ev_et)
185 event_free(ev_et);
186 if (ev_lt)
187 event_free(ev_lt);
188 if (base)
189 event_base_free(base);
192 struct testcase_t edgetriggered_testcases[] = {
193 { "et", test_edgetriggered, TT_FORK, NULL, NULL },
194 { "et_mix_error", test_edgetriggered_mix_error,
195 TT_FORK|TT_NEED_SOCKETPAIR|TT_NO_LOGS, &basic_setup, NULL },
196 END_OF_TESTCASES