/* $NetBSD: regress_finalize.c,v 1.1.1.2 2021/04/07 02:43:15 christos Exp $ */
/*
* Copyright (c) 2013 Niels Provos and Nick Mathewson
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* 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);
/* 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);
/* 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);
/* 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);
// Dispatch base to trigger callbacks
event_base_dispatch(base);
event_base_assert_ok_(base);
tt_int_op(test_ok, ==, 1);
// Now add again, since we did event_assign in event_finalize_callback_free
// This used to fail in event_debug_assert_is_setup_
tt_int_op(event_add(ev, NULL), ==, 0);
// Finalize and dispatch again
tt_int_op(event_finalize(0, ev, event_finalize_callback_free), ==, 0);
event_base_dispatch(base);
event_base_assert_ok_(base);
tt_int_op(test_ok, ==, 2);