repeatable_thread_test.cc 4.0 KB

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