2 * Copyright (c) 2013 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.
27 #include "event2/event-config.h"
28 #include "evconfig-private.h"
30 #include "tinytest_macros.h"
33 #include "event2/event.h"
34 #include "event2/util.h"
35 #include "event-internal.h"
36 #include "defer-internal.h"
39 #include "regress_thread.h"
42 timer_callback(evutil_socket_t fd, short what, void *arg)
50 simple_callback(struct event_callback *evcb, void *arg)
57 event_finalize_callback_1(struct event *ev, void *arg)
64 callback_finalize_callback_1(struct event_callback *evcb, void *arg)
73 test_fin_cb_invoked(void *arg)
75 struct basic_test_data *data = arg;
76 struct event_base *base = data->base;
80 struct event_callback evcb;
84 const struct timeval ten_sec = {10,0};
86 event_deferred_cb_init_(&evcb, 0, simple_callback, &cb_called);
87 ev = evtimer_new(base, timer_callback, &ev_called);
88 /* Just finalize them; don't bother adding. */
89 event_free_finalize(0, ev, event_finalize_callback_1);
90 event_callback_finalize_(base, 0, &evcb, callback_finalize_callback_1);
92 event_base_dispatch(base);
94 tt_int_op(cb_called, ==, 100);
95 tt_int_op(ev_called, ==, 100);
97 ev_called = cb_called = 0;
98 event_base_assert_ok_(base);
100 /* Now try it when they're active. (actually, don't finalize: make
101 * sure activation can happen! */
102 ev = evtimer_new(base, timer_callback, &ev_called);
103 event_deferred_cb_init_(&evcb, 0, simple_callback, &cb_called);
105 event_active(ev, EV_TIMEOUT, 1);
106 event_callback_activate_(base, &evcb);
108 event_base_dispatch(base);
109 tt_int_op(cb_called, ==, 1);
110 tt_int_op(ev_called, ==, 1);
112 ev_called = cb_called = 0;
113 event_base_assert_ok_(base);
115 /* Great, it worked. Now activate and finalize and make sure only
116 * finalizing happens. */
117 event_active(ev, EV_TIMEOUT, 1);
118 event_callback_activate_(base, &evcb);
119 event_free_finalize(0, ev, event_finalize_callback_1);
120 event_callback_finalize_(base, 0, &evcb, callback_finalize_callback_1);
122 event_base_dispatch(base);
123 tt_int_op(cb_called, ==, 100);
124 tt_int_op(ev_called, ==, 100);
128 event_base_assert_ok_(base);
130 /* Okay, now add but don't have it become active, and make sure *that*
132 ev = evtimer_new(base, timer_callback, &ev_called);
133 event_add(ev, &ten_sec);
134 event_free_finalize(0, ev, event_finalize_callback_1);
136 event_base_dispatch(base);
137 tt_int_op(ev_called, ==, 100);
140 event_base_assert_ok_(base);
142 /* Now try adding and deleting after finalizing. */
143 ev = evtimer_new(base, timer_callback, &ev_called);
144 evtimer_assign(&ev2, base, timer_callback, &ev_called);
145 event_add(ev, &ten_sec);
146 event_free_finalize(0, ev, event_finalize_callback_1);
147 event_finalize(0, &ev2, event_finalize_callback_1);
149 event_add(&ev2, &ten_sec);
151 event_active(&ev2, EV_TIMEOUT, 1);
153 event_base_dispatch(base);
154 tt_int_op(ev_called, ==, 200);
156 event_base_assert_ok_(base);
162 #ifndef EVENT__DISABLE_MM_REPLACEMENT
164 tfff_malloc(size_t n)
168 static void *tfff_p1=NULL, *tfff_p2=NULL;
169 static int tfff_p1_freed=0, tfff_p2_freed=0;
182 tfff_realloc(void *p, size_t sz)
184 return realloc(p,sz);
189 test_fin_free_finalize(void *arg)
191 #ifdef EVENT__DISABLE_MM_REPLACEMENT
192 tinytest_set_test_skipped_();
194 struct event_base *base = NULL;
195 struct event *ev, *ev2;
201 event_set_mem_functions(tfff_malloc, tfff_realloc, tfff_free);
203 base = event_base_new();
206 ev = evtimer_new(base, timer_callback, &ev_called);
207 ev2 = evtimer_new(base, timer_callback, &ev2_called);
210 event_free_finalize(0, ev, event_finalize_callback_1);
211 event_finalize(0, ev2, event_finalize_callback_1);
213 event_base_dispatch(base);
215 tt_int_op(ev_called, ==, 100);
216 tt_int_op(ev2_called, ==, 100);
218 event_base_assert_ok_(base);
219 tt_int_op(tfff_p1_freed, ==, 1);
220 tt_int_op(tfff_p2_freed, ==, 0);
226 event_base_free(base);
230 /* For test_fin_within_cb */
231 struct event_and_count {
237 event_finalize_callback_2(struct event *ev, void *arg)
239 struct event_and_count *evc = arg;
244 timer_callback_2(evutil_socket_t fd, short what, void *arg)
246 struct event_and_count *evc = arg;
247 event_finalize(0, evc->ev, event_finalize_callback_2);
248 event_finalize(0, evc->ev2, event_finalize_callback_2);
255 test_fin_within_cb(void *arg)
257 struct basic_test_data *data = arg;
258 struct event_base *base = data->base;
260 struct event_and_count evc1, evc2;
261 evc1.count = evc2.count = 0;
262 evc2.ev2 = evc1.ev = evtimer_new(base, timer_callback_2, &evc1);
263 evc1.ev2 = evc2.ev = evtimer_new(base, timer_callback_2, &evc2);
265 /* Activate both. The first one will have its callback run, which
266 * will finalize both of them, preventing the second one's callback
268 event_active(evc1.ev, EV_TIMEOUT, 1);
269 event_active(evc2.ev, EV_TIMEOUT, 1);
271 event_base_dispatch(base);
272 tt_int_op(evc1.count, ==, 101);
273 tt_int_op(evc2.count, ==, 100);
275 event_base_assert_ok_(base);
276 /* Now try with EV_PERSIST events. */
277 evc1.count = evc2.count = 0;
278 evc2.ev2 = evc1.ev = event_new(base, -1, EV_PERSIST, timer_callback_2, &evc1);
279 evc1.ev2 = evc2.ev = event_new(base, -1, EV_PERSIST, timer_callback_2, &evc2);
281 event_active(evc1.ev, EV_TIMEOUT, 1);
282 event_active(evc2.ev, EV_TIMEOUT, 1);
284 event_base_dispatch(base);
285 tt_int_op(evc1.count, ==, 101);
286 tt_int_op(evc2.count, ==, 100);
288 event_base_assert_ok_(base);
295 timer_callback_3(evutil_socket_t *fd, short what, void *arg)
302 test_fin_many(void *arg)
304 struct basic_test_data *data = arg;
305 struct event_base *base = data->base;
307 struct event *ev1, *ev2;
308 struct event_callback evcb1, evcb2;
309 int ev1_count = 0, ev2_count = 0;
310 int evcb1_count = 0, evcb2_count = 0;
311 struct event_callback *array[4];
315 /* First attempt: call finalize_many with no events running */
316 ev1 = evtimer_new(base, timer_callback, &ev1_count);
317 ev1 = evtimer_new(base, timer_callback, &ev2_count);
318 event_deferred_cb_init_(&evcb1, 0, simple_callback, &evcb1_called);
319 event_deferred_cb_init_(&evcb2, 0, simple_callback, &evcb2_called);
320 array[0] = &ev1->ev_evcallback;
321 array[1] = &ev2->ev_evcallback;
327 n = event_callback_finalize_many(base, 4, array,
328 callback_finalize_callback_1);
334 #define TEST(name, flags) \
335 { #name, test_fin_##name, (flags), &basic_setup, NULL }
337 struct testcase_t finalize_testcases[] = {
339 TEST(cb_invoked, TT_FORK|TT_NEED_BASE),
340 TEST(free_finalize, TT_FORK),
341 TEST(within_cb, TT_FORK|TT_NEED_BASE),
342 // TEST(many, TT_FORK|TT_NEED_BASE),