]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
timer: add unit tests for DeferReactivation 34591/head
authorMatteo Croce <teknoraver@meta.com>
Fri, 4 Oct 2024 23:39:37 +0000 (01:39 +0200)
committerMatteo Croce <teknoraver@meta.com>
Fri, 11 Oct 2024 20:54:16 +0000 (22:54 +0200)
Create a unit test for systemd timer DeferReactivation config option.
The test works by creating a timer which fires every 5 seconds and
starts an unit which runs for 5 seconds.
With DeferReactivation=true, the timer must fire every 5+5 seconds,
instead of the 5 it fires normally.

As we need at least two timer runs to check if the delta is correct,
the test duration on success will be at least 20 seconds.
To be safe, the test script waits 35 seconds: this is enough to get
at least three runs but low enough to avoid clogging the CI.

test/TEST-74-AUX-UTILS/TEST-74-AUX-UTILS.units/realtime-test.service [new file with mode: 0644]
test/TEST-74-AUX-UTILS/TEST-74-AUX-UTILS.units/realtime-test.timer [new file with mode: 0644]
test/TEST-74-AUX-UTILS/meson.build
test/units/TEST-74-AUX-UTILS.defer_reactivation.sh [new file with mode: 0755]

diff --git a/test/TEST-74-AUX-UTILS/TEST-74-AUX-UTILS.units/realtime-test.service b/test/TEST-74-AUX-UTILS/TEST-74-AUX-UTILS.units/realtime-test.service
new file mode 100644 (file)
index 0000000..a754626
--- /dev/null
@@ -0,0 +1,6 @@
+[Unit]
+Description=Testing systemd timers
+
+[Service]
+Type=simple
+ExecStart=sh -c 'date +%%s >>/tmp/realtime-test.log ; sleep 5'
diff --git a/test/TEST-74-AUX-UTILS/TEST-74-AUX-UTILS.units/realtime-test.timer b/test/TEST-74-AUX-UTILS/TEST-74-AUX-UTILS.units/realtime-test.timer
new file mode 100644 (file)
index 0000000..b870b41
--- /dev/null
@@ -0,0 +1,10 @@
+[Unit]
+Description=Testing systemd timers
+
+[Timer]
+OnCalendar=*:*:0/5
+AccuracySec=1us
+DeferReactivation=true
+
+[Install]
+WantedBy=timers.target
index 668ad492ca67feb1e3436d4820e1a6ee2f290740..543eee195ffec8110a266720990ca52ad402cbfc 100644 (file)
@@ -7,3 +7,5 @@ integration_tests += [
                 'vm' : true,
         },
 ]
+
+testdata_subdirs += [meson.current_source_dir() / 'TEST-74-AUX-UTILS.units']
diff --git a/test/units/TEST-74-AUX-UTILS.defer_reactivation.sh b/test/units/TEST-74-AUX-UTILS.defer_reactivation.sh
new file mode 100755 (executable)
index 0000000..d1b113d
--- /dev/null
@@ -0,0 +1,23 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: LGPL-2.1-or-later
+set -eux
+set -o pipefail
+
+systemctl start realtime-test.timer
+
+sleep 35
+mindelta=10
+
+last=
+while read -r time; do
+    if [ -n "$last" ]; then
+        delta=$((time - last))
+        if [ "$delta" -lt $mindelta ]; then
+            echo "Timer fired too early: $delta < $mindelta" >/failed
+            break
+        fi
+    fi
+    last=$time
+done </tmp/realtime-test.log
+
+test ! -s /failed