From 698ca77f2a326f8733f7d5618fe7da957ab894c6 Mon Sep 17 00:00:00 2001 From: Qin Wang Date: Wed, 26 Apr 2023 14:51:41 -0700 Subject: [PATCH] Mutex guard FakeClock PiperOrigin-RevId: 527382176 --- internal/test/fake_clock.cc | 7 +++++-- internal/test/fake_clock.h | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/test/fake_clock.cc b/internal/test/fake_clock.cc index 418863b6..57c6b29d 100644 --- a/internal/test/fake_clock.cc +++ b/internal/test/fake_clock.cc @@ -21,7 +21,10 @@ namespace nearby { -absl::Time FakeClock::Now() const { return now_; } +absl::Time FakeClock::Now() const { + absl::MutexLock lock(&mutex_); + return now_; +} void FakeClock::AddObserver(absl::string_view name, std::function observer) { @@ -36,9 +39,9 @@ void FakeClock::RemoveObserver(absl::string_view name) { void FakeClock::FastForward(absl::Duration duration) { std::vector timer_callback_ids; - now_ += duration; { absl::MutexLock lock(&mutex_); + now_ += duration; for (const auto& observer : observers_) { timer_callback_ids.push_back(observer.first); } diff --git a/internal/test/fake_clock.h b/internal/test/fake_clock.h index dc64aae0..7cef161a 100644 --- a/internal/test/fake_clock.h +++ b/internal/test/fake_clock.h @@ -45,8 +45,8 @@ class FakeClock : public Clock { int GetObserversCount() ABSL_LOCKS_EXCLUDED(mutex_); private: - absl::Time now_; mutable absl::Mutex mutex_; + absl::Time now_ ABSL_GUARDED_BY(mutex_); absl::flat_hash_map> observers_ ABSL_GUARDED_BY(mutex_); };