aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiuliano Procida <gprocida@google.com>2021-04-08 10:51:58 +0100
committerGiuliano Procida <gprocida@google.com>2021-04-08 10:58:57 +0100
commit096f5954e4bf8ad23ac518aa28e6135f22f0062f (patch)
treeeb3145edda1b46a06f1421a7048e84b2091dbc1c
parentebc88d26a5548d65b45bbec1091d5573d011cc0d (diff)
downloadbionic-096f5954e4bf8ad23ac518aa28e6135f22f0062f.tar.gz
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>
-rw-r--r--tests/time_test.cpp28
1 files changed, 13 insertions, 15 deletions
diff --git a/tests/time_test.cpp b/tests/time_test.cpp
index 16299cc52..b16fe16bc 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, timer_delete_from_timer_thread) {
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));
-
- // 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;
+ 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));
+
+ // 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);
+ }
+ ASSERT_LE(ts1.tv_sec, ts2.tv_sec);
+ if (ts1.tv_sec == ts2.tv_sec) {
+ ASSERT_LE(ts1.tv_nsec, ts2.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);
}
TEST(time, clock_gettime_CLOCK_REALTIME) {