From 166d71d18f01aa73fd35aae611692320952a75b5 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Tue, 20 Jun 2023 22:03:47 -0700 Subject: 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 --- absl/synchronization/mutex_test.cc | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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 -- cgit v1.2.3