[libcxx-commits] [PATCH] D99175: [libcxx] [test] Make the condvar wait_for tests less brittle

Martin Storsjö via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu Apr 1 02:40:18 PDT 2021


mstorsjo updated this revision to Diff 334627.
mstorsjo added a comment.

Changed to use a local variable for the duration. The variable used within the while clause needs to be declared outside of the loop, and thus can't use `auto`. But that allows removing the variable `now` from outside the loop.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D99175/new/

https://reviews.llvm.org/D99175

Files:
  libcxx/test/std/thread/thread.condition/thread.condition.condvar/wait_for.pass.cpp
  libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_for.pass.cpp


Index: libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_for.pass.cpp
===================================================================
--- libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_for.pass.cpp
+++ libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_for.pass.cpp
@@ -47,9 +47,12 @@
     test1 = 1;
     cv.notify_one();
     Clock::time_point t0 = Clock::now();
-    while (test2 == 0 &&
-           cv.wait_for(lk, milliseconds(250)) == std::cv_status::no_timeout)
-        ;
+    Clock::time_point wait_end = t0 + milliseconds(250);
+    Clock::duration d;
+    do {
+        d = wait_end - Clock::now();
+        if (d <= milliseconds(0)) break;
+    } while (test2 == 0 && cv.wait_for(lk, d) == std::cv_status::no_timeout);
     Clock::time_point t1 = Clock::now();
     if (runs == 0)
     {
Index: libcxx/test/std/thread/thread.condition/thread.condition.condvar/wait_for.pass.cpp
===================================================================
--- libcxx/test/std/thread/thread.condition/thread.condition.condvar/wait_for.pass.cpp
+++ libcxx/test/std/thread/thread.condition/thread.condition.condvar/wait_for.pass.cpp
@@ -44,9 +44,12 @@
     test1 = 1;
     cv.notify_one();
     Clock::time_point t0 = Clock::now();
-    while (test2 == 0 &&
-           cv.wait_for(lk, milliseconds(250)) == std::cv_status::no_timeout)
-        ;
+    Clock::time_point wait_end = t0 + milliseconds(250);
+    Clock::duration d;
+    do {
+        d = wait_end - Clock::now();
+        if (d <= milliseconds(0)) break;
+    } while (test2 == 0 && cv.wait_for(lk, d) == std::cv_status::no_timeout);
     Clock::time_point t1 = Clock::now();
     if (runs == 0)
     {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99175.334627.patch
Type: text/x-patch
Size: 1741 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210401/d6605aa2/attachment.bin>


More information about the libcxx-commits mailing list