feat fast test
Some checks failed
ci/push/linux-aarch64-gcc/1 Pipeline failed
ci/pr/linux-aarch64-gcc/1 Pipeline failed
ci/push/linux-x64-gcc/2 Pipeline failed
ci/push/linux-aarch64-gcc/2 Pipeline failed
ci/pr/linux-x64-gcc/1 Pipeline failed
ci/push/linux-x64-gcc/1 Pipeline failed
ci/pr/linux-x64-gcc/2 Pipeline failed
ci/pr/linux-aarch64-gcc/2 Pipeline failed

This commit is contained in:
tqcq 2024-08-11 10:36:49 +08:00
parent 5d28389cac
commit ef009a4ae9

View File

@ -13,9 +13,8 @@ class CondVarTest : public ::testing::Test {
void SetUp() override { void SetUp() override {
m = new Mutex(); m = new Mutex();
cv = new CondVar(); cv = new CondVar();
is_set = false;
is_timeout = false;
worker = nullptr; worker = nullptr;
ResetEnv();
} }
void TearDown() override { void TearDown() override {
@ -25,6 +24,13 @@ class CondVarTest : public ::testing::Test {
} }
delete worker; delete worker;
} }
delete m;
delete cv;
}
void ResetEnv() {
is_set = false;
is_timeout = false;
} }
protected: protected:
@ -62,12 +68,13 @@ 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;
constexpr auto kWaitTimeout = std::chrono::milliseconds(50);
auto WaitFor = [&] { auto WaitFor = [&] {
UniqueLock<Mutex> inner_locker(*m); UniqueLock<Mutex> inner_locker(*m);
if (cv->WaitFor(inner_locker, std::chrono::milliseconds(kWaitTimeoutMs))) { if (cv->WaitFor(inner_locker, kWaitTimeout)) {
cv->NotifyOne();
is_set = true; is_set = true;
cv->NotifyOne();
} else { } else {
is_timeout = true; is_timeout = true;
} }
@ -76,7 +83,7 @@ TEST_F(CondVarTest, NotifyOne_WaitFor) {
std::thread t1(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); ASSERT_FALSE(is_set);
{ {
@ -86,6 +93,7 @@ TEST_F(CondVarTest, NotifyOne_WaitFor) {
t1.join(); t1.join();
ASSERT_TRUE(is_set); ASSERT_TRUE(is_set);
ASSERT_FALSE(is_timeout);
} }
// timeout // timeout
@ -95,8 +103,7 @@ TEST_F(CondVarTest, NotifyOne_WaitFor) {
{ {
UniqueLock<Mutex> locker(*m); UniqueLock<Mutex> locker(*m);
ASSERT_FALSE(is_timeout); ASSERT_FALSE(is_timeout);
std::this_thread::sleep_for( std::this_thread::sleep_for(kWaitTimeout);
std::chrono::milliseconds(kWaitTimeoutMs * 2));
ASSERT_FALSE(is_timeout); ASSERT_FALSE(is_timeout);
cv->NotifyOne(); cv->NotifyOne();
} }