我正在尝试使用谷歌测试/模拟来测试阻塞与异步。
不幸的是,我无法想出一种测试方法来确保第一种情况下发生异步,第二种情况下发生阻塞。
有没有办法确认std::future的行为是否正常?
代码:
#include <gtest/gtest.h>
#include <future>
static unsigned a_slow_calc()
{
sleep( 1 );
return 1u;
}
TEST( Test_future, Ensure_async )
{
// 1. immediately returns
std::future<unsigned> answer = std::async( a_slow_calc );
// 2. std::future::get BLOCKS until the result is ready
EXPECT_EQ( 1u, answer.get() );
}