1 /* $NetBSD: work_test.c,v 1.2 2025/01/26 16:25:50 christos Exp $ */ 2 3 /* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * SPDX-License-Identifier: MPL-2.0 7 * 8 * This Source Code Form is subject to the terms of the Mozilla Public 9 * License, v. 2.0. If a copy of the MPL was not distributed with this 10 * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 * 12 * See the COPYRIGHT file distributed with this work for additional 13 * information regarding copyright ownership. 14 */ 15 16 #include <inttypes.h> 17 #include <sched.h> /* IWYU pragma: keep */ 18 #include <setjmp.h> 19 #include <stdarg.h> 20 #include <stddef.h> 21 #include <stdlib.h> 22 #include <string.h> 23 #include <sys/types.h> 24 #include <unistd.h> 25 26 #define UNIT_TESTING 27 #include <cmocka.h> 28 29 #include <isc/atomic.h> 30 #include <isc/loop.h> 31 #include <isc/os.h> 32 #include <isc/result.h> 33 #include <isc/tid.h> 34 #include <isc/util.h> 35 #include <isc/work.h> 36 37 #include "work.c" 38 39 #include <tests/isc.h> 40 41 static atomic_uint scheduled = 0; 42 43 static void 44 work_cb(void *arg) { 45 UNUSED(arg); 46 47 atomic_fetch_add(&scheduled, 1); 48 49 assert_int_equal(isc_tid(), UINT32_MAX); 50 } 51 52 static void 53 after_work_cb(void *arg) { 54 UNUSED(arg); 55 56 assert_int_equal(atomic_load(&scheduled), 1); 57 isc_loopmgr_shutdown(loopmgr); 58 } 59 60 static void 61 work_enqueue_cb(void *arg) { 62 UNUSED(arg); 63 uint32_t tid = isc_loopmgr_nloops(loopmgr) - 1; 64 65 isc_loop_t *loop = isc_loop_get(loopmgr, tid); 66 67 isc_work_enqueue(loop, work_cb, after_work_cb, loopmgr); 68 } 69 70 ISC_RUN_TEST_IMPL(isc_work_enqueue) { 71 atomic_init(&scheduled, 0); 72 73 isc_loop_setup(isc_loop_main(loopmgr), work_enqueue_cb, loopmgr); 74 75 isc_loopmgr_run(loopmgr); 76 77 assert_int_equal(atomic_load(&scheduled), 1); 78 } 79 80 ISC_TEST_LIST_START 81 ISC_TEST_ENTRY_CUSTOM(isc_work_enqueue, setup_loopmgr, teardown_loopmgr) 82 ISC_TEST_LIST_END 83 84 ISC_TEST_MAIN 85