[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:35:34 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/3] [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/3] 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);
   }
 }

>From 654dda1a1a1860116cbbb99dfff612273c5023ec Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Fri, 20 Oct 2023 10:34:50 -0400
Subject: [PATCH 3/3] Only check if gettimeofday call succeeds.

---
 libc/test/src/time/CMakeLists.txt        |  7 ---
 libc/test/src/time/gettimeofday_test.cpp | 55 ++----------------------
 2 files changed, 3 insertions(+), 59 deletions(-)

diff --git a/libc/test/src/time/CMakeLists.txt b/libc/test/src/time/CMakeLists.txt
index fd8f9762647f483..10b63ce6f39d295 100644
--- a/libc/test/src/time/CMakeLists.txt
+++ b/libc/test/src/time/CMakeLists.txt
@@ -56,16 +56,9 @@ add_libc_unittest(
     libc_time_unittests
   SRCS
     gettimeofday_test.cpp
-  HDRS
-    TmHelper.h
-    TmMatcher.h
-  CXX_STANDARD
-    20
   DEPENDS
     libc.include.time
     libc.src.time.gettimeofday
-    libc.src.time.nanosleep
-    libc.src.errno.errno
 )
 
 add_libc_unittest(
diff --git a/libc/test/src/time/gettimeofday_test.cpp b/libc/test/src/time/gettimeofday_test.cpp
index daec0526ea25746..ee934b7f3a20102 100644
--- a/libc/test/src/time/gettimeofday_test.cpp
+++ b/libc/test/src/time/gettimeofday_test.cpp
@@ -9,59 +9,10 @@
 #include <time.h>
 
 #include "src/time/gettimeofday.h"
-#include "src/time/nanosleep.h"
-#include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-namespace cpp = LIBC_NAMESPACE::cpp;
-
-using LIBC_NAMESPACE::testing::tlog;
-
 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;
-    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};
-    timespec tim2 = {0, 0};
-    ret = LIBC_NAMESPACE::nanosleep(&tim, &tim2);
-
-    if (ret < 0) {
-      tlog << "nanosleep call failed, skip the remaining of the test.";
-      continue;
-    }
-
-    // Call gettimeofday again and verify that it is more {sleep_time}
-    // microseconds.
-    timeval tv1;
-    ret = LIBC_NAMESPACE::gettimeofday(&tv1, tz);
-    ASSERT_EQ(ret, 0);
-
-    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);
-  }
+  timeval tv;
+  int ret = LIBC_NAMESPACE::gettimeofday(&tv, nullptr);
+  ASSERT_EQ(ret, 0);
 }



More information about the libc-commits mailing list