fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "base/run_loop.h" |
| 6 | |
jdoerrie | 9d7236f6 | 2019-03-05 13:00:23 | [diff] [blame^] | 7 | #include <functional> |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 10 | #include "base/bind.h" |
| 11 | #include "base/bind_helpers.h" |
Brett Wilson | a62d9c0 | 2017-09-20 20:53:20 | [diff] [blame] | 12 | #include "base/containers/queue.h" |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 13 | #include "base/location.h" |
| 14 | #include "base/macros.h" |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 15 | #include "base/memory/ptr_util.h" |
| 16 | #include "base/memory/ref_counted.h" |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 17 | #include "base/single_thread_task_runner.h" |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 18 | #include "base/synchronization/lock.h" |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 19 | #include "base/synchronization/waitable_event.h" |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 20 | #include "base/test/bind_test_util.h" |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 21 | #include "base/test/gtest_util.h" |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 22 | #include "base/test/scoped_task_environment.h" |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 23 | #include "base/test/test_timeouts.h" |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 24 | #include "base/threading/platform_thread.h" |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 25 | #include "base/threading/sequenced_task_runner_handle.h" |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 26 | #include "base/threading/thread.h" |
| 27 | #include "base/threading/thread_checker_impl.h" |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 28 | #include "base/threading/thread_task_runner_handle.h" |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 29 | #include "build/build_config.h" |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 30 | #include "testing/gmock/include/gmock/gmock.h" |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 31 | #include "testing/gtest/include/gtest/gtest.h" |
| 32 | |
| 33 | namespace base { |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | void QuitWhenIdleTask(RunLoop* run_loop, int* counter) { |
| 38 | run_loop->QuitWhenIdle(); |
| 39 | ++(*counter); |
| 40 | } |
| 41 | |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 42 | void RunNestedLoopTask(int* counter) { |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 43 | RunLoop nested_run_loop(RunLoop::Type::kNestableTasksAllowed); |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 44 | |
| 45 | // This task should quit |nested_run_loop| but not the main RunLoop. |
| 46 | ThreadTaskRunnerHandle::Get()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 47 | FROM_HERE, BindOnce(&QuitWhenIdleTask, Unretained(&nested_run_loop), |
| 48 | Unretained(counter))); |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 49 | |
| 50 | ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 51 | FROM_HERE, MakeExpectedNotRunClosure(FROM_HERE), TimeDelta::FromDays(1)); |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 52 | |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 53 | nested_run_loop.Run(); |
| 54 | |
| 55 | ++(*counter); |
| 56 | } |
| 57 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 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; |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 64 | |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 65 | bool PostDelayedTask(const Location& from_here, |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 66 | OnceClosure task, |
| 67 | base::TimeDelta delay) override { |
| 68 | if (delay > base::TimeDelta()) |
| 69 | return false; |
| 70 | AutoLock auto_lock(tasks_lock_); |
| 71 | pending_tasks_.push(std::move(task)); |
| 72 | return true; |
| 73 | } |
| 74 | |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 75 | bool PostNonNestableDelayedTask(const Location& from_here, |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 76 | OnceClosure task, |
| 77 | base::TimeDelta delay) override { |
| 78 | return PostDelayedTask(from_here, std::move(task), delay); |
| 79 | } |
| 80 | |
| 81 | bool RunsTasksInCurrentSequence() const override { |
| 82 | return origin_thread_checker_.CalledOnValidThread(); |
| 83 | } |
| 84 | |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 85 | bool ProcessSingleTask() { |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 86 | OnceClosure task; |
| 87 | { |
| 88 | AutoLock auto_lock(tasks_lock_); |
| 89 | if (pending_tasks_.empty()) |
| 90 | return false; |
| 91 | task = std::move(pending_tasks_.front()); |
| 92 | pending_tasks_.pop(); |
| 93 | } |
| 94 | // It's important to Run() after pop() and outside the lock as |task| may |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 95 | // run a nested loop which will re-enter ProcessSingleTask(). |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 96 | std::move(task).Run(); |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | ~SimpleSingleThreadTaskRunner() override = default; |
| 102 | |
| 103 | Lock tasks_lock_; |
Brett Wilson | a62d9c0 | 2017-09-20 20:53:20 | [diff] [blame] | 104 | base::queue<OnceClosure> pending_tasks_; |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 105 | |
| 106 | // RunLoop relies on RunsTasksInCurrentSequence() signal. Use a |
| 107 | // ThreadCheckerImpl to be able to reliably provide that signal even in |
| 108 | // non-dcheck builds. |
| 109 | ThreadCheckerImpl origin_thread_checker_; |
| 110 | |
| 111 | DISALLOW_COPY_AND_ASSIGN(SimpleSingleThreadTaskRunner); |
| 112 | }; |
| 113 | |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 114 | // The basis of all TestDelegates, allows safely injecting a OnceClosure to be |
| 115 | // run in the next idle phase of this delegate's Run() implementation. This can |
| 116 | // be used to have code run on a thread that is otherwise livelocked in an idle |
| 117 | // phase (sometimes a simple PostTask() won't do it -- e.g. when processing |
| 118 | // application tasks is disallowed). |
| 119 | class InjectableTestDelegate : public RunLoop::Delegate { |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 120 | public: |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 121 | void InjectClosureOnDelegate(OnceClosure closure) { |
| 122 | AutoLock auto_lock(closure_lock_); |
| 123 | closure_ = std::move(closure); |
| 124 | } |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 125 | |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 126 | bool RunInjectedClosure() { |
| 127 | AutoLock auto_lock(closure_lock_); |
| 128 | if (closure_.is_null()) |
| 129 | return false; |
| 130 | std::move(closure_).Run(); |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | private: |
| 135 | Lock closure_lock_; |
| 136 | OnceClosure closure_; |
| 137 | }; |
| 138 | |
| 139 | // A simple test RunLoop::Delegate to exercise Runloop logic independent of any |
| 140 | // other base constructs. BindToCurrentThread() must be called before this |
| 141 | // TestBoundDelegate is operational. |
| 142 | class TestBoundDelegate final : public InjectableTestDelegate { |
| 143 | public: |
| 144 | TestBoundDelegate() = default; |
| 145 | |
| 146 | // Makes this TestBoundDelegate become the RunLoop::Delegate and |
| 147 | // ThreadTaskRunnerHandle for this thread. |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 148 | void BindToCurrentThread() { |
| 149 | thread_task_runner_handle_ = |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 150 | std::make_unique<ThreadTaskRunnerHandle>(simple_task_runner_); |
Gabriel Charette | a3ec961 | 2017-12-14 17:22:40 | [diff] [blame] | 151 | RunLoop::RegisterDelegateForCurrentThread(this); |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | private: |
Gabriel Charette | b030a4a | 2017-10-26 01:04:40 | [diff] [blame] | 155 | void Run(bool application_tasks_allowed) override { |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 156 | if (nested_run_allowing_tasks_incoming_) { |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 157 | EXPECT_TRUE(RunLoop::IsNestedOnCurrentThread()); |
Gabriel Charette | b030a4a | 2017-10-26 01:04:40 | [diff] [blame] | 158 | EXPECT_TRUE(application_tasks_allowed); |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 159 | } else if (RunLoop::IsNestedOnCurrentThread()) { |
Gabriel Charette | b030a4a | 2017-10-26 01:04:40 | [diff] [blame] | 160 | EXPECT_FALSE(application_tasks_allowed); |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 161 | } |
| 162 | nested_run_allowing_tasks_incoming_ = false; |
| 163 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 164 | while (!should_quit_) { |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 165 | if (application_tasks_allowed && simple_task_runner_->ProcessSingleTask()) |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 166 | continue; |
| 167 | |
Gabriel Charette | a3ec961 | 2017-12-14 17:22:40 | [diff] [blame] | 168 | if (ShouldQuitWhenIdle()) |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 169 | break; |
| 170 | |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 171 | if (RunInjectedClosure()) |
| 172 | continue; |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 173 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 174 | PlatformThread::YieldCurrentThread(); |
| 175 | } |
| 176 | should_quit_ = false; |
| 177 | } |
| 178 | |
| 179 | void Quit() override { should_quit_ = true; } |
| 180 | |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 181 | void EnsureWorkScheduled() override { |
| 182 | nested_run_allowing_tasks_incoming_ = true; |
| 183 | } |
| 184 | |
| 185 | // True if the next invocation of Run() is expected to be from a |
| 186 | // kNestableTasksAllowed RunLoop. |
| 187 | bool nested_run_allowing_tasks_incoming_ = false; |
| 188 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 189 | scoped_refptr<SimpleSingleThreadTaskRunner> simple_task_runner_ = |
| 190 | MakeRefCounted<SimpleSingleThreadTaskRunner>(); |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 191 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 192 | std::unique_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; |
| 193 | |
| 194 | bool should_quit_ = false; |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 195 | }; |
| 196 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 197 | enum class RunLoopTestType { |
| 198 | // Runs all RunLoopTests under a ScopedTaskEnvironment to make sure real world |
| 199 | // scenarios work. |
| 200 | kRealEnvironment, |
| 201 | |
| 202 | // Runs all RunLoopTests under a test RunLoop::Delegate to make sure the |
| 203 | // delegate interface fully works standalone. |
| 204 | kTestDelegate, |
| 205 | }; |
| 206 | |
| 207 | // The task environment for the RunLoopTest of a given type. A separate class |
| 208 | // so it can be instantiated on the stack in the RunLoopTest fixture. |
| 209 | class RunLoopTestEnvironment { |
| 210 | public: |
| 211 | RunLoopTestEnvironment(RunLoopTestType type) { |
| 212 | switch (type) { |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 213 | case RunLoopTestType::kRealEnvironment: { |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 214 | task_environment_ = std::make_unique<test::ScopedTaskEnvironment>(); |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 215 | break; |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 216 | } |
| 217 | case RunLoopTestType::kTestDelegate: { |
| 218 | auto test_delegate = std::make_unique<TestBoundDelegate>(); |
| 219 | test_delegate->BindToCurrentThread(); |
| 220 | test_delegate_ = std::move(test_delegate); |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 221 | break; |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 222 | } |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | private: |
Gabriel Charette | 50518fc | 2018-05-22 17:53:22 | [diff] [blame] | 227 | // Instantiates one or the other based on the RunLoopTestType. |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 228 | std::unique_ptr<test::ScopedTaskEnvironment> task_environment_; |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 229 | std::unique_ptr<InjectableTestDelegate> test_delegate_; |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | class RunLoopTest : public testing::TestWithParam<RunLoopTestType> { |
| 233 | protected: |
| 234 | RunLoopTest() : test_environment_(GetParam()) {} |
| 235 | |
| 236 | RunLoopTestEnvironment test_environment_; |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 237 | RunLoop run_loop_; |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 238 | |
| 239 | private: |
| 240 | DISALLOW_COPY_AND_ASSIGN(RunLoopTest); |
| 241 | }; |
| 242 | |
| 243 | } // namespace |
| 244 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 245 | TEST_P(RunLoopTest, QuitWhenIdle) { |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 246 | int counter = 0; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 247 | ThreadTaskRunnerHandle::Get()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 248 | FROM_HERE, BindOnce(&QuitWhenIdleTask, Unretained(&run_loop_), |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 249 | Unretained(&counter))); |
| 250 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 251 | MakeExpectedRunClosure(FROM_HERE)); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 252 | ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 253 | FROM_HERE, MakeExpectedNotRunClosure(FROM_HERE), TimeDelta::FromDays(1)); |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 254 | |
| 255 | run_loop_.Run(); |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 256 | EXPECT_EQ(1, counter); |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 257 | } |
| 258 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 259 | TEST_P(RunLoopTest, QuitWhenIdleNestedLoop) { |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 260 | int counter = 0; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 261 | ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 262 | FROM_HERE, BindOnce(&RunNestedLoopTask, Unretained(&counter))); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 263 | ThreadTaskRunnerHandle::Get()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 264 | FROM_HERE, BindOnce(&QuitWhenIdleTask, Unretained(&run_loop_), |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 265 | Unretained(&counter))); |
| 266 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 267 | MakeExpectedRunClosure(FROM_HERE)); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 268 | ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 269 | FROM_HERE, MakeExpectedNotRunClosure(FROM_HERE), TimeDelta::FromDays(1)); |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 270 | |
| 271 | run_loop_.Run(); |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 272 | EXPECT_EQ(3, counter); |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 273 | } |
| 274 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 275 | TEST_P(RunLoopTest, QuitWhenIdleClosure) { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 276 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 277 | run_loop_.QuitWhenIdleClosure()); |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 278 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 279 | MakeExpectedRunClosure(FROM_HERE)); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 280 | ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 281 | FROM_HERE, MakeExpectedNotRunClosure(FROM_HERE), TimeDelta::FromDays(1)); |
fdoray | a365860 | 2016-06-10 18:23:15 | [diff] [blame] | 282 | |
| 283 | run_loop_.Run(); |
fdoray | a365860 | 2016-06-10 18:23:15 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // Verify that the QuitWhenIdleClosure() can run after the RunLoop has been |
| 287 | // deleted. It should have no effect. |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 288 | TEST_P(RunLoopTest, QuitWhenIdleClosureAfterRunLoopScope) { |
fdoray | a365860 | 2016-06-10 18:23:15 | [diff] [blame] | 289 | Closure quit_when_idle_closure; |
| 290 | { |
| 291 | RunLoop run_loop; |
| 292 | quit_when_idle_closure = run_loop.QuitWhenIdleClosure(); |
| 293 | run_loop.RunUntilIdle(); |
| 294 | } |
| 295 | quit_when_idle_closure.Run(); |
| 296 | } |
| 297 | |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 298 | // Verify that Quit can be executed from another sequence. |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 299 | TEST_P(RunLoopTest, QuitFromOtherSequence) { |
| 300 | Thread other_thread("test"); |
| 301 | other_thread.Start(); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 302 | scoped_refptr<SequencedTaskRunner> other_sequence = |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 303 | other_thread.task_runner(); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 304 | |
| 305 | // Always expected to run before asynchronous Quit() kicks in. |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 306 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 307 | MakeExpectedRunClosure(FROM_HERE)); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 308 | |
| 309 | WaitableEvent loop_was_quit(WaitableEvent::ResetPolicy::MANUAL, |
| 310 | WaitableEvent::InitialState::NOT_SIGNALED); |
| 311 | other_sequence->PostTask( |
| 312 | FROM_HERE, base::BindOnce([](RunLoop* run_loop) { run_loop->Quit(); }, |
| 313 | Unretained(&run_loop_))); |
| 314 | other_sequence->PostTask( |
| 315 | FROM_HERE, |
| 316 | base::BindOnce(&WaitableEvent::Signal, base::Unretained(&loop_was_quit))); |
| 317 | |
| 318 | // Anything that's posted after the Quit closure was posted back to this |
| 319 | // sequence shouldn't get a chance to run. |
| 320 | loop_was_quit.Wait(); |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 321 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 322 | MakeExpectedNotRunClosure(FROM_HERE)); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 323 | |
| 324 | run_loop_.Run(); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | // Verify that QuitClosure can be executed from another sequence. |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 328 | TEST_P(RunLoopTest, QuitFromOtherSequenceWithClosure) { |
| 329 | Thread other_thread("test"); |
| 330 | other_thread.Start(); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 331 | scoped_refptr<SequencedTaskRunner> other_sequence = |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 332 | other_thread.task_runner(); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 333 | |
| 334 | // Always expected to run before asynchronous Quit() kicks in. |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 335 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 336 | MakeExpectedRunClosure(FROM_HERE)); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 337 | |
| 338 | WaitableEvent loop_was_quit(WaitableEvent::ResetPolicy::MANUAL, |
| 339 | WaitableEvent::InitialState::NOT_SIGNALED); |
| 340 | other_sequence->PostTask(FROM_HERE, run_loop_.QuitClosure()); |
| 341 | other_sequence->PostTask( |
| 342 | FROM_HERE, |
| 343 | base::BindOnce(&WaitableEvent::Signal, base::Unretained(&loop_was_quit))); |
| 344 | |
| 345 | // Anything that's posted after the Quit closure was posted back to this |
| 346 | // sequence shouldn't get a chance to run. |
| 347 | loop_was_quit.Wait(); |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 348 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 349 | MakeExpectedNotRunClosure(FROM_HERE)); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 350 | |
| 351 | run_loop_.Run(); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | // Verify that Quit can be executed from another sequence even when the |
| 355 | // Quit is racing with Run() -- i.e. forgo the WaitableEvent used above. |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 356 | TEST_P(RunLoopTest, QuitFromOtherSequenceRacy) { |
| 357 | Thread other_thread("test"); |
| 358 | other_thread.Start(); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 359 | scoped_refptr<SequencedTaskRunner> other_sequence = |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 360 | other_thread.task_runner(); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 361 | |
| 362 | // Always expected to run before asynchronous Quit() kicks in. |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 363 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 364 | MakeExpectedRunClosure(FROM_HERE)); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 365 | |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 366 | other_sequence->PostTask(FROM_HERE, run_loop_.QuitClosure()); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 367 | |
| 368 | run_loop_.Run(); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | // Verify that QuitClosure can be executed from another sequence even when the |
| 372 | // Quit is racing with Run() -- i.e. forgo the WaitableEvent used above. |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 373 | TEST_P(RunLoopTest, QuitFromOtherSequenceRacyWithClosure) { |
| 374 | Thread other_thread("test"); |
| 375 | other_thread.Start(); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 376 | scoped_refptr<SequencedTaskRunner> other_sequence = |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 377 | other_thread.task_runner(); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 378 | |
| 379 | // Always expected to run before asynchronous Quit() kicks in. |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 380 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 381 | MakeExpectedRunClosure(FROM_HERE)); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 382 | |
| 383 | other_sequence->PostTask(FROM_HERE, run_loop_.QuitClosure()); |
| 384 | |
| 385 | run_loop_.Run(); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // Verify that QuitWhenIdle can be executed from another sequence. |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 389 | TEST_P(RunLoopTest, QuitWhenIdleFromOtherSequence) { |
| 390 | Thread other_thread("test"); |
| 391 | other_thread.Start(); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 392 | scoped_refptr<SequencedTaskRunner> other_sequence = |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 393 | other_thread.task_runner(); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 394 | |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 395 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 396 | MakeExpectedRunClosure(FROM_HERE)); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 397 | |
| 398 | other_sequence->PostTask( |
| 399 | FROM_HERE, |
| 400 | base::BindOnce([](RunLoop* run_loop) { run_loop->QuitWhenIdle(); }, |
| 401 | Unretained(&run_loop_))); |
| 402 | |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 403 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 404 | MakeExpectedRunClosure(FROM_HERE)); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 405 | |
| 406 | run_loop_.Run(); |
| 407 | |
| 408 | // Regardless of the outcome of the race this thread shouldn't have been idle |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 409 | // until both tasks posted to this sequence have run. |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | // Verify that QuitWhenIdleClosure can be executed from another sequence. |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 413 | TEST_P(RunLoopTest, QuitWhenIdleFromOtherSequenceWithClosure) { |
| 414 | Thread other_thread("test"); |
| 415 | other_thread.Start(); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 416 | scoped_refptr<SequencedTaskRunner> other_sequence = |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 417 | other_thread.task_runner(); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 418 | |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 419 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 420 | MakeExpectedRunClosure(FROM_HERE)); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 421 | |
| 422 | other_sequence->PostTask(FROM_HERE, run_loop_.QuitWhenIdleClosure()); |
| 423 | |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 424 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 425 | MakeExpectedRunClosure(FROM_HERE)); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 426 | |
| 427 | run_loop_.Run(); |
| 428 | |
| 429 | // Regardless of the outcome of the race this thread shouldn't have been idle |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 430 | // until the both tasks posted to this sequence have run. |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 431 | } |
| 432 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 433 | TEST_P(RunLoopTest, IsRunningOnCurrentThread) { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 434 | EXPECT_FALSE(RunLoop::IsRunningOnCurrentThread()); |
| 435 | ThreadTaskRunnerHandle::Get()->PostTask( |
| 436 | FROM_HERE, |
tzik | 0c100dc | 2017-06-26 06:13:17 | [diff] [blame] | 437 | BindOnce([]() { EXPECT_TRUE(RunLoop::IsRunningOnCurrentThread()); })); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 438 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_loop_.QuitClosure()); |
| 439 | run_loop_.Run(); |
| 440 | } |
| 441 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 442 | TEST_P(RunLoopTest, IsNestedOnCurrentThread) { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 443 | EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread()); |
| 444 | |
| 445 | ThreadTaskRunnerHandle::Get()->PostTask( |
tzik | 0c100dc | 2017-06-26 06:13:17 | [diff] [blame] | 446 | FROM_HERE, BindOnce([]() { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 447 | EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread()); |
| 448 | |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 449 | RunLoop nested_run_loop(RunLoop::Type::kNestableTasksAllowed); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 450 | |
| 451 | ThreadTaskRunnerHandle::Get()->PostTask( |
tzik | 0c100dc | 2017-06-26 06:13:17 | [diff] [blame] | 452 | FROM_HERE, BindOnce([]() { |
| 453 | EXPECT_TRUE(RunLoop::IsNestedOnCurrentThread()); |
| 454 | })); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 455 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 456 | nested_run_loop.QuitClosure()); |
| 457 | |
| 458 | EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread()); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 459 | nested_run_loop.Run(); |
| 460 | EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread()); |
| 461 | })); |
| 462 | |
| 463 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_loop_.QuitClosure()); |
| 464 | run_loop_.Run(); |
| 465 | } |
| 466 | |
Francois Doray | 80bdddf | 2018-01-04 16:17:32 | [diff] [blame] | 467 | namespace { |
| 468 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 469 | class MockNestingObserver : public RunLoop::NestingObserver { |
| 470 | public: |
| 471 | MockNestingObserver() = default; |
| 472 | |
| 473 | // RunLoop::NestingObserver: |
| 474 | MOCK_METHOD0(OnBeginNestedRunLoop, void()); |
Francois Doray | 80bdddf | 2018-01-04 16:17:32 | [diff] [blame] | 475 | MOCK_METHOD0(OnExitNestedRunLoop, void()); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 476 | |
| 477 | private: |
| 478 | DISALLOW_COPY_AND_ASSIGN(MockNestingObserver); |
| 479 | }; |
| 480 | |
Francois Doray | 80bdddf | 2018-01-04 16:17:32 | [diff] [blame] | 481 | class MockTask { |
| 482 | public: |
| 483 | MockTask() = default; |
| 484 | MOCK_METHOD0(Task, void()); |
| 485 | |
| 486 | private: |
| 487 | DISALLOW_COPY_AND_ASSIGN(MockTask); |
| 488 | }; |
| 489 | |
| 490 | } // namespace |
| 491 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 492 | TEST_P(RunLoopTest, NestingObservers) { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 493 | testing::StrictMock<MockNestingObserver> nesting_observer; |
Francois Doray | 80bdddf | 2018-01-04 16:17:32 | [diff] [blame] | 494 | testing::StrictMock<MockTask> mock_task_a; |
| 495 | testing::StrictMock<MockTask> mock_task_b; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 496 | |
| 497 | RunLoop::AddNestingObserverOnCurrentThread(&nesting_observer); |
| 498 | |
| 499 | const RepeatingClosure run_nested_loop = Bind([]() { |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 500 | RunLoop nested_run_loop(RunLoop::Type::kNestableTasksAllowed); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 501 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 502 | nested_run_loop.QuitClosure()); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 503 | nested_run_loop.Run(); |
| 504 | }); |
| 505 | |
Francois Doray | 80bdddf | 2018-01-04 16:17:32 | [diff] [blame] | 506 | // Generate a stack of nested RunLoops. OnBeginNestedRunLoop() is expected |
| 507 | // when beginning each nesting depth and OnExitNestedRunLoop() is expected |
Gabriel Charette | d883944 | 2018-03-15 18:56:22 | [diff] [blame] | 508 | // when exiting each nesting depth. Each one of these tasks is ahead of the |
| 509 | // QuitClosures as those are only posted at the end of the queue when |
| 510 | // |run_nested_loop| is executed. |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 511 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_nested_loop); |
Francois Doray | 80bdddf | 2018-01-04 16:17:32 | [diff] [blame] | 512 | ThreadTaskRunnerHandle::Get()->PostTask( |
| 513 | FROM_HERE, |
| 514 | base::BindOnce(&MockTask::Task, base::Unretained(&mock_task_a))); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 515 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_nested_loop); |
Francois Doray | 80bdddf | 2018-01-04 16:17:32 | [diff] [blame] | 516 | ThreadTaskRunnerHandle::Get()->PostTask( |
| 517 | FROM_HERE, |
| 518 | base::BindOnce(&MockTask::Task, base::Unretained(&mock_task_b))); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 519 | |
Francois Doray | 80bdddf | 2018-01-04 16:17:32 | [diff] [blame] | 520 | { |
| 521 | testing::InSequence in_sequence; |
| 522 | EXPECT_CALL(nesting_observer, OnBeginNestedRunLoop()); |
| 523 | EXPECT_CALL(mock_task_a, Task()); |
| 524 | EXPECT_CALL(nesting_observer, OnBeginNestedRunLoop()); |
| 525 | EXPECT_CALL(mock_task_b, Task()); |
| 526 | EXPECT_CALL(nesting_observer, OnExitNestedRunLoop()).Times(2); |
| 527 | } |
| 528 | run_loop_.RunUntilIdle(); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 529 | |
| 530 | RunLoop::RemoveNestingObserverOnCurrentThread(&nesting_observer); |
| 531 | } |
| 532 | |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 533 | TEST_P(RunLoopTest, DisallowRunningForTesting) { |
| 534 | RunLoop::ScopedDisallowRunningForTesting disallow_running; |
Gabriel Charette | f3fd8f0 | 2018-05-22 15:31:48 | [diff] [blame] | 535 | EXPECT_DCHECK_DEATH({ run_loop_.RunUntilIdle(); }); |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | TEST_P(RunLoopTest, ExpiredDisallowRunningForTesting) { |
| 539 | { RunLoop::ScopedDisallowRunningForTesting disallow_running; } |
| 540 | // Running should be fine after |disallow_running| goes out of scope. |
| 541 | run_loop_.RunUntilIdle(); |
| 542 | } |
| 543 | |
Victor Costan | 033b9ac | 2019-01-29 00:52:16 | [diff] [blame] | 544 | INSTANTIATE_TEST_SUITE_P(Real, |
| 545 | RunLoopTest, |
| 546 | testing::Values(RunLoopTestType::kRealEnvironment)); |
| 547 | INSTANTIATE_TEST_SUITE_P(Mock, |
| 548 | RunLoopTest, |
| 549 | testing::Values(RunLoopTestType::kTestDelegate)); |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 550 | |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 551 | TEST(ScopedRunTimeoutForTestTest, TimesOut) { |
| 552 | test::ScopedTaskEnvironment task_environment; |
| 553 | RunLoop run_loop; |
| 554 | |
| 555 | static constexpr auto kArbitraryTimeout = |
| 556 | base::TimeDelta::FromMilliseconds(10); |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 557 | RunLoop::ScopedRunTimeoutForTest run_timeout( |
| 558 | kArbitraryTimeout, MakeExpectedRunAtLeastOnceClosure(FROM_HERE)); |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 559 | |
| 560 | // Since the delayed task will be posted only after the message pump starts |
| 561 | // running, the ScopedRunTimeoutForTest will already have started to elapse, |
| 562 | // so if Run() exits at the correct time then our delayed task will not run. |
| 563 | SequencedTaskRunnerHandle::Get()->PostTask( |
| 564 | FROM_HERE, |
| 565 | base::BindOnce(base::IgnoreResult(&SequencedTaskRunner::PostDelayedTask), |
| 566 | SequencedTaskRunnerHandle::Get(), FROM_HERE, |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 567 | MakeExpectedNotRunClosure(FROM_HERE), kArbitraryTimeout)); |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 568 | |
| 569 | // This task should get to run before Run() times-out. |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 570 | SequencedTaskRunnerHandle::Get()->PostDelayedTask( |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 571 | FROM_HERE, MakeExpectedRunClosure(FROM_HERE), kArbitraryTimeout); |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 572 | |
| 573 | run_loop.Run(); |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | TEST(ScopedRunTimeoutForTestTest, RunTasksUntilTimeout) { |
| 577 | test::ScopedTaskEnvironment task_environment; |
| 578 | RunLoop run_loop; |
| 579 | |
| 580 | static constexpr auto kArbitraryTimeout = |
| 581 | base::TimeDelta::FromMilliseconds(10); |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 582 | RunLoop::ScopedRunTimeoutForTest run_timeout( |
| 583 | kArbitraryTimeout, MakeExpectedRunAtLeastOnceClosure(FROM_HERE)); |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 584 | |
| 585 | // Posting a task with the same delay as our timeout, immediately before |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 586 | // calling Run(), means it should get to run. Since this uses QuitWhenIdle(), |
| 587 | // the Run() timeout callback should also get to run. |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 588 | SequencedTaskRunnerHandle::Get()->PostDelayedTask( |
Wez | bbffcc5 | 2019-02-21 02:01:20 | [diff] [blame] | 589 | FROM_HERE, MakeExpectedRunClosure(FROM_HERE), kArbitraryTimeout); |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 590 | |
| 591 | run_loop.Run(); |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 592 | } |
| 593 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 594 | TEST(RunLoopDeathTest, MustRegisterBeforeInstantiating) { |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 595 | TestBoundDelegate unbound_test_delegate_; |
Wez | 39ee610 | 2018-04-14 21:33:59 | [diff] [blame] | 596 | // RunLoop::RunLoop() should CHECK fetching the ThreadTaskRunnerHandle. |
| 597 | EXPECT_DEATH_IF_SUPPORTED({ RunLoop(); }, ""); |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 598 | } |
| 599 | |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 600 | TEST(RunLoopDelegateTest, NestableTasksDontRunInDefaultNestedLoops) { |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 601 | TestBoundDelegate test_delegate; |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 602 | test_delegate.BindToCurrentThread(); |
| 603 | |
| 604 | base::Thread other_thread("test"); |
| 605 | other_thread.Start(); |
| 606 | |
| 607 | RunLoop main_loop; |
| 608 | // A nested run loop which isn't kNestableTasksAllowed. |
| 609 | RunLoop nested_run_loop(RunLoop::Type::kDefault); |
| 610 | |
| 611 | bool nested_run_loop_ended = false; |
| 612 | |
| 613 | // The first task on the main loop will result in a nested run loop. Since |
| 614 | // it's not kNestableTasksAllowed, no further task should be processed until |
| 615 | // it's quit. |
| 616 | ThreadTaskRunnerHandle::Get()->PostTask( |
| 617 | FROM_HERE, |
| 618 | BindOnce([](RunLoop* nested_run_loop) { nested_run_loop->Run(); }, |
| 619 | Unretained(&nested_run_loop))); |
| 620 | |
| 621 | // Post a task that will fail if it runs inside the nested run loop. |
| 622 | ThreadTaskRunnerHandle::Get()->PostTask( |
jdoerrie | 9d7236f6 | 2019-03-05 13:00:23 | [diff] [blame^] | 623 | FROM_HERE, |
| 624 | BindOnce( |
| 625 | [](const bool& nested_run_loop_ended, |
| 626 | OnceClosure continuation_callback) { |
| 627 | EXPECT_TRUE(nested_run_loop_ended); |
| 628 | EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread()); |
| 629 | std::move(continuation_callback).Run(); |
| 630 | }, |
| 631 | std::cref(nested_run_loop_ended), main_loop.QuitClosure())); |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 632 | |
| 633 | // Post a task flipping the boolean bit for extra verification right before |
| 634 | // quitting |nested_run_loop|. |
| 635 | other_thread.task_runner()->PostDelayedTask( |
| 636 | FROM_HERE, |
| 637 | BindOnce( |
| 638 | [](bool* nested_run_loop_ended) { |
| 639 | EXPECT_FALSE(*nested_run_loop_ended); |
| 640 | *nested_run_loop_ended = true; |
| 641 | }, |
| 642 | Unretained(&nested_run_loop_ended)), |
| 643 | TestTimeouts::tiny_timeout()); |
| 644 | // Post an async delayed task to exit the run loop when idle. This confirms |
| 645 | // that (1) the test task only ran in the main loop after the nested loop |
| 646 | // exited and (2) the nested run loop actually considers itself idle while |
| 647 | // spinning. Note: The quit closure needs to be injected directly on the |
| 648 | // delegate as invoking QuitWhenIdle() off-thread results in a thread bounce |
| 649 | // which will not processed because of the very logic under test (nestable |
| 650 | // tasks don't run in |nested_run_loop|). |
| 651 | other_thread.task_runner()->PostDelayedTask( |
| 652 | FROM_HERE, |
| 653 | BindOnce( |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 654 | [](TestBoundDelegate* test_delegate, OnceClosure injected_closure) { |
| 655 | test_delegate->InjectClosureOnDelegate(std::move(injected_closure)); |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 656 | }, |
| 657 | Unretained(&test_delegate), nested_run_loop.QuitWhenIdleClosure()), |
| 658 | TestTimeouts::tiny_timeout()); |
| 659 | |
| 660 | main_loop.Run(); |
| 661 | } |
| 662 | |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 663 | } // namespace base |