diff --git a/libcxx/test/std/thread/futures/futures.unique_future/wait_for.pass.cpp b/libcxx/test/std/thread/futures/futures.unique_future/wait_for.pass.cpp index d055f6468467..bdd6b9e2b4d3 100644 --- a/libcxx/test/std/thread/futures/futures.unique_future/wait_for.pass.cpp +++ b/libcxx/test/std/thread/futures/futures.unique_future/wait_for.pass.cpp @@ -8,7 +8,6 @@ // // UNSUPPORTED: libcpp-has-no-threads // UNSUPPORTED: c++98, c++03 -// ALLOW_RETRIES: 2 // @@ -25,49 +24,63 @@ typedef std::chrono::milliseconds ms; +static const ms sleepTime(500); +static const ms waitTime(5000); + void func1(std::promise p) { - std::this_thread::sleep_for(ms(500)); - p.set_value(3); + std::this_thread::sleep_for(sleepTime); + p.set_value(3); } int j = 0; void func3(std::promise p) { - std::this_thread::sleep_for(ms(500)); - j = 5; - p.set_value(j); + std::this_thread::sleep_for(sleepTime); + j = 5; + p.set_value(j); } void func5(std::promise p) { - std::this_thread::sleep_for(ms(500)); - p.set_value(); + std::this_thread::sleep_for(sleepTime); + p.set_value(); } template -void test(F func) { - typedef std::chrono::high_resolution_clock Clock; - std::promise p; - std::future f = p.get_future(); - std::thread(func, std::move(p)).detach(); - assert(f.valid()); - assert(f.wait_for(ms(300)) == std::future_status::timeout); - assert(f.valid()); - assert(f.wait_for(ms(300)) == std::future_status::ready); - assert(f.valid()); - Clock::time_point t0 = Clock::now(); +void test(F func, bool waitFirst) { + typedef std::chrono::high_resolution_clock Clock; + std::promise p; + std::future f = p.get_future(); + Clock::time_point t1, t0 = Clock::now(); + std::thread(func, std::move(p)).detach(); + assert(f.valid()); + assert(f.wait_for(ms(1)) == std::future_status::timeout); + assert(f.valid()); + if (waitFirst) { f.wait(); - Clock::time_point t1 = Clock::now(); assert(f.valid()); - assert(t1-t0 < ms(50)); + t1 = Clock::now(); + assert(f.wait_for(ms(waitTime)) == std::future_status::ready); + assert(f.valid()); + } else { + assert(f.wait_for(ms(waitTime)) == std::future_status::ready); + assert(f.valid()); + t1 = Clock::now(); + f.wait(); + assert(f.valid()); + } + assert(t1 - t0 >= sleepTime); } int main(int, char**) { - test(func1); - test(func3); - test(func5); - return 0; + test(func1, true); + test(func3, true); + test(func5, true); + test(func1, false); + test(func3, false); + test(func5, false); + return 0; }