Commit da38b063 authored by Wez's avatar Wez Committed by Commit Bot

Replace Time with TimeTicks in MessageLoop tests.

PostDelayedTask_Basic verifies that the call waits at least as long as
the requested delay, but was using wall-clock time to verify this,
rather than the tick-clock that PostDelayedTask itself should use.

Bug: 738001, 738275
Change-Id: Ia12423ae46646a348932a8eff6dd1c4e8047fdb7
Reviewed-on: https://chromium-review.googlesource.com/575788Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Wez <wez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487512}
parent 081a3f3a
...@@ -81,12 +81,12 @@ void SlowFunc(TimeDelta pause, int* quit_counter) { ...@@ -81,12 +81,12 @@ void SlowFunc(TimeDelta pause, int* quit_counter) {
// This function records the time when Run was called in a Time object, which is // This function records the time when Run was called in a Time object, which is
// useful for building a variety of MessageLoop tests. // useful for building a variety of MessageLoop tests.
// TODO(sky): remove? // TODO(sky): remove?
void RecordRunTimeFunc(Time* run_time, int* quit_counter) { void RecordRunTimeFunc(TimeTicks* run_time, int* quit_counter) {
*run_time = Time::Now(); *run_time = TimeTicks::Now();
// Cause our Run function to take some time to execute. As a result we can // Cause our Run function to take some time to execute. As a result we can
// count on subsequent RecordRunTimeFunc()s running at a future time, // count on subsequent RecordRunTimeFunc()s running at a future time,
// without worry about the resolution of our system clock being an issue. // without worry about the resolution of our system clock being an issue.
SlowFunc(TimeDelta::FromMilliseconds(10), quit_counter); SlowFunc(TimeDelta::FromMilliseconds(10), quit_counter);
} }
...@@ -131,14 +131,14 @@ void RunTest_PostDelayedTask_Basic(MessagePumpFactory factory) { ...@@ -131,14 +131,14 @@ void RunTest_PostDelayedTask_Basic(MessagePumpFactory factory) {
const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); const TimeDelta kDelay = TimeDelta::FromMilliseconds(100);
int num_tasks = 1; int num_tasks = 1;
Time run_time; TimeTicks run_time;
loop.task_runner()->PostDelayedTask( loop.task_runner()->PostDelayedTask(
FROM_HERE, BindOnce(&RecordRunTimeFunc, &run_time, &num_tasks), kDelay); FROM_HERE, BindOnce(&RecordRunTimeFunc, &run_time, &num_tasks), kDelay);
Time time_before_run = Time::Now(); TimeTicks time_before_run = TimeTicks::Now();
RunLoop().Run(); RunLoop().Run();
Time time_after_run = Time::Now(); TimeTicks time_after_run = TimeTicks::Now();
EXPECT_EQ(0, num_tasks); EXPECT_EQ(0, num_tasks);
EXPECT_LT(kDelay, time_after_run - time_before_run); EXPECT_LT(kDelay, time_after_run - time_before_run);
...@@ -150,7 +150,7 @@ void RunTest_PostDelayedTask_InDelayOrder(MessagePumpFactory factory) { ...@@ -150,7 +150,7 @@ void RunTest_PostDelayedTask_InDelayOrder(MessagePumpFactory factory) {
// Test that two tasks with different delays run in the right order. // Test that two tasks with different delays run in the right order.
int num_tasks = 2; int num_tasks = 2;
Time run_time1, run_time2; TimeTicks run_time1, run_time2;
loop.task_runner()->PostDelayedTask( loop.task_runner()->PostDelayedTask(
FROM_HERE, BindOnce(&RecordRunTimeFunc, &run_time1, &num_tasks), FROM_HERE, BindOnce(&RecordRunTimeFunc, &run_time1, &num_tasks),
...@@ -182,7 +182,7 @@ void RunTest_PostDelayedTask_InPostOrder(MessagePumpFactory factory) { ...@@ -182,7 +182,7 @@ void RunTest_PostDelayedTask_InPostOrder(MessagePumpFactory factory) {
const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); const TimeDelta kDelay = TimeDelta::FromMilliseconds(100);
int num_tasks = 2; int num_tasks = 2;
Time run_time1, run_time2; TimeTicks run_time1, run_time2;
loop.task_runner()->PostDelayedTask( loop.task_runner()->PostDelayedTask(
FROM_HERE, BindOnce(&RecordRunTimeFunc, &run_time1, &num_tasks), kDelay); FROM_HERE, BindOnce(&RecordRunTimeFunc, &run_time1, &num_tasks), kDelay);
...@@ -205,7 +205,7 @@ void RunTest_PostDelayedTask_InPostOrder_2(MessagePumpFactory factory) { ...@@ -205,7 +205,7 @@ void RunTest_PostDelayedTask_InPostOrder_2(MessagePumpFactory factory) {
const TimeDelta kPause = TimeDelta::FromMilliseconds(50); const TimeDelta kPause = TimeDelta::FromMilliseconds(50);
int num_tasks = 2; int num_tasks = 2;
Time run_time; TimeTicks run_time;
loop.task_runner()->PostTask(FROM_HERE, loop.task_runner()->PostTask(FROM_HERE,
BindOnce(&SlowFunc, kPause, &num_tasks)); BindOnce(&SlowFunc, kPause, &num_tasks));
...@@ -213,9 +213,9 @@ void RunTest_PostDelayedTask_InPostOrder_2(MessagePumpFactory factory) { ...@@ -213,9 +213,9 @@ void RunTest_PostDelayedTask_InPostOrder_2(MessagePumpFactory factory) {
FROM_HERE, BindOnce(&RecordRunTimeFunc, &run_time, &num_tasks), FROM_HERE, BindOnce(&RecordRunTimeFunc, &run_time, &num_tasks),
TimeDelta::FromMilliseconds(10)); TimeDelta::FromMilliseconds(10));
Time time_before_run = Time::Now(); TimeTicks time_before_run = TimeTicks::Now();
RunLoop().Run(); RunLoop().Run();
Time time_after_run = Time::Now(); TimeTicks time_after_run = TimeTicks::Now();
EXPECT_EQ(0, num_tasks); EXPECT_EQ(0, num_tasks);
...@@ -233,7 +233,7 @@ void RunTest_PostDelayedTask_InPostOrder_3(MessagePumpFactory factory) { ...@@ -233,7 +233,7 @@ void RunTest_PostDelayedTask_InPostOrder_3(MessagePumpFactory factory) {
// delayed task's delay has passed. // delayed task's delay has passed.
int num_tasks = 11; int num_tasks = 11;
Time run_time1, run_time2; TimeTicks run_time1, run_time2;
// Clutter the ML with tasks. // Clutter the ML with tasks.
for (int i = 1; i < num_tasks; ++i) for (int i = 1; i < num_tasks; ++i)
...@@ -260,7 +260,7 @@ void RunTest_PostDelayedTask_SharedTimer(MessagePumpFactory factory) { ...@@ -260,7 +260,7 @@ void RunTest_PostDelayedTask_SharedTimer(MessagePumpFactory factory) {
// By setting num_tasks to 1, we ensure that the first task to run causes the // By setting num_tasks to 1, we ensure that the first task to run causes the
// run loop to exit. // run loop to exit.
int num_tasks = 1; int num_tasks = 1;
Time run_time1, run_time2; TimeTicks run_time1, run_time2;
loop.task_runner()->PostDelayedTask( loop.task_runner()->PostDelayedTask(
FROM_HERE, BindOnce(&RecordRunTimeFunc, &run_time1, &num_tasks), FROM_HERE, BindOnce(&RecordRunTimeFunc, &run_time1, &num_tasks),
...@@ -269,13 +269,13 @@ void RunTest_PostDelayedTask_SharedTimer(MessagePumpFactory factory) { ...@@ -269,13 +269,13 @@ void RunTest_PostDelayedTask_SharedTimer(MessagePumpFactory factory) {
FROM_HERE, BindOnce(&RecordRunTimeFunc, &run_time2, &num_tasks), FROM_HERE, BindOnce(&RecordRunTimeFunc, &run_time2, &num_tasks),
TimeDelta::FromMilliseconds(10)); TimeDelta::FromMilliseconds(10));
Time start_time = Time::Now(); TimeTicks start_time = TimeTicks::Now();
RunLoop().Run(); RunLoop().Run();
EXPECT_EQ(0, num_tasks); EXPECT_EQ(0, num_tasks);
// Ensure that we ran in far less time than the slower timer. // Ensure that we ran in far less time than the slower timer.
TimeDelta total_time = Time::Now() - start_time; TimeDelta total_time = TimeTicks::Now() - start_time;
EXPECT_GT(5000, total_time.InMilliseconds()); EXPECT_GT(5000, total_time.InMilliseconds());
// In case both timers somehow run at nearly the same time, sleep a little // In case both timers somehow run at nearly the same time, sleep a little
......
...@@ -63,11 +63,9 @@ class BASE_EXPORT TaskRunner ...@@ -63,11 +63,9 @@ class BASE_EXPORT TaskRunner
// Equivalent to PostDelayedTask(from_here, task, 0). // Equivalent to PostDelayedTask(from_here, task, 0).
bool PostTask(const tracked_objects::Location& from_here, OnceClosure task); bool PostTask(const tracked_objects::Location& from_here, OnceClosure task);
// Like PostTask, but tries to run the posted task only after // Like PostTask, but tries to run the posted task only after |delay_ms|
// |delay_ms| has passed. // has passed. Implementations should use a tick clock, rather than wall-
// // clock time, to implement |delay|.
// It is valid for an implementation to ignore |delay_ms|; that is,
// to have PostDelayedTask behave the same as PostTask.
virtual bool PostDelayedTask(const tracked_objects::Location& from_here, virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
OnceClosure task, OnceClosure task,
base::TimeDelta delay) = 0; base::TimeDelta delay) = 0;
......
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