Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / components / scheduler / child / task_queue_manager_unittest.cc
blobd9eb15416ca36ee2dac0f1c65dfbaaf036046099
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
3 // found in the LICENSE file.
5 #include "components/scheduler/child/task_queue_manager.h"
7 #include "base/location.h"
8 #include "base/run_loop.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/test/simple_test_tick_clock.h"
11 #include "base/threading/thread.h"
12 #include "cc/test/ordered_simple_task_runner.h"
13 #include "components/scheduler/child/nestable_task_runner_for_test.h"
14 #include "components/scheduler/child/scheduler_task_runner_delegate_impl.h"
15 #include "components/scheduler/child/task_queue_impl.h"
16 #include "components/scheduler/child/task_queue_selector.h"
17 #include "components/scheduler/child/task_queue_sets.h"
18 #include "components/scheduler/child/test_time_source.h"
19 #include "components/scheduler/test/test_always_fail_time_source.h"
20 #include "testing/gmock/include/gmock/gmock.h"
22 using testing::ElementsAre;
23 using testing::_;
25 namespace scheduler {
27 class TaskQueueManagerTest : public testing::Test {
28 public:
29 void DeleteTaskQueueManager() { manager_.reset(); }
31 protected:
32 void Initialize(size_t num_queues) {
33 now_src_.reset(new base::SimpleTestTickClock());
34 now_src_->Advance(base::TimeDelta::FromMicroseconds(1000));
35 test_task_runner_ = make_scoped_refptr(
36 new cc::OrderedSimpleTaskRunner(now_src_.get(), false));
37 main_task_runner_ =
38 NestableTaskRunnerForTest::Create(test_task_runner_.get());
39 manager_ = make_scoped_ptr(new TaskQueueManager(
40 main_task_runner_, "test.scheduler", "test.scheduler.debug"));
41 manager_->SetTimeSourceForTesting(
42 make_scoped_ptr(new TestTimeSource(now_src_.get())));
44 for (size_t i = 0; i < num_queues; i++)
45 runners_.push_back(manager_->NewTaskQueue(TaskQueue::Spec("test_queue")));
48 void InitializeWithRealMessageLoop(size_t num_queues) {
49 message_loop_.reset(new base::MessageLoop());
50 manager_ = make_scoped_ptr(new TaskQueueManager(
51 SchedulerTaskRunnerDelegateImpl::Create(message_loop_.get()),
52 "test.scheduler", "test.scheduler.debug"));
54 for (size_t i = 0; i < num_queues; i++)
55 runners_.push_back(manager_->NewTaskQueue(TaskQueue::Spec("test_queue")));
58 scoped_ptr<base::MessageLoop> message_loop_;
59 scoped_ptr<base::SimpleTestTickClock> now_src_;
60 scoped_refptr<NestableTaskRunnerForTest> main_task_runner_;
61 scoped_refptr<cc::OrderedSimpleTaskRunner> test_task_runner_;
62 scoped_ptr<TaskQueueManager> manager_;
63 std::vector<scoped_refptr<internal::TaskQueueImpl>> runners_;
66 void PostFromNestedRunloop(base::MessageLoop* message_loop,
67 base::SingleThreadTaskRunner* runner,
68 std::vector<std::pair<base::Closure, bool>>* tasks) {
69 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop);
70 for (std::pair<base::Closure, bool>& pair : *tasks) {
71 if (pair.second) {
72 runner->PostTask(FROM_HERE, pair.first);
73 } else {
74 runner->PostNonNestableTask(FROM_HERE, pair.first);
77 message_loop->RunUntilIdle();
80 void NullTask() {
83 void TestTask(int value, std::vector<int>* out_result) {
84 out_result->push_back(value);
87 TEST_F(TaskQueueManagerTest, SingleQueuePosting) {
88 Initialize(1u);
90 std::vector<int> run_order;
91 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
92 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
93 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order));
95 test_task_runner_->RunUntilIdle();
96 EXPECT_THAT(run_order, ElementsAre(1, 2, 3));
99 TEST_F(TaskQueueManagerTest, MultiQueuePosting) {
100 Initialize(3u);
102 std::vector<int> run_order;
103 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
104 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
105 runners_[1]->PostTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order));
106 runners_[1]->PostTask(FROM_HERE, base::Bind(&TestTask, 4, &run_order));
107 runners_[2]->PostTask(FROM_HERE, base::Bind(&TestTask, 5, &run_order));
108 runners_[2]->PostTask(FROM_HERE, base::Bind(&TestTask, 6, &run_order));
110 test_task_runner_->RunUntilIdle();
111 EXPECT_THAT(run_order, ElementsAre(1, 2, 3, 4, 5, 6));
114 void NopTask() {
117 TEST_F(TaskQueueManagerTest, NowNotCalledWhenThereAreNoDelayedTasks) {
118 Initialize(3u);
120 manager_->SetTimeSourceForTesting(
121 make_scoped_ptr(new TestAlwaysFailTimeSource()));
123 runners_[0]->PostTask(FROM_HERE, base::Bind(&NopTask));
124 runners_[0]->PostTask(FROM_HERE, base::Bind(&NopTask));
125 runners_[1]->PostTask(FROM_HERE, base::Bind(&NopTask));
126 runners_[1]->PostTask(FROM_HERE, base::Bind(&NopTask));
127 runners_[2]->PostTask(FROM_HERE, base::Bind(&NopTask));
128 runners_[2]->PostTask(FROM_HERE, base::Bind(&NopTask));
130 test_task_runner_->RunUntilIdle();
133 TEST_F(TaskQueueManagerTest, NonNestableTaskPosting) {
134 InitializeWithRealMessageLoop(1u);
136 std::vector<int> run_order;
137 runners_[0]->PostNonNestableTask(FROM_HERE,
138 base::Bind(&TestTask, 1, &run_order));
140 message_loop_->RunUntilIdle();
141 EXPECT_THAT(run_order, ElementsAre(1));
144 TEST_F(TaskQueueManagerTest, NonNestableTaskExecutesInExpectedOrder) {
145 InitializeWithRealMessageLoop(1u);
147 std::vector<int> run_order;
148 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
149 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
150 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order));
151 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 4, &run_order));
152 runners_[0]->PostNonNestableTask(FROM_HERE,
153 base::Bind(&TestTask, 5, &run_order));
155 message_loop_->RunUntilIdle();
156 EXPECT_THAT(run_order, ElementsAre(1, 2, 3, 4, 5));
159 TEST_F(TaskQueueManagerTest, NonNestableTaskDoesntExecuteInNestedLoop) {
160 InitializeWithRealMessageLoop(1u);
162 std::vector<int> run_order;
163 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
164 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
166 std::vector<std::pair<base::Closure, bool>> tasks_to_post_from_nested_loop;
167 tasks_to_post_from_nested_loop.push_back(
168 std::make_pair(base::Bind(&TestTask, 3, &run_order), false));
169 tasks_to_post_from_nested_loop.push_back(
170 std::make_pair(base::Bind(&TestTask, 4, &run_order), true));
171 tasks_to_post_from_nested_loop.push_back(
172 std::make_pair(base::Bind(&TestTask, 5, &run_order), true));
174 runners_[0]->PostTask(
175 FROM_HERE,
176 base::Bind(&PostFromNestedRunloop, message_loop_.get(), runners_[0],
177 base::Unretained(&tasks_to_post_from_nested_loop)));
179 message_loop_->RunUntilIdle();
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));
184 TEST_F(TaskQueueManagerTest, QueuePolling) {
185 Initialize(1u);
187 std::vector<int> run_order;
188 EXPECT_TRUE(runners_[0]->IsQueueEmpty());
189 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
190 EXPECT_FALSE(runners_[0]->IsQueueEmpty());
192 test_task_runner_->RunUntilIdle();
193 EXPECT_TRUE(runners_[0]->IsQueueEmpty());
196 TEST_F(TaskQueueManagerTest, DelayedTaskPosting) {
197 Initialize(1u);
199 std::vector<int> run_order;
200 base::TimeDelta delay(base::TimeDelta::FromMilliseconds(10));
201 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order),
202 delay);
203 EXPECT_EQ(delay, test_task_runner_->DelayToNextTaskTime());
204 EXPECT_TRUE(runners_[0]->IsQueueEmpty());
205 EXPECT_TRUE(run_order.empty());
207 // The task doesn't run before the delay has completed.
208 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(9));
209 EXPECT_TRUE(run_order.empty());
211 // After the delay has completed, the task runs normally.
212 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(1));
213 EXPECT_THAT(run_order, ElementsAre(1));
216 TEST_F(TaskQueueManagerTest, DelayedTaskPosting_MultipleTasks_DecendingOrder) {
217 Initialize(1u);
219 std::vector<int> run_order;
220 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order),
221 base::TimeDelta::FromMilliseconds(10));
223 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order),
224 base::TimeDelta::FromMilliseconds(8));
226 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order),
227 base::TimeDelta::FromMilliseconds(5));
229 EXPECT_EQ(base::TimeDelta::FromMilliseconds(5),
230 test_task_runner_->DelayToNextTaskTime());
232 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(5));
233 EXPECT_THAT(run_order, ElementsAre(3));
234 EXPECT_EQ(base::TimeDelta::FromMilliseconds(3),
235 test_task_runner_->DelayToNextTaskTime());
237 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(3));
238 EXPECT_THAT(run_order, ElementsAre(3, 2));
239 EXPECT_EQ(base::TimeDelta::FromMilliseconds(2),
240 test_task_runner_->DelayToNextTaskTime());
242 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(2));
243 EXPECT_THAT(run_order, ElementsAre(3, 2, 1));
246 TEST_F(TaskQueueManagerTest, DelayedTaskPosting_MultipleTasks_AscendingOrder) {
247 Initialize(1u);
249 std::vector<int> run_order;
250 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order),
251 base::TimeDelta::FromMilliseconds(1));
253 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order),
254 base::TimeDelta::FromMilliseconds(5));
256 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order),
257 base::TimeDelta::FromMilliseconds(10));
259 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1),
260 test_task_runner_->DelayToNextTaskTime());
262 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(1));
263 EXPECT_THAT(run_order, ElementsAre(1));
264 EXPECT_EQ(base::TimeDelta::FromMilliseconds(4),
265 test_task_runner_->DelayToNextTaskTime());
267 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(4));
268 EXPECT_THAT(run_order, ElementsAre(1, 2));
269 EXPECT_EQ(base::TimeDelta::FromMilliseconds(5),
270 test_task_runner_->DelayToNextTaskTime());
272 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(5));
273 EXPECT_THAT(run_order, ElementsAre(1, 2, 3));
276 TEST_F(TaskQueueManagerTest, PostDelayedTask_SharesUnderlyingDelayedTasks) {
277 Initialize(1u);
279 std::vector<int> run_order;
280 base::TimeDelta delay(base::TimeDelta::FromMilliseconds(10));
281 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order),
282 delay);
283 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order),
284 delay);
285 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order),
286 delay);
288 EXPECT_EQ(1u, test_task_runner_->NumPendingTasks());
291 class TestObject {
292 public:
293 ~TestObject() { destructor_count_++; }
295 void Run() { FAIL() << "TestObject::Run should not be called"; }
297 static int destructor_count_;
300 int TestObject::destructor_count_ = 0;
302 TEST_F(TaskQueueManagerTest, PendingDelayedTasksRemovedOnShutdown) {
303 Initialize(1u);
305 TestObject::destructor_count_ = 0;
307 base::TimeDelta delay(base::TimeDelta::FromMilliseconds(10));
308 runners_[0]->PostDelayedTask(
309 FROM_HERE, base::Bind(&TestObject::Run, base::Owned(new TestObject())),
310 delay);
311 runners_[0]->PostTask(
312 FROM_HERE, base::Bind(&TestObject::Run, base::Owned(new TestObject())));
314 manager_.reset();
316 EXPECT_EQ(2, TestObject::destructor_count_);
319 TEST_F(TaskQueueManagerTest, ManualPumping) {
320 Initialize(1u);
321 runners_[0]->SetPumpPolicy(TaskQueue::PumpPolicy::MANUAL);
323 std::vector<int> run_order;
324 // Posting a task when pumping is disabled doesn't result in work getting
325 // posted.
326 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
327 EXPECT_FALSE(test_task_runner_->HasPendingTasks());
329 // However polling still works.
330 EXPECT_FALSE(runners_[0]->IsQueueEmpty());
332 // After pumping the task runs normally.
333 runners_[0]->PumpQueue();
334 EXPECT_TRUE(test_task_runner_->HasPendingTasks());
335 test_task_runner_->RunUntilIdle();
336 EXPECT_THAT(run_order, ElementsAre(1));
339 TEST_F(TaskQueueManagerTest, ManualPumpingToggle) {
340 Initialize(1u);
341 runners_[0]->SetPumpPolicy(TaskQueue::PumpPolicy::MANUAL);
343 std::vector<int> run_order;
344 // Posting a task when pumping is disabled doesn't result in work getting
345 // posted.
346 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
347 EXPECT_FALSE(test_task_runner_->HasPendingTasks());
349 // When pumping is enabled the task runs normally.
350 runners_[0]->SetPumpPolicy(TaskQueue::PumpPolicy::AUTO);
351 EXPECT_TRUE(test_task_runner_->HasPendingTasks());
352 test_task_runner_->RunUntilIdle();
353 EXPECT_THAT(run_order, ElementsAre(1));
356 TEST_F(TaskQueueManagerTest, DenyRunning) {
357 Initialize(1u);
359 std::vector<int> run_order;
360 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
361 runners_[0]->SetQueuePriority(TaskQueue::DISABLED_PRIORITY);
363 test_task_runner_->RunUntilIdle();
364 EXPECT_TRUE(run_order.empty());
366 runners_[0]->SetQueuePriority(TaskQueue::NORMAL_PRIORITY);
367 test_task_runner_->RunUntilIdle();
368 EXPECT_THAT(run_order, ElementsAre(1));
371 TEST_F(TaskQueueManagerTest, ManualPumpingWithDelayedTask) {
372 Initialize(1u);
373 runners_[0]->SetPumpPolicy(TaskQueue::PumpPolicy::MANUAL);
375 std::vector<int> run_order;
376 // Posting a delayed task when pumping will apply the delay, but won't cause
377 // work to executed afterwards.
378 base::TimeDelta delay(base::TimeDelta::FromMilliseconds(10));
379 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order),
380 delay);
382 // After pumping but before the delay period has expired, task does not run.
383 runners_[0]->PumpQueue();
384 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(5));
385 EXPECT_TRUE(run_order.empty());
387 // Once the delay has expired, pumping causes the task to run.
388 now_src_->Advance(base::TimeDelta::FromMilliseconds(5));
389 runners_[0]->PumpQueue();
390 EXPECT_TRUE(test_task_runner_->HasPendingTasks());
391 test_task_runner_->RunPendingTasks();
392 EXPECT_THAT(run_order, ElementsAre(1));
395 TEST_F(TaskQueueManagerTest, ManualPumpingWithMultipleDelayedTasks) {
396 Initialize(1u);
397 runners_[0]->SetPumpPolicy(TaskQueue::PumpPolicy::MANUAL);
399 std::vector<int> run_order;
400 // Posting a delayed task when pumping will apply the delay, but won't cause
401 // work to executed afterwards.
402 base::TimeDelta delay1(base::TimeDelta::FromMilliseconds(1));
403 base::TimeDelta delay2(base::TimeDelta::FromMilliseconds(10));
404 base::TimeDelta delay3(base::TimeDelta::FromMilliseconds(20));
405 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order),
406 delay1);
407 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order),
408 delay2);
409 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order),
410 delay3);
412 now_src_->Advance(base::TimeDelta::FromMilliseconds(15));
413 test_task_runner_->RunUntilIdle();
414 EXPECT_TRUE(run_order.empty());
416 // Once the delay has expired, pumping causes the task to run.
417 runners_[0]->PumpQueue();
418 test_task_runner_->RunUntilIdle();
419 EXPECT_THAT(run_order, ElementsAre(1, 2));
422 TEST_F(TaskQueueManagerTest, DelayedTasksDontAutoRunWithManualPumping) {
423 Initialize(1u);
424 runners_[0]->SetPumpPolicy(TaskQueue::PumpPolicy::MANUAL);
426 std::vector<int> run_order;
427 base::TimeDelta delay(base::TimeDelta::FromMilliseconds(10));
428 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order),
429 delay);
431 test_task_runner_->RunForPeriod(base::TimeDelta::FromMilliseconds(10));
432 EXPECT_TRUE(run_order.empty());
435 TEST_F(TaskQueueManagerTest, ManualPumpingWithNonEmptyWorkQueue) {
436 Initialize(1u);
437 runners_[0]->SetPumpPolicy(TaskQueue::PumpPolicy::MANUAL);
439 std::vector<int> run_order;
440 // Posting two tasks and pumping twice should result in two tasks in the work
441 // queue.
442 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
443 runners_[0]->PumpQueue();
444 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
445 runners_[0]->PumpQueue();
447 EXPECT_EQ(2u, runners_[0]->WorkQueueSizeForTest());
450 void ReentrantTestTask(scoped_refptr<base::SingleThreadTaskRunner> runner,
451 int countdown,
452 std::vector<int>* out_result) {
453 out_result->push_back(countdown);
454 if (--countdown) {
455 runner->PostTask(FROM_HERE,
456 Bind(&ReentrantTestTask, runner, countdown, out_result));
460 TEST_F(TaskQueueManagerTest, ReentrantPosting) {
461 Initialize(1u);
463 std::vector<int> run_order;
464 runners_[0]->PostTask(FROM_HERE,
465 Bind(&ReentrantTestTask, runners_[0], 3, &run_order));
467 test_task_runner_->RunUntilIdle();
468 EXPECT_THAT(run_order, ElementsAre(3, 2, 1));
471 TEST_F(TaskQueueManagerTest, NoTasksAfterShutdown) {
472 Initialize(1u);
474 std::vector<int> run_order;
475 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
476 manager_.reset();
477 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
479 test_task_runner_->RunUntilIdle();
480 EXPECT_TRUE(run_order.empty());
483 void PostTaskToRunner(scoped_refptr<base::SingleThreadTaskRunner> runner,
484 std::vector<int>* run_order) {
485 runner->PostTask(FROM_HERE, base::Bind(&TestTask, 1, run_order));
488 TEST_F(TaskQueueManagerTest, PostFromThread) {
489 InitializeWithRealMessageLoop(1u);
491 std::vector<int> run_order;
492 base::Thread thread("TestThread");
493 thread.Start();
494 thread.task_runner()->PostTask(
495 FROM_HERE, base::Bind(&PostTaskToRunner, runners_[0], &run_order));
496 thread.Stop();
498 message_loop_->RunUntilIdle();
499 EXPECT_THAT(run_order, ElementsAre(1));
502 void RePostingTestTask(scoped_refptr<base::SingleThreadTaskRunner> runner,
503 int* run_count) {
504 (*run_count)++;
505 runner->PostTask(FROM_HERE, Bind(&RePostingTestTask,
506 base::Unretained(runner.get()), run_count));
509 TEST_F(TaskQueueManagerTest, DoWorkCantPostItselfMultipleTimes) {
510 Initialize(1u);
512 int run_count = 0;
513 runners_[0]->PostTask(
514 FROM_HERE, base::Bind(&RePostingTestTask, runners_[0], &run_count));
516 test_task_runner_->RunPendingTasks();
517 // NOTE without the executing_task_ check in MaybePostDoWorkOnMainRunner there
518 // will be two tasks here.
519 EXPECT_EQ(1u, test_task_runner_->NumPendingTasks());
520 EXPECT_EQ(1, run_count);
523 TEST_F(TaskQueueManagerTest, PostFromNestedRunloop) {
524 InitializeWithRealMessageLoop(1u);
526 std::vector<int> run_order;
527 std::vector<std::pair<base::Closure, bool>> tasks_to_post_from_nested_loop;
528 tasks_to_post_from_nested_loop.push_back(
529 std::make_pair(base::Bind(&TestTask, 1, &run_order), true));
531 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 0, &run_order));
532 runners_[0]->PostTask(
533 FROM_HERE,
534 base::Bind(&PostFromNestedRunloop, message_loop_.get(), runners_[0],
535 base::Unretained(&tasks_to_post_from_nested_loop)));
536 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
538 message_loop_->RunUntilIdle();
540 EXPECT_THAT(run_order, ElementsAre(0, 2, 1));
543 TEST_F(TaskQueueManagerTest, WorkBatching) {
544 Initialize(1u);
546 manager_->SetWorkBatchSize(2);
548 std::vector<int> run_order;
549 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
550 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
551 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order));
552 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 4, &run_order));
554 // Running one task in the host message loop should cause two posted tasks to
555 // get executed.
556 EXPECT_EQ(test_task_runner_->NumPendingTasks(), 1u);
557 test_task_runner_->RunPendingTasks();
558 EXPECT_THAT(run_order, ElementsAre(1, 2));
560 // The second task runs the remaining two posted tasks.
561 EXPECT_EQ(test_task_runner_->NumPendingTasks(), 1u);
562 test_task_runner_->RunPendingTasks();
563 EXPECT_THAT(run_order, ElementsAre(1, 2, 3, 4));
566 TEST_F(TaskQueueManagerTest, AutoPumpAfterWakeup) {
567 Initialize(2u);
568 runners_[0]->SetPumpPolicy(TaskQueue::PumpPolicy::AFTER_WAKEUP);
570 std::vector<int> run_order;
571 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
572 test_task_runner_->RunUntilIdle();
573 EXPECT_TRUE(run_order.empty()); // Shouldn't run - no other task to wake TQM.
575 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
576 test_task_runner_->RunUntilIdle();
577 EXPECT_TRUE(run_order.empty()); // Still shouldn't wake TQM.
579 runners_[1]->PostTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order));
580 test_task_runner_->RunUntilIdle();
581 // Executing a task on an auto pumped queue should wake the TQM.
582 EXPECT_THAT(run_order, ElementsAre(3, 1, 2));
585 TEST_F(TaskQueueManagerTest, AutoPumpAfterWakeupWhenAlreadyAwake) {
586 Initialize(2u);
587 runners_[0]->SetPumpPolicy(TaskQueue::PumpPolicy::AFTER_WAKEUP);
589 std::vector<int> run_order;
590 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
591 runners_[1]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
592 test_task_runner_->RunUntilIdle();
593 EXPECT_THAT(run_order, ElementsAre(2, 1)); // TQM was already awake.
596 TEST_F(TaskQueueManagerTest,
597 AutoPumpAfterWakeupTriggeredByManuallyPumpedQueue) {
598 Initialize(2u);
599 runners_[0]->SetPumpPolicy(TaskQueue::PumpPolicy::AFTER_WAKEUP);
600 runners_[1]->SetPumpPolicy(TaskQueue::PumpPolicy::MANUAL);
602 std::vector<int> run_order;
603 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
604 test_task_runner_->RunUntilIdle();
605 EXPECT_TRUE(run_order.empty()); // Shouldn't run - no other task to wake TQM.
607 runners_[1]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
608 test_task_runner_->RunUntilIdle();
609 // This still shouldn't wake TQM as manual queue was not pumped.
610 EXPECT_TRUE(run_order.empty());
612 runners_[1]->PumpQueue();
613 test_task_runner_->RunUntilIdle();
614 // Executing a task on an auto pumped queue should wake the TQM.
615 EXPECT_THAT(run_order, ElementsAre(2, 1));
618 void TestPostingTask(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
619 base::Closure task) {
620 task_runner->PostTask(FROM_HERE, task);
623 TEST_F(TaskQueueManagerTest, AutoPumpAfterWakeupFromTask) {
624 Initialize(2u);
625 runners_[0]->SetPumpPolicy(TaskQueue::PumpPolicy::AFTER_WAKEUP);
627 std::vector<int> run_order;
628 // Check that a task which posts a task to an auto pump after wakeup queue
629 // doesn't cause the queue to wake up.
630 base::Closure after_wakeup_task = base::Bind(&TestTask, 1, &run_order);
631 runners_[1]->PostTask(
632 FROM_HERE, base::Bind(&TestPostingTask, runners_[0], after_wakeup_task));
633 test_task_runner_->RunUntilIdle();
634 EXPECT_TRUE(run_order.empty());
636 // Wake up the queue.
637 runners_[1]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
638 test_task_runner_->RunUntilIdle();
639 EXPECT_THAT(run_order, ElementsAre(2, 1));
642 TEST_F(TaskQueueManagerTest, AutoPumpAfterWakeupFromMultipleTasks) {
643 Initialize(2u);
644 runners_[0]->SetPumpPolicy(TaskQueue::PumpPolicy::AFTER_WAKEUP);
646 std::vector<int> run_order;
647 // Check that a task which posts a task to an auto pump after wakeup queue
648 // doesn't cause the queue to wake up.
649 base::Closure after_wakeup_task_1 = base::Bind(&TestTask, 1, &run_order);
650 base::Closure after_wakeup_task_2 = base::Bind(&TestTask, 2, &run_order);
651 runners_[1]->PostTask(FROM_HERE, base::Bind(&TestPostingTask, runners_[0],
652 after_wakeup_task_1));
653 runners_[1]->PostTask(FROM_HERE, base::Bind(&TestPostingTask, runners_[0],
654 after_wakeup_task_2));
655 test_task_runner_->RunUntilIdle();
656 EXPECT_TRUE(run_order.empty());
658 // Wake up the queue.
659 runners_[1]->PostTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order));
660 test_task_runner_->RunUntilIdle();
661 EXPECT_THAT(run_order, ElementsAre(3, 1, 2));
664 TEST_F(TaskQueueManagerTest, AutoPumpAfterWakeupBecomesQuiescent) {
665 Initialize(2u);
666 runners_[0]->SetPumpPolicy(TaskQueue::PumpPolicy::AFTER_WAKEUP);
668 int run_count = 0;
669 // Check that if multiple tasks reposts themselves onto a pump-after-wakeup
670 // queue they don't wake each other and will eventually stop when no other
671 // tasks execute.
672 runners_[0]->PostTask(
673 FROM_HERE, base::Bind(&RePostingTestTask, runners_[0], &run_count));
674 runners_[0]->PostTask(
675 FROM_HERE, base::Bind(&RePostingTestTask, runners_[0], &run_count));
676 runners_[1]->PostTask(FROM_HERE, base::Bind(&NopTask));
677 test_task_runner_->RunUntilIdle();
678 // The reposting tasks posted to the after wakeup queue shouldn't have woken
679 // each other up.
680 EXPECT_EQ(2, run_count);
683 TEST_F(TaskQueueManagerTest, AutoPumpAfterWakeupWithDontWakeQueue) {
684 Initialize(1u);
686 scoped_refptr<internal::TaskQueueImpl> queue0 = manager_->NewTaskQueue(
687 TaskQueue::Spec("test_queue 0")
688 .SetPumpPolicy(TaskQueue::PumpPolicy::AFTER_WAKEUP));
689 scoped_refptr<internal::TaskQueueImpl> queue1 = manager_->NewTaskQueue(
690 TaskQueue::Spec("test_queue 0")
691 .SetWakeupPolicy(TaskQueue::WakeupPolicy::DONT_WAKE_OTHER_QUEUES));
692 scoped_refptr<internal::TaskQueueImpl> queue2 = runners_[0];
694 std::vector<int> run_order;
695 queue0->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
696 queue1->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
697 test_task_runner_->RunUntilIdle();
698 // Executing a DONT_WAKE_OTHER_QUEUES queue shouldn't wake the autopump after
699 // wakeup queue.
700 EXPECT_THAT(run_order, ElementsAre(2));
702 queue2->PostTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order));
703 test_task_runner_->RunUntilIdle();
704 // Executing a CAN_WAKE_OTHER_QUEUES queue should wake the autopump after
705 // wakeup queue.
706 EXPECT_THAT(run_order, ElementsAre(2, 3, 1));
709 class MockTaskObserver : public base::MessageLoop::TaskObserver {
710 public:
711 MOCK_METHOD1(DidProcessTask, void(const base::PendingTask& task));
712 MOCK_METHOD1(WillProcessTask, void(const base::PendingTask& task));
715 TEST_F(TaskQueueManagerTest, TaskObserverAdding) {
716 InitializeWithRealMessageLoop(1u);
717 MockTaskObserver observer;
719 manager_->SetWorkBatchSize(2);
720 manager_->AddTaskObserver(&observer);
722 std::vector<int> run_order;
723 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
724 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
726 EXPECT_CALL(observer, WillProcessTask(_)).Times(2);
727 EXPECT_CALL(observer, DidProcessTask(_)).Times(2);
728 message_loop_->RunUntilIdle();
731 TEST_F(TaskQueueManagerTest, TaskObserverRemoving) {
732 InitializeWithRealMessageLoop(1u);
733 MockTaskObserver observer;
734 manager_->SetWorkBatchSize(2);
735 manager_->AddTaskObserver(&observer);
736 manager_->RemoveTaskObserver(&observer);
738 std::vector<int> run_order;
739 runners_[0]->PostTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order));
741 EXPECT_CALL(observer, WillProcessTask(_)).Times(0);
742 EXPECT_CALL(observer, DidProcessTask(_)).Times(0);
744 message_loop_->RunUntilIdle();
747 void RemoveObserverTask(TaskQueueManager* manager,
748 base::MessageLoop::TaskObserver* observer) {
749 manager->RemoveTaskObserver(observer);
752 TEST_F(TaskQueueManagerTest, TaskObserverRemovingInsideTask) {
753 InitializeWithRealMessageLoop(1u);
754 MockTaskObserver observer;
755 manager_->SetWorkBatchSize(3);
756 manager_->AddTaskObserver(&observer);
758 runners_[0]->PostTask(
759 FROM_HERE, base::Bind(&RemoveObserverTask, manager_.get(), &observer));
761 EXPECT_CALL(observer, WillProcessTask(_)).Times(1);
762 EXPECT_CALL(observer, DidProcessTask(_)).Times(0);
763 message_loop_->RunUntilIdle();
766 TEST_F(TaskQueueManagerTest, ThreadCheckAfterTermination) {
767 Initialize(1u);
768 EXPECT_TRUE(runners_[0]->RunsTasksOnCurrentThread());
769 manager_.reset();
770 EXPECT_TRUE(runners_[0]->RunsTasksOnCurrentThread());
773 TEST_F(TaskQueueManagerTest, NextPendingDelayedTaskRunTime) {
774 scoped_ptr<base::SimpleTestTickClock> clock(new base::SimpleTestTickClock());
775 clock->Advance(base::TimeDelta::FromMicroseconds(10000));
776 Initialize(2u);
777 manager_->SetTimeSourceForTesting(
778 make_scoped_ptr(new TestTimeSource(clock.get())));
780 // With no delayed tasks.
781 EXPECT_TRUE(manager_->NextPendingDelayedTaskRunTime().is_null());
783 // With a non-delayed task.
784 runners_[0]->PostTask(FROM_HERE, base::Bind(&NopTask));
785 EXPECT_TRUE(manager_->NextPendingDelayedTaskRunTime().is_null());
787 // With a delayed task.
788 base::TimeDelta expected_delay = base::TimeDelta::FromMilliseconds(50);
789 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), expected_delay);
790 EXPECT_EQ(clock->NowTicks() + expected_delay,
791 manager_->NextPendingDelayedTaskRunTime());
793 // With another delayed task in the same queue with a longer delay.
794 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&NopTask),
795 base::TimeDelta::FromMilliseconds(100));
796 EXPECT_EQ(clock->NowTicks() + expected_delay,
797 manager_->NextPendingDelayedTaskRunTime());
799 // With another delayed task in the same queue with a shorter delay.
800 expected_delay = base::TimeDelta::FromMilliseconds(20);
801 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), expected_delay);
802 EXPECT_EQ(clock->NowTicks() + expected_delay,
803 manager_->NextPendingDelayedTaskRunTime());
805 // With another delayed task in a different queue with a shorter delay.
806 expected_delay = base::TimeDelta::FromMilliseconds(10);
807 runners_[1]->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), expected_delay);
808 EXPECT_EQ(clock->NowTicks() + expected_delay,
809 manager_->NextPendingDelayedTaskRunTime());
811 // Test it updates as time progresses
812 clock->Advance(expected_delay);
813 EXPECT_EQ(clock->NowTicks(), manager_->NextPendingDelayedTaskRunTime());
816 TEST_F(TaskQueueManagerTest, NextPendingDelayedTaskRunTime_MultipleQueues) {
817 Initialize(3u);
819 base::TimeDelta delay1 = base::TimeDelta::FromMilliseconds(50);
820 base::TimeDelta delay2 = base::TimeDelta::FromMilliseconds(5);
821 base::TimeDelta delay3 = base::TimeDelta::FromMilliseconds(10);
822 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), delay1);
823 runners_[1]->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), delay2);
824 runners_[2]->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), delay3);
826 EXPECT_EQ(now_src_->NowTicks() + delay2,
827 manager_->NextPendingDelayedTaskRunTime());
830 TEST_F(TaskQueueManagerTest, DeleteTaskQueueManagerInsideATask) {
831 Initialize(1u);
833 runners_[0]->PostTask(
834 FROM_HERE, base::Bind(&TaskQueueManagerTest::DeleteTaskQueueManager,
835 base::Unretained(this)));
837 // This should not crash, assuming DoWork detects the TaskQueueManager has
838 // been deleted.
839 test_task_runner_->RunUntilIdle();
842 TEST_F(TaskQueueManagerTest, GetAndClearSystemIsQuiescentBit) {
843 Initialize(3u);
845 scoped_refptr<internal::TaskQueueImpl> queue0 = manager_->NewTaskQueue(
846 TaskQueue::Spec("test_queue 0").SetShouldMonitorQuiescence(true));
847 scoped_refptr<internal::TaskQueueImpl> queue1 = manager_->NewTaskQueue(
848 TaskQueue::Spec("test_queue 1").SetShouldMonitorQuiescence(true));
849 scoped_refptr<internal::TaskQueueImpl> queue2 = manager_->NewTaskQueue(
850 TaskQueue::Spec("test_queue 2").SetShouldMonitorQuiescence(false));
852 EXPECT_TRUE(manager_->GetAndClearSystemIsQuiescentBit());
854 queue0->PostTask(FROM_HERE, base::Bind(&NopTask));
855 test_task_runner_->RunUntilIdle();
856 EXPECT_FALSE(manager_->GetAndClearSystemIsQuiescentBit());
857 EXPECT_TRUE(manager_->GetAndClearSystemIsQuiescentBit());
859 queue1->PostTask(FROM_HERE, base::Bind(&NopTask));
860 test_task_runner_->RunUntilIdle();
861 EXPECT_FALSE(manager_->GetAndClearSystemIsQuiescentBit());
862 EXPECT_TRUE(manager_->GetAndClearSystemIsQuiescentBit());
864 queue2->PostTask(FROM_HERE, base::Bind(&NopTask));
865 test_task_runner_->RunUntilIdle();
866 EXPECT_TRUE(manager_->GetAndClearSystemIsQuiescentBit());
868 queue0->PostTask(FROM_HERE, base::Bind(&NopTask));
869 queue1->PostTask(FROM_HERE, base::Bind(&NopTask));
870 test_task_runner_->RunUntilIdle();
871 EXPECT_FALSE(manager_->GetAndClearSystemIsQuiescentBit());
872 EXPECT_TRUE(manager_->GetAndClearSystemIsQuiescentBit());
875 TEST_F(TaskQueueManagerTest, IsQueueEmpty) {
876 Initialize(2u);
877 internal::TaskQueueImpl* queue0 = runners_[0].get();
878 internal::TaskQueueImpl* queue1 = runners_[1].get();
879 queue0->SetPumpPolicy(TaskQueue::PumpPolicy::AUTO);
880 queue1->SetPumpPolicy(TaskQueue::PumpPolicy::MANUAL);
882 EXPECT_TRUE(queue0->IsQueueEmpty());
883 EXPECT_TRUE(queue1->IsQueueEmpty());
885 queue0->PostTask(FROM_HERE, base::Bind(NullTask));
886 queue1->PostTask(FROM_HERE, base::Bind(NullTask));
887 EXPECT_FALSE(queue0->IsQueueEmpty());
888 EXPECT_FALSE(queue1->IsQueueEmpty());
890 test_task_runner_->RunUntilIdle();
891 EXPECT_TRUE(queue0->IsQueueEmpty());
892 EXPECT_FALSE(queue1->IsQueueEmpty());
894 queue1->PumpQueue();
895 EXPECT_TRUE(queue0->IsQueueEmpty());
896 EXPECT_FALSE(queue1->IsQueueEmpty());
898 test_task_runner_->RunUntilIdle();
899 EXPECT_TRUE(queue0->IsQueueEmpty());
900 EXPECT_TRUE(queue1->IsQueueEmpty());
903 TEST_F(TaskQueueManagerTest, GetQueueState) {
904 Initialize(2u);
905 internal::TaskQueueImpl* queue0 = runners_[0].get();
906 internal::TaskQueueImpl* queue1 = runners_[1].get();
907 queue0->SetPumpPolicy(TaskQueue::PumpPolicy::AUTO);
908 queue1->SetPumpPolicy(TaskQueue::PumpPolicy::MANUAL);
910 EXPECT_EQ(TaskQueue::QueueState::EMPTY, queue0->GetQueueState());
911 EXPECT_EQ(TaskQueue::QueueState::EMPTY, queue1->GetQueueState());
913 queue0->PostTask(FROM_HERE, base::Bind(NullTask));
914 queue0->PostTask(FROM_HERE, base::Bind(NullTask));
915 queue1->PostTask(FROM_HERE, base::Bind(NullTask));
916 EXPECT_EQ(TaskQueue::QueueState::NEEDS_PUMPING, queue0->GetQueueState());
917 EXPECT_EQ(TaskQueue::QueueState::NEEDS_PUMPING, queue1->GetQueueState());
919 test_task_runner_->SetRunTaskLimit(1);
920 test_task_runner_->RunPendingTasks();
921 EXPECT_EQ(TaskQueue::QueueState::HAS_WORK, queue0->GetQueueState());
922 EXPECT_EQ(TaskQueue::QueueState::NEEDS_PUMPING, queue1->GetQueueState());
924 test_task_runner_->ClearRunTaskLimit();
925 test_task_runner_->RunUntilIdle();
926 EXPECT_EQ(TaskQueue::QueueState::EMPTY, queue0->GetQueueState());
927 EXPECT_EQ(TaskQueue::QueueState::NEEDS_PUMPING, queue1->GetQueueState());
929 queue1->PumpQueue();
930 EXPECT_EQ(TaskQueue::QueueState::EMPTY, queue0->GetQueueState());
931 EXPECT_EQ(TaskQueue::QueueState::HAS_WORK, queue1->GetQueueState());
933 test_task_runner_->RunUntilIdle();
934 EXPECT_EQ(TaskQueue::QueueState::EMPTY, queue0->GetQueueState());
935 EXPECT_EQ(TaskQueue::QueueState::EMPTY, queue1->GetQueueState());
938 TEST_F(TaskQueueManagerTest, DelayedTaskDoesNotSkipAHeadOfNonDelayedTask) {
939 Initialize(2u);
941 std::vector<int> run_order;
942 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(10);
943 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order),
944 delay);
945 runners_[1]->PostTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order));
946 runners_[1]->PostTask(FROM_HERE, base::Bind(&TestTask, 3, &run_order));
948 now_src_->Advance(delay * 2);
949 // After task 2 has run, the automatic selector will have to choose between
950 // tasks 1 and 3. The sequence numbers are used to choose between the two
951 // tasks and if they are correct task 1 will run last.
952 test_task_runner_->RunUntilIdle();
954 EXPECT_THAT(run_order, ElementsAre(2, 3, 1));
957 TEST_F(TaskQueueManagerTest, DelayedTaskDoesNotSkipAHeadOfShorterDelayedTask) {
958 Initialize(2u);
960 std::vector<int> run_order;
961 base::TimeDelta delay1 = base::TimeDelta::FromMilliseconds(10);
962 base::TimeDelta delay2 = base::TimeDelta::FromMilliseconds(5);
963 runners_[0]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 1, &run_order),
964 delay1);
965 runners_[1]->PostDelayedTask(FROM_HERE, base::Bind(&TestTask, 2, &run_order),
966 delay2);
968 now_src_->Advance(delay1 * 2);
969 test_task_runner_->RunUntilIdle();
971 EXPECT_THAT(run_order, ElementsAre(2, 1));
974 TEST_F(TaskQueueManagerTest, DelayedTaskWithAbsoluteRunTime) {
975 Initialize(1u);
977 // One task in the past, two with the exact same run time and one in the
978 // future.
979 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(10);
980 base::TimeTicks runTime1 = now_src_->NowTicks() - delay;
981 base::TimeTicks runTime2 = now_src_->NowTicks();
982 base::TimeTicks runTime3 = now_src_->NowTicks();
983 base::TimeTicks runTime4 = now_src_->NowTicks() + delay;
985 std::vector<int> run_order;
986 runners_[0]->PostDelayedTaskAt(
987 FROM_HERE, base::Bind(&TestTask, 1, &run_order), runTime1);
988 runners_[0]->PostDelayedTaskAt(
989 FROM_HERE, base::Bind(&TestTask, 2, &run_order), runTime2);
990 runners_[0]->PostDelayedTaskAt(
991 FROM_HERE, base::Bind(&TestTask, 3, &run_order), runTime3);
992 runners_[0]->PostDelayedTaskAt(
993 FROM_HERE, base::Bind(&TestTask, 4, &run_order), runTime4);
995 now_src_->Advance(2 * delay);
996 test_task_runner_->RunUntilIdle();
998 EXPECT_THAT(run_order, ElementsAre(1, 2, 3, 4));
1001 void CheckIsNested(bool* is_nested) {
1002 *is_nested = base::MessageLoop::current()->IsNested();
1005 void PostAndQuitFromNestedRunloop(base::RunLoop* run_loop,
1006 base::SingleThreadTaskRunner* runner,
1007 bool* was_nested) {
1008 base::MessageLoop::ScopedNestableTaskAllower allow(
1009 base::MessageLoop::current());
1010 runner->PostTask(FROM_HERE, run_loop->QuitClosure());
1011 runner->PostTask(FROM_HERE, base::Bind(&CheckIsNested, was_nested));
1012 run_loop->Run();
1015 TEST_F(TaskQueueManagerTest, QuitWhileNested) {
1016 // This test makes sure we don't continue running a work batch after a nested
1017 // run loop has been exited in the middle of the batch.
1018 InitializeWithRealMessageLoop(1u);
1019 manager_->SetWorkBatchSize(2);
1021 bool was_nested = true;
1022 base::RunLoop run_loop;
1023 runners_[0]->PostTask(
1024 FROM_HERE,
1025 base::Bind(&PostAndQuitFromNestedRunloop, base::Unretained(&run_loop),
1026 runners_[0], base::Unretained(&was_nested)));
1028 message_loop_->RunUntilIdle();
1029 EXPECT_FALSE(was_nested);
1032 } // namespace scheduler