Deflake time.clock_gettime test
The test aims to check that the time obtained the VDSO is the "same"
as that obtained via the system call. Unfortunately, time progresses.
Any check involving some fixed tolerance will have some non-zero
probability of failure.
We can instead check that a VDSO time value lies between two system
call times.
Bug: 184819133
Change-Id: Idb9c17b9f612613f6e18a56ee0f256971ddbdf1f
Signed-off-by: Giuliano Procida <gprocida@google.com>
diff --git a/tests/time_test.cpp b/tests/time_test.cpp
index 16299cc..b16fe16 100644
--- a/tests/time_test.cpp
+++ b/tests/time_test.cpp
@@ -31,8 +31,6 @@
#include "SignalUtils.h"
#include "utils.h"
-#include "private/bionic_constants.h"
-
using namespace std::chrono_literals;
TEST(time, time) {
@@ -760,22 +758,22 @@
TEST(time, clock_gettime) {
// Try to ensure that our vdso clock_gettime is working.
+ timespec ts0;
timespec ts1;
- ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1));
timespec ts2;
- ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2));
+ ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts0));
+ ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts1));
+ ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts2));
- // What's the difference between the two?
- ts2.tv_sec -= ts1.tv_sec;
- ts2.tv_nsec -= ts1.tv_nsec;
- if (ts2.tv_nsec < 0) {
- --ts2.tv_sec;
- ts2.tv_nsec += NS_PER_S;
+ // Check we have a nice monotonic timestamp sandwich.
+ ASSERT_LE(ts0.tv_sec, ts1.tv_sec);
+ if (ts0.tv_sec == ts1.tv_sec) {
+ ASSERT_LE(ts0.tv_nsec, ts1.tv_nsec);
}
-
- // To try to avoid flakiness we'll accept answers within 10,000,000ns (0.01s).
- ASSERT_EQ(0, ts2.tv_sec);
- ASSERT_LT(ts2.tv_nsec, 10'000'000);
+ ASSERT_LE(ts1.tv_sec, ts2.tv_sec);
+ if (ts1.tv_sec == ts2.tv_sec) {
+ ASSERT_LE(ts1.tv_nsec, ts2.tv_nsec);
+ }
}
TEST(time, clock_gettime_CLOCK_REALTIME) {