feat add timeout
Some checks failed
ci/pr/linux-aarch64-gcc/1 Pipeline failed
ci/push/linux-aarch64-gcc/1 Pipeline was successful
ci/push/linux-x64-gcc/2 Pipeline failed
ci/push/linux-aarch64-gcc/2 Pipeline failed
ci/pr/linux-x64-gcc/1 Pipeline was successful
ci/pr/linux-aarch64-gcc/2 Pipeline failed
ci/push/linux-x64-gcc/1 Pipeline was successful
ci/pr/linux-x64-gcc/2 Pipeline failed

This commit is contained in:
tqcq 2024-08-11 10:49:31 +08:00
parent ef009a4ae9
commit 7b13e5584a

View File

@ -36,8 +36,8 @@ class CondVarTest : public ::testing::Test {
protected: protected:
Mutex *m; Mutex *m;
CondVar *cv; CondVar *cv;
volatile bool is_set; bool is_set;
volatile bool is_timeout; bool is_timeout;
std::thread *worker; std::thread *worker;
}; };
@ -69,6 +69,7 @@ TEST_F(CondVarTest, NotifyOne_Wait) {
TEST_F(CondVarTest, NotifyOne_WaitFor) { TEST_F(CondVarTest, NotifyOne_WaitFor) {
// Wait // Wait
// constexpr auto kWaitTimeoutMs = 100; // constexpr auto kWaitTimeoutMs = 100;
auto start = ReadSteadyClock();
constexpr auto kWaitTimeout = std::chrono::milliseconds(50); constexpr auto kWaitTimeout = std::chrono::milliseconds(50);
auto WaitFor = [&] { auto WaitFor = [&] {
UniqueLock<Mutex> inner_locker(*m); UniqueLock<Mutex> inner_locker(*m);
@ -91,9 +92,16 @@ TEST_F(CondVarTest, NotifyOne_WaitFor) {
cv->NotifyOne(); cv->NotifyOne();
} }
auto now = ReadSteadyClock();
t1.join(); t1.join();
ASSERT_TRUE(is_set); if (now - start > kWaitTimeout) {
// timeout
ASSERT_TRUE(is_timeout);
ASSERT_FALSE(is_set);
} else {
ASSERT_FALSE(is_timeout); ASSERT_FALSE(is_timeout);
ASSERT_TRUE(is_set);
}
} }
// timeout // timeout