Commit 28b5e293 authored by tzik's avatar tzik Committed by Commit Bot

Add MockOneShotTimer and MockRepeatingTimer

MockOneShotTimer and MockRepeatingTimer are subclasses of OneShotTimer
and RepeatingTimer respectively, that are intended to replace most of
MockTimer.

As MockTimer is not a OneShotTimer nor RepeatingTimer, when we want to
use MockTimer for other timers, we had to use the generic Timer to hold
a timer instance, that loses the timer traits from its type.

Change-Id: Iea4a469708be2b89d4feb228460d78ab2a2f96d1
Reviewed-on: https://chromium-review.googlesource.com/1095195
Commit-Queue: Taiju Tsuiki <tzik@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#568371}
parent 124e793c
......@@ -8,6 +8,21 @@
namespace base {
namespace {
void FlushPendingTasks(TestSimpleTaskRunner* task_runner) {
// Do not use TestSimpleTaskRunner::RunPendingTasks() here. As RunPendingTasks
// overrides ThreadTaskRunnerHandle when it runs tasks, tasks posted by timer
// tasks to TTRH go to |test_task_runner_|, though they should be posted to
// the original task runner.
// Do not use TestSimpleTaskRunner::RunPendingTasks(), as its overridden
// ThreadTaskRunnerHandle causes unexpected side effects.
for (TestPendingTask& task : task_runner->TakePendingTasks())
std::move(task.task).Run();
}
} // namespace
MockTimer::MockTimer(bool retain_user_task, bool is_repeating)
: Timer(retain_user_task, is_repeating, &clock_),
test_task_runner_(MakeRefCounted<TestSimpleTaskRunner>()) {
......@@ -23,13 +38,45 @@ void MockTimer::SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) {
void MockTimer::Fire() {
DCHECK(IsRunning());
clock_.Advance(std::max(TimeDelta(), desired_run_time() - clock_.NowTicks()));
FlushPendingTasks(test_task_runner_.get());
}
// Do not use TestSimpleTaskRunner::RunPendingTasks() here. As RunPendingTasks
// overrides ThreadTaskRunnerHandle when it runs tasks, tasks posted by timer
// tasks to TTRH go to |test_task_runner_|, though they should be posted to
// the original task runner.
for (TestPendingTask& task : test_task_runner_->TakePendingTasks())
std::move(task.task).Run();
MockOneShotTimer::MockOneShotTimer()
: OneShotTimer(&clock_),
test_task_runner_(MakeRefCounted<TestSimpleTaskRunner>()) {
OneShotTimer::SetTaskRunner(test_task_runner_);
}
MockOneShotTimer::~MockOneShotTimer() = default;
void MockOneShotTimer::SetTaskRunner(
scoped_refptr<SequencedTaskRunner> task_runner) {
NOTREACHED() << "MockOneShotTimer doesn't support SetTaskRunner().";
}
void MockOneShotTimer::Fire() {
DCHECK(IsRunning());
clock_.Advance(std::max(TimeDelta(), desired_run_time() - clock_.NowTicks()));
FlushPendingTasks(test_task_runner_.get());
}
MockRepeatingTimer::MockRepeatingTimer()
: RepeatingTimer(&clock_),
test_task_runner_(MakeRefCounted<TestSimpleTaskRunner>()) {
RepeatingTimer::SetTaskRunner(test_task_runner_);
}
MockRepeatingTimer::~MockRepeatingTimer() = default;
void MockRepeatingTimer::SetTaskRunner(
scoped_refptr<SequencedTaskRunner> task_runner) {
NOTREACHED() << "MockRepeatingTimer doesn't support SetTaskRunner().";
}
void MockRepeatingTimer::Fire() {
DCHECK(IsRunning());
clock_.Advance(std::max(TimeDelta(), desired_run_time() - clock_.NowTicks()));
FlushPendingTasks(test_task_runner_.get());
}
} // namespace base
......@@ -33,6 +33,42 @@ class MockTimer : public Timer {
scoped_refptr<TestSimpleTaskRunner> test_task_runner_;
};
// See MockTimer's comment. Prefer using ScopedTaskEnvironment::MOCK_TIME.
class MockOneShotTimer : public OneShotTimer {
public:
MockOneShotTimer();
~MockOneShotTimer() override;
// Testing method.
void Fire();
private:
// Timer implementation.
// MockOneShotTimer doesn't support SetTaskRunner. Do not use this.
void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) override;
SimpleTestTickClock clock_;
scoped_refptr<TestSimpleTaskRunner> test_task_runner_;
};
// See MockTimer's comment. Prefer using ScopedTaskEnvironment::MOCK_TIME.
class MockRepeatingTimer : public RepeatingTimer {
public:
MockRepeatingTimer();
~MockRepeatingTimer() override;
// Testing method.
void Fire();
private:
// Timer implementation.
// MockRepeatingTimer doesn't support SetTaskRunner. Do not use this.
void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) override;
SimpleTestTickClock clock_;
scoped_refptr<TestSimpleTaskRunner> test_task_runner_;
};
} // namespace base
#endif // BASE_TIMER_MOCK_TIMER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment