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" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/task_scheduler/scheduler_lock.h" | 14 #include "base/task_scheduler/scheduler_lock.h" |
15 #include "base/task_scheduler/scheduler_thread_pool.h" | 15 #include "base/task_scheduler/scheduler_worker_pool.h" |
16 #include "base/task_scheduler/sequence.h" | 16 #include "base/task_scheduler/sequence.h" |
17 #include "base/task_scheduler/task.h" | 17 #include "base/task_scheduler/task.h" |
18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
19 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 | 21 |
22 namespace base { | 22 namespace base { |
23 namespace internal { | 23 namespace internal { |
24 namespace { | 24 namespace { |
25 | 25 |
(...skipping 10 matching lines...) Expand all Loading... |
36 TimeTicks Now() const override { return now_; } | 36 TimeTicks Now() const override { return now_; } |
37 | 37 |
38 MOCK_METHOD0(OnDelayedRunTimeUpdated, void()); | 38 MOCK_METHOD0(OnDelayedRunTimeUpdated, void()); |
39 | 39 |
40 private: | 40 private: |
41 TimeTicks now_ = TimeTicks::Now(); | 41 TimeTicks now_ = TimeTicks::Now(); |
42 | 42 |
43 DISALLOW_COPY_AND_ASSIGN(TestDelayedTaskManager); | 43 DISALLOW_COPY_AND_ASSIGN(TestDelayedTaskManager); |
44 }; | 44 }; |
45 | 45 |
46 class MockSchedulerThreadPool : public SchedulerThreadPool { | 46 class MockSchedulerWorkerPool : public SchedulerWorkerPool { |
47 public: | 47 public: |
48 // SchedulerThreadPool: | 48 // SchedulerWorkerPool: |
49 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 49 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
50 const TaskTraits& traits, | 50 const TaskTraits& traits, |
51 ExecutionMode execution_mode) override { | 51 ExecutionMode execution_mode) override { |
52 NOTREACHED(); | 52 NOTREACHED(); |
53 return nullptr; | 53 return nullptr; |
54 } | 54 } |
55 | 55 |
56 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, | 56 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, |
57 const SequenceSortKey& sequence_sort_key) override { | 57 const SequenceSortKey& sequence_sort_key) override { |
58 NOTREACHED(); | 58 NOTREACHED(); |
(...skipping 30 matching lines...) Expand all Loading... |
89 | 89 |
90 // Verify that a delayed task isn't posted before it is ripe for execution. | 90 // Verify that a delayed task isn't posted before it is ripe for execution. |
91 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTaskBeforeDelayedRunTime) { | 91 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTaskBeforeDelayedRunTime) { |
92 testing::StrictMock<TestDelayedTaskManager> manager; | 92 testing::StrictMock<TestDelayedTaskManager> manager; |
93 | 93 |
94 std::unique_ptr<Task> task( | 94 std::unique_ptr<Task> task( |
95 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); | 95 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); |
96 task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1); | 96 task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1); |
97 const Task* task_raw = task.get(); | 97 const Task* task_raw = task.get(); |
98 scoped_refptr<Sequence> sequence(new Sequence); | 98 scoped_refptr<Sequence> sequence(new Sequence); |
99 testing::StrictMock<MockSchedulerThreadPool> thread_pool; | 99 testing::StrictMock<MockSchedulerWorkerPool> worker_pool; |
100 | 100 |
101 // Add |task| to the DelayedTaskManager. | 101 // Add |task| to the DelayedTaskManager. |
102 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); | 102 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); |
103 manager.AddDelayedTask(std::move(task), sequence, nullptr, &thread_pool); | 103 manager.AddDelayedTask(std::move(task), sequence, nullptr, &worker_pool); |
104 testing::Mock::VerifyAndClear(&manager); | 104 testing::Mock::VerifyAndClear(&manager); |
105 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); | 105 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); |
106 | 106 |
107 // Ask the DelayedTaskManager to post tasks that are ripe for execution. Don't | 107 // Ask the DelayedTaskManager to post tasks that are ripe for execution. Don't |
108 // expect any call to the mock method of |thread_pool|. | 108 // expect any call to the mock method of |worker_pool|. |
109 manager.PostReadyTasks(); | 109 manager.PostReadyTasks(); |
110 | 110 |
111 // The delayed run time shouldn't have changed. | 111 // The delayed run time shouldn't have changed. |
112 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); | 112 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); |
113 } | 113 } |
114 | 114 |
115 // Verify that a delayed task is posted when PostReadyTasks() is called with the | 115 // Verify that a delayed task is posted when PostReadyTasks() is called with the |
116 // current time equal to the task's delayed run time. | 116 // current time equal to the task's delayed run time. |
117 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTasksAtDelayedRunTime) { | 117 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTasksAtDelayedRunTime) { |
118 testing::StrictMock<TestDelayedTaskManager> manager; | 118 testing::StrictMock<TestDelayedTaskManager> manager; |
119 | 119 |
120 std::unique_ptr<Task> task( | 120 std::unique_ptr<Task> task( |
121 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); | 121 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); |
122 task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1); | 122 task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1); |
123 const Task* task_raw = task.get(); | 123 const Task* task_raw = task.get(); |
124 scoped_refptr<Sequence> sequence(new Sequence); | 124 scoped_refptr<Sequence> sequence(new Sequence); |
125 testing::StrictMock<MockSchedulerThreadPool> thread_pool; | 125 testing::StrictMock<MockSchedulerWorkerPool> worker_pool; |
126 | 126 |
127 // Add |task| to the DelayedTaskManager. | 127 // Add |task| to the DelayedTaskManager. |
128 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); | 128 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); |
129 manager.AddDelayedTask(std::move(task), sequence, nullptr, &thread_pool); | 129 manager.AddDelayedTask(std::move(task), sequence, nullptr, &worker_pool); |
130 testing::Mock::VerifyAndClear(&manager); | 130 testing::Mock::VerifyAndClear(&manager); |
131 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); | 131 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); |
132 | 132 |
133 // Fast-forward time to |task_raw|'s delayed run time. | 133 // Fast-forward time to |task_raw|'s delayed run time. |
134 manager.SetCurrentTime(task_raw->delayed_run_time); | 134 manager.SetCurrentTime(task_raw->delayed_run_time); |
135 | 135 |
136 // Ask the DelayedTaskManager to post tasks that are ripe for execution. | 136 // Ask the DelayedTaskManager to post tasks that are ripe for execution. |
137 EXPECT_CALL(thread_pool, | 137 EXPECT_CALL(worker_pool, |
138 PostTaskWithSequenceNowMock(task_raw, sequence.get(), nullptr)); | 138 PostTaskWithSequenceNowMock(task_raw, sequence.get(), nullptr)); |
139 manager.PostReadyTasks(); | 139 manager.PostReadyTasks(); |
140 testing::Mock::VerifyAndClear(&manager); | 140 testing::Mock::VerifyAndClear(&manager); |
141 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); | 141 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); |
142 } | 142 } |
143 | 143 |
144 // Verify that a delayed task is posted when PostReadyTasks() is called with the | 144 // Verify that a delayed task is posted when PostReadyTasks() is called with the |
145 // current time greater than the task's delayed run time. | 145 // current time greater than the task's delayed run time. |
146 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTasksAfterDelayedRunTime) { | 146 TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTasksAfterDelayedRunTime) { |
147 testing::StrictMock<TestDelayedTaskManager> manager; | 147 testing::StrictMock<TestDelayedTaskManager> manager; |
148 | 148 |
149 std::unique_ptr<Task> task( | 149 std::unique_ptr<Task> task( |
150 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); | 150 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); |
151 task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1); | 151 task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1); |
152 const Task* task_raw = task.get(); | 152 const Task* task_raw = task.get(); |
153 scoped_refptr<Sequence> sequence(new Sequence); | 153 scoped_refptr<Sequence> sequence(new Sequence); |
154 testing::StrictMock<MockSchedulerThreadPool> thread_pool; | 154 testing::StrictMock<MockSchedulerWorkerPool> worker_pool; |
155 | 155 |
156 // Add |task| to the DelayedTaskManager. | 156 // Add |task| to the DelayedTaskManager. |
157 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); | 157 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); |
158 manager.AddDelayedTask(std::move(task), sequence, nullptr, &thread_pool); | 158 manager.AddDelayedTask(std::move(task), sequence, nullptr, &worker_pool); |
159 testing::Mock::VerifyAndClear(&manager); | 159 testing::Mock::VerifyAndClear(&manager); |
160 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); | 160 EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime()); |
161 | 161 |
162 // Fast-forward time to |task_raw|'s delayed run time. | 162 // Fast-forward time to |task_raw|'s delayed run time. |
163 manager.SetCurrentTime(task_raw->delayed_run_time + | 163 manager.SetCurrentTime(task_raw->delayed_run_time + |
164 TimeDelta::FromSeconds(10)); | 164 TimeDelta::FromSeconds(10)); |
165 | 165 |
166 // Ask the DelayedTaskManager to post tasks that are ripe for execution. | 166 // Ask the DelayedTaskManager to post tasks that are ripe for execution. |
167 EXPECT_CALL(thread_pool, | 167 EXPECT_CALL(worker_pool, |
168 PostTaskWithSequenceNowMock(task_raw, sequence.get(), nullptr)); | 168 PostTaskWithSequenceNowMock(task_raw, sequence.get(), nullptr)); |
169 manager.PostReadyTasks(); | 169 manager.PostReadyTasks(); |
170 testing::Mock::VerifyAndClear(&manager); | 170 testing::Mock::VerifyAndClear(&manager); |
171 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); | 171 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); |
172 } | 172 } |
173 | 173 |
174 // Verify that when multiple tasks are added to a DelayedTaskManager, they are | 174 // Verify that when multiple tasks are added to a DelayedTaskManager, they are |
175 // posted when they become ripe for execution. | 175 // posted when they become ripe for execution. |
176 TEST(TaskSchedulerDelayedTaskManagerTest, AddAndPostReadyTasks) { | 176 TEST(TaskSchedulerDelayedTaskManagerTest, AddAndPostReadyTasks) { |
177 testing::StrictMock<TestDelayedTaskManager> manager; | 177 testing::StrictMock<TestDelayedTaskManager> manager; |
178 | 178 |
179 scoped_refptr<Sequence> sequence(new Sequence); | 179 scoped_refptr<Sequence> sequence(new Sequence); |
180 testing::StrictMock<MockSchedulerThreadPool> thread_pool; | 180 testing::StrictMock<MockSchedulerWorkerPool> worker_pool; |
181 | 181 |
182 std::unique_ptr<Task> task_a( | 182 std::unique_ptr<Task> task_a( |
183 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); | 183 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); |
184 task_a->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(2); | 184 task_a->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(2); |
185 const Task* task_a_raw = task_a.get(); | 185 const Task* task_a_raw = task_a.get(); |
186 | 186 |
187 std::unique_ptr<Task> task_b( | 187 std::unique_ptr<Task> task_b( |
188 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); | 188 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); |
189 task_b->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(2); | 189 task_b->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(2); |
190 const Task* task_b_raw = task_b.get(); | 190 const Task* task_b_raw = task_b.get(); |
191 | 191 |
192 std::unique_ptr<Task> task_c( | 192 std::unique_ptr<Task> task_c( |
193 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); | 193 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta())); |
194 task_c->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1); | 194 task_c->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1); |
195 const Task* task_c_raw = task_c.get(); | 195 const Task* task_c_raw = task_c.get(); |
196 | 196 |
197 // Add |task_a| to the DelayedTaskManager. The delayed run time should be | 197 // Add |task_a| to the DelayedTaskManager. The delayed run time should be |
198 // updated to |task_a|'s delayed run time. | 198 // updated to |task_a|'s delayed run time. |
199 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); | 199 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); |
200 manager.AddDelayedTask(std::move(task_a), sequence, nullptr, &thread_pool); | 200 manager.AddDelayedTask(std::move(task_a), sequence, nullptr, &worker_pool); |
201 testing::Mock::VerifyAndClear(&manager); | 201 testing::Mock::VerifyAndClear(&manager); |
202 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime()); | 202 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime()); |
203 | 203 |
204 // Add |task_b| to the DelayedTaskManager. The delayed run time shouldn't | 204 // Add |task_b| to the DelayedTaskManager. The delayed run time shouldn't |
205 // change. | 205 // change. |
206 manager.AddDelayedTask(std::move(task_b), sequence, nullptr, &thread_pool); | 206 manager.AddDelayedTask(std::move(task_b), sequence, nullptr, &worker_pool); |
207 testing::Mock::VerifyAndClear(&manager); | 207 testing::Mock::VerifyAndClear(&manager); |
208 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime()); | 208 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime()); |
209 | 209 |
210 // Add |task_c| to the DelayedTaskManager. The delayed run time should be | 210 // Add |task_c| to the DelayedTaskManager. The delayed run time should be |
211 // updated to |task_c|'s delayed run time. | 211 // updated to |task_c|'s delayed run time. |
212 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); | 212 EXPECT_CALL(manager, OnDelayedRunTimeUpdated()); |
213 manager.AddDelayedTask(std::move(task_c), sequence, nullptr, &thread_pool); | 213 manager.AddDelayedTask(std::move(task_c), sequence, nullptr, &worker_pool); |
214 testing::Mock::VerifyAndClear(&manager); | 214 testing::Mock::VerifyAndClear(&manager); |
215 EXPECT_EQ(task_c_raw->delayed_run_time, manager.GetDelayedRunTime()); | 215 EXPECT_EQ(task_c_raw->delayed_run_time, manager.GetDelayedRunTime()); |
216 | 216 |
217 // Fast-forward time to |task_c_raw|'s delayed run time. | 217 // Fast-forward time to |task_c_raw|'s delayed run time. |
218 manager.SetCurrentTime(task_c_raw->delayed_run_time); | 218 manager.SetCurrentTime(task_c_raw->delayed_run_time); |
219 | 219 |
220 // Ask the DelayedTaskManager to post tasks that are ripe for execution. | 220 // Ask the DelayedTaskManager to post tasks that are ripe for execution. |
221 // |task_c_raw| should be posted and the delayed run time should become | 221 // |task_c_raw| should be posted and the delayed run time should become |
222 // |task_a_raw|'s delayed run time. | 222 // |task_a_raw|'s delayed run time. |
223 EXPECT_CALL(thread_pool, | 223 EXPECT_CALL(worker_pool, |
224 PostTaskWithSequenceNowMock(task_c_raw, sequence.get(), nullptr)); | 224 PostTaskWithSequenceNowMock(task_c_raw, sequence.get(), nullptr)); |
225 manager.PostReadyTasks(); | 225 manager.PostReadyTasks(); |
226 testing::Mock::VerifyAndClear(&thread_pool); | 226 testing::Mock::VerifyAndClear(&worker_pool); |
227 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime()); | 227 EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime()); |
228 | 228 |
229 // Fast-forward time to |task_a_raw|'s delayed run time. | 229 // Fast-forward time to |task_a_raw|'s delayed run time. |
230 manager.SetCurrentTime(task_a_raw->delayed_run_time); | 230 manager.SetCurrentTime(task_a_raw->delayed_run_time); |
231 | 231 |
232 // Ask the DelayedTaskManager to post tasks that are ripe for execution. | 232 // Ask the DelayedTaskManager to post tasks that are ripe for execution. |
233 // |task_a_raw| and |task_b_raw| should be posted and the delayed run time | 233 // |task_a_raw| and |task_b_raw| should be posted and the delayed run time |
234 // should become a null TimeTicks. | 234 // should become a null TimeTicks. |
235 EXPECT_CALL(thread_pool, | 235 EXPECT_CALL(worker_pool, |
236 PostTaskWithSequenceNowMock(task_a_raw, sequence.get(), nullptr)); | 236 PostTaskWithSequenceNowMock(task_a_raw, sequence.get(), nullptr)); |
237 EXPECT_CALL(thread_pool, | 237 EXPECT_CALL(worker_pool, |
238 PostTaskWithSequenceNowMock(task_b_raw, sequence.get(), nullptr)); | 238 PostTaskWithSequenceNowMock(task_b_raw, sequence.get(), nullptr)); |
239 manager.PostReadyTasks(); | 239 manager.PostReadyTasks(); |
240 testing::Mock::VerifyAndClear(&thread_pool); | 240 testing::Mock::VerifyAndClear(&worker_pool); |
241 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); | 241 EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime()); |
242 } | 242 } |
243 | 243 |
244 } // namespace internal | 244 } // namespace internal |
245 } // namespace base | 245 } // namespace base |
OLD | NEW |