[libcxx] [test] Make the condvar wait_for tests less brittle

These seem to fail occasionally (they are marked as possibly requiring
a retry).

When doing a condvar wait_for(), it can wake up before the timeout
as a spurious wakeup. In these cases, the wait_for() method returns that
the timeout wasn't hit, and the test reruns another wait_for().

On Windows, it seems like the wait_for() operation often can end up
returning slightly before the intended deadline - when intending to
wait for 250 milliseconds, it can return after e.g. 235 milliseconds.
In these cases, the wait_for() doesn't indicate a timeout.

Previously, the test then reran a new wait_for() for a full 250
milliseconds each time. So for N consecutive wakeups slightly too early,
we'd wait for (N+1)*250 milliseconds. Now it only reruns wait_for() for
the remaining intended wait duration.

Differential Revision: https://reviews.llvm.org/D99175
This commit is contained in:
Martin Storsjö 2021-03-23 13:09:26 +02:00
parent 6ef4505298
commit 01aa9e1f6e
2 changed files with 12 additions and 6 deletions

View File

@ -44,9 +44,12 @@ void f()
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)
{

View File

@ -47,9 +47,12 @@ void f()
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)
{