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.
4
5 #include "base/run_loop.h"
6
7 #include <functional>
8 #include <memory>
9 #include <utility>
10
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"
32
33 namespace base {
34
35 namespace {
36
QuitWhenIdleTask(RunLoop * run_loop,int * counter)37 void QuitWhenIdleTask(RunLoop* run_loop, int* counter) {
38 run_loop->QuitWhenIdle();
39 ++(*counter);
40 }
41
RunNestedLoopTask(int * counter)42 void RunNestedLoopTask(int* counter) {
43 RunLoop nested_run_loop(RunLoop::Type::kNestableTasksAllowed);
44
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)));
49
50 SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
51 FROM_HERE, MakeExpectedNotRunClosure(FROM_HERE), Days(1));
52
53 nested_run_loop.Run();
54
55 ++(*counter);
56 }
57
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 {
62 public:
63 SimpleSingleThreadTaskRunner() = default;
64 SimpleSingleThreadTaskRunner(const SimpleSingleThreadTaskRunner&) = delete;
65 SimpleSingleThreadTaskRunner& operator=(const SimpleSingleThreadTaskRunner&) =
66 delete;
67
PostDelayedTask(const Location & from_here,OnceClosure task,base::TimeDelta delay)68 bool PostDelayedTask(const Location& from_here,
69 OnceClosure task,
70 base::TimeDelta delay) override {
71 if (delay.is_positive())
72 return false;
73 AutoLock auto_lock(tasks_lock_);
74 pending_tasks_.push(std::move(task));
75 return true;
76 }
77
PostNonNestableDelayedTask(const Location & from_here,OnceClosure task,base::TimeDelta delay)78 bool PostNonNestableDelayedTask(const Location& from_here,
79 OnceClosure task,
80 base::TimeDelta delay) override {
81 return PostDelayedTask(from_here, std::move(task), delay);
82 }
83
RunsTasksInCurrentSequence() const84 bool RunsTasksInCurrentSequence() const override {
85 return origin_thread_checker_.CalledOnValidThread();
86 }
87
ProcessSingleTask()88 bool ProcessSingleTask() {
89 OnceClosure task;
90 {
91 AutoLock auto_lock(tasks_lock_);
92 if (pending_tasks_.empty())
93 return false;
94 task = std::move(pending_tasks_.front());
95 pending_tasks_.pop();
96 }
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();
100 return true;
101 }
102
103 private:
104 ~SimpleSingleThreadTaskRunner() override = default;
105
106 Lock tasks_lock_;
107 base::queue<OnceClosure> pending_tasks_;
108
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_;
113 };
114
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 {
121 public:
InjectClosureOnDelegate(OnceClosure closure)122 void InjectClosureOnDelegate(OnceClosure closure) {
123 AutoLock auto_lock(closure_lock_);
124 closure_ = std::move(closure);
125 }
126
RunInjectedClosure()127 bool RunInjectedClosure() {
128 AutoLock auto_lock(closure_lock_);
129 if (closure_.is_null())
130 return false;
131 std::move(closure_).Run();
132 return true;
133 }
134
135 private:
136 Lock closure_lock_;
137 OnceClosure closure_;
138 };
139
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 {
144 public:
145 TestBoundDelegate() = default;
146
147 // Makes this TestBoundDelegate become the RunLoop::Delegate and
148 // SingleThreadTaskRunner::CurrentDefaultHandle for this thread.
BindToCurrentThread()149 void BindToCurrentThread() {
150 thread_task_runner_handle_ =
151 std::make_unique<SingleThreadTaskRunner::CurrentDefaultHandle>(
152 simple_task_runner_);
153 RunLoop::RegisterDelegateForCurrentThread(this);
154 }
155
156 private:
Run(bool application_tasks_allowed,TimeDelta timeout)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);
163 }
164 nested_run_allowing_tasks_incoming_ = false;
165
166 while (!should_quit_) {
167 if (application_tasks_allowed && simple_task_runner_->ProcessSingleTask())
168 continue;
169
170 if (ShouldQuitWhenIdle())
171 break;
172
173 if (RunInjectedClosure())
174 continue;
175
176 PlatformThread::YieldCurrentThread();
177 }
178 should_quit_ = false;
179 }
180
Quit()181 void Quit() override { should_quit_ = true; }
182
EnsureWorkScheduled()183 void EnsureWorkScheduled() override {
184 nested_run_allowing_tasks_incoming_ = true;
185 }
186
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;
190
191 scoped_refptr<SimpleSingleThreadTaskRunner> simple_task_runner_ =
192 MakeRefCounted<SimpleSingleThreadTaskRunner>();
193
194 std::unique_ptr<SingleThreadTaskRunner::CurrentDefaultHandle>
195 thread_task_runner_handle_;
196
197 bool should_quit_ = false;
198 };
199
200 enum class RunLoopTestType {
201 // Runs all RunLoopTests under a TaskEnvironment to make sure real world
202 // scenarios work.
203 kRealEnvironment,
204
205 // Runs all RunLoopTests under a test RunLoop::Delegate to make sure the
206 // delegate interface fully works standalone.
207 kTestDelegate,
208 };
209
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 {
213 public:
RunLoopTestEnvironment(RunLoopTestType type)214 explicit RunLoopTestEnvironment(RunLoopTestType type) {
215 switch (type) {
216 case RunLoopTestType::kRealEnvironment: {
217 task_environment_ = std::make_unique<test::TaskEnvironment>();
218 break;
219 }
220 case RunLoopTestType::kTestDelegate: {
221 auto test_delegate = std::make_unique<TestBoundDelegate>();
222 test_delegate->BindToCurrentThread();
223 test_delegate_ = std::move(test_delegate);
224 break;
225 }
226 }
227 }
228
229 private:
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_;
233 };
234
235 class RunLoopTest : public testing::TestWithParam<RunLoopTestType> {
236 public:
237 RunLoopTest(const RunLoopTest&) = delete;
238 RunLoopTest& operator=(const RunLoopTest&) = delete;
239
240 protected:
RunLoopTest()241 RunLoopTest() : test_environment_(GetParam()) {}
242
243 RunLoopTestEnvironment test_environment_;
244 RunLoop run_loop_;
245 };
246
247 } // namespace
248
TEST_P(RunLoopTest,QuitWhenIdle)249 TEST_P(RunLoopTest, QuitWhenIdle) {
250 int counter = 0;
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));
258
259 run_loop_.Run();
260 EXPECT_EQ(1, counter);
261 }
262
TEST_P(RunLoopTest,QuitWhenIdleNestedLoop)263 TEST_P(RunLoopTest, QuitWhenIdleNestedLoop) {
264 int counter = 0;
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));
274
275 run_loop_.Run();
276 EXPECT_EQ(3, counter);
277 }
278
TEST_P(RunLoopTest,QuitWhenIdleClosure)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));
286
287 run_loop_.Run();
288 }
289
290 // Verify that the QuitWhenIdleClosure() can run after the RunLoop has been
291 // deleted. It should have no effect.
TEST_P(RunLoopTest,QuitWhenIdleClosureAfterRunLoopScope)292 TEST_P(RunLoopTest, QuitWhenIdleClosureAfterRunLoopScope) {
293 RepeatingClosure quit_when_idle_closure;
294 {
295 RunLoop run_loop;
296 quit_when_idle_closure = run_loop.QuitWhenIdleClosure();
297 run_loop.RunUntilIdle();
298 }
299 quit_when_idle_closure.Run();
300 }
301
302 // Verify that Quit can be executed from another sequence.
TEST_P(RunLoopTest,QuitFromOtherSequence)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();
308
309 // Always expected to run before asynchronous Quit() kicks in.
310 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
311 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
312
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(
319 FROM_HERE,
320 base::BindOnce(&WaitableEvent::Signal, base::Unretained(&loop_was_quit)));
321
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));
327
328 run_loop_.Run();
329 }
330
331 // Verify that QuitClosure can be executed from another sequence.
TEST_P(RunLoopTest,QuitFromOtherSequenceWithClosure)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();
337
338 // Always expected to run before asynchronous Quit() kicks in.
339 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
340 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
341
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(
346 FROM_HERE,
347 base::BindOnce(&WaitableEvent::Signal, base::Unretained(&loop_was_quit)));
348
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));
354
355 run_loop_.Run();
356 }
357
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.
TEST_P(RunLoopTest,QuitFromOtherSequenceRacy)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();
365
366 // Always expected to run before asynchronous Quit() kicks in.
367 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
368 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
369
370 other_sequence->PostTask(FROM_HERE, run_loop_.QuitClosure());
371
372 run_loop_.Run();
373 }
374
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.
TEST_P(RunLoopTest,QuitFromOtherSequenceRacyWithClosure)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();
382
383 // Always expected to run before asynchronous Quit() kicks in.
384 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
385 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
386
387 other_sequence->PostTask(FROM_HERE, run_loop_.QuitClosure());
388
389 run_loop_.Run();
390 }
391
392 // Verify that QuitWhenIdle can be executed from another sequence.
TEST_P(RunLoopTest,QuitWhenIdleFromOtherSequence)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();
398
399 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
400 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
401
402 other_sequence->PostTask(
403 FROM_HERE,
404 base::BindOnce([](RunLoop* run_loop) { run_loop->QuitWhenIdle(); },
405 Unretained(&run_loop_)));
406
407 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
408 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
409
410 run_loop_.Run();
411
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.
414 }
415
416 // Verify that QuitWhenIdleClosure can be executed from another sequence.
TEST_P(RunLoopTest,QuitWhenIdleFromOtherSequenceWithClosure)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();
422
423 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
424 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
425
426 other_sequence->PostTask(FROM_HERE, run_loop_.QuitWhenIdleClosure());
427
428 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
429 FROM_HERE, MakeExpectedRunClosure(FROM_HERE));
430
431 run_loop_.Run();
432
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.
435 }
436
TEST_P(RunLoopTest,IsRunningOnCurrentThread)437 TEST_P(RunLoopTest, IsRunningOnCurrentThread) {
438 EXPECT_FALSE(RunLoop::IsRunningOnCurrentThread());
439 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
440 FROM_HERE,
441 BindOnce([]() { EXPECT_TRUE(RunLoop::IsRunningOnCurrentThread()); }));
442 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
443 FROM_HERE, run_loop_.QuitClosure());
444 run_loop_.Run();
445 }
446
TEST_P(RunLoopTest,IsNestedOnCurrentThread)447 TEST_P(RunLoopTest, IsNestedOnCurrentThread) {
448 EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
449
450 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
451 FROM_HERE, BindOnce([]() {
452 EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
453
454 RunLoop nested_run_loop(RunLoop::Type::kNestableTasksAllowed);
455
456 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
457 FROM_HERE, BindOnce([]() {
458 EXPECT_TRUE(RunLoop::IsNestedOnCurrentThread());
459 }));
460 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
461 FROM_HERE, nested_run_loop.QuitClosure());
462
463 EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
464 nested_run_loop.Run();
465 EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
466 }));
467
468 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
469 FROM_HERE, run_loop_.QuitClosure());
470 run_loop_.Run();
471 }
472
TEST_P(RunLoopTest,CannotRunMoreThanOnce)473 TEST_P(RunLoopTest, CannotRunMoreThanOnce) {
474 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
475 FROM_HERE, run_loop_.QuitClosure());
476 run_loop_.Run();
477 EXPECT_DCHECK_DEATH({ run_loop_.Run(); });
478 }
479
TEST_P(RunLoopTest,CanRunUntilIdleMoreThanOnce)480 TEST_P(RunLoopTest, CanRunUntilIdleMoreThanOnce) {
481 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, DoNothing());
482 run_loop_.RunUntilIdle();
483
484 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, DoNothing());
485 run_loop_.RunUntilIdle();
486 run_loop_.RunUntilIdle();
487 }
488
TEST_P(RunLoopTest,CanRunUntilIdleThenRunIfNotQuit)489 TEST_P(RunLoopTest, CanRunUntilIdleThenRunIfNotQuit) {
490 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, DoNothing());
491 run_loop_.RunUntilIdle();
492
493 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
494 FROM_HERE, run_loop_.QuitClosure());
495 run_loop_.Run();
496 }
497
TEST_P(RunLoopTest,CannotRunUntilIdleThenRunIfQuit)498 TEST_P(RunLoopTest, CannotRunUntilIdleThenRunIfQuit) {
499 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
500 FROM_HERE, run_loop_.QuitClosure());
501 run_loop_.RunUntilIdle();
502
503 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, DoNothing());
504 EXPECT_DCHECK_DEATH({ run_loop_.Run(); });
505 }
506
TEST_P(RunLoopTest,CannotRunAgainIfQuitWhenIdle)507 TEST_P(RunLoopTest, CannotRunAgainIfQuitWhenIdle) {
508 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
509 FROM_HERE, run_loop_.QuitWhenIdleClosure());
510 run_loop_.RunUntilIdle();
511
512 EXPECT_DCHECK_DEATH({ run_loop_.RunUntilIdle(); });
513 }
514
515 namespace {
516
517 class MockNestingObserver : public RunLoop::NestingObserver {
518 public:
519 MockNestingObserver() = default;
520 MockNestingObserver(const MockNestingObserver&) = delete;
521 MockNestingObserver& operator=(const MockNestingObserver&) = delete;
522
523 // RunLoop::NestingObserver:
524 MOCK_METHOD0(OnBeginNestedRunLoop, void());
525 MOCK_METHOD0(OnExitNestedRunLoop, void());
526 };
527
528 class MockTask {
529 public:
530 MockTask() = default;
531 MockTask(const MockTask&) = delete;
532 MockTask& operator=(const MockTask&) = delete;
533
534 MOCK_METHOD0(Task, void());
535 };
536
537 } // namespace
538
TEST_P(RunLoopTest,NestingObservers)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;
543
544 RunLoop::AddNestingObserverOnCurrentThread(&nesting_observer);
545
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();
551 });
552
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,
559 run_nested_loop);
560 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
561 FROM_HERE,
562 base::BindOnce(&MockTask::Task, base::Unretained(&mock_task_a)));
563 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE,
564 run_nested_loop);
565 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
566 FROM_HERE,
567 base::BindOnce(&MockTask::Task, base::Unretained(&mock_task_b)));
568
569 {
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);
576 }
577 run_loop_.RunUntilIdle();
578
579 RunLoop::RemoveNestingObserverOnCurrentThread(&nesting_observer);
580 }
581
TEST_P(RunLoopTest,DisallowRunning)582 TEST_P(RunLoopTest, DisallowRunning) {
583 ScopedDisallowRunningRunLoop disallow_running;
584 EXPECT_DCHECK_DEATH({ run_loop_.RunUntilIdle(); });
585 }
586
TEST_P(RunLoopTest,ExpiredDisallowRunning)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();
591 }
592
593 INSTANTIATE_TEST_SUITE_P(Real,
594 RunLoopTest,
595 testing::Values(RunLoopTestType::kRealEnvironment));
596 INSTANTIATE_TEST_SUITE_P(Mock,
597 RunLoopTest,
598 testing::Values(RunLoopTestType::kTestDelegate));
599
TEST(RunLoopDeathTest,MustRegisterBeforeInstantiating)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(); }, "");
605 }
606
TEST(RunLoopDelegateTest,NestableTasksDontRunInDefaultNestedLoops)607 TEST(RunLoopDelegateTest, NestableTasksDontRunInDefaultNestedLoops) {
608 TestBoundDelegate test_delegate;
609 test_delegate.BindToCurrentThread();
610
611 base::Thread other_thread("test");
612 other_thread.Start();
613
614 RunLoop main_loop;
615 // A nested run loop which isn't kNestableTasksAllowed.
616 RunLoop nested_run_loop(RunLoop::Type::kDefault);
617
618 bool nested_run_loop_ended = false;
619
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
622 // it's quit.
623 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
624 FROM_HERE,
625 BindOnce([](RunLoop* nested_run_loop) { nested_run_loop->Run(); },
626 Unretained(&nested_run_loop)));
627
628 // Post a task that will fail if it runs inside the nested run loop.
629 SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
630 FROM_HERE,
631 BindOnce(
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();
637 },
638 std::cref(nested_run_loop_ended), main_loop.QuitClosure()));
639
640 // Post a task flipping the boolean bit for extra verification right before
641 // quitting |nested_run_loop|.
642 other_thread.task_runner()->PostDelayedTask(
643 FROM_HERE,
644 BindOnce(
645 [](bool* nested_run_loop_ended) {
646 EXPECT_FALSE(*nested_run_loop_ended);
647 *nested_run_loop_ended = true;
648 },
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(
659 FROM_HERE,
660 BindOnce(
661 [](TestBoundDelegate* test_delegate, OnceClosure injected_closure) {
662 test_delegate->InjectClosureOnDelegate(std::move(injected_closure));
663 },
664 Unretained(&test_delegate), nested_run_loop.QuitWhenIdleClosure()),
665 TestTimeouts::tiny_timeout());
666
667 main_loop.Run();
668 }
669
670 } // namespace base
671