Ejemplo n.º 1
0
static int
basic_test_cleanup(const struct testcase_t *testcase, void *ptr)
{
    struct basic_test_data *data = ptr;

    if (testcase->flags & TT_NO_LOGS)
        event_set_log_callback(NULL);

    if (testcase->flags & TT_NEED_SOCKETPAIR) {
        if (data->pair[0] != -1)
            evutil_closesocket(data->pair[0]);
        if (data->pair[1] != -1)
            evutil_closesocket(data->pair[1]);
    }

    if (testcase->flags & TT_NEED_DNS) {
        evdns_shutdown(0);
    }

    if (testcase->flags & TT_NEED_BASE) {
        if (data->base) {
            event_base_assert_ok_(data->base);
            event_base_free(data->base);
        }
    }

    if (testcase->flags & TT_FORK)
        libevent_global_shutdown();

    free(data);

    return 1;
}
Ejemplo n.º 2
0
static void
test_fin_within_cb(void *arg)
{
	struct basic_test_data *data = arg;
	struct event_base *base = data->base;

	struct event_and_count evc1, evc2;
	evc1.count = evc2.count = 0;
	evc2.ev2 = evc1.ev = evtimer_new(base, timer_callback_2, &evc1);
	evc1.ev2 = evc2.ev = evtimer_new(base, timer_callback_2, &evc2);

	/* Activate both.  The first one will have its callback run, which
	 * will finalize both of them, preventing the second one's callback
	 * from running. */
	event_active(evc1.ev, EV_TIMEOUT, 1);
	event_active(evc2.ev, EV_TIMEOUT, 1);

	event_base_dispatch(base);
	tt_int_op(evc1.count, ==, 101);
	tt_int_op(evc2.count, ==, 100);

	event_base_assert_ok_(base);
	/* Now try with EV_PERSIST events. */
	evc1.count = evc2.count = 0;
	evc2.ev2 = evc1.ev = event_new(base, -1, EV_PERSIST, timer_callback_2, &evc1);
	evc1.ev2 = evc2.ev = event_new(base, -1, EV_PERSIST, timer_callback_2, &evc2);

	event_active(evc1.ev, EV_TIMEOUT, 1);
	event_active(evc2.ev, EV_TIMEOUT, 1);

	event_base_dispatch(base);
	tt_int_op(evc1.count, ==, 101);
	tt_int_op(evc2.count, ==, 100);

	event_base_assert_ok_(base);
end:
	;
}
Ejemplo n.º 3
0
static void
test_fin_free_finalize(void *arg)
{
#ifdef EVENT__DISABLE_MM_REPLACEMENT
	tinytest_set_test_skipped_();
#else
	struct event_base *base = NULL;
	struct event *ev, *ev2;
	int ev_called = 0;
	int ev2_called = 0;

	(void)arg;

	event_set_mem_functions(tfff_malloc, tfff_realloc, tfff_free);

	base = event_base_new();

	ev = evtimer_new(base, timer_callback, &ev_called);
	ev2 = evtimer_new(base, timer_callback, &ev2_called);
	tfff_p1 = ev;
	tfff_p2 = ev2;
	event_free_finalize(0, ev, event_finalize_callback_1);
	event_finalize(0, ev2, event_finalize_callback_1);

	event_base_dispatch(base);

	tt_int_op(ev_called, ==, 100);
	tt_int_op(ev2_called, ==, 100);

	event_base_assert_ok_(base);
	tt_int_op(tfff_p1_freed, ==, 1);
	tt_int_op(tfff_p2_freed, ==, 0);

	event_free(ev2);

end:
	if (base)
		event_base_free(base);
#endif
}
Ejemplo n.º 4
0
static void
test_fin_cb_invoked(void *arg)
{
	struct basic_test_data *data = arg;
	struct event_base *base = data->base;

	struct event *ev;
	struct event ev2;
	struct event_callback evcb;
	int cb_called = 0;
	int ev_called = 0;

	const struct timeval ten_sec = {10,0};

	event_deferred_cb_init_(&evcb, 0, simple_callback, &cb_called);
	ev = evtimer_new(base, timer_callback, &ev_called);
	/* Just finalize them; don't bother adding. */
	event_free_finalize(0, ev, event_finalize_callback_1);
	event_callback_finalize_(base, 0, &evcb, callback_finalize_callback_1);

	event_base_dispatch(base);

	tt_int_op(cb_called, ==, 100);
	tt_int_op(ev_called, ==, 100);

	ev_called = cb_called = 0;
	event_base_assert_ok_(base);

	/* Now try it when they're active. (actually, don't finalize: make
	 * sure activation can happen! */
	ev = evtimer_new(base, timer_callback, &ev_called);
	event_deferred_cb_init_(&evcb, 0, simple_callback, &cb_called);

	event_active(ev, EV_TIMEOUT, 1);
	event_callback_activate_(base, &evcb);

	event_base_dispatch(base);
	tt_int_op(cb_called, ==, 1);
	tt_int_op(ev_called, ==, 1);

	ev_called = cb_called = 0;
	event_base_assert_ok_(base);

	/* Great, it worked. Now activate and finalize and make sure only
	 * finalizing happens. */
	event_active(ev, EV_TIMEOUT, 1);
	event_callback_activate_(base, &evcb);
	event_free_finalize(0, ev, event_finalize_callback_1);
	event_callback_finalize_(base, 0, &evcb, callback_finalize_callback_1);

	event_base_dispatch(base);
	tt_int_op(cb_called, ==, 100);
	tt_int_op(ev_called, ==, 100);

	ev_called = 0;

	event_base_assert_ok_(base);

	/* Okay, now add but don't have it become active, and make sure *that*
	 * works. */
	ev = evtimer_new(base, timer_callback, &ev_called);
	event_add(ev, &ten_sec);
	event_free_finalize(0, ev, event_finalize_callback_1);

	event_base_dispatch(base);
	tt_int_op(ev_called, ==, 100);

	ev_called = 0;
	event_base_assert_ok_(base);

	/* Now try adding and deleting after finalizing. */
	ev = evtimer_new(base, timer_callback, &ev_called);
	evtimer_assign(&ev2, base, timer_callback, &ev_called);
	event_add(ev, &ten_sec);
	event_free_finalize(0, ev, event_finalize_callback_1);
	event_finalize(0, &ev2, event_finalize_callback_1);

	event_add(&ev2, &ten_sec);
	event_del(ev);
	event_active(&ev2, EV_TIMEOUT, 1);

	event_base_dispatch(base);
	tt_int_op(ev_called, ==, 200);

	event_base_assert_ok_(base);

end:
	;
}