| 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/tid.h> |
| 32 |
#include <isc/util.h> |
| 33 |
#include <isc/work.h> |
| 34 |
|
| 35 |
#include "work.c" |
| 36 |
|
| 37 |
#include <tests/isc.h> |
| 38 |
|
| 39 |
static atomic_uint scheduled = 0; |
| 40 |
|
| 41 |
static void |
| 42 |
work_cb(void *arg) { |
| 43 |
UNUSED(arg); |
| 44 |
|
| 45 |
atomic_fetch_add(&scheduled, 1); |
| 46 |
|
| 47 |
assert_int_equal(isc_tid(), UINT32_MAX); |
| 48 |
} |
| 49 |
|
| 50 |
static void |
| 51 |
after_work_cb(void *arg) { |
| 52 |
UNUSED(arg); |
| 53 |
|
| 54 |
assert_int_equal(atomic_load(&scheduled), 1); |
| 55 |
isc_loopmgr_shutdown(loopmgr); |
| 56 |
} |
| 57 |
|
| 58 |
static void |
| 59 |
work_enqueue_cb(void *arg) { |
| 60 |
UNUSED(arg); |
| 61 |
uint32_t tid = isc_loopmgr_nloops(loopmgr) - 1; |
| 62 |
|
| 63 |
isc_loop_t *loop = isc_loop_get(loopmgr, tid); |
| 64 |
|
| 65 |
isc_work_enqueue(loop, work_cb, after_work_cb, loopmgr); |
| 66 |
} |
| 67 |
|
| 68 |
ISC_RUN_TEST_IMPL(isc_work_enqueue) { |
| 69 |
atomic_init(&scheduled, 0); |
| 70 |
|
| 71 |
isc_loop_setup(isc_loop_main(loopmgr), work_enqueue_cb, loopmgr); |
| 72 |
|
| 73 |
isc_loopmgr_run(loopmgr); |
| 74 |
|
| 75 |
assert_int_equal(atomic_load(&scheduled), 1); |
| 76 |
} |
| 77 |
|
| 78 |
ISC_TEST_LIST_START |
| 79 |
ISC_TEST_ENTRY_CUSTOM(isc_work_enqueue, setup_loopmgr, teardown_loopmgr) |
| 80 |
ISC_TEST_LIST_END |
| 81 |
|
| 82 |
ISC_TEST_MAIN |