git: 190a6b5aeaa0 - stable/13 - sem test: move sem_clockwait_np tests into individual cases

From: Eric van Gyzen <vangyzen_at_FreeBSD.org>
Date: Wed, 02 Mar 2022 21:57:23 UTC
The branch stable/13 has been updated by vangyzen:

URL: https://cgit.FreeBSD.org/src/commit/?id=190a6b5aeaa041bdeadd2f2534493356f5c8c71d

commit 190a6b5aeaa041bdeadd2f2534493356f5c8c71d
Author:     Eric van Gyzen <vangyzen@FreeBSD.org>
AuthorDate: 2021-10-01 11:24:27 +0000
Commit:     Eric van Gyzen <vangyzen@FreeBSD.org>
CommitDate: 2022-03-02 21:56:30 +0000

    sem test: move sem_clockwait_np tests into individual cases
    
    Move these tests into individual test cases for all the usual reasons.
    No functional change intended.
    
    MFC after:      1 week
    Sponsored by:   Dell EMC Isilon
    
    (cherry picked from commit 2334abfd01ba92dfa6a7ab6f2c4cc45fa64ffd3c)
---
 contrib/netbsd-tests/lib/librt/t_sem.c | 126 ++++++++++++++++++++++++++-------
 1 file changed, 102 insertions(+), 24 deletions(-)

diff --git a/contrib/netbsd-tests/lib/librt/t_sem.c b/contrib/netbsd-tests/lib/librt/t_sem.c
index 55a416083846..3156b11e9bf3 100644
--- a/contrib/netbsd-tests/lib/librt/t_sem.c
+++ b/contrib/netbsd-tests/lib/librt/t_sem.c
@@ -221,14 +221,27 @@ sigalrm_handler(int sig __unused)
 	got_sigalrm = 1;
 }
 
+#ifdef __FreeBSD__
+/* This is refactored from the timedwait test case. */
+static void
+setup_signals(void)
+{
+	struct sigaction act = {
+		.sa_handler = sigalrm_handler,
+		.sa_flags = 0	/* not SA_RESTART */
+	};
+
+	ATF_REQUIRE_MSG(sigemptyset(&act.sa_mask) == 0,
+	    "%s", strerror(errno));
+	ATF_REQUIRE_MSG(sigaction(SIGALRM, &act, NULL) == 0,
+	    "%s", strerror(errno));
+}
+#endif
+
 ATF_TC(timedwait);
 ATF_TC_HEAD(timedwait, tc)
 {
-	atf_tc_set_md_var(tc, "descr", "Tests sem_timedwait(3)"
-#ifdef __FreeBSD__
-	    " and sem_clockwait_np(3)"
-#endif
-	    );
+	atf_tc_set_md_var(tc, "descr", "Tests sem_timedwait(3)");
 	atf_tc_set_md_var(tc, "timeout", "20");
 }
 ATF_TC_BODY(timedwait, tc)
@@ -263,14 +276,10 @@ ATF_TC_BODY(timedwait, tc)
 	ATF_REQUIRE_ERRNO(EINVAL, sem_timedwait(&sem, &ts));
 
 	/* EINTR */
-	struct sigaction act = {
-		.sa_handler = sigalrm_handler,
-		.sa_flags = 0	/* not SA_RESTART */
-	};
-	ATF_REQUIRE_MSG(sigemptyset(&act.sa_mask) == 0,
-	    "%s", strerror(errno));
-	ATF_REQUIRE_MSG(sigaction(SIGALRM, &act, NULL) == 0,
-	    "%s", strerror(errno));
+#ifdef __FreeBSD__
+	/* This is refactored into a function. */
+	setup_signals();
+#endif
 	struct itimerval it = {
 		.it_value.tv_usec = 50*1000
 	};
@@ -281,9 +290,24 @@ ATF_TC_BODY(timedwait, tc)
         timespec_add_ms(&ts, 100);
 	ATF_REQUIRE_ERRNO(EINTR, sem_timedwait(&sem, &ts));
 	ATF_REQUIRE_MSG(got_sigalrm, "did not get SIGALRM");
+}
 
 #ifdef __FreeBSD__
-	/* CLOCK_MONOTONIC, absolute */
+
+ATF_TC(clockwait_monotonic_absolute);
+ATF_TC_HEAD(clockwait_monotonic_absolute, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "sem_clockwait_np(3) with monotonic clock and absolute time");
+	atf_tc_set_md_var(tc, "timeout", "20");
+}
+ATF_TC_BODY(clockwait_monotonic_absolute, tc)
+{
+	struct timespec ts;
+	sem_t sem;
+
+	SEM_REQUIRE(sem_init(&sem, 0, 0));
+
 	SEM_REQUIRE(sem_post(&sem));
 	ATF_REQUIRE_MSG(clock_gettime(CLOCK_MONOTONIC, &ts) == 0,
 	    "%s", strerror(errno));
@@ -292,8 +316,22 @@ ATF_TC_BODY(timedwait, tc)
 	    &ts, NULL));
 	ATF_REQUIRE_ERRNO(ETIMEDOUT,
 	    sem_clockwait_np(&sem, CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL));
+}
+
+ATF_TC(clockwait_monotonic_relative);
+ATF_TC_HEAD(clockwait_monotonic_relative, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "sem_clockwait_np(3) with monotonic clock and relative time");
+	atf_tc_set_md_var(tc, "timeout", "20");
+}
+ATF_TC_BODY(clockwait_monotonic_relative, tc)
+{
+	struct timespec ts;
+	sem_t sem;
+
+	SEM_REQUIRE(sem_init(&sem, 0, 0));
 
-	/* CLOCK_MONOTONIC, relative */
 	SEM_REQUIRE(sem_post(&sem));
 	ts.tv_sec = 0;
 	ts.tv_nsec = 100*1000*1000;
@@ -301,11 +339,28 @@ ATF_TC_BODY(timedwait, tc)
 	    &ts, NULL));
 	ATF_REQUIRE_ERRNO(ETIMEDOUT,
 	    sem_clockwait_np(&sem, CLOCK_MONOTONIC, 0, &ts, NULL));
+}
+
+ATF_TC(clockwait_absolute_intr_remaining);
+ATF_TC_HEAD(clockwait_absolute_intr_remaining, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "sem_clockwait_np(3) with absolute time does not update "
+	    "remaining time on EINTR");
+	atf_tc_set_md_var(tc, "timeout", "20");
+}
+ATF_TC_BODY(clockwait_absolute_intr_remaining, tc)
+{
+	struct timespec ts;
+	sem_t sem;
+
+	SEM_REQUIRE(sem_init(&sem, 0, 0));
+	setup_signals();
 
-	/* absolute does not update remaining time on EINTR */
 	struct timespec remain = {42, 1000*1000*1000};
-	got_sigalrm = 0;
-	it.it_value.tv_usec = 50*1000;
+	struct itimerval it = {
+		.it_value.tv_usec = 50*1000
+	};
 	ATF_REQUIRE_MSG(setitimer(ITIMER_REAL, &it, NULL) == 0,
 	    "%s", strerror(errno));
 	ATF_REQUIRE_MSG(clock_gettime(CLOCK_MONOTONIC, &ts) == 0,
@@ -316,12 +371,28 @@ ATF_TC_BODY(timedwait, tc)
 	ATF_REQUIRE_MSG(got_sigalrm, "did not get SIGALRM");
 	ATF_REQUIRE_MSG(remain.tv_sec == 42 && remain.tv_nsec == 1000*1000*1000,
 	    "an absolute clockwait modified the remaining time on EINTR");
+}
+
+ATF_TC(clockwait_relative_intr_remaining);
+ATF_TC_HEAD(clockwait_relative_intr_remaining, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "sem_clockwait_np(3) with relative time updates "
+	    "remaining time on EINTR");
+	atf_tc_set_md_var(tc, "timeout", "20");
+}
+ATF_TC_BODY(clockwait_relative_intr_remaining, tc)
+{
+	struct timespec ts;
+	sem_t sem;
 
-	/* relative updates remaining time on EINTR */
-	remain.tv_sec = 42;
-	remain.tv_nsec = 1000*1000*1000;
-	got_sigalrm = 0;
-	it.it_value.tv_usec = 50*1000;
+	SEM_REQUIRE(sem_init(&sem, 0, 0));
+	setup_signals();
+
+	struct timespec remain = {42, 1000*1000*1000};
+	struct itimerval it = {
+		.it_value.tv_usec = 50*1000
+	};
 	ATF_REQUIRE_MSG(setitimer(ITIMER_REAL, &it, NULL) == 0,
 	    "%s", strerror(errno));
 	ts.tv_sec = 0;
@@ -334,15 +405,22 @@ ATF_TC_BODY(timedwait, tc)
 	    remain.tv_nsec <= 75*1000*1000,
 	    "the remaining time was not as expected when a relative clockwait"
 	    " got EINTR: %ld.%09ld", remain.tv_sec, remain.tv_nsec);
-#endif
 }
 
+#endif	/* __FreeBSD__ */
+
 ATF_TP_ADD_TCS(tp)
 {
 
 	ATF_TP_ADD_TC(tp, basic);
 	ATF_TP_ADD_TC(tp, child);
 	ATF_TP_ADD_TC(tp, timedwait);
+#ifdef __FreeBSD__
+	ATF_TP_ADD_TC(tp, clockwait_monotonic_absolute);
+	ATF_TP_ADD_TC(tp, clockwait_monotonic_relative);
+	ATF_TP_ADD_TC(tp, clockwait_absolute_intr_remaining);
+	ATF_TP_ADD_TC(tp, clockwait_relative_intr_remaining);
+#endif
 
 	return atf_no_error();
 }