Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shutdown timer in destructor #7292

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When deleting the timer, fn_sch_test may or may not be running, so maybe test two cases separately?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added another test for deleting a timer with running function (similar to shutdown running function test).

}

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