mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 07:34:45 +01:00
[libcxx testing] Remove ALLOW_RETRIES from last futures test
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.
This commit is contained in:
@@ -8,7 +8,6 @@
|
||||
//
|
||||
// UNSUPPORTED: libcpp-has-no-threads
|
||||
// UNSUPPORTED: c++98, c++03
|
||||
// ALLOW_RETRIES: 2
|
||||
|
||||
// <future>
|
||||
|
||||
@@ -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();
|
||||
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<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();
|
||||
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<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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user