Skip to contentSkip to conversation

lib/tests: add KUnit tests for the timerqueue

PATCH In review Amit Patole 1 participant 2 messages Nov 6 – Nov 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/tests: add KUnit tests for the timerqueue

lib/timerqueue.c is the time-ordered queue behind hrtimers, POSIX CPU timers and the alarm timers, and since commit 1339eeb73d6b ("timerqueue: Provide linked timerqueue") it also has a linked variant that keeps prev/next pointers for hrtimer rearming. Neither has unit tests.

Add a KUnit suite that covers:

- an empty queue, and timerqueue_iterate_next(NULL);
- timerqueue_add() returning true only when the node becomes the first, and timerqueue_del() returning true while nodes remain;
- iteration in expiry order, and nodes with the same expiry time staying in the order they were added;
- a deleted node being queued again;
- 256 nodes with pseudo-random expiry times (fixed seed): order and count after inserting, deleting from the middle and draining from the front;
- the linked variant: prev/next links in both directions after inserts, deletes from the middle and the front, and re-adding; and the same pseudo-random stress with every prev link checked.

An AI coding assistant (Claude Opus 5.5) was used to draft the tests. They were run with "tools/testing/kunit/kunit.py run" on UML (6/6 pass), built with W=1 without warnings, and checked against deliberately broken versions of the two comparison functions and of timerqueue_del()'s return value, which the suite catches.

---
Tested on UML with a kunitconfig of CONFIG_KUNIT=y and

CONFIG_TIMERQUEUE_KUNIT_TEST=y: 6/6 pass.

---
 lib/Kconfig.debug            |  13 ++
 lib/tests/Makefile           |   1 +
 lib/tests/timerqueue_kunit.c | 293 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 307 insertions(+)
lib/Kconfig.debug +13 -0
index 2d22638ec13a..5707ed622d5d 100644
@@ -3461,6 +3461,19 @@ config RATELIMIT_KUNIT_TEST
 
 	  If unsure, say N.
 
+config TIMERQUEUE_KUNIT_TEST
+	tristate "KUnit test for the timerqueue" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  This option enables the KUnit test suite for lib/timerqueue.c,
+	  the time-ordered queue behind hrtimers and POSIX CPU timers.
+	  The tests cover ordering, the return values of the add and
+	  delete helpers, equal expiry times and the prev/next links of
+	  the linked timerqueue.
+
+	  If unsure, say N.
+
 config UUID_KUNIT_TEST
 	tristate "KUnit test for UUID" if !KUNIT_ALL_TESTS
 	depends on KUNIT
lib/tests/Makefile +1 -0
index df0537e1b91f..1d18cfdca256 100644
@@ -58,5 +58,6 @@ obj-$(CONFIG_USERCOPY_KUNIT_TEST) += usercopy_kunit.o
 obj-$(CONFIG_UTIL_MACROS_KUNIT) += util_macros_kunit.o
 obj-$(CONFIG_RATELIMIT_KUNIT_TEST) += test_ratelimit.o
 obj-$(CONFIG_UUID_KUNIT_TEST) += uuid_kunit.o
+obj-$(CONFIG_TIMERQUEUE_KUNIT_TEST) += timerqueue_kunit.o
 
 obj-$(CONFIG_TEST_RUNTIME_MODULE)		+= module/
lib/tests/timerqueue_kunit.c new file +293 -0

293 lines not on this page · raw message

---
base-commit: 05441d697f7f9a927264f4058cbbc6a03dfcac9d
change-id: 20261106-kunit-timerqueue-20064ac35b2d

Best regards,

Signature · 1 line
Amit Patole <[email protected]>
#1 lore raw
Quoted 2 lines · Amit Patole

lib/timerqueue.c is the time-ordered queue behind hrtimers, POSIX CPU timers and the alarm timers, [...] Neither has unit tests.

Gentle ping on this one. It adds a KUnit suite for lib/timerqueue.c, covering both the cached rbtree and the linked variant.

If it should go through the timers tree instead of -mm, that's fine with me, and I'm happy to rework anything.

Thanks,
Amit