2020-04-07 18:53:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
|
|
|
|
#include "util/timer.h"
|
|
|
|
|
|
|
|
#include "db/db_test_util.h"
|
2021-11-03 19:20:19 +00:00
|
|
|
#include "rocksdb/file_system.h"
|
2021-01-26 06:07:26 +00:00
|
|
|
#include "test_util/mock_time_env.h"
|
2020-04-07 18:53:00 +00:00
|
|
|
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
|
|
|
|
class TimerTest : public testing::Test {
|
|
|
|
public:
|
2021-01-26 06:07:26 +00:00
|
|
|
TimerTest()
|
|
|
|
: mock_clock_(std::make_shared<MockSystemClock>(SystemClock::Default())) {
|
|
|
|
}
|
2020-04-07 18:53:00 +00:00
|
|
|
|
|
|
|
protected:
|
2021-01-26 06:07:26 +00:00
|
|
|
std::shared_ptr<MockSystemClock> mock_clock_;
|
2020-08-20 15:42:05 +00:00
|
|
|
|
2021-01-26 06:07:26 +00:00
|
|
|
void SetUp() override { mock_clock_->InstallTimedWaitFixCallback(); }
|
2020-08-21 18:31:27 +00:00
|
|
|
|
|
|
|
const int kUsPerSec = 1000000;
|
2020-04-07 18:53:00 +00:00
|
|
|
};
|
|
|
|
|
2020-08-21 18:31:27 +00:00
|
|
|
TEST_F(TimerTest, SingleScheduleOnce) {
|
|
|
|
const int kInitDelayUs = 1 * kUsPerSec;
|
2021-03-15 11:32:24 +00:00
|
|
|
Timer timer(mock_clock_.get());
|
2020-08-20 15:42:05 +00:00
|
|
|
|
2020-04-07 18:53:00 +00:00
|
|
|
int count = 0;
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.Add([&] { count++; }, "fn_sch_test", kInitDelayUs, 0);
|
2020-04-07 18:53:00 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(timer.Start());
|
|
|
|
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(0, count);
|
2020-04-07 18:53:00 +00:00
|
|
|
// Wait for execution to finish
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.TEST_WaitForRun(
|
2021-09-21 15:53:03 +00:00
|
|
|
[&] { mock_clock_->SleepForMicroseconds(kInitDelayUs); });
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(1, count);
|
2020-04-07 18:53:00 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(timer.Shutdown());
|
|
|
|
}
|
|
|
|
|
2020-08-21 18:31:27 +00:00
|
|
|
TEST_F(TimerTest, MultipleScheduleOnce) {
|
|
|
|
const int kInitDelay1Us = 1 * kUsPerSec;
|
|
|
|
const int kInitDelay2Us = 3 * kUsPerSec;
|
2021-03-15 11:32:24 +00:00
|
|
|
Timer timer(mock_clock_.get());
|
2020-08-20 15:42:05 +00:00
|
|
|
|
2020-04-07 18:53:00 +00:00
|
|
|
int count1 = 0;
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.Add([&] { count1++; }, "fn_sch_test1", kInitDelay1Us, 0);
|
2020-04-07 18:53:00 +00:00
|
|
|
|
|
|
|
int count2 = 0;
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.Add([&] { count2++; }, "fn_sch_test2", kInitDelay2Us, 0);
|
2020-04-07 18:53:00 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(timer.Start());
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(0, count1);
|
|
|
|
ASSERT_EQ(0, count2);
|
2020-04-07 18:53:00 +00:00
|
|
|
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.TEST_WaitForRun(
|
2021-09-21 15:53:03 +00:00
|
|
|
[&] { mock_clock_->SleepForMicroseconds(kInitDelay1Us); });
|
2020-04-07 18:53:00 +00:00
|
|
|
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(1, count1);
|
|
|
|
ASSERT_EQ(0, count2);
|
2020-04-07 18:53:00 +00:00
|
|
|
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.TEST_WaitForRun([&] {
|
2021-09-21 15:53:03 +00:00
|
|
|
mock_clock_->SleepForMicroseconds(kInitDelay2Us - kInitDelay1Us);
|
2020-08-21 18:31:27 +00:00
|
|
|
});
|
2020-04-07 18:53:00 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(1, count1);
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(1, count2);
|
|
|
|
|
|
|
|
ASSERT_TRUE(timer.Shutdown());
|
2020-04-07 18:53:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 18:31:27 +00:00
|
|
|
TEST_F(TimerTest, SingleScheduleRepeatedly) {
|
2020-04-07 18:53:00 +00:00
|
|
|
const int kIterations = 5;
|
2020-08-21 18:31:27 +00:00
|
|
|
const int kInitDelayUs = 1 * kUsPerSec;
|
|
|
|
const int kRepeatUs = 1 * kUsPerSec;
|
2020-04-07 18:53:00 +00:00
|
|
|
|
2021-03-15 11:32:24 +00:00
|
|
|
Timer timer(mock_clock_.get());
|
2020-04-07 18:53:00 +00:00
|
|
|
int count = 0;
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.Add([&] { count++; }, "fn_sch_test", kInitDelayUs, kRepeatUs);
|
2020-04-07 18:53:00 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(timer.Start());
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(0, count);
|
|
|
|
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.TEST_WaitForRun(
|
2021-09-21 15:53:03 +00:00
|
|
|
[&] { mock_clock_->SleepForMicroseconds(kInitDelayUs); });
|
2020-08-20 15:42:05 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(1, count);
|
2020-04-07 18:53:00 +00:00
|
|
|
|
|
|
|
// Wait for execution to finish
|
2020-08-20 15:42:05 +00:00
|
|
|
for (int i = 1; i < kIterations; i++) {
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.TEST_WaitForRun(
|
2021-09-21 15:53:03 +00:00
|
|
|
[&] { mock_clock_->SleepForMicroseconds(kRepeatUs); });
|
2020-04-07 18:53:00 +00:00
|
|
|
}
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(kIterations, count);
|
2020-04-07 18:53:00 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(timer.Shutdown());
|
|
|
|
}
|
|
|
|
|
2020-08-21 18:31:27 +00:00
|
|
|
TEST_F(TimerTest, MultipleScheduleRepeatedly) {
|
2020-08-20 15:42:05 +00:00
|
|
|
const int kIterations = 5;
|
2020-08-21 18:31:27 +00:00
|
|
|
const int kInitDelay1Us = 0 * kUsPerSec;
|
|
|
|
const int kInitDelay2Us = 1 * kUsPerSec;
|
|
|
|
const int kInitDelay3Us = 0 * kUsPerSec;
|
|
|
|
const int kRepeatUs = 2 * kUsPerSec;
|
|
|
|
const int kLargeRepeatUs = 100 * kUsPerSec;
|
2020-08-20 15:42:05 +00:00
|
|
|
|
2021-03-15 11:32:24 +00:00
|
|
|
Timer timer(mock_clock_.get());
|
2020-04-07 18:53:00 +00:00
|
|
|
|
|
|
|
int count1 = 0;
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.Add([&] { count1++; }, "fn_sch_test1", kInitDelay1Us, kRepeatUs);
|
2020-04-07 18:53:00 +00:00
|
|
|
|
|
|
|
int count2 = 0;
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.Add([&] { count2++; }, "fn_sch_test2", kInitDelay2Us, kRepeatUs);
|
2020-08-20 15:42:05 +00:00
|
|
|
|
|
|
|
// Add a function with relatively large repeat interval
|
|
|
|
int count3 = 0;
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.Add([&] { count3++; }, "fn_sch_test3", kInitDelay3Us, kLargeRepeatUs);
|
2020-04-07 18:53:00 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(timer.Start());
|
|
|
|
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(0, count2);
|
2020-04-07 18:53:00 +00:00
|
|
|
// Wait for execution to finish
|
2020-08-21 18:31:27 +00:00
|
|
|
for (int i = 1; i < kIterations * (kRepeatUs / kUsPerSec); i++) {
|
|
|
|
timer.TEST_WaitForRun(
|
2021-09-21 15:53:03 +00:00
|
|
|
[&] { mock_clock_->SleepForMicroseconds(1 * kUsPerSec); });
|
2020-08-21 18:31:27 +00:00
|
|
|
ASSERT_EQ((i + 2) / (kRepeatUs / kUsPerSec), count1);
|
|
|
|
ASSERT_EQ((i + 1) / (kRepeatUs / kUsPerSec), count2);
|
2020-08-20 15:42:05 +00:00
|
|
|
|
|
|
|
// large interval function should only run once (the first one).
|
|
|
|
ASSERT_EQ(1, count3);
|
2020-04-07 18:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
timer.Cancel("fn_sch_test1");
|
|
|
|
|
|
|
|
// Wait for execution to finish
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.TEST_WaitForRun(
|
2021-09-21 15:53:03 +00:00
|
|
|
[&] { mock_clock_->SleepForMicroseconds(1 * kUsPerSec); });
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(kIterations, count1);
|
|
|
|
ASSERT_EQ(kIterations, count2);
|
|
|
|
ASSERT_EQ(1, count3);
|
2020-04-07 18:53:00 +00:00
|
|
|
|
|
|
|
timer.Cancel("fn_sch_test2");
|
|
|
|
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(kIterations, count1);
|
|
|
|
ASSERT_EQ(kIterations, count2);
|
2020-04-07 18:53:00 +00:00
|
|
|
|
2020-08-20 15:42:05 +00:00
|
|
|
// execute the long interval one
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.TEST_WaitForRun([&] {
|
2021-09-21 15:53:03 +00:00
|
|
|
mock_clock_->SleepForMicroseconds(
|
2021-01-26 06:07:26 +00:00
|
|
|
kLargeRepeatUs - static_cast<int>(mock_clock_->NowMicros()));
|
2020-08-21 18:31:27 +00:00
|
|
|
});
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(2, count3);
|
|
|
|
|
|
|
|
ASSERT_TRUE(timer.Shutdown());
|
2020-04-07 18:53:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 16:18:45 +00:00
|
|
|
TEST_F(TimerTest, AddAfterStartTest) {
|
|
|
|
const int kIterations = 5;
|
2020-08-21 18:31:27 +00:00
|
|
|
const int kInitDelayUs = 1 * kUsPerSec;
|
|
|
|
const int kRepeatUs = 1 * kUsPerSec;
|
2020-08-04 16:18:45 +00:00
|
|
|
|
|
|
|
// wait timer to run and then add a new job
|
|
|
|
SyncPoint::GetInstance()->LoadDependency(
|
|
|
|
{{"Timer::Run::Waiting", "TimerTest:AddAfterStartTest:1"}});
|
|
|
|
SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
|
2021-03-15 11:32:24 +00:00
|
|
|
Timer timer(mock_clock_.get());
|
2020-08-04 16:18:45 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(timer.Start());
|
|
|
|
|
|
|
|
TEST_SYNC_POINT("TimerTest:AddAfterStartTest:1");
|
|
|
|
int count = 0;
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.Add([&] { count++; }, "fn_sch_test", kInitDelayUs, kRepeatUs);
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(0, count);
|
2020-08-04 16:18:45 +00:00
|
|
|
// Wait for execution to finish
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.TEST_WaitForRun(
|
2021-09-21 15:53:03 +00:00
|
|
|
[&] { mock_clock_->SleepForMicroseconds(kInitDelayUs); });
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(1, count);
|
|
|
|
|
|
|
|
for (int i = 1; i < kIterations; i++) {
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.TEST_WaitForRun(
|
2021-09-21 15:53:03 +00:00
|
|
|
[&] { mock_clock_->SleepForMicroseconds(kRepeatUs); });
|
2020-08-04 16:18:45 +00:00
|
|
|
}
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(kIterations, count);
|
2020-08-04 16:18:45 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(timer.Shutdown());
|
|
|
|
}
|
|
|
|
|
2020-08-12 05:36:12 +00:00
|
|
|
TEST_F(TimerTest, CancelRunningTask) {
|
2020-09-14 23:59:00 +00:00
|
|
|
static constexpr char kTestFuncName[] = "test_func";
|
2020-08-21 18:31:27 +00:00
|
|
|
const int kRepeatUs = 1 * kUsPerSec;
|
2021-03-15 11:32:24 +00:00
|
|
|
Timer timer(mock_clock_.get());
|
2020-08-12 05:36:12 +00:00
|
|
|
ASSERT_TRUE(timer.Start());
|
|
|
|
int* value = new int;
|
|
|
|
*value = 0;
|
|
|
|
SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
SyncPoint::GetInstance()->LoadDependency({
|
|
|
|
{"TimerTest::CancelRunningTask:test_func:0",
|
|
|
|
"TimerTest::CancelRunningTask:BeforeCancel"},
|
|
|
|
{"Timer::WaitForTaskCompleteIfNecessary:TaskExecuting",
|
|
|
|
"TimerTest::CancelRunningTask:test_func:1"},
|
|
|
|
});
|
|
|
|
SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
timer.Add(
|
|
|
|
[&]() {
|
|
|
|
*value = 1;
|
|
|
|
TEST_SYNC_POINT("TimerTest::CancelRunningTask:test_func:0");
|
|
|
|
TEST_SYNC_POINT("TimerTest::CancelRunningTask:test_func:1");
|
|
|
|
},
|
2020-08-21 18:31:27 +00:00
|
|
|
kTestFuncName, 0, kRepeatUs);
|
2020-08-12 05:36:12 +00:00
|
|
|
port::Thread control_thr([&]() {
|
|
|
|
TEST_SYNC_POINT("TimerTest::CancelRunningTask:BeforeCancel");
|
|
|
|
timer.Cancel(kTestFuncName);
|
|
|
|
// Verify that *value has been set to 1.
|
|
|
|
ASSERT_EQ(1, *value);
|
|
|
|
delete value;
|
|
|
|
value = nullptr;
|
|
|
|
});
|
2021-09-21 15:53:03 +00:00
|
|
|
mock_clock_->SleepForMicroseconds(kRepeatUs);
|
2020-08-12 05:36:12 +00:00
|
|
|
control_thr.join();
|
|
|
|
ASSERT_TRUE(timer.Shutdown());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TimerTest, ShutdownRunningTask) {
|
2020-08-21 18:31:27 +00:00
|
|
|
const int kRepeatUs = 1 * kUsPerSec;
|
2020-08-12 05:36:12 +00:00
|
|
|
constexpr char kTestFunc1Name[] = "test_func1";
|
|
|
|
constexpr char kTestFunc2Name[] = "test_func2";
|
2021-03-15 11:32:24 +00:00
|
|
|
Timer timer(mock_clock_.get());
|
2020-08-12 05:36:12 +00:00
|
|
|
|
|
|
|
SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
SyncPoint::GetInstance()->LoadDependency({
|
|
|
|
{"TimerTest::ShutdownRunningTest:test_func:0",
|
|
|
|
"TimerTest::ShutdownRunningTest:BeforeShutdown"},
|
|
|
|
{"Timer::WaitForTaskCompleteIfNecessary:TaskExecuting",
|
|
|
|
"TimerTest::ShutdownRunningTest:test_func:1"},
|
|
|
|
});
|
|
|
|
SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
|
|
|
|
ASSERT_TRUE(timer.Start());
|
|
|
|
|
|
|
|
int* value = new int;
|
|
|
|
*value = 0;
|
|
|
|
timer.Add(
|
|
|
|
[&]() {
|
|
|
|
TEST_SYNC_POINT("TimerTest::ShutdownRunningTest:test_func:0");
|
|
|
|
*value = 1;
|
|
|
|
TEST_SYNC_POINT("TimerTest::ShutdownRunningTest:test_func:1");
|
|
|
|
},
|
2020-08-21 18:31:27 +00:00
|
|
|
kTestFunc1Name, 0, kRepeatUs);
|
2020-08-12 05:36:12 +00:00
|
|
|
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.Add([&]() { ++(*value); }, kTestFunc2Name, 0, kRepeatUs);
|
2020-08-12 05:36:12 +00:00
|
|
|
|
|
|
|
port::Thread control_thr([&]() {
|
|
|
|
TEST_SYNC_POINT("TimerTest::ShutdownRunningTest:BeforeShutdown");
|
|
|
|
timer.Shutdown();
|
|
|
|
});
|
2021-09-21 15:53:03 +00:00
|
|
|
mock_clock_->SleepForMicroseconds(kRepeatUs);
|
2020-08-12 05:36:12 +00:00
|
|
|
control_thr.join();
|
|
|
|
delete value;
|
|
|
|
}
|
|
|
|
|
2020-08-20 15:42:05 +00:00
|
|
|
TEST_F(TimerTest, AddSameFuncName) {
|
2020-08-21 18:31:27 +00:00
|
|
|
const int kInitDelayUs = 1 * kUsPerSec;
|
|
|
|
const int kRepeat1Us = 5 * kUsPerSec;
|
|
|
|
const int kRepeat2Us = 4 * kUsPerSec;
|
2020-08-20 15:42:05 +00:00
|
|
|
|
2021-03-15 11:32:24 +00:00
|
|
|
Timer timer(mock_clock_.get());
|
2020-08-15 03:11:35 +00:00
|
|
|
ASSERT_TRUE(timer.Start());
|
|
|
|
|
|
|
|
int func_counter1 = 0;
|
2022-03-12 19:45:56 +00:00
|
|
|
ASSERT_TRUE(timer.Add([&] { func_counter1++; }, "duplicated_func",
|
|
|
|
kInitDelayUs, kRepeat1Us));
|
2020-08-15 03:11:35 +00:00
|
|
|
|
|
|
|
int func2_counter = 0;
|
2022-03-12 19:45:56 +00:00
|
|
|
ASSERT_TRUE(
|
|
|
|
timer.Add([&] { func2_counter++; }, "func2", kInitDelayUs, kRepeat2Us));
|
2020-08-15 03:11:35 +00:00
|
|
|
|
2022-03-12 19:45:56 +00:00
|
|
|
// New function with the same name should fail to add
|
2020-08-15 03:11:35 +00:00
|
|
|
int func_counter2 = 0;
|
2022-03-12 19:45:56 +00:00
|
|
|
ASSERT_FALSE(timer.Add([&] { func_counter2++; }, "duplicated_func",
|
|
|
|
kInitDelayUs, kRepeat1Us));
|
2020-08-20 15:42:05 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(0, func_counter1);
|
|
|
|
ASSERT_EQ(0, func2_counter);
|
|
|
|
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.TEST_WaitForRun(
|
2021-09-21 15:53:03 +00:00
|
|
|
[&] { mock_clock_->SleepForMicroseconds(kInitDelayUs); });
|
2020-08-20 15:42:05 +00:00
|
|
|
|
2022-03-12 19:45:56 +00:00
|
|
|
ASSERT_EQ(1, func_counter1);
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(1, func2_counter);
|
2020-08-15 03:11:35 +00:00
|
|
|
|
2021-09-21 15:53:03 +00:00
|
|
|
timer.TEST_WaitForRun([&] { mock_clock_->SleepForMicroseconds(kRepeat1Us); });
|
2020-08-15 03:11:35 +00:00
|
|
|
|
2022-03-12 19:45:56 +00:00
|
|
|
ASSERT_EQ(2, func_counter1);
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(2, func2_counter);
|
2022-03-12 19:45:56 +00:00
|
|
|
ASSERT_EQ(0, func_counter2);
|
2020-08-15 03:11:35 +00:00
|
|
|
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_TRUE(timer.Shutdown());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TimerTest, RepeatIntervalWithFuncRunningTime) {
|
2020-08-21 18:31:27 +00:00
|
|
|
const int kInitDelayUs = 1 * kUsPerSec;
|
|
|
|
const int kRepeatUs = 5 * kUsPerSec;
|
|
|
|
const int kFuncRunningTimeUs = 1 * kUsPerSec;
|
2020-08-20 15:42:05 +00:00
|
|
|
|
2021-03-15 11:32:24 +00:00
|
|
|
Timer timer(mock_clock_.get());
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_TRUE(timer.Start());
|
|
|
|
|
|
|
|
int func_counter = 0;
|
|
|
|
timer.Add(
|
|
|
|
[&] {
|
2021-09-21 15:53:03 +00:00
|
|
|
mock_clock_->SleepForMicroseconds(kFuncRunningTimeUs);
|
2020-08-20 15:42:05 +00:00
|
|
|
func_counter++;
|
|
|
|
},
|
2020-08-21 18:31:27 +00:00
|
|
|
"func", kInitDelayUs, kRepeatUs);
|
2020-08-20 15:42:05 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(0, func_counter);
|
2020-08-21 18:31:27 +00:00
|
|
|
timer.TEST_WaitForRun(
|
2021-09-21 15:53:03 +00:00
|
|
|
[&] { mock_clock_->SleepForMicroseconds(kInitDelayUs); });
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(1, func_counter);
|
2021-01-26 06:07:26 +00:00
|
|
|
ASSERT_EQ(kInitDelayUs + kFuncRunningTimeUs, mock_clock_->NowMicros());
|
2020-08-20 15:42:05 +00:00
|
|
|
|
|
|
|
// After repeat interval time, the function is not executed, as running
|
|
|
|
// the function takes some time (`kFuncRunningTimeSec`). The repeat interval
|
|
|
|
// is the time between ending time of the last call and starting time of the
|
|
|
|
// next call.
|
2020-08-21 18:31:27 +00:00
|
|
|
uint64_t next_abs_interval_time_us = kInitDelayUs + kRepeatUs;
|
|
|
|
timer.TEST_WaitForRun([&] {
|
2021-01-26 06:07:26 +00:00
|
|
|
mock_clock_->SetCurrentTime(next_abs_interval_time_us / kUsPerSec);
|
2020-08-21 18:31:27 +00:00
|
|
|
});
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(1, func_counter);
|
|
|
|
|
2020-08-21 18:31:27 +00:00
|
|
|
// After the function running time, it's executed again
|
|
|
|
timer.TEST_WaitForRun(
|
2021-09-21 15:53:03 +00:00
|
|
|
[&] { mock_clock_->SleepForMicroseconds(kFuncRunningTimeUs); });
|
2020-08-20 15:42:05 +00:00
|
|
|
ASSERT_EQ(2, func_counter);
|
2020-08-15 03:11:35 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(timer.Shutdown());
|
|
|
|
}
|
|
|
|
|
2020-08-21 22:47:09 +00:00
|
|
|
TEST_F(TimerTest, DestroyRunningTimer) {
|
|
|
|
const int kInitDelayUs = 1 * kUsPerSec;
|
|
|
|
const int kRepeatUs = 1 * kUsPerSec;
|
|
|
|
|
2021-03-15 11:32:24 +00:00
|
|
|
auto timer_ptr = new Timer(mock_clock_.get());
|
2020-08-21 22:47:09 +00:00
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
timer_ptr->Add([&] { count++; }, "fn_sch_test", kInitDelayUs, kRepeatUs);
|
|
|
|
ASSERT_TRUE(timer_ptr->Start());
|
|
|
|
|
|
|
|
timer_ptr->TEST_WaitForRun(
|
2021-09-21 15:53:03 +00:00
|
|
|
[&] { mock_clock_->SleepForMicroseconds(kInitDelayUs); });
|
2020-08-21 22:47:09 +00:00
|
|
|
|
|
|
|
// delete a running timer should not cause any exception
|
|
|
|
delete timer_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TimerTest, DestroyTimerWithRunningFunc) {
|
|
|
|
const int kRepeatUs = 1 * kUsPerSec;
|
2021-03-15 11:32:24 +00:00
|
|
|
auto timer_ptr = new Timer(mock_clock_.get());
|
2020-08-21 22:47:09 +00:00
|
|
|
|
|
|
|
SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
SyncPoint::GetInstance()->LoadDependency({
|
|
|
|
{"TimerTest::DestroyTimerWithRunningFunc:test_func:0",
|
|
|
|
"TimerTest::DestroyTimerWithRunningFunc:BeforeDelete"},
|
|
|
|
{"Timer::WaitForTaskCompleteIfNecessary:TaskExecuting",
|
|
|
|
"TimerTest::DestroyTimerWithRunningFunc:test_func:1"},
|
|
|
|
});
|
|
|
|
SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
|
|
|
|
ASSERT_TRUE(timer_ptr->Start());
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
timer_ptr->Add(
|
|
|
|
[&]() {
|
|
|
|
TEST_SYNC_POINT("TimerTest::DestroyTimerWithRunningFunc:test_func:0");
|
|
|
|
count++;
|
|
|
|
TEST_SYNC_POINT("TimerTest::DestroyTimerWithRunningFunc:test_func:1");
|
|
|
|
},
|
|
|
|
"fn_running_test", 0, kRepeatUs);
|
|
|
|
|
|
|
|
port::Thread control_thr([&] {
|
|
|
|
TEST_SYNC_POINT("TimerTest::DestroyTimerWithRunningFunc:BeforeDelete");
|
|
|
|
delete timer_ptr;
|
|
|
|
});
|
2021-09-21 15:53:03 +00:00
|
|
|
mock_clock_->SleepForMicroseconds(kRepeatUs);
|
2020-08-21 22:47:09 +00:00
|
|
|
control_thr.join();
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:53:00 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|