[libc-commits] [libc] [libc][NFC] Attempt to deflake gettimeofday_test. (PR #69719)

via libc-commits libc-commits at lists.llvm.org
Fri Oct 20 07:15:13 PDT 2023


https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/69719

>From f824b6bc701d6f31629d154f8dc5209b558ba571 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Fri, 20 Oct 2023 08:58:21 -0400
Subject: [PATCH 1/2] [libc][NFC] Attempt to deflake gettimeofday_test.

---
 libc/test/src/time/gettimeofday_test.cpp | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/libc/test/src/time/gettimeofday_test.cpp b/libc/test/src/time/gettimeofday_test.cpp
index 2deb7726264ee39..382c27d29612dee 100644
--- a/libc/test/src/time/gettimeofday_test.cpp
+++ b/libc/test/src/time/gettimeofday_test.cpp
@@ -15,24 +15,31 @@
 
 namespace cpp = LIBC_NAMESPACE::cpp;
 
+using LIBC_NAMESPACE::testing::tlog;
+
 TEST(LlvmLibcGettimeofday, SmokeTest) {
   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
   void *tz = nullptr;
-  timeval tv;
 
   suseconds_t sleep_times[2] = {200, 1000};
   for (int i = 0; i < 2; i++) {
+    timeval tv;
     int ret = LIBC_NAMESPACE::gettimeofday(&tv, tz);
     ASSERT_EQ(ret, 0);
 
     suseconds_t sleep_time = sleep_times[i];
-    // Sleep for {sleep_time} microsceconds.
-    timespec tim = {0, sleep_time * 1000};
+    // Sleep for {2 * sleep_time} microsceconds.
+    timespec tim = {0, 2 * sleep_time * 1000};
     timespec tim2 = {0, 0};
     ret = LIBC_NAMESPACE::nanosleep(&tim, &tim2);
 
+    if (ret < 0) {
+      tlog << "nanosleep call failed, skip the remaining of the test.";
+      return;
+    }
+
     // Call gettimeofday again and verify that it is more {sleep_time}
-    // microscecods.
+    // microseconds.
     timeval tv1;
     ret = LIBC_NAMESPACE::gettimeofday(&tv1, tz);
     ASSERT_EQ(ret, 0);

>From fe7e5eb73c609796df847ed72307b125e455e030 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Fri, 20 Oct 2023 10:13:37 -0400
Subject: [PATCH 2/2] Check if the clock goes backward and retry once.

---
 libc/test/src/time/gettimeofday_test.cpp | 27 ++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)

diff --git a/libc/test/src/time/gettimeofday_test.cpp b/libc/test/src/time/gettimeofday_test.cpp
index 382c27d29612dee..daec0526ea25746 100644
--- a/libc/test/src/time/gettimeofday_test.cpp
+++ b/libc/test/src/time/gettimeofday_test.cpp
@@ -21,6 +21,8 @@ TEST(LlvmLibcGettimeofday, SmokeTest) {
   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
   void *tz = nullptr;
 
+  bool retry = false;
+
   suseconds_t sleep_times[2] = {200, 1000};
   for (int i = 0; i < 2; i++) {
     timeval tv;
@@ -28,14 +30,14 @@ TEST(LlvmLibcGettimeofday, SmokeTest) {
     ASSERT_EQ(ret, 0);
 
     suseconds_t sleep_time = sleep_times[i];
-    // Sleep for {2 * sleep_time} microsceconds.
-    timespec tim = {0, 2 * sleep_time * 1000};
+    // Sleep for {sleep_time} microsceconds.
+    timespec tim = {0, sleep_time * 1000};
     timespec tim2 = {0, 0};
     ret = LIBC_NAMESPACE::nanosleep(&tim, &tim2);
 
     if (ret < 0) {
       tlog << "nanosleep call failed, skip the remaining of the test.";
-      return;
+      continue;
     }
 
     // Call gettimeofday again and verify that it is more {sleep_time}
@@ -43,6 +45,23 @@ TEST(LlvmLibcGettimeofday, SmokeTest) {
     timeval tv1;
     ret = LIBC_NAMESPACE::gettimeofday(&tv1, tz);
     ASSERT_EQ(ret, 0);
-    ASSERT_GE(tv1.tv_usec - tv.tv_usec, sleep_time);
+
+    auto diff = tv1.tv_usec - tv.tv_usec;
+    if (diff < 0) {
+      tlog << "Time goes backward from tv: " << tv.tv_usec
+           << " to tv1: " << tv1.tv_usec;
+
+      if (!retry) {
+        tlog << "Retry the test.";
+        retry = true;
+        --i;
+      } else {
+        tlog << "Skip the remaining of the test.";
+      }
+
+      continue;
+    }
+
+    ASSERT_GE(diff, sleep_time);
   }
 }



More information about the libc-commits mailing list