OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/scheduler/child/task_queue_manager.h" | 5 #include "components/scheduler/child/task_queue_manager.h" |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/test/simple_test_tick_clock.h" | 10 #include "base/test/simple_test_tick_clock.h" |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 runners_[0]->PostTask( | 174 runners_[0]->PostTask( |
175 FROM_HERE, | 175 FROM_HERE, |
176 base::Bind(&PostFromNestedRunloop, message_loop_.get(), runners_[0], | 176 base::Bind(&PostFromNestedRunloop, message_loop_.get(), runners_[0], |
177 base::Unretained(&tasks_to_post_from_nested_loop))); | 177 base::Unretained(&tasks_to_post_from_nested_loop))); |
178 | 178 |
179 message_loop_->RunUntilIdle(); | 179 message_loop_->RunUntilIdle(); |
180 // Note we expect task 3 to run last because it's non-nestable. | 180 // Note we expect task 3 to run last because it's non-nestable. |
181 EXPECT_THAT(run_order, ElementsAre(1, 2, 4, 5, 3)); | 181 EXPECT_THAT(run_order, ElementsAre(1, 2, 4, 5, 3)); |
182 } | 182 } |
183 | 183 |
184 void RepostOnceTestTask(scoped_refptr<base::SingleThreadTaskRunner> runner, | |
185 int value, std::vector<int>* out_result) { | |
186 out_result->push_back(value); | |
187 runner->PostTask(FROM_HERE, base::Bind(&NullTask)); | |
188 } | |
189 | |
190 void PostFromNestedRunloopWithAfterWakeupQueue( | |
191 base::MessageLoop* message_loop, | |
192 base::SingleThreadTaskRunner* after_wakeup_runner, | |
193 base::SingleThreadTaskRunner* wakeup_runner, | |
194 std::vector<std::pair<base::Closure, bool>>* tasks) { | |
195 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop); | |
196 for (std::pair<base::Closure, bool>& pair : *tasks) { | |
197 if (pair.second) { | |
198 after_wakeup_runner->PostTask(FROM_HERE, pair.first); | |
199 } else { | |
200 after_wakeup_runner->PostNonNestableTask(FROM_HERE, pair.first); | |
201 } | |
202 } | |
203 // Post a task to wake up the after wakeup queue. | |
204 wakeup_runner->PostTask(FROM_HERE, base::Bind(&NullTask)); | |
205 message_loop->RunUntilIdle(); | |
206 } | |
207 | |
208 TEST_F(TaskQueueManagerTest, NonNestableTaskUsesPreviousTaskForUpdateQueues) { | |
209 InitializeWithRealMessageLoop(2u); | |
210 runners_[0]->SetPumpPolicy(TaskQueue::PumpPolicy::AFTER_WAKEUP); | |
211 | |
212 std::vector<int> run_order; | |
213 std::vector<std::pair<base::Closure, bool>> tasks_to_post_from_nested_loop; | |
214 // This task posts a task, so that there is something in the incoming queue | |
215 // of the after wakeup task queue when the non-nestable task runs. | |
216 tasks_to_post_from_nested_loop.push_back(std::make_pair( | |
217 base::Bind(&RepostOnceTestTask, runners_[0], 1, &run_order), true)); | |
218 tasks_to_post_from_nested_loop.push_back( | |
219 std::make_pair(base::Bind(&TestTask, 2, &run_order), false)); | |
220 | |
221 runners_[1]->PostTask( | |
222 FROM_HERE, | |
223 base::Bind(&PostFromNestedRunloopWithAfterWakeupQueue, | |
224 message_loop_.get(), runners_[0], runners_[1], | |
225 base::Unretained(&tasks_to_post_from_nested_loop))); | |
226 | |
227 message_loop_->RunUntilIdle(); | |
228 EXPECT_THAT(run_order, ElementsAre(1, 2)); | |
229 } | |
230 | |
231 TEST_F(TaskQueueManagerTest, QueuePolling) { | 184 TEST_F(TaskQueueManagerTest, QueuePolling) { |
232 Initialize(1u); | 185 Initialize(1u); |
233 | 186 |
234 std::vector<int> run_order; | 187 std::vector<int> run_order; |
235 EXPECT_TRUE(runners_[0]->IsQueueEmpty()); | 188 EXPECT_TRUE(runners_[0]->IsQueueEmpty()); |
236 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order)); | 189 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order)); |
237 EXPECT_FALSE(runners_[0]->IsQueueEmpty()); | 190 EXPECT_FALSE(runners_[0]->IsQueueEmpty()); |
238 | 191 |
239 test_task_runner_->RunUntilIdle(); | 192 test_task_runner_->RunUntilIdle(); |
240 EXPECT_TRUE(runners_[0]->IsQueueEmpty()); | 193 EXPECT_TRUE(runners_[0]->IsQueueEmpty()); |
(...skipping 920 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1161 | 1114 |
1162 // The sequence numbers are a zero-based monotonically incrememting counter | 1115 // The sequence numbers are a zero-based monotonically incrememting counter |
1163 // which should be set when the task is posted rather than when it's enqueued | 1116 // which should be set when the task is posted rather than when it's enqueued |
1164 // onto the incomming queue. | 1117 // onto the incomming queue. |
1165 EXPECT_THAT(observer.sequence_numbers(), ElementsAre(3, 2, 1, 0)); | 1118 EXPECT_THAT(observer.sequence_numbers(), ElementsAre(3, 2, 1, 0)); |
1166 | 1119 |
1167 manager_->RemoveTaskObserver(&observer); | 1120 manager_->RemoveTaskObserver(&observer); |
1168 } | 1121 } |
1169 | 1122 |
1170 } // namespace scheduler | 1123 } // namespace scheduler |
OLD | NEW |