Skip to content

Commit 166d71d

Browse files
Abseil Teamcopybara-github
authored andcommitted
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
1 parent 55e8345 commit 166d71d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

absl/synchronization/mutex_test.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,4 +1868,29 @@ TEST(Mutex, WriterPriority) {
18681868
EXPECT_TRUE(saw_wrote.load());
18691869
}
18701870

1871+
TEST(Mutex, LockWhenWithTimeoutResult) {
1872+
// Check various corner cases for Await/LockWhen return value
1873+
// with always true/always false conditions.
1874+
absl::Mutex mu;
1875+
const bool kAlwaysTrue = true, kAlwaysFalse = false;
1876+
const absl::Condition kTrueCond(&kAlwaysTrue), kFalseCond(&kAlwaysFalse);
1877+
EXPECT_TRUE(mu.LockWhenWithTimeout(kTrueCond, absl::Milliseconds(1)));
1878+
mu.Unlock();
1879+
EXPECT_FALSE(mu.LockWhenWithTimeout(kFalseCond, absl::Milliseconds(1)));
1880+
EXPECT_TRUE(mu.AwaitWithTimeout(kTrueCond, absl::Milliseconds(1)));
1881+
EXPECT_FALSE(mu.AwaitWithTimeout(kFalseCond, absl::Milliseconds(1)));
1882+
std::thread th1([&]() {
1883+
EXPECT_TRUE(mu.LockWhenWithTimeout(kTrueCond, absl::Milliseconds(1)));
1884+
mu.Unlock();
1885+
});
1886+
std::thread th2([&]() {
1887+
EXPECT_FALSE(mu.LockWhenWithTimeout(kFalseCond, absl::Milliseconds(1)));
1888+
mu.Unlock();
1889+
});
1890+
absl::SleepFor(absl::Milliseconds(100));
1891+
mu.Unlock();
1892+
th1.join();
1893+
th2.join();
1894+
}
1895+
18711896
} // namespace

0 commit comments

Comments
 (0)