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