Shutdown timer in destructor (#7292)

Summary:
Make sure deleting a running timer works fine.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/7292

Test Plan: unittest and an invalid benchmark command: `./db_bench --db=/tmp --use_existing_db=false --benchmarks=fred --compression_type=none`

Reviewed By: riversand963

Differential Revision: D23248500

Pulled By: jay-zhuang

fbshipit-source-id: 04111681b389a9aa23a439db4568d5ca351f1144
main
Jay Zhuang 4 years ago committed by Facebook GitHub Bot
parent 3844612625
commit e500c730cf
  1. 2
      util/timer.h
  2. 49
      util/timer_test.cc

@ -42,6 +42,8 @@ class Timer {
running_(false),
executing_task_(false) {}
~Timer() { Shutdown(); }
// Add a new function to run.
// fn_name has to be identical, otherwise, the new one overrides the existing
// one, regardless if the function is pending removed (invalid) or not.

@ -341,6 +341,55 @@ TEST_F(TimerTest, RepeatIntervalWithFuncRunningTime) {
ASSERT_TRUE(timer.Shutdown());
}
TEST_F(TimerTest, DestroyRunningTimer) {
const int kInitDelayUs = 1 * kUsPerSec;
const int kRepeatUs = 1 * kUsPerSec;
auto timer_ptr = new Timer(mock_env_.get());
int count = 0;
timer_ptr->Add([&] { count++; }, "fn_sch_test", kInitDelayUs, kRepeatUs);
ASSERT_TRUE(timer_ptr->Start());
timer_ptr->TEST_WaitForRun(
[&] { mock_env_->MockSleepForMicroseconds(kInitDelayUs); });
// delete a running timer should not cause any exception
delete timer_ptr;
}
TEST_F(TimerTest, DestroyTimerWithRunningFunc) {
const int kRepeatUs = 1 * kUsPerSec;
auto timer_ptr = new Timer(mock_env_.get());
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;
});
mock_env_->MockSleepForMicroseconds(kRepeatUs);
control_thr.join();
}
} // namespace ROCKSDB_NAMESPACE
int main(int argc, char** argv) {

Loading…
Cancel
Save