Skip to content

Commit

Permalink
Shutdown timer in destructor (#7292)
Browse files Browse the repository at this point in the history
Summary:
Make sure deleting a running timer works fine.

Pull Request resolved: #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
  • Loading branch information
jay-zhuang authored and facebook-github-bot committed Aug 21, 2020
1 parent 3844612 commit e500c73
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions util/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
49 changes: 49 additions & 0 deletions util/timer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e500c73

Please sign in to comment.