feat/support_fiber #6

Merged
tqcq merged 52 commits from feat/support_fiber into master 2024-08-11 13:03:04 +08:00
Showing only changes of commit b5541c581c - Show all commits

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);
}
}