Skip to content

Commit

Permalink
[libcxx testing] Remove ALLOW_RETRIES from last futures test
Browse files Browse the repository at this point in the history
Like other uses of ALLOW_RETRIES, this test tried to verify that an API
returned "quickly" but quick is not safe to define given slow and/or
busy machines.

Instead, we now verify that these "wait" APIs actually wait, which the
old test did not.
  • Loading branch information
davezarzycki committed May 16, 2020
1 parent 13d44b2 commit 3f66bb2
Showing 1 changed file with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03
// ALLOW_RETRIES: 2

// <future>

Expand All @@ -25,49 +24,63 @@

typedef std::chrono::milliseconds ms;

static const ms sleepTime(500);
static const ms waitTime(5000);

void func1(std::promise<int> 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<int&> 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<void> p)
{
std::this_thread::sleep_for(ms(500));
p.set_value();
std::this_thread::sleep_for(sleepTime);
p.set_value();
}

template <typename T, typename F>
void test(F func) {
typedef std::chrono::high_resolution_clock Clock;
std::promise<T> p;
std::future<T> f = p.get_future();
std::thread(func, std::move(p)).detach();
void test(F func, bool waitFirst) {
typedef std::chrono::high_resolution_clock Clock;
std::promise<T> p;
std::future<T> 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();
assert(f.valid());
assert(f.wait_for(ms(300)) == std::future_status::timeout);
t1 = Clock::now();
assert(f.wait_for(ms(waitTime)) == std::future_status::ready);
assert(f.valid());
assert(f.wait_for(ms(300)) == std::future_status::ready);
} else {
assert(f.wait_for(ms(waitTime)) == std::future_status::ready);
assert(f.valid());
Clock::time_point t0 = Clock::now();
t1 = Clock::now();
f.wait();
Clock::time_point t1 = Clock::now();
assert(f.valid());
assert(t1-t0 < ms(50));
}
assert(t1 - t0 >= sleepTime);
}

int main(int, char**)
{
test<int>(func1);
test<int&>(func3);
test<void>(func5);
return 0;
test<int>(func1, true);
test<int&>(func3, true);
test<void>(func5, true);
test<int>(func1, false);
test<int&>(func3, false);
test<void>(func5, false);
return 0;
}

0 comments on commit 3f66bb2

Please sign in to comment.