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