Debian Codegraph @

tests/isc/job_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/atomic.h>
28 #include <isc/job.h>
29 #include <isc/loop.h>
30 #include <isc/os.h>
31 #include <isc/result.h>
32 #include <isc/util.h>
33
34 #include <tests/isc.h>
35
36 static atomic_uint scheduled;
37 static atomic_uint executed;
38
39 #define MAX_EXECUTED 1000000
40
41 struct test_arg {
42 isc_job_t job;
43 union {
44 int n;
45 void *ptr;
46 } arg;
47 };
48
49 static void
50 shutdown_cb(void *arg) {
51 struct test_arg *ta = arg;
52
53 isc_mem_put(mctx, ta, sizeof(*ta));
54
55 isc_loopmgr_shutdown(loopmgr);
56 }
57
58 static void
59 job_cb(void *arg) {
60 struct test_arg *ta = arg;
61 unsigned int n = atomic_fetch_add(&executed, 1);
62
63 if (n <= MAX_EXECUTED) {
64 atomic_fetch_add(&scheduled, 1);
65 isc_job_run(isc_loop(), &ta->job, job_cb, ta);
66 } else {
67 isc_job_run(isc_loop(), &ta->job, shutdown_cb, ta);
68 }
69 }
70
71 static void
72 job_run_cb(void *arg) {
73 struct test_arg *ta = arg;
74 atomic_fetch_add(&scheduled, 1);
75
76 if (arg == NULL) {
77 ta = isc_mem_get(mctx, sizeof(*ta));
78 *ta = (struct test_arg){ .job = ISC_JOB_INITIALIZER };
79 }
80
81 isc_job_run(isc_loop(), &ta->job, job_cb, ta);
82 }
83
84 ISC_RUN_TEST_IMPL(isc_job_run) {
85 atomic_init(&scheduled, 0);
86 atomic_init(&executed, 0);
87
88 isc_loopmgr_setup(loopmgr, job_run_cb, NULL);
89
90 isc_loopmgr_run(loopmgr);
91
92 assert_int_equal(atomic_load(&scheduled), atomic_load(&executed));
93 }
94
95 static char string[32] = "";
96 struct test_arg n1 = { .job = ISC_JOB_INITIALIZER, .arg.n = 1 };
97 struct test_arg n2 = { .job = ISC_JOB_INITIALIZER, .arg.n = 2 };
98 struct test_arg n3 = { .job = ISC_JOB_INITIALIZER, .arg.n = 3 };
99 struct test_arg n4 = { .job = ISC_JOB_INITIALIZER, .arg.n = 4 };
100 struct test_arg n5 = { .job = ISC_JOB_INITIALIZER, .arg.n = 5 };
101
102 static void
103 append(void *arg) {
104 struct test_arg *ta = arg;
105
106 char value[32];
107 sprintf(value, "%d", ta->arg.n);
108 strlcat(string, value, 10);
109 }
110
111 static void
112 job_multiple(void *arg) {
113 UNUSED(arg);
114
115 /* These will be processed in normal order */
116 isc_job_run(mainloop, &n1.job, append, &n1);
117 isc_job_run(mainloop, &n2.job, append, &n2);
118 isc_job_run(mainloop, &n3.job, append, &n3);
119 isc_job_run(mainloop, &n4.job, append, &n4);
120 isc_job_run(mainloop, &n5.job, append, &n5);
121 isc_loopmgr_shutdown(loopmgr);
122 }
123
124 ISC_RUN_TEST_IMPL(isc_job_multiple) {
125 string[0] = '\0';
126 isc_loop_setup(isc_loop_main(loopmgr), job_multiple, loopmgr);
127 isc_loopmgr_run(loopmgr);
128 assert_string_equal(string, "12345");
129 }
130
131 ISC_TEST_LIST_START
132 ISC_TEST_ENTRY_CUSTOM(isc_job_run, setup_loopmgr, teardown_loopmgr)
133 ISC_TEST_ENTRY_CUSTOM(isc_job_multiple, setup_loopmgr, teardown_loopmgr)
134 ISC_TEST_LIST_END
135
136 ISC_TEST_MAIN