From ef009a4ae95128102fef8432cebffe7ca518f256 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Sun, 11 Aug 2024 10:36:49 +0800 Subject: [PATCH] feat fast test --- tile/base/thread/cond_var_test.cc | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/tile/base/thread/cond_var_test.cc b/tile/base/thread/cond_var_test.cc index 033182c..1cc5404 100644 --- a/tile/base/thread/cond_var_test.cc +++ b/tile/base/thread/cond_var_test.cc @@ -13,9 +13,8 @@ class CondVarTest : public ::testing::Test { void SetUp() override { m = new Mutex(); cv = new CondVar(); - is_set = false; - is_timeout = false; worker = nullptr; + ResetEnv(); } void TearDown() override { @@ -25,6 +24,13 @@ class CondVarTest : public ::testing::Test { } delete worker; } + delete m; + delete cv; + } + + void ResetEnv() { + is_set = false; + is_timeout = false; } protected: @@ -62,12 +68,13 @@ TEST_F(CondVarTest, NotifyOne_Wait) { TEST_F(CondVarTest, NotifyOne_WaitFor) { // Wait - constexpr auto kWaitTimeoutMs = 100; + // constexpr auto kWaitTimeoutMs = 100; + constexpr auto kWaitTimeout = std::chrono::milliseconds(50); auto WaitFor = [&] { UniqueLock inner_locker(*m); - if (cv->WaitFor(inner_locker, std::chrono::milliseconds(kWaitTimeoutMs))) { - cv->NotifyOne(); + if (cv->WaitFor(inner_locker, kWaitTimeout)) { is_set = true; + cv->NotifyOne(); } else { is_timeout = true; } @@ -76,7 +83,7 @@ TEST_F(CondVarTest, NotifyOne_WaitFor) { std::thread t1(WaitFor); - std::this_thread::sleep_for(std::chrono::milliseconds(kWaitTimeoutMs / 2)); + std::this_thread::sleep_for(kWaitTimeout / 2); ASSERT_FALSE(is_set); { @@ -86,6 +93,7 @@ TEST_F(CondVarTest, NotifyOne_WaitFor) { t1.join(); ASSERT_TRUE(is_set); + ASSERT_FALSE(is_timeout); } // timeout @@ -95,8 +103,7 @@ TEST_F(CondVarTest, NotifyOne_WaitFor) { { UniqueLock locker(*m); ASSERT_FALSE(is_timeout); - std::this_thread::sleep_for( - std::chrono::milliseconds(kWaitTimeoutMs * 2)); + std::this_thread::sleep_for(kWaitTimeout); ASSERT_FALSE(is_timeout); cv->NotifyOne(); }