| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <inttypes.h> |
| 15 |
#include <sched.h> /* IWYU pragma: keep */ |
| 16 |
#include <setjmp.h> |
| 17 |
#include <stdarg.h> |
| 18 |
#include <stddef.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include <sys/types.h> |
| 22 |
#include <unistd.h> |
| 23 |
|
| 24 |
#define UNIT_TESTING |
| 25 |
#include <cmocka.h> |
| 26 |
|
| 27 |
#include <isc/atomic.h> |
| 28 |
#include <isc/loop.h> |
| 29 |
#include <isc/os.h> |
| 30 |
#include <isc/result.h> |
| 31 |
#include <isc/util.h> |
| 32 |
|
| 33 |
#include "loop.c" |
| 34 |
|
| 35 |
#include <tests/isc.h> |
| 36 |
|
| 37 |
static atomic_uint scheduled = 0; |
| 38 |
|
| 39 |
static void |
| 40 |
count(void *arg) { |
| 41 |
UNUSED(arg); |
| 42 |
|
| 43 |
atomic_fetch_add(&scheduled, 1); |
| 44 |
} |
| 45 |
|
| 46 |
static void |
| 47 |
shutdown_loopmgr(void *arg) { |
| 48 |
UNUSED(arg); |
| 49 |
|
| 50 |
while (atomic_load(&scheduled) != loopmgr->nloops) { |
| 51 |
isc_thread_yield(); |
| 52 |
} |
| 53 |
|
| 54 |
isc_loopmgr_shutdown(loopmgr); |
| 55 |
} |
| 56 |
|
| 57 |
ISC_RUN_TEST_IMPL(isc_loopmgr) { |
| 58 |
atomic_store(&scheduled, 0); |
| 59 |
|
| 60 |
isc_loopmgr_setup(loopmgr, count, loopmgr); |
| 61 |
isc_loop_setup(mainloop, shutdown_loopmgr, loopmgr); |
| 62 |
|
| 63 |
isc_loopmgr_run(loopmgr); |
| 64 |
|
| 65 |
assert_int_equal(atomic_load(&scheduled), loopmgr->nloops); |
| 66 |
} |
| 67 |
|
| 68 |
static void |
| 69 |
runjob(void *arg ISC_ATTR_UNUSED) { |
| 70 |
isc_async_current(count, loopmgr); |
| 71 |
if (isc_tid() == 0) { |
| 72 |
isc_async_current(shutdown_loopmgr, loopmgr); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
ISC_RUN_TEST_IMPL(isc_loopmgr_runjob) { |
| 77 |
atomic_store(&scheduled, 0); |
| 78 |
|
| 79 |
isc_loopmgr_setup(loopmgr, runjob, loopmgr); |
| 80 |
isc_loopmgr_run(loopmgr); |
| 81 |
assert_int_equal(atomic_load(&scheduled), loopmgr->nloops); |
| 82 |
} |
| 83 |
|
| 84 |
static void |
| 85 |
pause_loopmgr(void *arg) { |
| 86 |
UNUSED(arg); |
| 87 |
|
| 88 |
isc_loopmgr_pause(loopmgr); |
| 89 |
|
| 90 |
assert_true(atomic_load(&loopmgr->paused)); |
| 91 |
|
| 92 |
for (size_t i = 0; i < loopmgr->nloops; i++) { |
| 93 |
isc_loop_t *loop = &loopmgr->loops[i]; |
| 94 |
|
| 95 |
assert_true(loop->paused); |
| 96 |
} |
| 97 |
|
| 98 |
atomic_init(&scheduled, loopmgr->nloops); |
| 99 |
|
| 100 |
isc_loopmgr_resume(loopmgr); |
| 101 |
} |
| 102 |
|
| 103 |
ISC_RUN_TEST_IMPL(isc_loopmgr_pause) { |
| 104 |
isc_loop_setup(mainloop, pause_loopmgr, loopmgr); |
| 105 |
isc_loop_setup(mainloop, shutdown_loopmgr, loopmgr); |
| 106 |
isc_loopmgr_run(loopmgr); |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
send_sigint(void *arg) { |
| 111 |
UNUSED(arg); |
| 112 |
|
| 113 |
kill(getpid(), SIGINT); |
| 114 |
} |
| 115 |
|
| 116 |
ISC_RUN_TEST_IMPL(isc_loopmgr_sigint) { |
| 117 |
isc_loop_setup(mainloop, send_sigint, loopmgr); |
| 118 |
isc_loopmgr_run(loopmgr); |
| 119 |
} |
| 120 |
|
| 121 |
static void |
| 122 |
send_sigterm(void *arg) { |
| 123 |
UNUSED(arg); |
| 124 |
|
| 125 |
kill(getpid(), SIGINT); |
| 126 |
} |
| 127 |
|
| 128 |
ISC_RUN_TEST_IMPL(isc_loopmgr_sigterm) { |
| 129 |
isc_loop_setup(mainloop, send_sigterm, loopmgr); |
| 130 |
isc_loopmgr_run(loopmgr); |
| 131 |
} |
| 132 |
|
| 133 |
ISC_TEST_LIST_START |
| 134 |
ISC_TEST_ENTRY_CUSTOM(isc_loopmgr, setup_loopmgr, teardown_loopmgr) |
| 135 |
ISC_TEST_ENTRY_CUSTOM(isc_loopmgr_pause, setup_loopmgr, teardown_loopmgr) |
| 136 |
ISC_TEST_ENTRY_CUSTOM(isc_loopmgr_runjob, setup_loopmgr, teardown_loopmgr) |
| 137 |
ISC_TEST_ENTRY_CUSTOM(isc_loopmgr_sigint, setup_loopmgr, teardown_loopmgr) |
| 138 |
ISC_TEST_ENTRY_CUSTOM(isc_loopmgr_sigterm, setup_loopmgr, teardown_loopmgr) |
| 139 |
ISC_TEST_LIST_END |
| 140 |
|
| 141 |
ISC_TEST_MAIN |