Commit 166d71d1 by Abseil Team Committed by Copybara-Service

absl: add a Mutex::Await/LockWhen test

Check various corner cases for Await/LockWhen return value
with always true/always false conditions.
I don't see this explicitly tested anywhere else.

PiperOrigin-RevId: 542141533
Change-Id: Ia116c6dc199de606ad446c205951169ec5e2abe1
parent 55e8345c
......@@ -1868,4 +1868,29 @@ TEST(Mutex, WriterPriority) {
EXPECT_TRUE(saw_wrote.load());
}
TEST(Mutex, LockWhenWithTimeoutResult) {
// Check various corner cases for Await/LockWhen return value
// with always true/always false conditions.
absl::Mutex mu;
const bool kAlwaysTrue = true, kAlwaysFalse = false;
const absl::Condition kTrueCond(&kAlwaysTrue), kFalseCond(&kAlwaysFalse);
EXPECT_TRUE(mu.LockWhenWithTimeout(kTrueCond, absl::Milliseconds(1)));
mu.Unlock();
EXPECT_FALSE(mu.LockWhenWithTimeout(kFalseCond, absl::Milliseconds(1)));
EXPECT_TRUE(mu.AwaitWithTimeout(kTrueCond, absl::Milliseconds(1)));
EXPECT_FALSE(mu.AwaitWithTimeout(kFalseCond, absl::Milliseconds(1)));
std::thread th1([&]() {
EXPECT_TRUE(mu.LockWhenWithTimeout(kTrueCond, absl::Milliseconds(1)));
mu.Unlock();
});
std::thread th2([&]() {
EXPECT_FALSE(mu.LockWhenWithTimeout(kFalseCond, absl::Milliseconds(1)));
mu.Unlock();
});
absl::SleepFor(absl::Milliseconds(100));
mu.Unlock();
th1.join();
th2.join();
}
} // namespace
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment