更新时间:2023-09-17 17:58:58
我刚遇到这种情况.
我想为我的反应堆添加一个失败的测试.反应堆永远不会完工. (它必须首先失败).但是我不希望测试永远运行.
I wanted to add a failing test for my reactor. The reactor never finishes. (it has to fail first). But I don't want the test to run forever.
我跟踪了您的链接,但仍然不满意.因此,我决定使用某些C ++ 14功能,并使它相对简单.
I followed your link but still not joy there. So I decided to use some of the C++14 features and it makes it relatively simple.
但是我实现了这样的超时:
But I implemented the timeout like this:
TEST(Init, run)
{
// Step 1 Set up my code to run.
ThorsAnvil::Async::Reactor reactor;
std::unique_ptr<ThorsAnvil::Async::Handler> handler(new TestHandler("test/data/input"));
ThorsAnvil::Async::HandlerId id = reactor.registerHandler(std::move(handler));
// Step 2
// Run the code async.
auto asyncFuture = std::async(
std::launch::async, [&reactor]() {
reactor.run(); // The TestHandler
// should call reactor.shutDown()
// when it is finished.
// if it does not then
// the test failed.
});
// Step 3
// DO your timeout test.
EXPECT_TRUE(asyncFuture.wait_for(std::chrono::milliseconds(5000)) != std::future_status::timeout);
// Step 4
// Clean up your resources.
reactor.shutDown(); // this will allow run() to exit.
// and the thread to die.
}
现在我的测试失败了,我可以编写修复测试的代码了.
Now that I have my failing test I can write the code that fixes the test.