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/base/task_queue_manager.h" | 5 #include "components/scheduler/base/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 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 | 226 |
227 TEST_F(TaskQueueManagerTest, DelayedTaskPosting) { | 227 TEST_F(TaskQueueManagerTest, DelayedTaskPosting) { |
228 Initialize(1u); | 228 Initialize(1u); |
229 | 229 |
230 std::vector<int> run_order; | 230 std::vector<int> run_order; |
231 base::TimeDelta delay(base::TimeDelta::FromMilliseconds(10)); | 231 base::TimeDelta delay(base::TimeDelta::FromMilliseconds(10)); |
232 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order), | 232 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order), |
233 delay); | 233 delay); |
234 EXPECT_EQ(delay, test_task_runner_->DelayToNextTaskTime()); | 234 EXPECT_EQ(delay, test_task_runner_->DelayToNextTaskTime()); |
235 EXPECT_TRUE(runners_[0]->IsQueueEmpty()); | 235 EXPECT_TRUE(runners_[0]->IsQueueEmpty()); |
| 236 EXPECT_TRUE(runners_[0]->HasPendingDelayedTask()); |
236 EXPECT_TRUE(run_order.empty()); | 237 EXPECT_TRUE(run_order.empty()); |
237 | 238 |
238 // The task doesn't run before the delay has completed. | 239 // The task doesn't run before the delay has completed. |
239 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(9)); | 240 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(9)); |
240 EXPECT_TRUE(run_order.empty()); | 241 EXPECT_TRUE(run_order.empty()); |
241 | 242 |
242 // After the delay has completed, the task runs normally. | 243 // After the delay has completed, the task runs normally. |
243 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(1)); | 244 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(1)); |
244 EXPECT_THAT(run_order, ElementsAre(1)); | 245 EXPECT_THAT(run_order, ElementsAre(1)); |
| 246 EXPECT_FALSE(runners_[0]->HasPendingDelayedTask()); |
245 } | 247 } |
246 | 248 |
247 bool MessageLoopTaskCounter(size_t* count) { | 249 bool MessageLoopTaskCounter(size_t* count) { |
248 *count = *count + 1; | 250 *count = *count + 1; |
249 return true; | 251 return true; |
250 } | 252 } |
251 | 253 |
252 TEST_F(TaskQueueManagerTest, DelayedTaskExecutedInOneMessageLoopTask) { | 254 TEST_F(TaskQueueManagerTest, DelayedTaskExecutedInOneMessageLoopTask) { |
253 Initialize(1u); | 255 Initialize(1u); |
254 | 256 |
(...skipping 840 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1095 delay1); | 1097 delay1); |
1096 runners_[1]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order), | 1098 runners_[1]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order), |
1097 delay2); | 1099 delay2); |
1098 | 1100 |
1099 now_src_->Advance(delay1 * 2); | 1101 now_src_->Advance(delay1 * 2); |
1100 test_task_runner_->RunUntilIdle(); | 1102 test_task_runner_->RunUntilIdle(); |
1101 | 1103 |
1102 EXPECT_THAT(run_order, ElementsAre(2, 1)); | 1104 EXPECT_THAT(run_order, ElementsAre(2, 1)); |
1103 } | 1105 } |
1104 | 1106 |
1105 TEST_F(TaskQueueManagerTest, DelayedTaskWithAbsoluteRunTime) { | |
1106 Initialize(1u); | |
1107 | |
1108 // One task in the past, two with the exact same run time and one in the | |
1109 // future. | |
1110 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(10); | |
1111 base::TimeTicks runTime1 = now_src_->NowTicks() - delay; | |
1112 base::TimeTicks runTime2 = now_src_->NowTicks(); | |
1113 base::TimeTicks runTime3 = now_src_->NowTicks(); | |
1114 base::TimeTicks runTime4 = now_src_->NowTicks() + delay; | |
1115 | |
1116 std::vector<int> run_order; | |
1117 runners_[0]->PostDelayedTaskAt( | |
1118 FROM_HERE, base::Bind(&TestTask, 1, &run_order), runTime1); | |
1119 runners_[0]->PostDelayedTaskAt( | |
1120 FROM_HERE, base::Bind(&TestTask, 2, &run_order), runTime2); | |
1121 runners_[0]->PostDelayedTaskAt( | |
1122 FROM_HERE, base::Bind(&TestTask, 3, &run_order), runTime3); | |
1123 runners_[0]->PostDelayedTaskAt( | |
1124 FROM_HERE, base::Bind(&TestTask, 4, &run_order), runTime4); | |
1125 | |
1126 now_src_->Advance(2 * delay); | |
1127 test_task_runner_->RunUntilIdle(); | |
1128 | |
1129 EXPECT_THAT(run_order, ElementsAre(1, 2, 3, 4)); | |
1130 } | |
1131 | |
1132 void CheckIsNested(bool* is_nested) { | 1107 void CheckIsNested(bool* is_nested) { |
1133 *is_nested = base::MessageLoop::current()->IsNested(); | 1108 *is_nested = base::MessageLoop::current()->IsNested(); |
1134 } | 1109 } |
1135 | 1110 |
1136 void PostAndQuitFromNestedRunloop(base::RunLoop* run_loop, | 1111 void PostAndQuitFromNestedRunloop(base::RunLoop* run_loop, |
1137 base::SingleThreadTaskRunner* runner, | 1112 base::SingleThreadTaskRunner* runner, |
1138 bool* was_nested) { | 1113 bool* was_nested) { |
1139 base::MessageLoop::ScopedNestableTaskAllower allow( | 1114 base::MessageLoop::ScopedNestableTaskAllower allow( |
1140 base::MessageLoop::current()); | 1115 base::MessageLoop::current()); |
1141 runner->PostTask(FROM_HERE, run_loop->QuitClosure()); | 1116 runner->PostTask(FROM_HERE, run_loop->QuitClosure()); |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1316 | 1291 |
1317 scoped_refptr<internal::TaskQueueImpl> task_queue = | 1292 scoped_refptr<internal::TaskQueueImpl> task_queue = |
1318 manager_->NewTaskQueue(TaskQueue::Spec("test_queue")); | 1293 manager_->NewTaskQueue(TaskQueue::Spec("test_queue")); |
1319 | 1294 |
1320 EXPECT_CALL(observer, OnUnregisterTaskQueue(_)).Times(1); | 1295 EXPECT_CALL(observer, OnUnregisterTaskQueue(_)).Times(1); |
1321 task_queue->UnregisterTaskQueue(); | 1296 task_queue->UnregisterTaskQueue(); |
1322 | 1297 |
1323 manager_->SetObserver(nullptr); | 1298 manager_->SetObserver(nullptr); |
1324 } | 1299 } |
1325 | 1300 |
1326 TEST_F(TaskQueueManagerTest, ScheduleDelayedWorkIsNotReEntrant) { | |
1327 Initialize(1u); | |
1328 | |
1329 // Post two tasks into the past. The second one used to trigger a deadlock | |
1330 // because it tried to re-entrantly wake the first task in the same queue. | |
1331 runners_[0]->PostDelayedTaskAt( | |
1332 FROM_HERE, base::Bind(&NullTask), | |
1333 base::TimeTicks() + base::TimeDelta::FromMicroseconds(100)); | |
1334 runners_[0]->PostDelayedTaskAt( | |
1335 FROM_HERE, base::Bind(&NullTask), | |
1336 base::TimeTicks() + base::TimeDelta::FromMicroseconds(200)); | |
1337 test_task_runner_->RunUntilIdle(); | |
1338 } | |
1339 | |
1340 void HasOneRefTask(std::vector<bool>* log, internal::TaskQueueImpl* tq) { | 1301 void HasOneRefTask(std::vector<bool>* log, internal::TaskQueueImpl* tq) { |
1341 log->push_back(tq->HasOneRef()); | 1302 log->push_back(tq->HasOneRef()); |
1342 } | 1303 } |
1343 | 1304 |
1344 TEST_F(TaskQueueManagerTest, UnregisterTaskQueueInNestedLoop) { | 1305 TEST_F(TaskQueueManagerTest, UnregisterTaskQueueInNestedLoop) { |
1345 InitializeWithRealMessageLoop(1u); | 1306 InitializeWithRealMessageLoop(1u); |
1346 | 1307 |
1347 // We retain a reference to the task queue even when the manager has deleted | 1308 // We retain a reference to the task queue even when the manager has deleted |
1348 // its reference. | 1309 // its reference. |
1349 scoped_refptr<internal::TaskQueueImpl> task_queue = | 1310 scoped_refptr<internal::TaskQueueImpl> task_queue = |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1476 for (int i = 1; i < 100; i++) { | 1437 for (int i = 1; i < 100; i++) { |
1477 runners_[0]->PostDelayedTask( | 1438 runners_[0]->PostDelayedTask( |
1478 FROM_HERE, | 1439 FROM_HERE, |
1479 base::Bind(&ChromiumRunloopInspectionTask, test_task_runner_), | 1440 base::Bind(&ChromiumRunloopInspectionTask, test_task_runner_), |
1480 base::TimeDelta::FromMilliseconds(i)); | 1441 base::TimeDelta::FromMilliseconds(i)); |
1481 } | 1442 } |
1482 test_task_runner_->RunUntilIdle(); | 1443 test_task_runner_->RunUntilIdle(); |
1483 } | 1444 } |
1484 | 1445 |
1485 } // namespace scheduler | 1446 } // namespace scheduler |
OLD | NEW |