Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/task_scheduler/delayed_task_manager.h" | 5 #include "base/task_scheduler/delayed_task_manager.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 35 DoPostTaskNowCallback(task.get()); | 35 DoPostTaskNowCallback(task.get()); |
| 36 } | 36 } |
| 37 | 37 |
| 38 private: | 38 private: |
| 39 DISALLOW_COPY_AND_ASSIGN(MockTaskTarget); | 39 DISALLOW_COPY_AND_ASSIGN(MockTaskTarget); |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 class TaskSchedulerDelayedTaskManagerTest : public testing::Test { | 42 class TaskSchedulerDelayedTaskManagerTest : public testing::Test { |
| 43 public: | 43 public: |
| 44 TaskSchedulerDelayedTaskManagerTest() | 44 TaskSchedulerDelayedTaskManagerTest() |
| 45 : service_thread_task_runner_(new TestMockTimeTaskRunner), | 45 : service_thread_task_runner_(new TestMockTimeTaskRunner) {} |
| 46 delayed_task_manager_(service_thread_task_runner_) {} | |
| 47 ~TaskSchedulerDelayedTaskManagerTest() override = default; | 46 ~TaskSchedulerDelayedTaskManagerTest() override = default; |
| 48 | 47 |
| 49 protected: | 48 protected: |
| 50 scoped_refptr<TestMockTimeTaskRunner> service_thread_task_runner_; | 49 scoped_refptr<TestMockTimeTaskRunner> service_thread_task_runner_; |
| 51 DelayedTaskManager delayed_task_manager_; | 50 DelayedTaskManager delayed_task_manager_; |
| 52 | 51 |
| 53 private: | 52 private: |
| 54 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerDelayedTaskManagerTest); | 53 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerDelayedTaskManagerTest); |
| 55 }; | 54 }; |
| 56 | 55 |
| 57 } // namespace | 56 } // namespace |
| 58 | 57 |
| 59 // Verify that a delayed task isn't forwarded before it is ripe for execution. | 58 // Verify that a delayed task isn't forwarded before Start(). |
| 60 TEST_F(TaskSchedulerDelayedTaskManagerTest, DelayedTaskDoesNotRunTooEarly) { | 59 TEST_F(TaskSchedulerDelayedTaskManagerTest, DelayedTaskDoesNotRunBeforeStart) { |
| 61 auto task = | 60 auto task = |
| 62 MakeUnique<Task>(FROM_HERE, Bind(&DoNothing), TaskTraits(), kLongDelay); | 61 MakeUnique<Task>(FROM_HERE, Bind(&DoNothing), TaskTraits(), kLongDelay); |
| 63 | 62 |
| 63 testing::StrictMock<MockTaskTarget> task_target; | |
| 64 | |
| 65 // Send |task| to the DelayedTaskManager. | |
| 66 delayed_task_manager_.AddDelayedTask( | |
| 67 std::move(task), | |
| 68 Bind(&MockTaskTarget::PostTaskNowCallback, Unretained(&task_target))); | |
|
gab
2017/04/12 19:15:51
BindOnce
fdoray
2017/04/19 15:37:55
Done.
| |
| 69 | |
| 70 // Fast-forward time. Don't expect any forwarding to |task_target|. | |
|
gab
2017/04/12 19:15:51
What ensures this? Shouldn't your task trigger a f
fdoray
2017/04/19 15:37:55
Done.
| |
| 71 service_thread_task_runner_->FastForwardBy(kLongDelay); | |
| 72 } | |
| 73 | |
| 74 // Verify that a delayed task added before Start() is forwarded when it is ripe | |
| 75 // for execution and Start() is called. | |
| 76 TEST_F(TaskSchedulerDelayedTaskManagerTest, | |
| 77 DelayedTaskPostedBeforeStartRunsAfterStart) { | |
| 78 auto task = | |
| 79 MakeUnique<Task>(FROM_HERE, Bind(&DoNothing), TaskTraits(), kLongDelay); | |
| 80 const Task* task_raw = task.get(); | |
| 81 | |
| 82 testing::StrictMock<MockTaskTarget> task_target; | |
| 83 | |
| 84 // Send |task| to the DelayedTaskManager. | |
| 85 delayed_task_manager_.AddDelayedTask( | |
| 86 std::move(task), | |
| 87 Bind(&MockTaskTarget::PostTaskNowCallback, Unretained(&task_target))); | |
| 88 | |
| 89 delayed_task_manager_.Start(service_thread_task_runner_); | |
| 90 | |
| 91 // Run tasks on the service thread. Don't expect any forwarding to | |
| 92 // |task_target| since the task isn't ripe for execution. | |
| 93 service_thread_task_runner_->RunUntilIdle(); | |
| 94 | |
| 95 // Fast-forward time. Expect the task to be forwarded to |task_target|. | |
| 96 EXPECT_CALL(task_target, DoPostTaskNowCallback(task_raw)); | |
| 97 service_thread_task_runner_->FastForwardBy(kLongDelay); | |
| 98 } | |
|
gab
2017/04/12 19:15:51
Also test that task that became ripe before start
fdoray
2017/04/19 15:37:55
Done.
| |
| 99 | |
| 100 // Verify that a delayed task added after Start() isn't forwarded before it is | |
| 101 // ripe for execution. | |
| 102 TEST_F(TaskSchedulerDelayedTaskManagerTest, DelayedTaskDoesNotRunTooEarly) { | |
| 103 delayed_task_manager_.Start(service_thread_task_runner_); | |
| 104 auto task = | |
| 105 MakeUnique<Task>(FROM_HERE, Bind(&DoNothing), TaskTraits(), kLongDelay); | |
| 106 | |
| 64 testing::StrictMock<MockTaskTarget> task_target; | 107 testing::StrictMock<MockTaskTarget> task_target; |
| 65 | 108 |
| 66 // Send |task| to the DelayedTaskManager. | 109 // Send |task| to the DelayedTaskManager. |
| 67 delayed_task_manager_.AddDelayedTask( | 110 delayed_task_manager_.AddDelayedTask( |
| 68 std::move(task), | 111 std::move(task), |
| 69 Bind(&MockTaskTarget::PostTaskNowCallback, Unretained(&task_target))); | 112 Bind(&MockTaskTarget::PostTaskNowCallback, Unretained(&task_target))); |
| 70 | 113 |
| 71 // Run tasks that are ripe for execution. Don't expect any forwarding to | 114 // Run tasks that are ripe for execution. Don't expect any forwarding to |
| 72 // |task_target|. | 115 // |task_target|. |
| 73 service_thread_task_runner_->RunUntilIdle(); | 116 service_thread_task_runner_->RunUntilIdle(); |
| 74 } | 117 } |
| 75 | 118 |
| 76 // Verify that a delayed task is forwarded when it is ripe for execution. | 119 // Verify that a delayed task added after Start() is forwarded when it is ripe |
| 120 // for execution. | |
| 77 TEST_F(TaskSchedulerDelayedTaskManagerTest, DelayedTaskRunsAfterDelay) { | 121 TEST_F(TaskSchedulerDelayedTaskManagerTest, DelayedTaskRunsAfterDelay) { |
| 122 delayed_task_manager_.Start(service_thread_task_runner_); | |
| 78 auto task = | 123 auto task = |
| 79 MakeUnique<Task>(FROM_HERE, Bind(&DoNothing), TaskTraits(), kLongDelay); | 124 MakeUnique<Task>(FROM_HERE, Bind(&DoNothing), TaskTraits(), kLongDelay); |
| 80 const Task* task_raw = task.get(); | 125 const Task* task_raw = task.get(); |
| 81 | 126 |
| 82 testing::StrictMock<MockTaskTarget> task_target; | 127 testing::StrictMock<MockTaskTarget> task_target; |
| 83 | 128 |
| 84 // Send |task| to the DelayedTaskManager. | 129 // Send |task| to the DelayedTaskManager. |
| 85 delayed_task_manager_.AddDelayedTask( | 130 delayed_task_manager_.AddDelayedTask( |
| 86 std::move(task), | 131 std::move(task), |
| 87 Bind(&MockTaskTarget::PostTaskNowCallback, Unretained(&task_target))); | 132 Bind(&MockTaskTarget::PostTaskNowCallback, Unretained(&task_target))); |
| 88 | 133 |
| 89 // Fast-forward time. Expect the task is forwarded to |task_target|. | 134 // Fast-forward time. Expect the task is forwarded to |task_target|. |
| 90 EXPECT_CALL(task_target, DoPostTaskNowCallback(task_raw)); | 135 EXPECT_CALL(task_target, DoPostTaskNowCallback(task_raw)); |
| 91 service_thread_task_runner_->FastForwardBy(kLongDelay); | 136 service_thread_task_runner_->FastForwardBy(kLongDelay); |
| 92 } | 137 } |
| 93 | 138 |
| 94 // Verify that multiple delayed tasks are forwarded when they are ripe for | 139 // Verify that multiple delayed tasks added after Start() are forwarded when |
| 95 // execution. | 140 // they are ripe for execution. |
| 96 TEST_F(TaskSchedulerDelayedTaskManagerTest, DelayedTasksRunAfterDelay) { | 141 TEST_F(TaskSchedulerDelayedTaskManagerTest, DelayedTasksRunAfterDelay) { |
| 142 delayed_task_manager_.Start(service_thread_task_runner_); | |
| 97 auto task_a = MakeUnique<Task>(FROM_HERE, Bind(&DoNothing), TaskTraits(), | 143 auto task_a = MakeUnique<Task>(FROM_HERE, Bind(&DoNothing), TaskTraits(), |
| 98 TimeDelta::FromHours(1)); | 144 TimeDelta::FromHours(1)); |
| 99 const Task* task_a_raw = task_a.get(); | 145 const Task* task_a_raw = task_a.get(); |
| 100 | 146 |
| 101 auto task_b = MakeUnique<Task>(FROM_HERE, Bind(&DoNothing), TaskTraits(), | 147 auto task_b = MakeUnique<Task>(FROM_HERE, Bind(&DoNothing), TaskTraits(), |
| 102 TimeDelta::FromHours(2)); | 148 TimeDelta::FromHours(2)); |
| 103 const Task* task_b_raw = task_b.get(); | 149 const Task* task_b_raw = task_b.get(); |
| 104 | 150 |
| 105 auto task_c = MakeUnique<Task>(FROM_HERE, Bind(&DoNothing), TaskTraits(), | 151 auto task_c = MakeUnique<Task>(FROM_HERE, Bind(&DoNothing), TaskTraits(), |
| 106 TimeDelta::FromHours(1)); | 152 TimeDelta::FromHours(1)); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 131 testing::Mock::VerifyAndClear(&task_target); | 177 testing::Mock::VerifyAndClear(&task_target); |
| 132 | 178 |
| 133 // Fast-forward time. Expect |task_b_raw| to be forwarded to |task_target|. | 179 // Fast-forward time. Expect |task_b_raw| to be forwarded to |task_target|. |
| 134 EXPECT_CALL(task_target, DoPostTaskNowCallback(task_b_raw)); | 180 EXPECT_CALL(task_target, DoPostTaskNowCallback(task_b_raw)); |
| 135 service_thread_task_runner_->FastForwardBy(TimeDelta::FromHours(1)); | 181 service_thread_task_runner_->FastForwardBy(TimeDelta::FromHours(1)); |
| 136 testing::Mock::VerifyAndClear(&task_target); | 182 testing::Mock::VerifyAndClear(&task_target); |
| 137 } | 183 } |
| 138 | 184 |
| 139 } // namespace internal | 185 } // namespace internal |
| 140 } // namespace base | 186 } // namespace base |
| OLD | NEW |