]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/repeatable_thread_test.cc
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).
9 #include "db/db_test_util.h"
10 #include "util/repeatable_thread.h"
11 #include "util/testharness.h"
13 class RepeatableThreadTest
: public testing::Test
{
15 RepeatableThreadTest()
16 : mock_env_(new rocksdb::MockTimeEnv(rocksdb::Env::Default())) {}
19 std::unique_ptr
<rocksdb::MockTimeEnv
> mock_env_
;
22 TEST_F(RepeatableThreadTest
, TimedTest
) {
23 constexpr uint64_t kSecond
= 1000000; // 1s = 1000000us
24 constexpr int kIteration
= 3;
25 rocksdb::Env
* env
= rocksdb::Env::Default();
26 rocksdb::port::Mutex mutex
;
27 rocksdb::port::CondVar
test_cv(&mutex
);
29 uint64_t prev_time
= env
->NowMicros();
30 rocksdb::RepeatableThread
thread(
32 rocksdb::MutexLock
l(&mutex
);
34 uint64_t now
= env
->NowMicros();
35 assert(count
== 1 || prev_time
+ 1 * kSecond
<= now
);
37 if (count
>= kIteration
) {
41 "rt_test", env
, 1 * kSecond
);
42 // Wait for execution finish.
44 rocksdb::MutexLock
l(&mutex
);
45 while (count
< kIteration
) {
54 TEST_F(RepeatableThreadTest
, MockEnvTest
) {
55 constexpr uint64_t kSecond
= 1000000; // 1s = 1000000us
56 constexpr int kIteration
= 3;
57 mock_env_
->set_current_time(0); // in seconds
58 std::atomic
<int> count
{0};
59 rocksdb::RepeatableThread
thread([&] { count
++; }, "rt_test", mock_env_
.get(),
60 1 * kSecond
, 1 * kSecond
);
61 for (int i
= 1; i
<= kIteration
; i
++) {
63 thread
.TEST_WaitForRun([&] { mock_env_
->set_current_time(i
); });
65 // Test function should be exectued exactly kIteraion times.
66 ASSERT_EQ(kIteration
, count
.load());
72 int main(int argc
, char** argv
) {
73 ::testing::InitGoogleTest(&argc
, argv
);
75 return RUN_ALL_TESTS();