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