From 7b13e5584a605120371a6641735bf08ade71943e Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Sun, 11 Aug 2024 10:49:31 +0800 Subject: [PATCH] feat add timeout --- tile/base/thread/cond_var_test.cc | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tile/base/thread/cond_var_test.cc b/tile/base/thread/cond_var_test.cc index 1cc5404..086bfed 100644 --- a/tile/base/thread/cond_var_test.cc +++ b/tile/base/thread/cond_var_test.cc @@ -36,8 +36,8 @@ class CondVarTest : public ::testing::Test { protected: Mutex *m; CondVar *cv; - volatile bool is_set; - volatile bool is_timeout; + bool is_set; + bool is_timeout; std::thread *worker; }; @@ -69,6 +69,7 @@ TEST_F(CondVarTest, NotifyOne_Wait) { TEST_F(CondVarTest, NotifyOne_WaitFor) { // Wait // constexpr auto kWaitTimeoutMs = 100; + auto start = ReadSteadyClock(); constexpr auto kWaitTimeout = std::chrono::milliseconds(50); auto WaitFor = [&] { UniqueLock inner_locker(*m); @@ -91,9 +92,16 @@ TEST_F(CondVarTest, NotifyOne_WaitFor) { cv->NotifyOne(); } + auto now = ReadSteadyClock(); t1.join(); - ASSERT_TRUE(is_set); - ASSERT_FALSE(is_timeout); + if (now - start > kWaitTimeout) { + // timeout + ASSERT_TRUE(is_timeout); + ASSERT_FALSE(is_set); + } else { + ASSERT_FALSE(is_timeout); + ASSERT_TRUE(is_set); + } } // timeout