fix modify test
Some checks failed
ci/pr/linux-aarch64-gcc/1 Pipeline was successful
ci/push/linux-x64-gcc/1 Pipeline was successful
ci/push/linux-x64-gcc/2 Pipeline was successful
ci/push/linux-aarch64-gcc/2 Pipeline was successful
ci/pr/linux-x64-gcc/1 Pipeline was successful
ci/pr/linux-aarch64-gcc/2 Pipeline was successful
ci/push/linux-aarch64-gcc/1 Pipeline failed
ci/pr/linux-x64-gcc/2 Pipeline was successful

This commit is contained in:
tqcq 2024-08-11 11:12:08 +08:00
parent 7b13e5584a
commit b5541c581c

View File

@ -10,6 +10,7 @@
namespace tile {
class CondVarTest : public ::testing::Test {
protected:
void SetUp() override {
m = new Mutex();
cv = new CondVar();
@ -18,24 +19,39 @@ class CondVarTest : public ::testing::Test {
}
void TearDown() override {
SetWorker(nullptr);
delete m;
delete cv;
}
void SetWorker(std::thread *new_worker) {
if (worker) {
if (worker->joinable()) {
worker->join();
}
delete worker;
}
delete m;
delete cv;
worker = new_worker;
}
void ResetEnv() {
is_started = false;
is_set = false;
is_timeout = false;
}
void SetStarted() { is_started = true; }
void WaitStarted() {
while (!is_started) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
protected:
Mutex *m;
CondVar *cv;
bool is_started;
bool is_set;
bool is_timeout;
std::thread *worker;
@ -73,6 +89,7 @@ TEST_F(CondVarTest, NotifyOne_WaitFor) {
constexpr auto kWaitTimeout = std::chrono::milliseconds(50);
auto WaitFor = [&] {
UniqueLock<Mutex> inner_locker(*m);
SetStarted();
if (cv->WaitFor(inner_locker, kWaitTimeout)) {
is_set = true;
cv->NotifyOne();
@ -80,10 +97,11 @@ TEST_F(CondVarTest, NotifyOne_WaitFor) {
is_timeout = true;
}
};
{
std::thread t1(WaitFor);
SetWorker(new std::thread(WaitFor));
WaitStarted();
std::this_thread::sleep_for(kWaitTimeout / 2);
ASSERT_FALSE(is_set);
@ -93,7 +111,7 @@ TEST_F(CondVarTest, NotifyOne_WaitFor) {
}
auto now = ReadSteadyClock();
t1.join();
worker->join();
if (now - start > kWaitTimeout) {
// timeout
ASSERT_TRUE(is_timeout);
@ -106,18 +124,15 @@ TEST_F(CondVarTest, NotifyOne_WaitFor) {
// timeout
{
std::thread t1(WaitFor);
ResetEnv();
SetWorker(new std::thread(WaitFor));
{
UniqueLock<Mutex> locker(*m);
ASSERT_FALSE(is_timeout);
std::this_thread::sleep_for(kWaitTimeout);
ASSERT_FALSE(is_timeout);
cv->NotifyOne();
}
t1.join();
worker->join();
ASSERT_TRUE(is_timeout);
ASSERT_FALSE(is_set);
}
}