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

Windows: Make task_runner_unittests.cc more deterministic. #29500

Merged
merged 1 commit into from
Jan 19, 2022
Merged
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
6 changes: 3 additions & 3 deletions shell/platform/windows/task_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ TaskRunner::TaskRunner(CurrentTimeProc get_current_time,
on_task_expired_(std::move(on_task_expired)) {}

std::chrono::nanoseconds TaskRunner::ProcessTasks() {
const TaskTimePoint now = TaskTimePoint::clock::now();
const TaskTimePoint now = GetCurrentTimeForTask();

std::vector<Task> expired_tasks;

Expand Down Expand Up @@ -64,7 +64,7 @@ std::chrono::nanoseconds TaskRunner::ProcessTasks() {

TaskRunner::TaskTimePoint TaskRunner::TimePointFromFlutterTime(
uint64_t flutter_target_time_nanos) const {
const auto now = TaskTimePoint::clock::now();
const auto now = GetCurrentTimeForTask();
const auto flutter_duration = flutter_target_time_nanos - get_current_time_();
return now + std::chrono::nanoseconds(flutter_duration);
}
Expand All @@ -79,7 +79,7 @@ void TaskRunner::PostFlutterTask(FlutterTask flutter_task,

void TaskRunner::PostTask(TaskClosure closure) {
Task task;
task.fire_time = TaskTimePoint::clock::now();
task.fire_time = GetCurrentTimeForTask();
task.variant = std::move(closure);
EnqueueTask(std::move(task));
}
Expand Down
8 changes: 8 additions & 0 deletions shell/platform/windows/task_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ class TaskRunner {
// Each platform implementations must call this to schedule the tasks.
std::chrono::nanoseconds ProcessTasks();

// Returns the current TaskTimePoint that can be used to determine whether a
// task is expired.
//
// Tests can override this to mock up the time.
virtual TaskTimePoint GetCurrentTimeForTask() const {
return TaskTimePoint::clock::now();
}

private:
typedef std::variant<FlutterTask, TaskClosure> TaskVariant;

Expand Down
6 changes: 6 additions & 0 deletions shell/platform/windows/task_runner_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class MockTaskRunner : public TaskRunner {
// Do nothing to avoid processing tasks immediately after the tasks is
// posted.
}

virtual TaskTimePoint GetCurrentTimeForTask() const override {
return TaskTimePoint(
std::chrono::duration_cast<std::chrono::steady_clock::duration>(
std::chrono::nanoseconds(10000)));
}
};

uint64_t MockGetCurrentTime() {
Expand Down