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
3 // found in the LICENSE file.
5 #include "base/run_loop.h"
10 #include "base/bind_helpers.h"
11 #include "base/containers/queue.h"
12 #include "base/location.h"
13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/synchronization/lock.h"
18 #include "base/synchronization/waitable_event.h"
19 #include "base/test/gtest_util.h"
20 #include "base/test/scoped_task_environment.h"
21 #include "base/test/test_timeouts.h"
22 #include "base/threading/platform_thread.h"
23 #include "base/threading/sequenced_task_runner_handle.h"
24 #include "base/threading/thread.h"
25 #include "base/threading/thread_checker_impl.h"
26 #include "base/threading/thread_task_runner_handle.h"
27 #include "build/build_config.h"
28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h"
35 void QuitWhenIdleTask(RunLoop* run_loop, int* counter) {
36 run_loop->QuitWhenIdle();
40 void ShouldRunTask(int* counter) {
44 void ShouldNotRunTask() {
45 ADD_FAILURE() << "Ran a task that shouldn't run.";
48 void RunNestedLoopTask(int* counter) {
49 RunLoop nested_run_loop(RunLoop::Type::kNestableTasksAllowed);
51 // This task should quit |nested_run_loop| but not the main RunLoop.
52 ThreadTaskRunnerHandle::Get()->PostTask(
53 FROM_HERE, BindOnce(&QuitWhenIdleTask, Unretained(&nested_run_loop),
54 Unretained(counter)));
56 ThreadTaskRunnerHandle::Get()->PostDelayedTask(
57 FROM_HERE, BindOnce(&ShouldNotRunTask), TimeDelta::FromDays(1));
59 nested_run_loop.Run();
64 // A simple SingleThreadTaskRunner that just queues undelayed tasks (and ignores
65 // delayed tasks). Tasks can then be processed one by one by ProcessTask() which
66 // will return true if it processed a task and false otherwise.
67 class SimpleSingleThreadTaskRunner : public SingleThreadTaskRunner {
69 SimpleSingleThreadTaskRunner() = default;
71 bool PostDelayedTask(const Location& from_here,
73 base::TimeDelta delay) override {
74 if (delay > base::TimeDelta())
76 AutoLock auto_lock(tasks_lock_);
77 pending_tasks_.push(std::move(task));
81 bool PostNonNestableDelayedTask(const Location& from_here,
83 base::TimeDelta delay) override {
84 return PostDelayedTask(from_here, std::move(task), delay);
87 bool RunsTasksInCurrentSequence() const override {
88 return origin_thread_checker_.CalledOnValidThread();
91 bool ProcessSingleTask() {
94 AutoLock auto_lock(tasks_lock_);
95 if (pending_tasks_.empty())
97 task = std::move(pending_tasks_.front());
100 // It's important to Run() after pop() and outside the lock as |task| may
101 // run a nested loop which will re-enter ProcessSingleTask().
102 std::move(task).Run();
107 ~SimpleSingleThreadTaskRunner() override = default;
110 base::queue<OnceClosure> pending_tasks_;
112 // RunLoop relies on RunsTasksInCurrentSequence() signal. Use a
113 // ThreadCheckerImpl to be able to reliably provide that signal even in
114 // non-dcheck builds.
115 ThreadCheckerImpl origin_thread_checker_;
117 DISALLOW_COPY_AND_ASSIGN(SimpleSingleThreadTaskRunner);
120 // The basis of all TestDelegates, allows safely injecting a OnceClosure to be
121 // run in the next idle phase of this delegate's Run() implementation. This can
122 // be used to have code run on a thread that is otherwise livelocked in an idle
123 // phase (sometimes a simple PostTask() won't do it -- e.g. when processing
124 // application tasks is disallowed).
125 class InjectableTestDelegate : public RunLoop::Delegate {
127 void InjectClosureOnDelegate(OnceClosure closure) {
128 AutoLock auto_lock(closure_lock_);
129 closure_ = std::move(closure);
132 bool RunInjectedClosure() {
133 AutoLock auto_lock(closure_lock_);
134 if (closure_.is_null())
136 std::move(closure_).Run();
142 OnceClosure closure_;
145 // A simple test RunLoop::Delegate to exercise Runloop logic independent of any
146 // other base constructs. BindToCurrentThread() must be called before this
147 // TestBoundDelegate is operational.
148 class TestBoundDelegate final : public InjectableTestDelegate {
150 TestBoundDelegate() = default;
152 // Makes this TestBoundDelegate become the RunLoop::Delegate and
153 // ThreadTaskRunnerHandle for this thread.
154 void BindToCurrentThread() {
155 thread_task_runner_handle_ =
156 std::make_unique<ThreadTaskRunnerHandle>(simple_task_runner_);
157 RunLoop::RegisterDelegateForCurrentThread(this);
161 void Run(bool application_tasks_allowed) override {
162 if (nested_run_allowing_tasks_incoming_) {
163 EXPECT_TRUE(RunLoop::IsNestedOnCurrentThread());
164 EXPECT_TRUE(application_tasks_allowed);
165 } else if (RunLoop::IsNestedOnCurrentThread()) {
166 EXPECT_FALSE(application_tasks_allowed);
168 nested_run_allowing_tasks_incoming_ = false;
170 while (!should_quit_) {
171 if (application_tasks_allowed && simple_task_runner_->ProcessSingleTask())
174 if (ShouldQuitWhenIdle())
177 if (RunInjectedClosure())
180 PlatformThread::YieldCurrentThread();
182 should_quit_ = false;
185 void Quit() override { should_quit_ = true; }
187 void EnsureWorkScheduled() override {
188 nested_run_allowing_tasks_incoming_ = true;
191 // True if the next invocation of Run() is expected to be from a
192 // kNestableTasksAllowed RunLoop.
193 bool nested_run_allowing_tasks_incoming_ = false;
195 scoped_refptr<SimpleSingleThreadTaskRunner> simple_task_runner_ =
196 MakeRefCounted<SimpleSingleThreadTaskRunner>();
198 std::unique_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_;
200 bool should_quit_ = false;
203 enum class RunLoopTestType {
204 // Runs all RunLoopTests under a ScopedTaskEnvironment to make sure real world
208 // Runs all RunLoopTests under a test RunLoop::Delegate to make sure the
209 // delegate interface fully works standalone.
213 // The task environment for the RunLoopTest of a given type. A separate class
214 // so it can be instantiated on the stack in the RunLoopTest fixture.
215 class RunLoopTestEnvironment {
217 RunLoopTestEnvironment(RunLoopTestType type) {
219 case RunLoopTestType::kRealEnvironment: {
220 task_environment_ = std::make_unique<test::ScopedTaskEnvironment>();
223 case RunLoopTestType::kTestDelegate: {
224 auto test_delegate = std::make_unique<TestBoundDelegate>();
225 test_delegate->BindToCurrentThread();
226 test_delegate_ = std::move(test_delegate);
233 // Instantiates one or the other based on the RunLoopTestType.
234 std::unique_ptr<test::ScopedTaskEnvironment> task_environment_;
235 std::unique_ptr<InjectableTestDelegate> test_delegate_;
238 class RunLoopTest : public testing::TestWithParam<RunLoopTestType> {
240 RunLoopTest() : test_environment_(GetParam()) {}
242 RunLoopTestEnvironment test_environment_;
247 DISALLOW_COPY_AND_ASSIGN(RunLoopTest);
252 TEST_P(RunLoopTest, QuitWhenIdle) {
253 ThreadTaskRunnerHandle::Get()->PostTask(
254 FROM_HERE, BindOnce(&QuitWhenIdleTask, Unretained(&run_loop_),
255 Unretained(&counter_)));
256 ThreadTaskRunnerHandle::Get()->PostTask(
257 FROM_HERE, BindOnce(&ShouldRunTask, Unretained(&counter_)));
258 ThreadTaskRunnerHandle::Get()->PostDelayedTask(
259 FROM_HERE, BindOnce(&ShouldNotRunTask), TimeDelta::FromDays(1));
262 EXPECT_EQ(2, counter_);
265 TEST_P(RunLoopTest, QuitWhenIdleNestedLoop) {
266 ThreadTaskRunnerHandle::Get()->PostTask(
267 FROM_HERE, BindOnce(&RunNestedLoopTask, Unretained(&counter_)));
268 ThreadTaskRunnerHandle::Get()->PostTask(
269 FROM_HERE, BindOnce(&QuitWhenIdleTask, Unretained(&run_loop_),
270 Unretained(&counter_)));
271 ThreadTaskRunnerHandle::Get()->PostTask(
272 FROM_HERE, BindOnce(&ShouldRunTask, Unretained(&counter_)));
273 ThreadTaskRunnerHandle::Get()->PostDelayedTask(
274 FROM_HERE, BindOnce(&ShouldNotRunTask), TimeDelta::FromDays(1));
277 EXPECT_EQ(4, counter_);
280 TEST_P(RunLoopTest, QuitWhenIdleClosure) {
281 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
282 run_loop_.QuitWhenIdleClosure());
283 ThreadTaskRunnerHandle::Get()->PostTask(
284 FROM_HERE, BindOnce(&ShouldRunTask, Unretained(&counter_)));
285 ThreadTaskRunnerHandle::Get()->PostDelayedTask(
286 FROM_HERE, BindOnce(&ShouldNotRunTask), TimeDelta::FromDays(1));
289 EXPECT_EQ(1, counter_);
292 // Verify that the QuitWhenIdleClosure() can run after the RunLoop has been
293 // deleted. It should have no effect.
294 TEST_P(RunLoopTest, QuitWhenIdleClosureAfterRunLoopScope) {
295 Closure quit_when_idle_closure;
298 quit_when_idle_closure = run_loop.QuitWhenIdleClosure();
299 run_loop.RunUntilIdle();
301 quit_when_idle_closure.Run();
304 // Verify that Quit can be executed from another sequence.
305 TEST_P(RunLoopTest, QuitFromOtherSequence) {
306 Thread other_thread("test");
307 other_thread.Start();
308 scoped_refptr<SequencedTaskRunner> other_sequence =
309 other_thread.task_runner();
311 // Always expected to run before asynchronous Quit() kicks in.
312 ThreadTaskRunnerHandle::Get()->PostTask(
313 FROM_HERE, base::BindOnce(&ShouldRunTask, Unretained(&counter_)));
315 WaitableEvent loop_was_quit(WaitableEvent::ResetPolicy::MANUAL,
316 WaitableEvent::InitialState::NOT_SIGNALED);
317 other_sequence->PostTask(
318 FROM_HERE, base::BindOnce([](RunLoop* run_loop) { run_loop->Quit(); },
319 Unretained(&run_loop_)));
320 other_sequence->PostTask(
322 base::BindOnce(&WaitableEvent::Signal, base::Unretained(&loop_was_quit)));
324 // Anything that's posted after the Quit closure was posted back to this
325 // sequence shouldn't get a chance to run.
326 loop_was_quit.Wait();
327 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
328 base::BindOnce(&ShouldNotRunTask));
332 EXPECT_EQ(1, counter_);
335 // Verify that QuitClosure can be executed from another sequence.
336 TEST_P(RunLoopTest, QuitFromOtherSequenceWithClosure) {
337 Thread other_thread("test");
338 other_thread.Start();
339 scoped_refptr<SequencedTaskRunner> other_sequence =
340 other_thread.task_runner();
342 // Always expected to run before asynchronous Quit() kicks in.
343 ThreadTaskRunnerHandle::Get()->PostTask(
344 FROM_HERE, base::BindOnce(&ShouldRunTask, Unretained(&counter_)));
346 WaitableEvent loop_was_quit(WaitableEvent::ResetPolicy::MANUAL,
347 WaitableEvent::InitialState::NOT_SIGNALED);
348 other_sequence->PostTask(FROM_HERE, run_loop_.QuitClosure());
349 other_sequence->PostTask(
351 base::BindOnce(&WaitableEvent::Signal, base::Unretained(&loop_was_quit)));
353 // Anything that's posted after the Quit closure was posted back to this
354 // sequence shouldn't get a chance to run.
355 loop_was_quit.Wait();
356 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
357 base::BindOnce(&ShouldNotRunTask));
361 EXPECT_EQ(1, counter_);
364 // Verify that Quit can be executed from another sequence even when the
365 // Quit is racing with Run() -- i.e. forgo the WaitableEvent used above.
366 TEST_P(RunLoopTest, QuitFromOtherSequenceRacy) {
367 Thread other_thread("test");
368 other_thread.Start();
369 scoped_refptr<SequencedTaskRunner> other_sequence =
370 other_thread.task_runner();
372 // Always expected to run before asynchronous Quit() kicks in.
373 ThreadTaskRunnerHandle::Get()->PostTask(
374 FROM_HERE, base::BindOnce(&ShouldRunTask, Unretained(&counter_)));
376 other_sequence->PostTask(
377 FROM_HERE, base::BindOnce([](RunLoop* run_loop) { run_loop->Quit(); },
378 Unretained(&run_loop_)));
382 EXPECT_EQ(1, counter_);
385 // Verify that QuitClosure can be executed from another sequence even when the
386 // Quit is racing with Run() -- i.e. forgo the WaitableEvent used above.
387 TEST_P(RunLoopTest, QuitFromOtherSequenceRacyWithClosure) {
388 Thread other_thread("test");
389 other_thread.Start();
390 scoped_refptr<SequencedTaskRunner> other_sequence =
391 other_thread.task_runner();
393 // Always expected to run before asynchronous Quit() kicks in.
394 ThreadTaskRunnerHandle::Get()->PostTask(
395 FROM_HERE, base::BindOnce(&ShouldRunTask, Unretained(&counter_)));
397 other_sequence->PostTask(FROM_HERE, run_loop_.QuitClosure());
401 EXPECT_EQ(1, counter_);
404 // Verify that QuitWhenIdle can be executed from another sequence.
405 TEST_P(RunLoopTest, QuitWhenIdleFromOtherSequence) {
406 Thread other_thread("test");
407 other_thread.Start();
408 scoped_refptr<SequencedTaskRunner> other_sequence =
409 other_thread.task_runner();
411 ThreadTaskRunnerHandle::Get()->PostTask(
412 FROM_HERE, base::BindOnce(&ShouldRunTask, Unretained(&counter_)));
414 other_sequence->PostTask(
416 base::BindOnce([](RunLoop* run_loop) { run_loop->QuitWhenIdle(); },
417 Unretained(&run_loop_)));
419 ThreadTaskRunnerHandle::Get()->PostTask(
420 FROM_HERE, base::BindOnce(&ShouldRunTask, Unretained(&counter_)));
424 // Regardless of the outcome of the race this thread shouldn't have been idle
425 // until the counter was ticked twice.
426 EXPECT_EQ(2, counter_);
429 // Verify that QuitWhenIdleClosure can be executed from another sequence.
430 TEST_P(RunLoopTest, QuitWhenIdleFromOtherSequenceWithClosure) {
431 Thread other_thread("test");
432 other_thread.Start();
433 scoped_refptr<SequencedTaskRunner> other_sequence =
434 other_thread.task_runner();
436 ThreadTaskRunnerHandle::Get()->PostTask(
437 FROM_HERE, base::BindOnce(&ShouldRunTask, Unretained(&counter_)));
439 other_sequence->PostTask(FROM_HERE, run_loop_.QuitWhenIdleClosure());
441 ThreadTaskRunnerHandle::Get()->PostTask(
442 FROM_HERE, base::BindOnce(&ShouldRunTask, Unretained(&counter_)));
446 // Regardless of the outcome of the race this thread shouldn't have been idle
447 // until the counter was ticked twice.
448 EXPECT_EQ(2, counter_);
451 TEST_P(RunLoopTest, IsRunningOnCurrentThread) {
452 EXPECT_FALSE(RunLoop::IsRunningOnCurrentThread());
453 ThreadTaskRunnerHandle::Get()->PostTask(
455 BindOnce([]() { EXPECT_TRUE(RunLoop::IsRunningOnCurrentThread()); }));
456 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_loop_.QuitClosure());
460 TEST_P(RunLoopTest, IsNestedOnCurrentThread) {
461 EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
463 ThreadTaskRunnerHandle::Get()->PostTask(
464 FROM_HERE, BindOnce([]() {
465 EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
467 RunLoop nested_run_loop(RunLoop::Type::kNestableTasksAllowed);
469 ThreadTaskRunnerHandle::Get()->PostTask(
470 FROM_HERE, BindOnce([]() {
471 EXPECT_TRUE(RunLoop::IsNestedOnCurrentThread());
473 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
474 nested_run_loop.QuitClosure());
476 EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
477 nested_run_loop.Run();
478 EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
481 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_loop_.QuitClosure());
487 class MockNestingObserver : public RunLoop::NestingObserver {
489 MockNestingObserver() = default;
491 // RunLoop::NestingObserver:
492 MOCK_METHOD0(OnBeginNestedRunLoop, void());
493 MOCK_METHOD0(OnExitNestedRunLoop, void());
496 DISALLOW_COPY_AND_ASSIGN(MockNestingObserver);
501 MockTask() = default;
502 MOCK_METHOD0(Task, void());
505 DISALLOW_COPY_AND_ASSIGN(MockTask);
510 TEST_P(RunLoopTest, NestingObservers) {
511 testing::StrictMock<MockNestingObserver> nesting_observer;
512 testing::StrictMock<MockTask> mock_task_a;
513 testing::StrictMock<MockTask> mock_task_b;
515 RunLoop::AddNestingObserverOnCurrentThread(&nesting_observer);
517 const RepeatingClosure run_nested_loop = Bind([]() {
518 RunLoop nested_run_loop(RunLoop::Type::kNestableTasksAllowed);
519 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
520 nested_run_loop.QuitClosure());
521 nested_run_loop.Run();
524 // Generate a stack of nested RunLoops. OnBeginNestedRunLoop() is expected
525 // when beginning each nesting depth and OnExitNestedRunLoop() is expected
526 // when exiting each nesting depth. Each one of these tasks is ahead of the
527 // QuitClosures as those are only posted at the end of the queue when
528 // |run_nested_loop| is executed.
529 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_nested_loop);
530 ThreadTaskRunnerHandle::Get()->PostTask(
532 base::BindOnce(&MockTask::Task, base::Unretained(&mock_task_a)));
533 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_nested_loop);
534 ThreadTaskRunnerHandle::Get()->PostTask(
536 base::BindOnce(&MockTask::Task, base::Unretained(&mock_task_b)));
539 testing::InSequence in_sequence;
540 EXPECT_CALL(nesting_observer, OnBeginNestedRunLoop());
541 EXPECT_CALL(mock_task_a, Task());
542 EXPECT_CALL(nesting_observer, OnBeginNestedRunLoop());
543 EXPECT_CALL(mock_task_b, Task());
544 EXPECT_CALL(nesting_observer, OnExitNestedRunLoop()).Times(2);
546 run_loop_.RunUntilIdle();
548 RunLoop::RemoveNestingObserverOnCurrentThread(&nesting_observer);
551 TEST_P(RunLoopTest, DisallowRunningForTesting) {
552 RunLoop::ScopedDisallowRunningForTesting disallow_running;
553 EXPECT_DCHECK_DEATH({ run_loop_.RunUntilIdle(); });
556 TEST_P(RunLoopTest, ExpiredDisallowRunningForTesting) {
557 { RunLoop::ScopedDisallowRunningForTesting disallow_running; }
558 // Running should be fine after |disallow_running| goes out of scope.
559 run_loop_.RunUntilIdle();
562 INSTANTIATE_TEST_CASE_P(Real,
564 testing::Values(RunLoopTestType::kRealEnvironment));
565 INSTANTIATE_TEST_CASE_P(Mock,
567 testing::Values(RunLoopTestType::kTestDelegate));
569 TEST(ScopedRunTimeoutForTestTest, TimesOut) {
570 test::ScopedTaskEnvironment task_environment;
573 static constexpr auto kArbitraryTimeout =
574 base::TimeDelta::FromMilliseconds(10);
575 RunLoop::ScopedRunTimeoutForTest run_timeout(kArbitraryTimeout);
577 // Since the delayed task will be posted only after the message pump starts
578 // running, the ScopedRunTimeoutForTest will already have started to elapse,
579 // so if Run() exits at the correct time then our delayed task will not run.
580 SequencedTaskRunnerHandle::Get()->PostTask(
582 base::BindOnce(base::IgnoreResult(&SequencedTaskRunner::PostDelayedTask),
583 SequencedTaskRunnerHandle::Get(), FROM_HERE,
584 base::BindOnce(&ShouldNotRunTask), kArbitraryTimeout));
586 // This task should get to run before Run() times-out.
588 SequencedTaskRunnerHandle::Get()->PostDelayedTask(
589 FROM_HERE, base::BindOnce(&ShouldRunTask, &counter), kArbitraryTimeout);
592 EXPECT_EQ(counter, 1);
595 TEST(ScopedRunTimeoutForTestTest, RunTasksUntilTimeout) {
596 test::ScopedTaskEnvironment task_environment;
599 static constexpr auto kArbitraryTimeout =
600 base::TimeDelta::FromMilliseconds(10);
601 RunLoop::ScopedRunTimeoutForTest run_timeout(kArbitraryTimeout);
603 // Posting a task with the same delay as our timeout, immediately before
604 // calling Run(), means it should get to run.
606 SequencedTaskRunnerHandle::Get()->PostDelayedTask(
607 FROM_HERE, base::BindOnce(&QuitWhenIdleTask, &run_loop, &counter),
611 EXPECT_EQ(counter, 1);
614 TEST(RunLoopDeathTest, MustRegisterBeforeInstantiating) {
615 TestBoundDelegate unbound_test_delegate_;
616 // RunLoop::RunLoop() should CHECK fetching the ThreadTaskRunnerHandle.
617 EXPECT_DEATH_IF_SUPPORTED({ RunLoop(); }, "");
620 TEST(RunLoopDelegateTest, NestableTasksDontRunInDefaultNestedLoops) {
621 TestBoundDelegate test_delegate;
622 test_delegate.BindToCurrentThread();
624 base::Thread other_thread("test");
625 other_thread.Start();
628 // A nested run loop which isn't kNestableTasksAllowed.
629 RunLoop nested_run_loop(RunLoop::Type::kDefault);
631 bool nested_run_loop_ended = false;
633 // The first task on the main loop will result in a nested run loop. Since
634 // it's not kNestableTasksAllowed, no further task should be processed until
636 ThreadTaskRunnerHandle::Get()->PostTask(
638 BindOnce([](RunLoop* nested_run_loop) { nested_run_loop->Run(); },
639 Unretained(&nested_run_loop)));
641 // Post a task that will fail if it runs inside the nested run loop.
642 ThreadTaskRunnerHandle::Get()->PostTask(
644 [](const bool& nested_run_loop_ended,
645 OnceClosure continuation_callback) {
646 EXPECT_TRUE(nested_run_loop_ended);
647 EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
648 std::move(continuation_callback).Run();
650 ConstRef(nested_run_loop_ended), main_loop.QuitClosure()));
652 // Post a task flipping the boolean bit for extra verification right before
653 // quitting |nested_run_loop|.
654 other_thread.task_runner()->PostDelayedTask(
657 [](bool* nested_run_loop_ended) {
658 EXPECT_FALSE(*nested_run_loop_ended);
659 *nested_run_loop_ended = true;
661 Unretained(&nested_run_loop_ended)),
662 TestTimeouts::tiny_timeout());
663 // Post an async delayed task to exit the run loop when idle. This confirms
664 // that (1) the test task only ran in the main loop after the nested loop
665 // exited and (2) the nested run loop actually considers itself idle while
666 // spinning. Note: The quit closure needs to be injected directly on the
667 // delegate as invoking QuitWhenIdle() off-thread results in a thread bounce
668 // which will not processed because of the very logic under test (nestable
669 // tasks don't run in |nested_run_loop|).
670 other_thread.task_runner()->PostDelayedTask(
673 [](TestBoundDelegate* test_delegate, OnceClosure injected_closure) {
674 test_delegate->InjectClosureOnDelegate(std::move(injected_closure));
676 Unretained(&test_delegate), nested_run_loop.QuitWhenIdleClosure()),
677 TestTimeouts::tiny_timeout());