1 // Copyright 2016 The Chromium Authors
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"
11 #include "base/containers/queue.h"
12 #include "base/functional/bind.h"
13 #include "base/functional/callback_helpers.h"
14 #include "base/location.h"
15 #include "base/memory/ptr_util.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/synchronization/lock.h"
18 #include "base/synchronization/waitable_event.h"
19 #include "base/task/sequenced_task_runner.h"
20 #include "base/task/single_thread_task_runner.h"
21 #include "base/test/bind.h"
22 #include "base/test/gtest_util.h"
23 #include "base/test/scoped_run_loop_timeout.h"
24 #include "base/test/task_environment.h"
25 #include "base/test/test_timeouts.h"
26 #include "base/threading/platform_thread.h"
27 #include "base/threading/thread.h"
28 #include "base/threading/thread_checker_impl.h"
29 #include "build/build_config.h"
30 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest.h"
37 void QuitWhenIdleTask(RunLoop* run_loop, int* counter) {
38 run_loop->QuitWhenIdle();
42 void RunNestedLoopTask(int* counter) {
43 RunLoop nested_run_loop(RunLoop::Type::kNestableTasksAllowed);
45 // This task should quit |nested_run_loop| but not the main RunLoop.
46 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
47 FROM_HERE, BindOnce(&QuitWhenIdleTask, Unretained(&nested_run_loop),
48 Unretained(counter)));
50 SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
51 FROM_HERE, MakeExpectedNotRunClosure(FROM_HERE), Days(1));
53 nested_run_loop.Run();
58 // A simple SingleThreadTaskRunner that just queues undelayed tasks (and ignores
59 // delayed tasks). Tasks can then be processed one by one by ProcessTask() which
60 // will return true if it processed a task and false otherwise.
61 class SimpleSingleThreadTaskRunner : public SingleThreadTaskRunner {
63 SimpleSingleThreadTaskRunner() = default;
64 SimpleSingleThreadTaskRunner(const SimpleSingleThreadTaskRunner&) = delete;
65 SimpleSingleThreadTaskRunner& operator=(const SimpleSingleThreadTaskRunner&) =
68 bool PostDelayedTask(const Location& from_here,
70 base::TimeDelta delay) override {
71 if (delay.is_positive())
73 AutoLock auto_lock(tasks_lock_);
74 pending_tasks_.push(std::move(task));
78 bool PostNonNestableDelayedTask(const Location& from_here,
80 base::TimeDelta delay) override {
81 return PostDelayedTask(from_here, std::move(task), delay);
84 bool RunsTasksInCurrentSequence() const override {
85 return origin_thread_checker_.CalledOnValidThread();
88 bool ProcessSingleTask() {
91 AutoLock auto_lock(tasks_lock_);
92 if (pending_tasks_.empty())
94 task = std::move(pending_tasks_.front());
97 // It's important to Run() after pop() and outside the lock as |task| may
98 // run a nested loop which will re-enter ProcessSingleTask().
99 std::move(task).Run();
104 ~SimpleSingleThreadTaskRunner() override = default;
107 base::queue<OnceClosure> pending_tasks_;
109 // RunLoop relies on RunsTasksInCurrentSequence() signal. Use a
110 // ThreadCheckerImpl to be able to reliably provide that signal even in
111 // non-dcheck builds.
112 ThreadCheckerImpl origin_thread_checker_;
115 // The basis of all TestDelegates, allows safely injecting a OnceClosure to be
116 // run in the next idle phase of this delegate's Run() implementation. This can
117 // be used to have code run on a thread that is otherwise livelocked in an idle
118 // phase (sometimes a simple PostTask() won't do it -- e.g. when processing
119 // application tasks is disallowed).
120 class InjectableTestDelegate : public RunLoop::Delegate {
122 void InjectClosureOnDelegate(OnceClosure closure) {
123 AutoLock auto_lock(closure_lock_);
124 closure_ = std::move(closure);
127 bool RunInjectedClosure() {
128 AutoLock auto_lock(closure_lock_);
129 if (closure_.is_null())
131 std::move(closure_).Run();
137 OnceClosure closure_;
140 // A simple test RunLoop::Delegate to exercise Runloop logic independent of any
141 // other base constructs. BindToCurrentThread() must be called before this
142 // TestBoundDelegate is operational.
143 class TestBoundDelegate final : public InjectableTestDelegate {
145 TestBoundDelegate() = default;
147 // Makes this TestBoundDelegate become the RunLoop::Delegate and
148 // SingleThreadTaskRunner::CurrentDefaultHandle for this thread.
149 void BindToCurrentThread() {
150 thread_task_runner_handle_ =
151 std::make_unique<SingleThreadTaskRunner::CurrentDefaultHandle>(
152 simple_task_runner_);
153 RunLoop::RegisterDelegateForCurrentThread(this);
157 void Run(bool application_tasks_allowed, TimeDelta timeout) override {
158 if (nested_run_allowing_tasks_incoming_) {
159 EXPECT_TRUE(RunLoop::IsNestedOnCurrentThread());
160 EXPECT_TRUE(application_tasks_allowed);
161 } else if (RunLoop::IsNestedOnCurrentThread()) {
162 EXPECT_FALSE(application_tasks_allowed);
164 nested_run_allowing_tasks_incoming_ = false;
166 while (!should_quit_) {
167 if (application_tasks_allowed && simple_task_runner_->ProcessSingleTask())
170 if (ShouldQuitWhenIdle())
173 if (RunInjectedClosure())
176 PlatformThread::YieldCurrentThread();
178 should_quit_ = false;
181 void Quit() override { should_quit_ = true; }
183 void EnsureWorkScheduled() override {
184 nested_run_allowing_tasks_incoming_ = true;
187 // True if the next invocation of Run() is expected to be from a
188 // kNestableTasksAllowed RunLoop.
189 bool nested_run_allowing_tasks_incoming_ = false;
191 scoped_refptr<SimpleSingleThreadTaskRunner> simple_task_runner_ =
192 MakeRefCounted<SimpleSingleThreadTaskRunner>();
194 std::unique_ptr<SingleThreadTaskRunner::CurrentDefaultHandle>
195 thread_task_runner_handle_;
197 bool should_quit_ = false;
200 enum class RunLoopTestType {
201 // Runs all RunLoopTests under a TaskEnvironment to make sure real world
205 // Runs all RunLoopTests under a test RunLoop::Delegate to make sure the
206 // delegate interface fully works standalone.
210 // The task environment for the RunLoopTest of a given type. A separate class
211 // so it can be instantiated on the stack in the RunLoopTest fixture.
212 class RunLoopTestEnvironment {
214 explicit RunLoopTestEnvironment(RunLoopTestType type) {
216 case RunLoopTestType::kRealEnvironment: {
217 task_environment_ = std::make_unique<test::TaskEnvironment>();
220 case RunLoopTestType::kTestDelegate: {
221 auto test_delegate = std::make_unique<TestBoundDelegate>();
222 test_delegate->BindToCurrentThread();
223 test_delegate_ = std::move(test_delegate);
230 // Instantiates one or the other based on the RunLoopTestType.
231 std::unique_ptr<test::TaskEnvironment> task_environment_;
232 std::unique_ptr<InjectableTestDelegate> test_delegate_;
235 class RunLoopTest : public testing::TestWithParam<RunLoopTestType> {
237 RunLoopTest(const RunLoopTest&) = delete;
238 RunLoopTest& operator=(const RunLoopTest&) = delete;
241 RunLoopTest() : test_environment_(GetParam()) {}
243 RunLoopTestEnvironment test_environment_;
249 TEST_P(RunLoopTest, QuitWhenIdle) {
251 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
252 FROM_HERE, BindOnce(&QuitWhenIdleTask, Unretained(&run_loop_),
253 Unretained(&counter)));
254 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
255 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
256 SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
257 FROM_HERE, MakeExpectedNotRunClosure(FROM_HERE), Days(1));
260 EXPECT_EQ(1, counter);
263 TEST_P(RunLoopTest, QuitWhenIdleNestedLoop) {
265 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
266 FROM_HERE, BindOnce(&RunNestedLoopTask, Unretained(&counter)));
267 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
268 FROM_HERE, BindOnce(&QuitWhenIdleTask, Unretained(&run_loop_),
269 Unretained(&counter)));
270 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
271 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
272 SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
273 FROM_HERE, MakeExpectedNotRunClosure(FROM_HERE), Days(1));
276 EXPECT_EQ(3, counter);
279 TEST_P(RunLoopTest, QuitWhenIdleClosure) {
280 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
281 FROM_HERE, run_loop_.QuitWhenIdleClosure());
282 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
283 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
284 SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
285 FROM_HERE, MakeExpectedNotRunClosure(FROM_HERE), Days(1));
290 // Verify that the QuitWhenIdleClosure() can run after the RunLoop has been
291 // deleted. It should have no effect.
292 TEST_P(RunLoopTest, QuitWhenIdleClosureAfterRunLoopScope) {
293 RepeatingClosure quit_when_idle_closure;
296 quit_when_idle_closure = run_loop.QuitWhenIdleClosure();
297 run_loop.RunUntilIdle();
299 quit_when_idle_closure.Run();
302 // Verify that Quit can be executed from another sequence.
303 TEST_P(RunLoopTest, QuitFromOtherSequence) {
304 Thread other_thread("test");
305 other_thread.Start();
306 scoped_refptr<SequencedTaskRunner> other_sequence =
307 other_thread.task_runner();
309 // Always expected to run before asynchronous Quit() kicks in.
310 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
311 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
313 WaitableEvent loop_was_quit(WaitableEvent::ResetPolicy::MANUAL,
314 WaitableEvent::InitialState::NOT_SIGNALED);
315 other_sequence->PostTask(
316 FROM_HERE, base::BindOnce([](RunLoop* run_loop) { run_loop->Quit(); },
317 Unretained(&run_loop_)));
318 other_sequence->PostTask(
320 base::BindOnce(&WaitableEvent::Signal, base::Unretained(&loop_was_quit)));
322 // Anything that's posted after the Quit closure was posted back to this
323 // sequence shouldn't get a chance to run.
324 loop_was_quit.Wait();
325 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
326 FROM_HERE, MakeExpectedNotRunClosure(FROM_HERE));
331 // Verify that QuitClosure can be executed from another sequence.
332 TEST_P(RunLoopTest, QuitFromOtherSequenceWithClosure) {
333 Thread other_thread("test");
334 other_thread.Start();
335 scoped_refptr<SequencedTaskRunner> other_sequence =
336 other_thread.task_runner();
338 // Always expected to run before asynchronous Quit() kicks in.
339 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
340 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
342 WaitableEvent loop_was_quit(WaitableEvent::ResetPolicy::MANUAL,
343 WaitableEvent::InitialState::NOT_SIGNALED);
344 other_sequence->PostTask(FROM_HERE, run_loop_.QuitClosure());
345 other_sequence->PostTask(
347 base::BindOnce(&WaitableEvent::Signal, base::Unretained(&loop_was_quit)));
349 // Anything that's posted after the Quit closure was posted back to this
350 // sequence shouldn't get a chance to run.
351 loop_was_quit.Wait();
352 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
353 FROM_HERE, MakeExpectedNotRunClosure(FROM_HERE));
358 // Verify that Quit can be executed from another sequence even when the
359 // Quit is racing with Run() -- i.e. forgo the WaitableEvent used above.
360 TEST_P(RunLoopTest, QuitFromOtherSequenceRacy) {
361 Thread other_thread("test");
362 other_thread.Start();
363 scoped_refptr<SequencedTaskRunner> other_sequence =
364 other_thread.task_runner();
366 // Always expected to run before asynchronous Quit() kicks in.
367 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
368 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
370 other_sequence->PostTask(FROM_HERE, run_loop_.QuitClosure());
375 // Verify that QuitClosure can be executed from another sequence even when the
376 // Quit is racing with Run() -- i.e. forgo the WaitableEvent used above.
377 TEST_P(RunLoopTest, QuitFromOtherSequenceRacyWithClosure) {
378 Thread other_thread("test");
379 other_thread.Start();
380 scoped_refptr<SequencedTaskRunner> other_sequence =
381 other_thread.task_runner();
383 // Always expected to run before asynchronous Quit() kicks in.
384 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
385 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
387 other_sequence->PostTask(FROM_HERE, run_loop_.QuitClosure());
392 // Verify that QuitWhenIdle can be executed from another sequence.
393 TEST_P(RunLoopTest, QuitWhenIdleFromOtherSequence) {
394 Thread other_thread("test");
395 other_thread.Start();
396 scoped_refptr<SequencedTaskRunner> other_sequence =
397 other_thread.task_runner();
399 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
400 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
402 other_sequence->PostTask(
404 base::BindOnce([](RunLoop* run_loop) { run_loop->QuitWhenIdle(); },
405 Unretained(&run_loop_)));
407 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
408 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
412 // Regardless of the outcome of the race this thread shouldn't have been idle
413 // until both tasks posted to this sequence have run.
416 // Verify that QuitWhenIdleClosure can be executed from another sequence.
417 TEST_P(RunLoopTest, QuitWhenIdleFromOtherSequenceWithClosure) {
418 Thread other_thread("test");
419 other_thread.Start();
420 scoped_refptr<SequencedTaskRunner> other_sequence =
421 other_thread.task_runner();
423 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
424 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
426 other_sequence->PostTask(FROM_HERE, run_loop_.QuitWhenIdleClosure());
428 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
429 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
433 // Regardless of the outcome of the race this thread shouldn't have been idle
434 // until the both tasks posted to this sequence have run.
437 TEST_P(RunLoopTest, IsRunningOnCurrentThread) {
438 EXPECT_FALSE(RunLoop::IsRunningOnCurrentThread());
439 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
441 BindOnce([]() { EXPECT_TRUE(RunLoop::IsRunningOnCurrentThread()); }));
442 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
443 FROM_HERE, run_loop_.QuitClosure());
447 TEST_P(RunLoopTest, IsNestedOnCurrentThread) {
448 EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
450 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
451 FROM_HERE, BindOnce([]() {
452 EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
454 RunLoop nested_run_loop(RunLoop::Type::kNestableTasksAllowed);
456 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
457 FROM_HERE, BindOnce([]() {
458 EXPECT_TRUE(RunLoop::IsNestedOnCurrentThread());
460 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
461 FROM_HERE, nested_run_loop.QuitClosure());
463 EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
464 nested_run_loop.Run();
465 EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
468 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
469 FROM_HERE, run_loop_.QuitClosure());
473 TEST_P(RunLoopTest, CannotRunMoreThanOnce) {
474 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
475 FROM_HERE, run_loop_.QuitClosure());
477 EXPECT_DCHECK_DEATH({ run_loop_.Run(); });
480 TEST_P(RunLoopTest, CanRunUntilIdleMoreThanOnce) {
481 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, DoNothing());
482 run_loop_.RunUntilIdle();
484 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, DoNothing());
485 run_loop_.RunUntilIdle();
486 run_loop_.RunUntilIdle();
489 TEST_P(RunLoopTest, CanRunUntilIdleThenRunIfNotQuit) {
490 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, DoNothing());
491 run_loop_.RunUntilIdle();
493 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
494 FROM_HERE, run_loop_.QuitClosure());
498 TEST_P(RunLoopTest, CannotRunUntilIdleThenRunIfQuit) {
499 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
500 FROM_HERE, run_loop_.QuitClosure());
501 run_loop_.RunUntilIdle();
503 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, DoNothing());
504 EXPECT_DCHECK_DEATH({ run_loop_.Run(); });
507 TEST_P(RunLoopTest, CannotRunAgainIfQuitWhenIdle) {
508 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
509 FROM_HERE, run_loop_.QuitWhenIdleClosure());
510 run_loop_.RunUntilIdle();
512 EXPECT_DCHECK_DEATH({ run_loop_.RunUntilIdle(); });
517 class MockNestingObserver : public RunLoop::NestingObserver {
519 MockNestingObserver() = default;
520 MockNestingObserver(const MockNestingObserver&) = delete;
521 MockNestingObserver& operator=(const MockNestingObserver&) = delete;
523 // RunLoop::NestingObserver:
524 MOCK_METHOD0(OnBeginNestedRunLoop, void());
525 MOCK_METHOD0(OnExitNestedRunLoop, void());
530 MockTask() = default;
531 MockTask(const MockTask&) = delete;
532 MockTask& operator=(const MockTask&) = delete;
534 MOCK_METHOD0(Task, void());
539 TEST_P(RunLoopTest, NestingObservers) {
540 testing::StrictMock<MockNestingObserver> nesting_observer;
541 testing::StrictMock<MockTask> mock_task_a;
542 testing::StrictMock<MockTask> mock_task_b;
544 RunLoop::AddNestingObserverOnCurrentThread(&nesting_observer);
546 const RepeatingClosure run_nested_loop = BindRepeating([]() {
547 RunLoop nested_run_loop(RunLoop::Type::kNestableTasksAllowed);
548 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
549 FROM_HERE, nested_run_loop.QuitClosure());
550 nested_run_loop.Run();
553 // Generate a stack of nested RunLoops. OnBeginNestedRunLoop() is expected
554 // when beginning each nesting depth and OnExitNestedRunLoop() is expected
555 // when exiting each nesting depth. Each one of these tasks is ahead of the
556 // QuitClosures as those are only posted at the end of the queue when
557 // |run_nested_loop| is executed.
558 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE,
560 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
562 base::BindOnce(&MockTask::Task, base::Unretained(&mock_task_a)));
563 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE,
565 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
567 base::BindOnce(&MockTask::Task, base::Unretained(&mock_task_b)));
570 testing::InSequence in_sequence;
571 EXPECT_CALL(nesting_observer, OnBeginNestedRunLoop());
572 EXPECT_CALL(mock_task_a, Task());
573 EXPECT_CALL(nesting_observer, OnBeginNestedRunLoop());
574 EXPECT_CALL(mock_task_b, Task());
575 EXPECT_CALL(nesting_observer, OnExitNestedRunLoop()).Times(2);
577 run_loop_.RunUntilIdle();
579 RunLoop::RemoveNestingObserverOnCurrentThread(&nesting_observer);
582 TEST_P(RunLoopTest, DisallowRunning) {
583 ScopedDisallowRunningRunLoop disallow_running;
584 EXPECT_DCHECK_DEATH({ run_loop_.RunUntilIdle(); });
587 TEST_P(RunLoopTest, ExpiredDisallowRunning) {
588 { ScopedDisallowRunningRunLoop disallow_running; }
589 // Running should be fine after |disallow_running| goes out of scope.
590 run_loop_.RunUntilIdle();
593 INSTANTIATE_TEST_SUITE_P(Real,
595 testing::Values(RunLoopTestType::kRealEnvironment));
596 INSTANTIATE_TEST_SUITE_P(Mock,
598 testing::Values(RunLoopTestType::kTestDelegate));
600 TEST(RunLoopDeathTest, MustRegisterBeforeInstantiating) {
601 TestBoundDelegate unbound_test_delegate_;
602 // RunLoop::RunLoop() should CHECK fetching the
603 // SingleThreadTaskRunner::CurrentDefaultHandle.
604 EXPECT_DEATH_IF_SUPPORTED({ RunLoop(); }, "");
607 TEST(RunLoopDelegateTest, NestableTasksDontRunInDefaultNestedLoops) {
608 TestBoundDelegate test_delegate;
609 test_delegate.BindToCurrentThread();
611 base::Thread other_thread("test");
612 other_thread.Start();
615 // A nested run loop which isn't kNestableTasksAllowed.
616 RunLoop nested_run_loop(RunLoop::Type::kDefault);
618 bool nested_run_loop_ended = false;
620 // The first task on the main loop will result in a nested run loop. Since
621 // it's not kNestableTasksAllowed, no further task should be processed until
623 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
625 BindOnce([](RunLoop* nested_run_loop) { nested_run_loop->Run(); },
626 Unretained(&nested_run_loop)));
628 // Post a task that will fail if it runs inside the nested run loop.
629 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
632 [](const bool& nested_run_loop_ended,
633 OnceClosure continuation_callback) {
634 EXPECT_TRUE(nested_run_loop_ended);
635 EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
636 std::move(continuation_callback).Run();
638 std::cref(nested_run_loop_ended), main_loop.QuitClosure()));
640 // Post a task flipping the boolean bit for extra verification right before
641 // quitting |nested_run_loop|.
642 other_thread.task_runner()->PostDelayedTask(
645 [](bool* nested_run_loop_ended) {
646 EXPECT_FALSE(*nested_run_loop_ended);
647 *nested_run_loop_ended = true;
649 Unretained(&nested_run_loop_ended)),
650 TestTimeouts::tiny_timeout());
651 // Post an async delayed task to exit the run loop when idle. This confirms
652 // that (1) the test task only ran in the main loop after the nested loop
653 // exited and (2) the nested run loop actually considers itself idle while
654 // spinning. Note: The quit closure needs to be injected directly on the
655 // delegate as invoking QuitWhenIdle() off-thread results in a thread bounce
656 // which will not processed because of the very logic under test (nestable
657 // tasks don't run in |nested_run_loop|).
658 other_thread.task_runner()->PostDelayedTask(
661 [](TestBoundDelegate* test_delegate, OnceClosure injected_closure) {
662 test_delegate->InjectClosureOnDelegate(std::move(injected_closure));
664 Unretained(&test_delegate), nested_run_loop.QuitWhenIdleClosure()),
665 TestTimeouts::tiny_timeout());