2018-02-27 14:29:49 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
#if HAVE_CMOCKA
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <setjmp.h>
|
2018-02-27 14:29:49 -08:00
|
|
|
|
Include <sched.h> where necessary for musl libc
All unit tests define the UNIT_TESTING macro, which causes <cmocka.h> to
replace malloc(), calloc(), realloc(), and free() with its own functions
tracking memory allocations. In order for this not to break
compilation, the system header declaring the prototypes for these
standard functions must be included before <cmocka.h>.
Normally, these prototypes are only present in <stdlib.h>, so we make
sure it is included before <cmocka.h>. However, musl libc also defines
the prototypes for calloc() and free() in <sched.h>, which is included
by <pthread.h>, which is included e.g. by <isc/mutex.h>. Thus, unit
tests including "dnstest.h" (which includes <isc/mem.h>, which includes
<isc/mutex.h>) after <cmocka.h> will not compile with musl libc as for
these programs, <sched.h> will be included after <cmocka.h>.
Always including <cmocka.h> after all other header files is not a
feasible solution as that causes the mock assertion macros defined in
<isc/util.h> to mangle the contents of <cmocka.h>, thus breaking
compilation. We cannot really use the __noreturn__ or analyzer_noreturn
attributes with cmocka assertion functions because they do return if the
tested condition is true. The problem is that what BIND unit tests do
is incompatible with Clang Static Analyzer's assumptions: since we use
cmocka, our custom assertion handlers are present in a shared library
(i.e. it is the cmocka library that checks the assertion condition, not
a macro in unit test code). Redefining cmocka's assertion macros in
<isc/util.h> is an ugly hack to overcome that problem - unfortunately,
this is the only way we can think of to make Clang Static Analyzer
properly process unit test code. Giving up on Clang Static Analyzer
being able to properly process unit test code is not a satisfactory
solution.
Undefining _GNU_SOURCE for unit test code could work around the problem
(musl libc's <sched.h> only defines the prototypes for calloc() and
free() when _GNU_SOURCE is defined), but doing that could introduce
discrepancies for unit tests including entire *.c files, so it is also
not a good solution.
All in all, including <sched.h> before <cmocka.h> for all affected unit
tests seems to be the most benign way of working around this musl libc
quirk. While quite an ugly solution, it achieves our goals here, which
are to keep the benefit of proper static analysis of unit test code and
to fix compilation against musl libc.
2019-07-30 21:08:40 +02:00
|
|
|
#include <sched.h> /* IWYU pragma: keep */
|
2018-10-24 08:48:41 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-02-27 14:29:49 -08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
|
|
|
|
2019-07-04 14:21:15 +02:00
|
|
|
#include <isc/atomic.h>
|
2018-02-27 14:29:49 -08:00
|
|
|
#include <isc/condition.h>
|
2018-10-24 08:48:41 -07:00
|
|
|
#include <isc/commandline.h>
|
2018-02-27 14:29:49 -08:00
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/platform.h>
|
2018-03-09 16:55:21 -08:00
|
|
|
#include <isc/print.h>
|
2018-02-27 14:29:49 -08:00
|
|
|
#include <isc/task.h>
|
|
|
|
#include <isc/time.h>
|
|
|
|
#include <isc/timer.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
#include "isctest.h"
|
|
|
|
|
2019-11-09 14:01:25 +01:00
|
|
|
#include "../timer.c"
|
|
|
|
|
2018-11-16 08:19:06 +00:00
|
|
|
/* Set to true (or use -v option) for verbose output */
|
2018-10-24 08:48:41 -07:00
|
|
|
static bool verbose = false;
|
|
|
|
|
2018-02-27 14:29:49 -08:00
|
|
|
#define FUDGE_SECONDS 0 /* in absence of clock_getres() */
|
|
|
|
#define FUDGE_NANOSECONDS 500000000 /* in absence of clock_getres() */
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
static isc_timer_t *timer = NULL;
|
|
|
|
static isc_condition_t cv;
|
|
|
|
static isc_mutex_t mx;
|
|
|
|
static isc_time_t endtime;
|
|
|
|
static isc_time_t lasttime;
|
|
|
|
static int seconds;
|
|
|
|
static int nanoseconds;
|
2019-07-04 14:21:15 +02:00
|
|
|
static atomic_int_fast32_t eventcnt;
|
2018-10-24 08:48:41 -07:00
|
|
|
static int nevents;
|
|
|
|
|
|
|
|
static int
|
|
|
|
_setup(void **state) {
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
/* Timer tests require two worker threads */
|
|
|
|
result = isc_test_begin(NULL, true, 2);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_teardown(void **state) {
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
isc_test_end();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
static void
|
|
|
|
shutdown(isc_task_t *task, isc_event_t *event) {
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(task);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Signal shutdown processing complete.
|
|
|
|
*/
|
|
|
|
result = isc_mutex_lock(&mx);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
result = isc_condition_signal(&cv);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
result = isc_mutex_unlock(&mx);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
isc_event_free(&event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setup_test(isc_timertype_t timertype, isc_time_t *expires,
|
|
|
|
isc_interval_t *interval,
|
|
|
|
void (*action)(isc_task_t *, isc_event_t *))
|
|
|
|
{
|
|
|
|
isc_result_t result;
|
|
|
|
isc_task_t *task = NULL;
|
|
|
|
isc_time_settoepoch(&endtime);
|
2019-07-04 14:21:15 +02:00
|
|
|
atomic_init(&eventcnt, 0);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_mutex_init(&mx);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
2018-11-15 17:20:36 +01:00
|
|
|
isc_condition_init(&cv);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
LOCK(&mx);
|
|
|
|
|
|
|
|
result = isc_task_create(taskmgr, 0, &task);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
result = isc_task_onshutdown(task, shutdown, NULL);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
result = isc_time_now(&lasttime);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
result = isc_timer_create(timermgr, timertype, expires, interval,
|
|
|
|
task, action, (void *)timertype,
|
|
|
|
&timer);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for shutdown processing to complete.
|
|
|
|
*/
|
2019-07-04 14:21:15 +02:00
|
|
|
while (atomic_load(&eventcnt) != nevents) {
|
2018-02-27 14:29:49 -08:00
|
|
|
result = isc_condition_wait(&cv, &mx);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
UNLOCK(&mx);
|
|
|
|
|
|
|
|
isc_task_detach(&task);
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(&mx);
|
2018-02-27 14:29:49 -08:00
|
|
|
(void) isc_condition_destroy(&cv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ticktock(isc_task_t *task, isc_event_t *event) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_time_t now;
|
|
|
|
isc_time_t base;
|
|
|
|
isc_time_t ulim;
|
|
|
|
isc_time_t llim;
|
|
|
|
isc_interval_t interval;
|
|
|
|
isc_eventtype_t expected_event_type;
|
|
|
|
|
2019-07-04 14:21:15 +02:00
|
|
|
int tick = atomic_fetch_add(&eventcnt, 1);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
if (verbose) {
|
2019-07-04 14:21:15 +02:00
|
|
|
print_message("# tick %d\n", tick);
|
2018-10-24 08:48:41 -07:00
|
|
|
}
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
expected_event_type = ISC_TIMEREVENT_LIFE;
|
|
|
|
if ((isc_timertype_t) event->ev_arg == isc_timertype_ticker) {
|
|
|
|
expected_event_type = ISC_TIMEREVENT_TICK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event->ev_type != expected_event_type) {
|
2018-10-24 08:48:41 -07:00
|
|
|
print_error("# expected event type %u, got %u\n",
|
|
|
|
expected_event_type, event->ev_type);
|
2018-02-27 14:29:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
result = isc_time_now(&now);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
isc_interval_set(&interval, seconds, nanoseconds);
|
|
|
|
result = isc_time_add(&lasttime, &interval, &base);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
isc_interval_set(&interval, FUDGE_SECONDS, FUDGE_NANOSECONDS);
|
|
|
|
result = isc_time_add(&base, &interval, &ulim);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
result = isc_time_subtract(&base, &interval, &llim);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_true(isc_time_compare(&llim, &now) <= 0);
|
|
|
|
assert_true(isc_time_compare(&ulim, &now) >= 0);
|
2018-02-27 14:29:49 -08:00
|
|
|
lasttime = now;
|
|
|
|
|
2019-07-04 14:21:15 +02:00
|
|
|
if (atomic_load(&eventcnt) == nevents) {
|
2018-02-27 14:29:49 -08:00
|
|
|
result = isc_time_now(&endtime);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
isc_timer_detach(&timer);
|
|
|
|
isc_task_shutdown(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_event_free(&event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Individual unit tests
|
|
|
|
*/
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
/* timer type ticker */
|
|
|
|
static void
|
|
|
|
ticker(void **state) {
|
2018-02-27 14:29:49 -08:00
|
|
|
isc_time_t expires;
|
|
|
|
isc_interval_t interval;
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
UNUSED(state);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
nevents = 12;
|
|
|
|
seconds = 0;
|
|
|
|
nanoseconds = 500000000;
|
|
|
|
|
|
|
|
isc_interval_set(&interval, seconds, nanoseconds);
|
|
|
|
isc_time_settoepoch(&expires);
|
|
|
|
|
|
|
|
setup_test(isc_timertype_ticker, &expires, &interval, ticktock);
|
|
|
|
}
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
/* timer type once reaches lifetime */
|
|
|
|
static void
|
|
|
|
once_life(void **state) {
|
2018-02-27 14:29:49 -08:00
|
|
|
isc_result_t result;
|
|
|
|
isc_time_t expires;
|
|
|
|
isc_interval_t interval;
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
UNUSED(state);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
nevents = 1;
|
|
|
|
seconds = 1;
|
|
|
|
nanoseconds = 100000000;
|
|
|
|
|
|
|
|
isc_interval_set(&interval, seconds, nanoseconds);
|
|
|
|
result = isc_time_nowplusinterval(&expires, &interval);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
isc_interval_set(&interval, 0, 0);
|
|
|
|
|
|
|
|
setup_test(isc_timertype_once, &expires, &interval, ticktock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_idle(isc_task_t *task, isc_event_t *event) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_time_t now;
|
|
|
|
isc_time_t base;
|
|
|
|
isc_time_t ulim;
|
|
|
|
isc_time_t llim;
|
|
|
|
isc_interval_t interval;
|
|
|
|
|
2019-07-04 14:21:15 +02:00
|
|
|
int tick = atomic_fetch_add(&eventcnt, 1);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
if (verbose) {
|
2019-07-04 14:21:15 +02:00
|
|
|
print_message("# tick %d\n", tick);
|
2018-10-24 08:48:41 -07:00
|
|
|
}
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
result = isc_time_now(&now);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
isc_interval_set(&interval, seconds, nanoseconds);
|
|
|
|
result = isc_time_add(&lasttime, &interval, &base);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
isc_interval_set(&interval, FUDGE_SECONDS, FUDGE_NANOSECONDS);
|
|
|
|
result = isc_time_add(&base, &interval, &ulim);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
result = isc_time_subtract(&base, &interval, &llim);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_true(isc_time_compare(&llim, &now) <= 0);
|
|
|
|
assert_true(isc_time_compare(&ulim, &now) >= 0);
|
2018-02-27 14:29:49 -08:00
|
|
|
lasttime = now;
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(event->ev_type, ISC_TIMEREVENT_IDLE);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
isc_timer_detach(&timer);
|
|
|
|
isc_task_shutdown(task);
|
|
|
|
isc_event_free(&event);
|
|
|
|
}
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
/* timer type once idles out */
|
|
|
|
static void
|
|
|
|
once_idle(void **state) {
|
2018-02-27 14:29:49 -08:00
|
|
|
isc_result_t result;
|
|
|
|
isc_time_t expires;
|
|
|
|
isc_interval_t interval;
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
UNUSED(state);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
nevents = 1;
|
|
|
|
seconds = 1;
|
|
|
|
nanoseconds = 200000000;
|
|
|
|
|
|
|
|
isc_interval_set(&interval, seconds + 1, nanoseconds);
|
|
|
|
result = isc_time_nowplusinterval(&expires, &interval);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
isc_interval_set(&interval, seconds, nanoseconds);
|
|
|
|
|
|
|
|
setup_test(isc_timertype_once, &expires, &interval, test_idle);
|
|
|
|
}
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
/* timer reset */
|
2018-02-27 14:29:49 -08:00
|
|
|
static void
|
|
|
|
test_reset(isc_task_t *task, isc_event_t *event) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_time_t now;
|
|
|
|
isc_time_t base;
|
|
|
|
isc_time_t ulim;
|
|
|
|
isc_time_t llim;
|
|
|
|
isc_time_t expires;
|
|
|
|
isc_interval_t interval;
|
|
|
|
|
2019-07-04 14:21:15 +02:00
|
|
|
int tick = atomic_fetch_add(&eventcnt, 1);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
if (verbose) {
|
2019-07-04 14:21:15 +02:00
|
|
|
print_message("# tick %d\n", tick);
|
2018-10-24 08:48:41 -07:00
|
|
|
}
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check expired time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
result = isc_time_now(&now);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
isc_interval_set(&interval, seconds, nanoseconds);
|
|
|
|
result = isc_time_add(&lasttime, &interval, &base);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
isc_interval_set(&interval, FUDGE_SECONDS, FUDGE_NANOSECONDS);
|
|
|
|
result = isc_time_add(&base, &interval, &ulim);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
result = isc_time_subtract(&base, &interval, &llim);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_true(isc_time_compare(&llim, &now) <= 0);
|
|
|
|
assert_true(isc_time_compare(&ulim, &now) >= 0);
|
2018-02-27 14:29:49 -08:00
|
|
|
lasttime = now;
|
|
|
|
|
2019-07-04 14:21:15 +02:00
|
|
|
int _eventcnt = atomic_load(&eventcnt);
|
|
|
|
|
|
|
|
if (_eventcnt < 3) {
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(event->ev_type, ISC_TIMEREVENT_TICK);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
2019-07-04 14:21:15 +02:00
|
|
|
if (_eventcnt == 2) {
|
2018-02-27 14:29:49 -08:00
|
|
|
isc_interval_set(&interval, seconds, nanoseconds);
|
|
|
|
result = isc_time_nowplusinterval(&expires, &interval);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
isc_interval_set(&interval, 0, 0);
|
|
|
|
result = isc_timer_reset(timer, isc_timertype_once,
|
|
|
|
&expires, &interval,
|
2018-04-17 08:29:14 -07:00
|
|
|
false);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
}
|
|
|
|
} else {
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(event->ev_type, ISC_TIMEREVENT_LIFE);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
isc_timer_detach(&timer);
|
|
|
|
isc_task_shutdown(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_event_free(&event);
|
|
|
|
}
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
static void
|
|
|
|
reset(void **state) {
|
2018-02-27 14:29:49 -08:00
|
|
|
isc_time_t expires;
|
|
|
|
isc_interval_t interval;
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
UNUSED(state);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
nevents = 3;
|
|
|
|
seconds = 0;
|
|
|
|
nanoseconds = 750000000;
|
|
|
|
|
|
|
|
isc_interval_set(&interval, seconds, nanoseconds);
|
|
|
|
isc_time_settoepoch(&expires);
|
|
|
|
|
|
|
|
setup_test(isc_timertype_ticker, &expires, &interval, test_reset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int startflag;
|
|
|
|
static int shutdownflag;
|
|
|
|
static isc_timer_t *tickertimer = NULL;
|
|
|
|
static isc_timer_t *oncetimer = NULL;
|
|
|
|
static isc_task_t *task1 = NULL;
|
|
|
|
static isc_task_t *task2 = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* task1 blocks on mx while events accumulate
|
|
|
|
* in its queue, until signaled by task2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
start_event(isc_task_t *task, isc_event_t *event) {
|
|
|
|
UNUSED(task);
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
if (verbose) {
|
|
|
|
print_message("# start_event\n");
|
|
|
|
}
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
LOCK(&mx);
|
|
|
|
while (! startflag) {
|
|
|
|
(void) isc_condition_wait(&cv, &mx);
|
|
|
|
}
|
|
|
|
UNLOCK(&mx);
|
|
|
|
|
|
|
|
isc_event_free(&event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tick_event(isc_task_t *task, isc_event_t *event) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_time_t expires;
|
|
|
|
isc_interval_t interval;
|
|
|
|
|
|
|
|
UNUSED(task);
|
|
|
|
|
2019-07-04 14:21:15 +02:00
|
|
|
int tick = atomic_fetch_add(&eventcnt, 1);
|
2018-10-24 08:48:41 -07:00
|
|
|
if (verbose) {
|
2019-07-04 14:21:15 +02:00
|
|
|
print_message("# tick_event %d\n", tick);
|
2018-10-24 08:48:41 -07:00
|
|
|
}
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* On the first tick, purge all remaining tick events
|
|
|
|
* and then shut down the task.
|
|
|
|
*/
|
2019-07-04 14:21:15 +02:00
|
|
|
if (tick == 0) {
|
2018-02-27 14:29:49 -08:00
|
|
|
isc_time_settoepoch(&expires);
|
|
|
|
isc_interval_set(&interval, seconds, 0);
|
|
|
|
result = isc_timer_reset(tickertimer, isc_timertype_ticker,
|
2018-04-17 08:29:14 -07:00
|
|
|
&expires, &interval, true);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
isc_task_shutdown(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_event_free(&event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
once_event(isc_task_t *task, isc_event_t *event) {
|
|
|
|
isc_result_t result;
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
if (verbose) {
|
|
|
|
print_message("# once_event\n");
|
|
|
|
}
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allow task1 to start processing events.
|
|
|
|
*/
|
|
|
|
LOCK(&mx);
|
|
|
|
startflag = 1;
|
|
|
|
|
|
|
|
result = isc_condition_broadcast(&cv);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
UNLOCK(&mx);
|
|
|
|
|
|
|
|
isc_event_free(&event);
|
|
|
|
isc_task_shutdown(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
shutdown_purge(isc_task_t *task, isc_event_t *event) {
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(task);
|
|
|
|
UNUSED(event);
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
if (verbose) {
|
|
|
|
print_message("# shutdown_event\n");
|
|
|
|
}
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Signal shutdown processing complete.
|
|
|
|
*/
|
|
|
|
LOCK(&mx);
|
|
|
|
shutdownflag = 1;
|
|
|
|
|
|
|
|
result = isc_condition_signal(&cv);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
UNLOCK(&mx);
|
|
|
|
|
|
|
|
isc_event_free(&event);
|
|
|
|
}
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
/* timer events purged */
|
|
|
|
static void
|
|
|
|
purge(void **state) {
|
2018-02-27 14:29:49 -08:00
|
|
|
isc_result_t result;
|
|
|
|
isc_event_t *event = NULL;
|
|
|
|
isc_time_t expires;
|
|
|
|
isc_interval_t interval;
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
UNUSED(state);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
startflag = 0;
|
|
|
|
shutdownflag = 0;
|
2019-07-04 14:21:15 +02:00
|
|
|
atomic_init(&eventcnt, 0);
|
2018-02-27 14:29:49 -08:00
|
|
|
seconds = 1;
|
|
|
|
nanoseconds = 0;
|
|
|
|
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_mutex_init(&mx);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
2018-11-15 17:20:36 +01:00
|
|
|
isc_condition_init(&cv);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
result = isc_task_create(taskmgr, 0, &task1);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
result = isc_task_onshutdown(task1, shutdown_purge, NULL);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
result = isc_task_create(taskmgr, 0, &task2);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
LOCK(&mx);
|
|
|
|
|
2019-11-09 14:01:08 +01:00
|
|
|
event = isc_event_allocate(test_mctx, (void *)1 , (isc_eventtype_t)1,
|
2018-02-27 14:29:49 -08:00
|
|
|
start_event, NULL, sizeof(*event));
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_non_null(event);
|
2018-02-27 14:29:49 -08:00
|
|
|
isc_task_send(task1, &event);
|
|
|
|
|
|
|
|
isc_time_settoepoch(&expires);
|
|
|
|
isc_interval_set(&interval, seconds, 0);
|
|
|
|
|
|
|
|
tickertimer = NULL;
|
|
|
|
result = isc_timer_create(timermgr, isc_timertype_ticker,
|
|
|
|
&expires, &interval, task1,
|
|
|
|
tick_event, NULL, &tickertimer);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
oncetimer = NULL;
|
|
|
|
|
|
|
|
isc_interval_set(&interval, (seconds * 2) + 1, 0);
|
|
|
|
result = isc_time_nowplusinterval(&expires, &interval);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
isc_interval_set(&interval, 0, 0);
|
|
|
|
result = isc_timer_create(timermgr, isc_timertype_once,
|
|
|
|
&expires, &interval, task2,
|
|
|
|
once_event, NULL, &oncetimer);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for shutdown processing to complete.
|
|
|
|
*/
|
|
|
|
while (! shutdownflag) {
|
|
|
|
result = isc_condition_wait(&cv, &mx);
|
2018-10-24 08:48:41 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 14:29:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
UNLOCK(&mx);
|
|
|
|
|
2019-07-04 14:21:15 +02:00
|
|
|
assert_int_equal(atomic_load(&eventcnt), 1);
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
isc_timer_detach(&tickertimer);
|
|
|
|
isc_timer_detach(&oncetimer);
|
|
|
|
isc_task_destroy(&task1);
|
|
|
|
isc_task_destroy(&task2);
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(&mx);
|
2018-10-24 08:48:41 -07:00
|
|
|
}
|
2018-02-27 14:29:49 -08:00
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
int
|
|
|
|
main(int argc, char **argv) {
|
|
|
|
const struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test_setup_teardown(ticker, _setup, _teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(once_life, _setup, _teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(once_idle, _setup, _teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(reset, _setup, _teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(purge, _setup, _teardown),
|
|
|
|
};
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while ((c = isc_commandline_parse(argc, argv, "v")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (cmocka_run_group_tests(tests, NULL, NULL));
|
2018-02-27 14:29:49 -08:00
|
|
|
}
|
|
|
|
|
2018-10-24 08:48:41 -07:00
|
|
|
#else /* HAVE_CMOCKA */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void) {
|
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
|
|
|
return (0);
|
2018-02-27 14:29:49 -08:00
|
|
|
}
|
2018-10-24 08:48:41 -07:00
|
|
|
|
|
|
|
#endif
|