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

via libc-commits libc-commits at lists.llvm.org
Fri Oct 20 06:00:55 PDT 2023


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

Double the sleeping time and check to make sure that nanosleep call succeeds.

>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] [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);



More information about the libc-commits mailing list