summaryrefslogtreecommitdiff
path: root/absl/synchronization
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2023-06-20 22:03:47 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2023-06-20 22:04:29 -0700
commit166d71d18f01aa73fd35aae611692320952a75b5 (patch)
tree35f2c63362bd3706746bda748b14c830359caf7e /absl/synchronization
parent55e8345c57bf8d88342f634f279239d19e77826c (diff)
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
Diffstat (limited to 'absl/synchronization')
-rw-r--r--absl/synchronization/mutex_test.cc25
1 files changed, 25 insertions, 0 deletions
diff --git a/absl/synchronization/mutex_test.cc b/absl/synchronization/mutex_test.cc
index 35802b2e..b585c342 100644
--- a/absl/synchronization/mutex_test.cc
+++ b/absl/synchronization/mutex_test.cc
@@ -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