Debian Codegraph @

tests/isc/async_test.c

raw
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/async.h>
28 #include <isc/atomic.h>
29 #include <isc/loop.h>
30 #include <isc/os.h>
31 #include <isc/result.h>
32 #include <isc/tid.h>
33 #include <isc/util.h>
34
35 #include "async.c"
36
37 #include <tests/isc.h>
38
39 static atomic_uint scheduled = 0;
40
41 static void
42 async_cb(void *arg) {
43 uint32_t tid = isc_tid();
44
45 UNUSED(arg);
46
47 atomic_fetch_add(&scheduled, 1);
48
49 if (tid > 0) {
50 isc_loop_t *loop = isc_loop_get(loopmgr, tid - 1);
51 isc_async_run(loop, async_cb, loopmgr);
52 } else {
53 isc_loopmgr_shutdown(loopmgr);
54 }
55 }
56
57 static void
58 async_setup_cb(void *arg) {
59 uint32_t tid = isc_loopmgr_nloops(loopmgr) - 1;
60 isc_loop_t *loop = isc_loop_get(loopmgr, tid);
61
62 UNUSED(arg);
63
64 isc_async_run(loop, async_cb, loopmgr);
65 }
66
67 ISC_RUN_TEST_IMPL(isc_async_run) {
68 isc_loop_setup(isc_loop_main(loopmgr), async_setup_cb, loopmgr);
69 isc_loopmgr_run(loopmgr);
70 assert_int_equal(atomic_load(&scheduled), loopmgr->nloops);
71 }
72
73 static char string[32] = "";
74 int n1 = 1, n2 = 2, n3 = 3, n4 = 4, n5 = 5;
75
76 static void
77 append(void *arg) {
78 char value[32];
79 sprintf(value, "%d", *(int *)arg);
80 strlcat(string, value, 10);
81 }
82
83 static void
84 async_multiple(void *arg) {
85 isc_loop_t *loop = isc_loop();
86
87 UNUSED(arg);
88
89 isc_async_run(loop, append, &n1);
90 isc_async_run(loop, append, &n2);
91 isc_async_run(loop, append, &n3);
92 isc_async_run(loop, append, &n4);
93 isc_async_run(loop, append, &n5);
94 isc_loopmgr_shutdown(loopmgr);
95 }
96
97 ISC_RUN_TEST_IMPL(isc_async_multiple) {
98 string[0] = '\0';
99 isc_loop_setup(isc_loop_main(loopmgr), async_multiple, loopmgr);
100 isc_loopmgr_run(loopmgr);
101 assert_string_equal(string, "12345");
102 }
103
104 ISC_TEST_LIST_START
105 ISC_TEST_ENTRY_CUSTOM(isc_async_run, setup_loopmgr, teardown_loopmgr)
106 ISC_TEST_ENTRY_CUSTOM(isc_async_multiple, setup_loopmgr, teardown_loopmgr)
107 ISC_TEST_LIST_END
108
109 ISC_TEST_MAIN