notification_test.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/synchronization/notification.h"
  15. #include <thread> // NOLINT(build/c++11)
  16. #include <vector>
  17. #include "gtest/gtest.h"
  18. #include "absl/synchronization/mutex.h"
  19. namespace absl {
  20. ABSL_NAMESPACE_BEGIN
  21. // A thread-safe class that holds a counter.
  22. class ThreadSafeCounter {
  23. public:
  24. ThreadSafeCounter() : count_(0) {}
  25. void Increment() {
  26. MutexLock lock(&mutex_);
  27. ++count_;
  28. }
  29. int Get() const {
  30. MutexLock lock(&mutex_);
  31. return count_;
  32. }
  33. void WaitUntilGreaterOrEqual(int n) {
  34. MutexLock lock(&mutex_);
  35. auto cond = [this, n]() { return count_ >= n; };
  36. mutex_.Await(Condition(&cond));
  37. }
  38. private:
  39. mutable Mutex mutex_;
  40. int count_;
  41. };
  42. // Runs the |i|'th worker thread for the tests in BasicTests(). Increments the
  43. // |ready_counter|, waits on the |notification|, and then increments the
  44. // |done_counter|.
  45. static void RunWorker(int i, ThreadSafeCounter* ready_counter,
  46. Notification* notification,
  47. ThreadSafeCounter* done_counter) {
  48. ready_counter->Increment();
  49. notification->WaitForNotification();
  50. done_counter->Increment();
  51. }
  52. // Tests that the |notification| properly blocks and awakens threads. Assumes
  53. // that the |notification| is not yet triggered. If |notify_before_waiting| is
  54. // true, the |notification| is triggered before any threads are created, so the
  55. // threads never block in WaitForNotification(). Otherwise, the |notification|
  56. // is triggered at a later point when most threads are likely to be blocking in
  57. // WaitForNotification().
  58. static void BasicTests(bool notify_before_waiting, Notification* notification) {
  59. EXPECT_FALSE(notification->HasBeenNotified());
  60. EXPECT_FALSE(
  61. notification->WaitForNotificationWithTimeout(absl::Milliseconds(0)));
  62. EXPECT_FALSE(notification->WaitForNotificationWithDeadline(absl::Now()));
  63. const absl::Duration delay = absl::Milliseconds(50);
  64. const absl::Time start = absl::Now();
  65. EXPECT_FALSE(notification->WaitForNotificationWithTimeout(delay));
  66. const absl::Duration elapsed = absl::Now() - start;
  67. // Allow for a slight early return, to account for quality of implementation
  68. // issues on various platforms.
  69. const absl::Duration slop = absl::Microseconds(200);
  70. EXPECT_LE(delay - slop, elapsed)
  71. << "WaitForNotificationWithTimeout returned " << delay - elapsed
  72. << " early (with " << slop << " slop), start time was " << start;
  73. ThreadSafeCounter ready_counter;
  74. ThreadSafeCounter done_counter;
  75. if (notify_before_waiting) {
  76. notification->Notify();
  77. }
  78. // Create a bunch of threads that increment the |done_counter| after being
  79. // notified.
  80. const int kNumThreads = 10;
  81. std::vector<std::thread> workers;
  82. for (int i = 0; i < kNumThreads; ++i) {
  83. workers.push_back(std::thread(&RunWorker, i, &ready_counter, notification,
  84. &done_counter));
  85. }
  86. if (!notify_before_waiting) {
  87. ready_counter.WaitUntilGreaterOrEqual(kNumThreads);
  88. // Workers have not been notified yet, so the |done_counter| should be
  89. // unmodified.
  90. EXPECT_EQ(0, done_counter.Get());
  91. notification->Notify();
  92. }
  93. // After notifying and then joining the workers, both counters should be
  94. // fully incremented.
  95. notification->WaitForNotification(); // should exit immediately
  96. EXPECT_TRUE(notification->HasBeenNotified());
  97. EXPECT_TRUE(notification->WaitForNotificationWithTimeout(absl::Seconds(0)));
  98. EXPECT_TRUE(notification->WaitForNotificationWithDeadline(absl::Now()));
  99. for (std::thread& worker : workers) {
  100. worker.join();
  101. }
  102. EXPECT_EQ(kNumThreads, ready_counter.Get());
  103. EXPECT_EQ(kNumThreads, done_counter.Get());
  104. }
  105. TEST(NotificationTest, SanityTest) {
  106. Notification local_notification1, local_notification2;
  107. BasicTests(false, &local_notification1);
  108. BasicTests(true, &local_notification2);
  109. }
  110. ABSL_NAMESPACE_END
  111. } // namespace absl