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 ef009a4ae9 - Show all commits

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