| 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 27 matching lines...) Expand all Loading... |
| 38 | 38 |
| 39 private: | 39 private: |
| 40 TimeTicks now_ = TimeTicks::Now(); | 40 TimeTicks now_ = TimeTicks::Now(); |
| 41 | 41 |
| 42 DISALLOW_COPY_AND_ASSIGN(TestDelayedTaskManager); | 42 DISALLOW_COPY_AND_ASSIGN(TestDelayedTaskManager); |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 class MockSchedulerThreadPool : public SchedulerThreadPoolInterface { | 45 class MockSchedulerThreadPool : public SchedulerThreadPoolInterface { |
| 46 public: | 46 public: |
| 47 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, | 47 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, |
| 48 scoped_refptr<Sequence> sequence) override { | 48 scoped_refptr<Sequence> sequence, |
| 49 PostTaskWithSequenceNowMock(task.get(), sequence.get()); | 49 SchedulerWorkerThread* worker_thread) override { |
| 50 PostTaskWithSequenceNowMock(task.get(), sequence.get(), worker_thread); |
| 50 } | 51 } |
| 51 | 52 |
| 52 MOCK_METHOD2(PostTaskWithSequenceNowMock, void(const Task*, const Sequence*)); | 53 MOCK_METHOD3(PostTaskWithSequenceNowMock, |
| 54 void(const Task*, |
| 55 const Sequence*, |
| 56 const SchedulerWorkerThread* worker_thread)); |
| 53 }; | 57 }; |
| 54 | 58 |
| 55 // Verify that GetDelayedRunTime() returns a null TimeTicks when there are | 59 // Verify that GetDelayedRunTime() returns a null TimeTicks when there are |
| 56 // no pending delayed tasks. | 60 // no pending delayed tasks. |
| 57 TEST(TaskSchedulerDelayedTaskManagerTest, | 61 TEST(TaskSchedulerDelayedTaskManagerTest, |
| 58 GetDelayedRunTimeNoPendingDelayedTasks) { | 62 GetDelayedRunTimeNoPendingDelayedTasks) { |
| 59 TestDelayedTaskManager manager; | 63 TestDelayedTaskManager manager; |
| 60 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); | 64 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); |
| 61 } | 65 } |
| 62 | 66 |
| 63 // Verify that a delayed task isn't posted before it is ripe for execution. | 67 // Verify that a delayed task isn't posted before it is ripe for execution. |
| 64 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTaskBeforeDelayedRunTime) { | 68 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTaskBeforeDelayedRunTime) { |
| 65 testing::StrictMock<TestDelayedTaskManager> manager; | 69 testing::StrictMock<TestDelayedTaskManager> manager; |
| 66 | 70 |
| 67 std::unique_ptr<Task> task( | 71 std::unique_ptr<Task> task( |
| 68 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); | 72 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); |
| 69 task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1); | 73 task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1); |
| 70 const Task* task_raw = task.get(); | 74 const Task* task_raw = task.get(); |
| 71 scoped_refptr<Sequence> sequence(new Sequence); | 75 scoped_refptr<Sequence> sequence(new Sequence); |
| 72 testing::StrictMock<MockSchedulerThreadPool> thread_pool; | 76 testing::StrictMock<MockSchedulerThreadPool> thread_pool; |
| 73 | 77 |
| 74 // Add |task| to the DelayedTaskManager. | 78 // Add |task| to the DelayedTaskManager. |
| 75 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); | 79 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); |
| 76 manager.AddDelayedTask(std::move(task), sequence, &thread_pool); | 80 manager.AddDelayedTask(std::move(task), sequence, nullptr, &thread_pool); |
| 77 testing::Mock::VerifyAndClear(&manager); | 81 testing::Mock::VerifyAndClear(&manager); |
| 78 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); | 82 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); |
| 79 | 83 |
| 80 // Ask the DelayedTaskManager to post tasks that are ripe for execution. Don't | 84 // Ask the DelayedTaskManager to post tasks that are ripe for execution. Don't |
| 81 // expect any call to the mock method of |thread_pool|. | 85 // expect any call to the mock method of |thread_pool|. |
| 82 manager.PostReadyTasks(); | 86 manager.PostReadyTasks(); |
| 83 | 87 |
| 84 // The delayed run time shouldn't have changed. | 88 // The delayed run time shouldn't have changed. |
| 85 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); | 89 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); |
| 86 } | 90 } |
| 87 | 91 |
| 88 // Verify that a delayed task is posted when PostReadyTasks() is called with the | 92 // Verify that a delayed task is posted when PostReadyTasks() is called with the |
| 89 // current time equal to the task's delayed run time. | 93 // current time equal to the task's delayed run time. |
| 90 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTasksAtDelayedRunTime) { | 94 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTasksAtDelayedRunTime) { |
| 91 testing::StrictMock<TestDelayedTaskManager> manager; | 95 testing::StrictMock<TestDelayedTaskManager> manager; |
| 92 | 96 |
| 93 std::unique_ptr<Task> task( | 97 std::unique_ptr<Task> task( |
| 94 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); | 98 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); |
| 95 task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1); | 99 task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1); |
| 96 const Task* task_raw = task.get(); | 100 const Task* task_raw = task.get(); |
| 97 scoped_refptr<Sequence> sequence(new Sequence); | 101 scoped_refptr<Sequence> sequence(new Sequence); |
| 98 testing::StrictMock<MockSchedulerThreadPool> thread_pool; | 102 testing::StrictMock<MockSchedulerThreadPool> thread_pool; |
| 99 | 103 |
| 100 // Add |task| to the DelayedTaskManager. | 104 // Add |task| to the DelayedTaskManager. |
| 101 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); | 105 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); |
| 102 manager.AddDelayedTask(std::move(task), sequence, &thread_pool); | 106 manager.AddDelayedTask(std::move(task), sequence, nullptr, &thread_pool); |
| 103 testing::Mock::VerifyAndClear(&manager); | 107 testing::Mock::VerifyAndClear(&manager); |
| 104 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); | 108 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); |
| 105 | 109 |
| 106 // Fast-forward time to |task_raw|'s delayed run time. | 110 // Fast-forward time to |task_raw|'s delayed run time. |
| 107 manager.SetCurrentTime(task_raw->delayed_run_time); | 111 manager.SetCurrentTime(task_raw->delayed_run_time); |
| 108 | 112 |
| 109 // Ask the DelayedTaskManager to post tasks that are ripe for execution. | 113 // Ask the DelayedTaskManager to post tasks that are ripe for execution. |
| 110 EXPECT_CALL(thread_pool, | 114 EXPECT_CALL(thread_pool, |
| 111 PostTaskWithSequenceNowMock(task_raw, sequence.get())); | 115 PostTaskWithSequenceNowMock(task_raw, sequence.get(), nullptr)); |
| 112 manager.PostReadyTasks(); | 116 manager.PostReadyTasks(); |
| 113 testing::Mock::VerifyAndClear(&manager); | 117 testing::Mock::VerifyAndClear(&manager); |
| 114 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); | 118 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); |
| 115 } | 119 } |
| 116 | 120 |
| 117 // Verify that a delayed task is posted when PostReadyTasks() is called with the | 121 // Verify that a delayed task is posted when PostReadyTasks() is called with the |
| 118 // current time greater than the task's delayed run time. | 122 // current time greater than the task's delayed run time. |
| 119 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTasksAfterDelayedRunTime) { | 123 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTasksAfterDelayedRunTime) { |
| 120 testing::StrictMock<TestDelayedTaskManager> manager; | 124 testing::StrictMock<TestDelayedTaskManager> manager; |
| 121 | 125 |
| 122 std::unique_ptr<Task> task( | 126 std::unique_ptr<Task> task( |
| 123 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); | 127 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); |
| 124 task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1); | 128 task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1); |
| 125 const Task* task_raw = task.get(); | 129 const Task* task_raw = task.get(); |
| 126 scoped_refptr<Sequence> sequence(new Sequence); | 130 scoped_refptr<Sequence> sequence(new Sequence); |
| 127 testing::StrictMock<MockSchedulerThreadPool> thread_pool; | 131 testing::StrictMock<MockSchedulerThreadPool> thread_pool; |
| 128 | 132 |
| 129 // Add |task| to the DelayedTaskManager. | 133 // Add |task| to the DelayedTaskManager. |
| 130 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); | 134 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); |
| 131 manager.AddDelayedTask(std::move(task), sequence, &thread_pool); | 135 manager.AddDelayedTask(std::move(task), sequence, nullptr, &thread_pool); |
| 132 testing::Mock::VerifyAndClear(&manager); | 136 testing::Mock::VerifyAndClear(&manager); |
| 133 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); | 137 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); |
| 134 | 138 |
| 135 // Fast-forward time to |task_raw|'s delayed run time. | 139 // Fast-forward time to |task_raw|'s delayed run time. |
| 136 manager.SetCurrentTime(task_raw->delayed_run_time + | 140 manager.SetCurrentTime(task_raw->delayed_run_time + |
| 137 TimeDelta::FromSeconds(10)); | 141 TimeDelta::FromSeconds(10)); |
| 138 | 142 |
| 139 // Ask the DelayedTaskManager to post tasks that are ripe for execution. | 143 // Ask the DelayedTaskManager to post tasks that are ripe for execution. |
| 140 EXPECT_CALL(thread_pool, | 144 EXPECT_CALL(thread_pool, |
| 141 PostTaskWithSequenceNowMock(task_raw, sequence.get())); | 145 PostTaskWithSequenceNowMock(task_raw, sequence.get(), nullptr)); |
| 142 manager.PostReadyTasks(); | 146 manager.PostReadyTasks(); |
| 143 testing::Mock::VerifyAndClear(&manager); | 147 testing::Mock::VerifyAndClear(&manager); |
| 144 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); | 148 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); |
| 145 } | 149 } |
| 146 | 150 |
| 147 // Verify that when multiple tasks are added to a DelayedTaskManager, they are | 151 // Verify that when multiple tasks are added to a DelayedTaskManager, they are |
| 148 // posted when they become ripe for execution. | 152 // posted when they become ripe for execution. |
| 149 TEST(TaskSchedulerDelayedTaskManagerTest, AddAndPostReadyTasks) { | 153 TEST(TaskSchedulerDelayedTaskManagerTest, AddAndPostReadyTasks) { |
| 150 testing::StrictMock<TestDelayedTaskManager> manager; | 154 testing::StrictMock<TestDelayedTaskManager> manager; |
| 151 | 155 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 163 const Task* task_b_raw = task_b.get(); | 167 const Task* task_b_raw = task_b.get(); |
| 164 | 168 |
| 165 std::unique_ptr<Task> task_c( | 169 std::unique_ptr<Task> task_c( |
| 166 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); | 170 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); |
| 167 task_c->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1); | 171 task_c->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1); |
| 168 const Task* task_c_raw = task_c.get(); | 172 const Task* task_c_raw = task_c.get(); |
| 169 | 173 |
| 170 // Add |task_a| to the DelayedTaskManager. The delayed run time should be | 174 // Add |task_a| to the DelayedTaskManager. The delayed run time should be |
| 171 // updated to |task_a|'s delayed run time. | 175 // updated to |task_a|'s delayed run time. |
| 172 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); | 176 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); |
| 173 manager.AddDelayedTask(std::move(task_a), sequence, &thread_pool); | 177 manager.AddDelayedTask(std::move(task_a), sequence, nullptr, &thread_pool); |
| 174 testing::Mock::VerifyAndClear(&manager); | 178 testing::Mock::VerifyAndClear(&manager); |
| 175 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime()); | 179 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime()); |
| 176 | 180 |
| 177 // Add |task_b| to the DelayedTaskManager. The delayed run time shouldn't | 181 // Add |task_b| to the DelayedTaskManager. The delayed run time shouldn't |
| 178 // change. | 182 // change. |
| 179 manager.AddDelayedTask(std::move(task_b), sequence, &thread_pool); | 183 manager.AddDelayedTask(std::move(task_b), sequence, nullptr, &thread_pool); |
| 180 testing::Mock::VerifyAndClear(&manager); | 184 testing::Mock::VerifyAndClear(&manager); |
| 181 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime()); | 185 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime()); |
| 182 | 186 |
| 183 // Add |task_c| to the DelayedTaskManager. The delayed run time should be | 187 // Add |task_c| to the DelayedTaskManager. The delayed run time should be |
| 184 // updated to |task_c|'s delayed run time. | 188 // updated to |task_c|'s delayed run time. |
| 185 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); | 189 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); |
| 186 manager.AddDelayedTask(std::move(task_c), sequence, &thread_pool); | 190 manager.AddDelayedTask(std::move(task_c), sequence, nullptr, &thread_pool); |
| 187 testing::Mock::VerifyAndClear(&manager); | 191 testing::Mock::VerifyAndClear(&manager); |
| 188 EXPECT_EQ(task_c_raw->delayed_run_time, manager.GetDelayedRunTime()); | 192 EXPECT_EQ(task_c_raw->delayed_run_time, manager.GetDelayedRunTime()); |
| 189 | 193 |
| 190 // Fast-forward time to |task_c_raw|'s delayed run time. | 194 // Fast-forward time to |task_c_raw|'s delayed run time. |
| 191 manager.SetCurrentTime(task_c_raw->delayed_run_time); | 195 manager.SetCurrentTime(task_c_raw->delayed_run_time); |
| 192 | 196 |
| 193 // Ask the DelayedTaskManager to post tasks that are ripe for execution. | 197 // Ask the DelayedTaskManager to post tasks that are ripe for execution. |
| 194 // |task_c_raw| should be posted and the delayed run time should become | 198 // |task_c_raw| should be posted and the delayed run time should become |
| 195 // |task_a_raw|'s delayed run time. | 199 // |task_a_raw|'s delayed run time. |
| 196 EXPECT_CALL(thread_pool, | 200 EXPECT_CALL(thread_pool, |
| 197 PostTaskWithSequenceNowMock(task_c_raw, sequence.get())); | 201 PostTaskWithSequenceNowMock(task_c_raw, sequence.get(), nullptr)); |
| 198 manager.PostReadyTasks(); | 202 manager.PostReadyTasks(); |
| 199 testing::Mock::VerifyAndClear(&thread_pool); | 203 testing::Mock::VerifyAndClear(&thread_pool); |
| 200 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime()); | 204 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime()); |
| 201 | 205 |
| 202 // Fast-forward time to |task_a_raw|'s delayed run time. | 206 // Fast-forward time to |task_a_raw|'s delayed run time. |
| 203 manager.SetCurrentTime(task_a_raw->delayed_run_time); | 207 manager.SetCurrentTime(task_a_raw->delayed_run_time); |
| 204 | 208 |
| 205 // Ask the DelayedTaskManager to post tasks that are ripe for execution. | 209 // Ask the DelayedTaskManager to post tasks that are ripe for execution. |
| 206 // |task_a_raw| and |task_b_raw| should be posted and the delayed run time | 210 // |task_a_raw| and |task_b_raw| should be posted and the delayed run time |
| 207 // should become a null TimeTicks. | 211 // should become a null TimeTicks. |
| 208 EXPECT_CALL(thread_pool, | 212 EXPECT_CALL(thread_pool, |
| 209 PostTaskWithSequenceNowMock(task_a_raw, sequence.get())); | 213 PostTaskWithSequenceNowMock(task_a_raw, sequence.get(), nullptr)); |
| 210 EXPECT_CALL(thread_pool, | 214 EXPECT_CALL(thread_pool, |
| 211 PostTaskWithSequenceNowMock(task_b_raw, sequence.get())); | 215 PostTaskWithSequenceNowMock(task_b_raw, sequence.get(), nullptr)); |
| 212 manager.PostReadyTasks(); | 216 manager.PostReadyTasks(); |
| 213 testing::Mock::VerifyAndClear(&thread_pool); | 217 testing::Mock::VerifyAndClear(&thread_pool); |
| 214 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); | 218 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); |
| 215 } | 219 } |
| 216 | 220 |
| 217 } // namespace | 221 } // namespace |
| 218 } // namespace internal | 222 } // namespace internal |
| 219 } // namespace base | 223 } // namespace base |
| OLD | NEW |