Debian Codegraph @

tests/isc/timer_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 <unistd.h>
22
23 #define UNIT_TESTING
24 #include <cmocka.h>
25
26 #include <isc/atomic.h>
27 #include <isc/commandline.h>
28 #include <isc/condition.h>
29 #include <isc/job.h>
30 #include <isc/loop.h>
31 #include <isc/mem.h>
32 #include <isc/os.h>
33 #include <isc/time.h>
34 #include <isc/timer.h>
35 #include <isc/util.h>
36
37 #include "timer.c"
38
39 #include <tests/isc.h>
40
41 /* Set to true (or use -v option) for verbose output */
42 static bool verbose = true;
43
44 #define FUDGE_SECONDS 0 /* in absence of clock_getres() */
45 #define FUDGE_NANOSECONDS 500000000 /* in absence of clock_getres() */
46
47 static isc_timer_t *timer = NULL;
48 static isc_time_t endtime;
49 static isc_mutex_t lasttime_mx;
50 static isc_time_t lasttime;
51 static int seconds;
52 static int nanoseconds;
53 static atomic_int_fast32_t eventcnt;
54 static atomic_uint_fast32_t errcnt;
55 static int nevents;
56
57 typedef struct setup_test_arg {
58 isc_timertype_t timertype;
59 isc_interval_t *interval;
60 isc_job_cb action;
61 } setup_test_arg_t;
62
63 static void
64 setup_test_run(void *data) {
65 isc_timertype_t timertype = ((setup_test_arg_t *)data)->timertype;
66 isc_interval_t *interval = ((setup_test_arg_t *)data)->interval;
67 isc_job_cb action = ((setup_test_arg_t *)data)->action;
68
69 isc_mutex_lock(&lasttime_mx);
70 lasttime = isc_time_now();
71 UNLOCK(&lasttime_mx);
72
73 isc_timer_create(mainloop, action, (void *)timertype, &timer);
74 isc_timer_start(timer, timertype, interval);
75 }
76
77 static void
78 setup_test(isc_timertype_t timertype, isc_interval_t *interval,
79 isc_job_cb action) {
80 setup_test_arg_t arg = { .timertype = timertype,
81 .interval = interval,
82 .action = action };
83
84 isc_time_settoepoch(&endtime);
85 atomic_init(&eventcnt, 0);
86
87 isc_mutex_init(&lasttime_mx);
88
89 atomic_store(&errcnt, ISC_R_SUCCESS);
90
91 isc_loop_setup(mainloop, setup_test_run, &arg);
92 isc_loopmgr_run(loopmgr);
93
94 assert_int_equal(atomic_load(&errcnt), ISC_R_SUCCESS);
95
96 isc_mutex_destroy(&lasttime_mx);
97 }
98
99 static void
100 set_global_error(isc_result_t result) {
101 (void)atomic_compare_exchange_strong(
102 &errcnt, &(uint_fast32_t){ ISC_R_SUCCESS }, result);
103 }
104
105 static void
106 subthread_assert_true(bool expected, const char *file, unsigned int line) {
107 if (!expected) {
108 printf("# %s:%u subthread_assert_true\n", file, line);
109 set_global_error(ISC_R_UNEXPECTED);
110 }
111 }
112 #define subthread_assert_true(expected) \
113 subthread_assert_true(expected, __FILE__, __LINE__)
114
115 static void
116 subthread_assert_result_equal(isc_result_t result, isc_result_t expected,
117 const char *file, unsigned int line) {
118 if (result != expected) {
119 printf("# %s:%u subthread_assert_result_equal(%u != %u)\n",
120 file, line, (unsigned int)result,
121 (unsigned int)expected);
122 set_global_error(result);
123 }
124 }
125 #define subthread_assert_result_equal(observed, expected) \
126 subthread_assert_result_equal(observed, expected, __FILE__, __LINE__)
127
128 static void
129 ticktock(void *arg) {
130 isc_result_t result;
131 isc_time_t now;
132 isc_time_t base;
133 isc_time_t ulim;
134 isc_time_t llim;
135 isc_interval_t interval;
136 int tick = atomic_fetch_add(&eventcnt, 1);
137
138 UNUSED(arg);
139
140 if (verbose) {
141 print_message("# tick %d\n", tick);
142 }
143
144 now = isc_time_now();
145
146 isc_interval_set(&interval, seconds, nanoseconds);
147 isc_mutex_lock(&lasttime_mx);
148 result = isc_time_add(&lasttime, &interval, &base);
149 isc_mutex_unlock(&lasttime_mx);
150 subthread_assert_result_equal(result, ISC_R_SUCCESS);
151
152 isc_interval_set(&interval, FUDGE_SECONDS, FUDGE_NANOSECONDS);
153 result = isc_time_add(&base, &interval, &ulim);
154 subthread_assert_result_equal(result, ISC_R_SUCCESS);
155
156 result = isc_time_subtract(&base, &interval, &llim);
157 subthread_assert_result_equal(result, ISC_R_SUCCESS);
158
159 subthread_assert_true(isc_time_compare(&llim, &now) <= 0);
160 subthread_assert_true(isc_time_compare(&ulim, &now) >= 0);
161
162 isc_interval_set(&interval, 0, 0);
163 isc_mutex_lock(&lasttime_mx);
164 result = isc_time_add(&now, &interval, &lasttime);
165 isc_mutex_unlock(&lasttime_mx);
166 subthread_assert_result_equal(result, ISC_R_SUCCESS);
167
168 if (atomic_load(&eventcnt) == nevents) {
169 endtime = isc_time_now();
170 isc_timer_destroy(&timer);
171 isc_loopmgr_shutdown(loopmgr);
172 }
173 }
174
175 /*
176 * Individual unit tests
177 */
178
179 /* timer type ticker */
180 ISC_RUN_TEST_IMPL(ticker) {
181 isc_interval_t interval;
182
183 UNUSED(state);
184
185 nevents = 12;
186 seconds = 0;
187 nanoseconds = 500000000;
188
189 isc_interval_set(&interval, seconds, nanoseconds);
190
191 setup_test(isc_timertype_ticker, &interval, ticktock);
192 }
193
194 static void
195 test_idle(void *arg) {
196 isc_result_t result;
197 isc_time_t now;
198 isc_time_t base;
199 isc_time_t ulim;
200 isc_time_t llim;
201 isc_interval_t interval;
202 int tick = atomic_fetch_add(&eventcnt, 1);
203
204 UNUSED(arg);
205
206 if (verbose) {
207 print_message("# tick %d\n", tick);
208 }
209
210 now = isc_time_now();
211
212 isc_interval_set(&interval, seconds, nanoseconds);
213 isc_mutex_lock(&lasttime_mx);
214 result = isc_time_add(&lasttime, &interval, &base);
215 isc_mutex_unlock(&lasttime_mx);
216 subthread_assert_result_equal(result, ISC_R_SUCCESS);
217
218 isc_interval_set(&interval, FUDGE_SECONDS, FUDGE_NANOSECONDS);
219 result = isc_time_add(&base, &interval, &ulim);
220 subthread_assert_result_equal(result, ISC_R_SUCCESS);
221
222 result = isc_time_subtract(&base, &interval, &llim);
223 subthread_assert_result_equal(result, ISC_R_SUCCESS);
224
225 subthread_assert_true(isc_time_compare(&llim, &now) <= 0);
226 subthread_assert_true(isc_time_compare(&ulim, &now) >= 0);
227
228 isc_interval_set(&interval, 0, 0);
229 isc_mutex_lock(&lasttime_mx);
230 isc_time_add(&now, &interval, &lasttime);
231 isc_mutex_unlock(&lasttime_mx);
232
233 isc_timer_destroy(&timer);
234 isc_loopmgr_shutdown(loopmgr);
235 }
236
237 /* timer type once idles out */
238 ISC_RUN_TEST_IMPL(once_idle) {
239 isc_interval_t interval;
240
241 UNUSED(state);
242
243 nevents = 1;
244 seconds = 1;
245 nanoseconds = 200000000;
246
247 isc_interval_set(&interval, seconds, nanoseconds);
248
249 setup_test(isc_timertype_once, &interval, test_idle);
250 }
251
252 /* timer reset */
253 static void
254 test_reset(void *arg) {
255 isc_result_t result;
256 isc_time_t now;
257 isc_time_t base;
258 isc_time_t ulim;
259 isc_time_t llim;
260 isc_interval_t interval;
261 int tick = atomic_fetch_add(&eventcnt, 1);
262
263 UNUSED(arg);
264
265 if (verbose) {
266 print_message("# tick %d\n", tick);
267 }
268
269 /*
270 * Check expired time.
271 */
272
273 now = isc_time_now();
274
275 isc_interval_set(&interval, seconds, nanoseconds);
276 isc_mutex_lock(&lasttime_mx);
277 result = isc_time_add(&lasttime, &interval, &base);
278 isc_mutex_unlock(&lasttime_mx);
279 subthread_assert_result_equal(result, ISC_R_SUCCESS);
280
281 isc_interval_set(&interval, FUDGE_SECONDS, FUDGE_NANOSECONDS);
282 result = isc_time_add(&base, &interval, &ulim);
283 subthread_assert_result_equal(result, ISC_R_SUCCESS);
284
285 result = isc_time_subtract(&base, &interval, &llim);
286 subthread_assert_result_equal(result, ISC_R_SUCCESS);
287
288 subthread_assert_true(isc_time_compare(&llim, &now) <= 0);
289 subthread_assert_true(isc_time_compare(&ulim, &now) >= 0);
290
291 isc_interval_set(&interval, 0, 0);
292 isc_mutex_lock(&lasttime_mx);
293 isc_time_add(&now, &interval, &lasttime);
294 isc_mutex_unlock(&lasttime_mx);
295
296 if (tick < 2) {
297 if (tick == 1) {
298 isc_interval_set(&interval, seconds, nanoseconds);
299 isc_timer_start(timer, isc_timertype_once, &interval);
300 }
301 } else {
302 isc_timer_destroy(&timer);
303 isc_loopmgr_shutdown(loopmgr);
304 }
305 }
306
307 ISC_RUN_TEST_IMPL(reset) {
308 isc_interval_t interval;
309
310 UNUSED(state);
311
312 nevents = 3;
313 seconds = 0;
314 nanoseconds = 750000000;
315
316 isc_interval_set(&interval, seconds, nanoseconds);
317
318 setup_test(isc_timertype_ticker, &interval, test_reset);
319 }
320
321 static atomic_bool startflag;
322 static isc_timer_t *tickertimer = NULL;
323 static isc_timer_t *oncetimer = NULL;
324
325 static void
326 tick_event(void *arg) {
327 int tick;
328
329 UNUSED(arg);
330
331 if (!atomic_load(&startflag)) {
332 if (verbose) {
333 print_message("# tick_event %d\n", -1);
334 }
335 return;
336 }
337
338 tick = atomic_fetch_add(&eventcnt, 1);
339 if (verbose) {
340 print_message("# tick_event %d\n", tick);
341 }
342
343 /*
344 * On the first tick, purge all remaining tick events.
345 */
346 if (tick == 0) {
347 isc_timer_destroy(&tickertimer);
348 isc_loopmgr_shutdown(loopmgr);
349 }
350 }
351
352 static void
353 once_event(void *arg) {
354 UNUSED(arg);
355
356 if (verbose) {
357 print_message("# once_event\n");
358 }
359
360 /*
361 * Allow task1 to start processing events.
362 */
363 atomic_store(&startflag, true);
364
365 isc_timer_destroy(&oncetimer);
366 }
367
368 ISC_LOOP_SETUP_IMPL(purge) {
369 atomic_init(&eventcnt, 0);
370 atomic_store(&errcnt, ISC_R_SUCCESS);
371 }
372
373 ISC_LOOP_TEARDOWN_IMPL(purge) {
374 assert_int_equal(atomic_load(&errcnt), ISC_R_SUCCESS);
375 assert_int_equal(atomic_load(&eventcnt), 1);
376 }
377
378 /* timer events purged */
379 ISC_LOOP_TEST_SETUP_TEARDOWN_IMPL(purge) {
380 isc_interval_t interval;
381
382 UNUSED(arg);
383
384 if (verbose) {
385 print_message("# purge_run\n");
386 }
387
388 atomic_init(&startflag, 0);
389 seconds = 1;
390 nanoseconds = 0;
391
392 isc_interval_set(&interval, seconds, 0);
393
394 tickertimer = NULL;
395 isc_timer_create(mainloop, tick_event, NULL, &tickertimer);
396 isc_timer_start(tickertimer, isc_timertype_ticker, &interval);
397
398 oncetimer = NULL;
399
400 isc_interval_set(&interval, (seconds * 2) + 1, 0);
401
402 isc_timer_create(mainloop, once_event, NULL, &oncetimer);
403 isc_timer_start(oncetimer, isc_timertype_once, &interval);
404 }
405
406 /*
407 * Set of tests that check whether the rescheduling works as expected.
408 */
409
410 isc_time_t timer_start;
411 isc_time_t timer_stop;
412 uint64_t timer_expect;
413 uint64_t timer_ticks;
414 isc_interval_t timer_interval;
415 isc_timertype_t timer_type;
416
417 ISC_LOOP_TEARDOWN_IMPL(timer_expect) {
418 uint64_t diff = isc_time_microdiff(&timer_stop, &timer_start) / 1000000;
419 assert_true(diff == timer_expect);
420 }
421
422 static void
423 timer_event(void *arg ISC_ATTR_UNUSED) {
424 if (--timer_ticks == 0) {
425 isc_timer_destroy(&timer);
426 isc_loopmgr_shutdown(loopmgr);
427 timer_stop = isc_loop_now(isc_loop());
428 } else {
429 isc_timer_start(timer, timer_type, &timer_interval);
430 }
431 }
432
433 ISC_LOOP_SETUP_IMPL(reschedule_up) {
434 timer_start = isc_loop_now(isc_loop());
435 timer_expect = 1;
436 timer_ticks = 1;
437 timer_type = isc_timertype_once;
438 }
439
440 ISC_LOOP_TEST_CUSTOM_IMPL(reschedule_up, setup_loop_reschedule_up,
441 teardown_loop_timer_expect) {
442 isc_timer_create(mainloop, timer_event, NULL, &timer);
443
444 /* Schedule the timer to fire immediately */
445 isc_interval_set(&timer_interval, 0, 0);
446 isc_timer_start(timer, timer_type, &timer_interval);
447
448 /* And then reschedule it to 1 second */
449 isc_interval_set(&timer_interval, 1, 0);
450 isc_timer_start(timer, timer_type, &timer_interval);
451 }
452
453 ISC_LOOP_SETUP_IMPL(reschedule_down) {
454 timer_start = isc_loop_now(isc_loop());
455 timer_expect = 0;
456 timer_ticks = 1;
457 timer_type = isc_timertype_once;
458 }
459
460 ISC_LOOP_TEST_CUSTOM_IMPL(reschedule_down, setup_loop_reschedule_down,
461 teardown_loop_timer_expect) {
462 isc_timer_create(mainloop, timer_event, NULL, &timer);
463
464 /* Schedule the timer to fire at 10 seconds */
465 isc_interval_set(&timer_interval, 10, 0);
466 isc_timer_start(timer, timer_type, &timer_interval);
467
468 /* And then reschedule it fire immediately */
469 isc_interval_set(&timer_interval, 0, 0);
470 isc_timer_start(timer, timer_type, &timer_interval);
471 }
472
473 ISC_LOOP_SETUP_IMPL(reschedule_from_callback) {
474 timer_start = isc_loop_now(isc_loop());
475 timer_expect = 1;
476 timer_ticks = 2;
477 timer_type = isc_timertype_once;
478 }
479
480 ISC_LOOP_TEST_CUSTOM_IMPL(reschedule_from_callback,
481 setup_loop_reschedule_from_callback,
482 teardown_loop_timer_expect) {
483 isc_timer_create(mainloop, timer_event, NULL, &timer);
484
485 isc_interval_set(&timer_interval, 0, NS_PER_SEC / 2);
486 isc_timer_start(timer, timer_type, &timer_interval);
487 }
488
489 ISC_LOOP_SETUP_IMPL(zero) {
490 timer_start = isc_loop_now(isc_loop());
491 timer_expect = 0;
492 timer_ticks = 1;
493 timer_type = isc_timertype_once;
494 }
495
496 ISC_LOOP_TEST_CUSTOM_IMPL(zero, setup_loop_zero, teardown_loop_timer_expect) {
497 isc_timer_create(mainloop, timer_event, NULL, &timer);
498
499 /* Schedule the timer to fire immediately (in the next event loop) */
500 isc_interval_set(&timer_interval, 0, 0);
501 isc_timer_start(timer, timer_type, &timer_interval);
502 }
503
504 ISC_LOOP_SETUP_IMPL(reschedule_ticker) {
505 timer_start = isc_loop_now(isc_loop());
506 timer_expect = 1;
507 timer_ticks = 5;
508 timer_type = isc_timertype_ticker;
509 }
510
511 ISC_LOOP_TEST_CUSTOM_IMPL(reschedule_ticker, setup_loop_reschedule_ticker,
512 teardown_loop_timer_expect) {
513 isc_timer_create(mainloop, timer_event, NULL, &timer);
514
515 /* Schedule the timer to fire immediately (in the next event loop) */
516 isc_interval_set(&timer_interval, 0, 0);
517 isc_timer_start(timer, timer_type, &timer_interval);
518
519 /* Then fire every 1/4 second */
520 isc_interval_set(&timer_interval, 0, NS_PER_SEC / 4);
521 }
522
523 ISC_TEST_LIST_START
524
525 ISC_TEST_ENTRY_CUSTOM(ticker, setup_loopmgr, teardown_loopmgr)
526 ISC_TEST_ENTRY_CUSTOM(once_idle, setup_loopmgr, teardown_loopmgr)
527 ISC_TEST_ENTRY_CUSTOM(reset, setup_loopmgr, teardown_loopmgr)
528 ISC_TEST_ENTRY_CUSTOM(purge, setup_loopmgr, teardown_loopmgr)
529 ISC_TEST_ENTRY_CUSTOM(reschedule_up, setup_loopmgr, teardown_loopmgr)
530 ISC_TEST_ENTRY_CUSTOM(reschedule_down, setup_loopmgr, teardown_loopmgr)
531 ISC_TEST_ENTRY_CUSTOM(reschedule_from_callback, setup_loopmgr, teardown_loopmgr)
532 ISC_TEST_ENTRY_CUSTOM(zero, setup_loopmgr, teardown_loopmgr)
533 ISC_TEST_ENTRY_CUSTOM(reschedule_ticker, setup_loopmgr, teardown_loopmgr)
534
535 ISC_TEST_LIST_END
536
537 ISC_TEST_MAIN