From 595a2bfbaa9894b06a3213886d1e53508d03b086 Mon Sep 17 00:00:00 2001 From: visa Date: Sat, 12 Jun 2021 13:30:14 +0000 Subject: [PATCH] Exercise resetting of expired and unexpired timers. --- regress/sys/kern/kqueue/Makefile | 5 ++- regress/sys/kern/kqueue/kqueue-timer.c | 58 +++++++++++++++++++++++++- regress/sys/kern/kqueue/main.c | 7 +++- regress/sys/kern/kqueue/main.h | 3 +- 4 files changed, 68 insertions(+), 5 deletions(-) diff --git a/regress/sys/kern/kqueue/Makefile b/regress/sys/kern/kqueue/Makefile index 5b3e8942832..f22393f3c6b 100644 --- a/regress/sys/kern/kqueue/Makefile +++ b/regress/sys/kern/kqueue/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.27 2020/03/08 09:40:52 visa Exp $ +# $OpenBSD: Makefile,v 1.28 2021/06/12 13:30:14 visa Exp $ PROG= kqueue-test CFLAGS+=-Wall @@ -36,6 +36,8 @@ kq-timer: ${PROG} ./${PROG} -i kq-invalid-timer: ${PROG} ./${PROG} -I +kq-reset-timer: ${PROG} + ./${PROG} -j kq-regress-1: ${PROG} ./${PROG} -R1 kq-regress-2: ${PROG} @@ -61,6 +63,7 @@ TESTS+= kq-regress-2 TESTS+= kq-regress-3 TESTS+= kq-regress-4 TESTS+= kq-regress-5 +TESTS+= kq-reset-timer TESTS+= kq-signal TESTS+= kq-timer diff --git a/regress/sys/kern/kqueue/kqueue-timer.c b/regress/sys/kern/kqueue/kqueue-timer.c index e95c3ba4e52..060e753ae2c 100644 --- a/regress/sys/kern/kqueue/kqueue-timer.c +++ b/regress/sys/kern/kqueue/kqueue-timer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kqueue-timer.c,v 1.3 2018/05/22 19:15:22 cheloha Exp $ */ +/* $OpenBSD: kqueue-timer.c,v 1.4 2021/06/12 13:30:14 visa Exp $ */ /* * Copyright (c) 2015 Bret Stephen Lambert * @@ -23,6 +23,7 @@ #include #include #include +#include #include #include "main.h" @@ -97,3 +98,58 @@ do_invalid_timer(void) return (0); } + +int +do_reset_timer(void) +{ + int kq, msecs, n; + struct kevent ev; + struct timespec ts, start, end; + + ASS((kq = kqueue()) >= 0, + warn("kqueue")); + + clock_gettime(CLOCK_MONOTONIC, &start); + + memset(&ev, 0, sizeof(ev)); + ev.filter = EVFILT_TIMER; + ev.flags = EV_ADD | EV_ENABLE | EV_ONESHOT; + ev.data = 10; + + n = kevent(kq, &ev, 1, NULL, 0, NULL); + ASSX(n != -1); + + /* Let the timer expire. */ + usleep(100000); + + /* Reset the expired timer. */ + ev.data = 60000; + n = kevent(kq, &ev, 1, NULL, 0, NULL); + ASSX(n != -1); + + /* Check that no event is pending. */ + ts.tv_sec = 0; + ts.tv_nsec = 0; + n = kevent(kq, NULL, 0, &ev, 1, &ts); + ASSX(n == 0); + + /* Reset again for quick expiry. */ + memset(&ev, 0, sizeof(ev)); + ev.filter = EVFILT_TIMER; + ev.flags = EV_ADD | EV_ENABLE | EV_ONESHOT; + ev.data = 100; + n = kevent(kq, &ev, 1, NULL, 0, NULL); + ASSX(n != -1); + + /* Wait for expiry. */ + n = kevent(kq, NULL, 0, &ev, 1, NULL); + ASSX(n == 1); + + clock_gettime(CLOCK_MONOTONIC, &end); + timespecsub(&end, &start, &ts); + msecs = ts.tv_sec * 1000 + ts.tv_nsec / 1000000; + ASSX(msecs > 200); + ASSX(msecs < 5000); /* allow wide margin */ + + return (0); +} diff --git a/regress/sys/kern/kqueue/main.c b/regress/sys/kern/kqueue/main.c index 72dbb1ffe00..d5f3d0e1806 100644 --- a/regress/sys/kern/kqueue/main.c +++ b/regress/sys/kern/kqueue/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.14 2019/03/04 19:33:41 anton Exp $ */ +/* $OpenBSD: main.c,v 1.15 2021/06/12 13:30:14 visa Exp $ */ /* * Written by Artur Grabowski 2002 Public Domain */ @@ -17,7 +17,7 @@ main(int argc, char **argv) int n, ret, c; ret = 0; - while ((c = getopt(argc, argv, "fFiIlpPrR:stT:")) != -1) { + while ((c = getopt(argc, argv, "fFiIjlpPrR:stT:")) != -1) { switch (c) { case 'f': ret |= check_inheritance(); @@ -31,6 +31,9 @@ main(int argc, char **argv) case 'I': ret |= do_invalid_timer(); break; + case 'j': + ret |= do_reset_timer(); + break; case 'l': ret |= do_flock(); break; diff --git a/regress/sys/kern/kqueue/main.h b/regress/sys/kern/kqueue/main.h index 2ba12b4159b..9fad3c24c7d 100644 --- a/regress/sys/kern/kqueue/main.h +++ b/regress/sys/kern/kqueue/main.h @@ -1,4 +1,4 @@ -/* $OpenBSD: main.h,v 1.5 2019/03/04 19:33:42 anton Exp $ */ +/* $OpenBSD: main.h,v 1.6 2021/06/12 13:30:14 visa Exp $ */ /* * Written by Alexaner Bluhm 2016 Public Domain */ @@ -24,6 +24,7 @@ int do_process(void); int do_pty(int); int do_random(void); int do_regress(int); +int do_reset_timer(void); int do_signal(void); int do_timer(void); int do_tun(void); -- 2.20.1