Skip to content

Commit

Permalink
Add test for delete running function
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-zhuang committed Aug 21, 2020
1 parent 84f3d9b commit 53e7883
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions util/timer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,26 +342,54 @@ TEST_F(TimerTest, RepeatIntervalWithFuncRunningTime) {
}

TEST_F(TimerTest, DestroyRunningTimer) {
const int kInitDelaySec = 1;
const int kRepeatSec = 1;
const int kInitDelayUs = 1 * kUsPerSec;
const int kRepeatUs = 1 * kUsPerSec;

int mock_time_sec = 0;
mock_env_->set_current_time(mock_time_sec);
auto timer_ptr = new Timer(mock_env_.get());

int count = 0;
timer_ptr->Add([&] { count++; }, "fn_sch_test", kInitDelaySec * kSecond,
kRepeatSec * kSecond);
timer_ptr->Add([&] { count++; }, "fn_sch_test", kInitDelayUs, kRepeatUs);
ASSERT_TRUE(timer_ptr->Start());

mock_time_sec += kInitDelaySec;
timer_ptr->TEST_WaitForRun(
[&] { mock_env_->set_current_time(mock_time_sec); });
[&] { 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 53e7883

Please sign in to comment.