repeatable_thread_test.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "util/repeatable_thread.h"
  6. #include <atomic>
  7. #include <memory>
  8. #include "db/db_test_util.h"
  9. #include "test_util/mock_time_env.h"
  10. #include "test_util/sync_point.h"
  11. #include "test_util/testharness.h"
  12. class RepeatableThreadTest : public testing::Test {
  13. public:
  14. RepeatableThreadTest()
  15. : mock_clock_(std::make_shared<ROCKSDB_NAMESPACE::MockSystemClock>(
  16. ROCKSDB_NAMESPACE::SystemClock::Default())) {}
  17. protected:
  18. std::shared_ptr<ROCKSDB_NAMESPACE::MockSystemClock> mock_clock_;
  19. };
  20. TEST_F(RepeatableThreadTest, TimedTest) {
  21. constexpr uint64_t kSecond = 1000000; // 1s = 1000000us
  22. constexpr int kIteration = 3;
  23. const auto& clock = ROCKSDB_NAMESPACE::SystemClock::Default();
  24. ROCKSDB_NAMESPACE::port::Mutex mutex;
  25. ROCKSDB_NAMESPACE::port::CondVar test_cv(&mutex);
  26. int count = 0;
  27. uint64_t prev_time = clock->NowMicros();
  28. ROCKSDB_NAMESPACE::RepeatableThread thread(
  29. [&] {
  30. ROCKSDB_NAMESPACE::MutexLock l(&mutex);
  31. count++;
  32. uint64_t now = clock->NowMicros();
  33. assert(count == 1 || prev_time + 1 * kSecond <= now);
  34. prev_time = now;
  35. if (count >= kIteration) {
  36. test_cv.SignalAll();
  37. }
  38. },
  39. "rt_test", clock.get(), 1 * kSecond);
  40. // Wait for execution finish.
  41. {
  42. ROCKSDB_NAMESPACE::MutexLock l(&mutex);
  43. while (count < kIteration) {
  44. test_cv.Wait();
  45. }
  46. }
  47. // Test cancel
  48. thread.cancel();
  49. }
  50. TEST_F(RepeatableThreadTest, MockEnvTest) {
  51. constexpr uint64_t kSecond = 1000000; // 1s = 1000000us
  52. constexpr int kIteration = 3;
  53. mock_clock_->SetCurrentTime(0); // in seconds
  54. std::atomic<int> count{0};
  55. #if defined(OS_MACOSX) && !defined(NDEBUG)
  56. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
  57. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
  58. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
  59. "InstrumentedCondVar::TimedWaitInternal", [&](void* arg) {
  60. // Obtain the current (real) time in seconds and add 1000 extra seconds
  61. // to ensure that RepeatableThread::wait invokes TimedWait with a time
  62. // greater than (real) current time. This is to prevent the TimedWait
  63. // function from returning immediately without sleeping and releasing
  64. // the mutex on certain platforms, e.g. OS X. If TimedWait returns
  65. // immediately, the mutex will not be released, and
  66. // RepeatableThread::TEST_WaitForRun never has a chance to execute the
  67. // callback which, in this case, updates the result returned by
  68. // mock_clock->NowMicros. Consequently, RepeatableThread::wait cannot
  69. // break out of the loop, causing test to hang. The extra 1000 seconds
  70. // is a best-effort approach because there seems no reliable and
  71. // deterministic way to provide the aforementioned guarantee. By the
  72. // time RepeatableThread::wait is called, it is no guarantee that the
  73. // delay + mock_clock->NowMicros will be greater than the current real
  74. // time. However, 1000 seconds should be sufficient in most cases.
  75. uint64_t time_us = *static_cast<uint64_t*>(arg);
  76. if (time_us < mock_clock_->RealNowMicros()) {
  77. *static_cast<uint64_t*>(arg) = mock_clock_->RealNowMicros() + 1000;
  78. }
  79. });
  80. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
  81. #endif // OS_MACOSX && !NDEBUG
  82. ROCKSDB_NAMESPACE::RepeatableThread thread(
  83. [&] { count++; }, "rt_test", mock_clock_.get(), 1 * kSecond, 1 * kSecond);
  84. for (int i = 1; i <= kIteration; i++) {
  85. // Bump current time
  86. thread.TEST_WaitForRun([&] { mock_clock_->SetCurrentTime(i); });
  87. }
  88. // Test function should be exectued exactly kIteraion times.
  89. ASSERT_EQ(kIteration, count.load());
  90. // Test cancel
  91. thread.cancel();
  92. }
  93. int main(int argc, char** argv) {
  94. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  95. ::testing::InitGoogleTest(&argc, argv);
  96. return RUN_ALL_TESTS();
  97. }