Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [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 | |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 5 | #include "base/message_loop/message_pump_glib.h" |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 6 | |
[email protected] | d90efd5 | 2012-06-28 19:57:26 | [diff] [blame] | 7 | #include <glib.h> |
[email protected] | 23d3539 | 2009-06-15 19:53:08 | [diff] [blame] | 8 | #include <math.h> |
| 9 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 10 | #include <algorithm> |
| 11 | #include <vector> |
[email protected] | 23d3539 | 2009-06-15 19:53:08 | [diff] [blame] | 12 | |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 13 | #include "base/bind.h" |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 14 | #include "base/callback.h" |
danakj | db9ae794 | 2020-11-11 16:01:35 | [diff] [blame] | 15 | #include "base/callback_helpers.h" |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 16 | #include "base/files/file_util.h" |
Hans Wennborg | afeb390 | 2020-06-17 14:42:29 | [diff] [blame] | 17 | #include "base/logging.h" |
Carlos Caballero | f5b6f91c | 2019-12-20 14:07:38 | [diff] [blame] | 18 | #include "base/memory/ptr_util.h" |
Keishi Hattori | f28f4f8 | 2022-06-21 11:32:15 | [diff] [blame] | 19 | #include "base/memory/raw_ptr.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 20 | #include "base/memory/ref_counted.h" |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 21 | #include "base/message_loop/message_pump_type.h" |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 22 | #include "base/posix/eintr_wrapper.h" |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 23 | #include "base/run_loop.h" |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 24 | #include "base/synchronization/waitable_event.h" |
| 25 | #include "base/synchronization/waitable_event_watcher.h" |
Carlos Caballero | b25fe847 | 2020-07-17 10:27:17 | [diff] [blame] | 26 | #include "base/task/current_thread.h" |
Carlos Caballero | f5b6f91c | 2019-12-20 14:07:38 | [diff] [blame] | 27 | #include "base/task/single_thread_task_executor.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 28 | #include "base/task/single_thread_task_runner.h" |
Carlos Caballero | f5b6f91c | 2019-12-20 14:07:38 | [diff] [blame] | 29 | #include "base/test/task_environment.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 30 | #include "base/threading/thread.h" |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 31 | #include "testing/gtest/include/gtest/gtest.h" |
| 32 | |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 33 | namespace base { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 34 | namespace { |
| 35 | |
| 36 | // This class injects dummy "events" into the GLib loop. When "handled" these |
| 37 | // events can run tasks. This is intended to mock gtk events (the corresponding |
| 38 | // GLib source runs at the same priority). |
| 39 | class EventInjector { |
| 40 | public: |
| 41 | EventInjector() : processed_events_(0) { |
| 42 | source_ = static_cast<Source*>(g_source_new(&SourceFuncs, sizeof(Source))); |
| 43 | source_->injector = this; |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 44 | g_source_attach(source_, nullptr); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 45 | g_source_set_can_recurse(source_, TRUE); |
| 46 | } |
| 47 | |
Peter Boström | 7319bbd | 2021-09-15 22:59:38 | [diff] [blame] | 48 | EventInjector(const EventInjector&) = delete; |
| 49 | EventInjector& operator=(const EventInjector&) = delete; |
| 50 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 51 | ~EventInjector() { |
| 52 | g_source_destroy(source_); |
Tom Sepez | 2a262d0c | 2022-12-22 02:01:19 | [diff] [blame] | 53 | g_source_unref(source_.ExtractAsDangling()); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | int HandlePrepare() { |
| 57 | // If the queue is empty, block. |
| 58 | if (events_.empty()) |
| 59 | return -1; |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 60 | TimeDelta delta = events_[0].time - Time::NowFromSystemTime(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 61 | return std::max(0, static_cast<int>(ceil(delta.InMillisecondsF()))); |
| 62 | } |
| 63 | |
| 64 | bool HandleCheck() { |
| 65 | if (events_.empty()) |
| 66 | return false; |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 67 | return events_[0].time <= Time::NowFromSystemTime(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void HandleDispatch() { |
| 71 | if (events_.empty()) |
| 72 | return; |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 73 | Event event = std::move(events_[0]); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 74 | events_.erase(events_.begin()); |
| 75 | ++processed_events_; |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 76 | if (!event.callback.is_null()) |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 77 | std::move(event.callback).Run(); |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 78 | else if (!event.task.is_null()) |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 79 | std::move(event.task).Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 80 | } |
| 81 | |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 82 | // Adds an event to the queue. When "handled", executes |callback|. |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 83 | // delay_ms is relative to the last event if any, or to Now() otherwise. |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 84 | void AddEvent(int delay_ms, OnceClosure callback) { |
| 85 | AddEventHelper(delay_ms, std::move(callback), OnceClosure()); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void AddDummyEvent(int delay_ms) { |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 89 | AddEventHelper(delay_ms, OnceClosure(), OnceClosure()); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 90 | } |
| 91 | |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 92 | void AddEventAsTask(int delay_ms, OnceClosure task) { |
| 93 | AddEventHelper(delay_ms, OnceClosure(), std::move(task)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void Reset() { |
| 97 | processed_events_ = 0; |
| 98 | events_.clear(); |
| 99 | } |
| 100 | |
| 101 | int processed_events() const { return processed_events_; } |
| 102 | |
| 103 | private: |
| 104 | struct Event { |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 105 | Time time; |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 106 | OnceClosure callback; |
| 107 | OnceClosure task; |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | struct Source : public GSource { |
Keishi Hattori | f28f4f8 | 2022-06-21 11:32:15 | [diff] [blame] | 111 | raw_ptr<EventInjector> injector; |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 112 | }; |
| 113 | |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 114 | void AddEventHelper(int delay_ms, OnceClosure callback, OnceClosure task) { |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 115 | Time last_time; |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 116 | if (!events_.empty()) |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 117 | last_time = (events_.end()-1)->time; |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 118 | else |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 119 | last_time = Time::NowFromSystemTime(); |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 120 | |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 121 | Time future = last_time + Milliseconds(delay_ms); |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 122 | EventInjector::Event event = {future, std::move(callback), std::move(task)}; |
| 123 | events_.push_back(std::move(event)); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 124 | } |
| 125 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 126 | static gboolean Prepare(GSource* source, gint* timeout_ms) { |
| 127 | *timeout_ms = static_cast<Source*>(source)->injector->HandlePrepare(); |
| 128 | return FALSE; |
| 129 | } |
| 130 | |
| 131 | static gboolean Check(GSource* source) { |
| 132 | return static_cast<Source*>(source)->injector->HandleCheck(); |
| 133 | } |
| 134 | |
| 135 | static gboolean Dispatch(GSource* source, |
| 136 | GSourceFunc unused_func, |
| 137 | gpointer unused_data) { |
| 138 | static_cast<Source*>(source)->injector->HandleDispatch(); |
| 139 | return TRUE; |
| 140 | } |
| 141 | |
Keishi Hattori | f28f4f8 | 2022-06-21 11:32:15 | [diff] [blame] | 142 | raw_ptr<Source> source_; |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 143 | std::vector<Event> events_; |
| 144 | int processed_events_; |
| 145 | static GSourceFuncs SourceFuncs; |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 146 | }; |
| 147 | |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 148 | GSourceFuncs EventInjector::SourceFuncs = {EventInjector::Prepare, |
| 149 | EventInjector::Check, |
| 150 | EventInjector::Dispatch, nullptr}; |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 151 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 152 | void IncrementInt(int *value) { |
| 153 | ++*value; |
| 154 | } |
| 155 | |
| 156 | // Checks how many events have been processed by the injector. |
| 157 | void ExpectProcessedEvents(EventInjector* injector, int count) { |
| 158 | EXPECT_EQ(injector->processed_events(), count); |
| 159 | } |
| 160 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 161 | // Posts a task on the current message loop. |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 162 | void PostMessageLoopTask(const Location& from_here, OnceClosure task) { |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 163 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask(from_here, |
| 164 | std::move(task)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // Test fixture. |
| 168 | class MessagePumpGLibTest : public testing::Test { |
| 169 | public: |
Carlos Caballero | f5b6f91c | 2019-12-20 14:07:38 | [diff] [blame] | 170 | MessagePumpGLibTest() = default; |
Peter Boström | 75cd3c0 | 2021-09-28 15:23:18 | [diff] [blame] | 171 | |
| 172 | MessagePumpGLibTest(const MessagePumpGLibTest&) = delete; |
| 173 | MessagePumpGLibTest& operator=(const MessagePumpGLibTest&) = delete; |
| 174 | |
Carlos Caballero | f5b6f91c | 2019-12-20 14:07:38 | [diff] [blame] | 175 | EventInjector* injector() { return &injector_; } |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 176 | |
| 177 | private: |
Carlos Caballero | f5b6f91c | 2019-12-20 14:07:38 | [diff] [blame] | 178 | test::SingleThreadTaskEnvironment task_environment_{ |
| 179 | test::SingleThreadTaskEnvironment::MainThreadType::UI}; |
| 180 | EventInjector injector_; |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | } // namespace |
| 184 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 185 | TEST_F(MessagePumpGLibTest, TestQuit) { |
| 186 | // Checks that Quit works and that the basic infrastructure is working. |
| 187 | |
| 188 | // Quit from a task |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 189 | RunLoop().RunUntilIdle(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 190 | EXPECT_EQ(0, injector()->processed_events()); |
| 191 | |
| 192 | injector()->Reset(); |
| 193 | // Quit from an event |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 194 | RunLoop run_loop; |
| 195 | injector()->AddEvent(0, run_loop.QuitClosure()); |
| 196 | run_loop.Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 197 | EXPECT_EQ(1, injector()->processed_events()); |
| 198 | } |
| 199 | |
| 200 | TEST_F(MessagePumpGLibTest, TestEventTaskInterleave) { |
| 201 | // Checks that tasks posted by events are executed before the next event if |
| 202 | // the posted task queue is empty. |
| 203 | // MessageLoop doesn't make strong guarantees that it is the case, but the |
| 204 | // current implementation ensures it and the tests below rely on it. |
| 205 | // If changes cause this test to fail, it is reasonable to change it, but |
| 206 | // TestWorkWhileWaitingForEvents and TestEventsWhileWaitingForWork have to be |
| 207 | // changed accordingly, otherwise they can become flaky. |
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 208 | injector()->AddEventAsTask(0, DoNothing()); |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 209 | OnceClosure check_task = |
| 210 | BindOnce(&ExpectProcessedEvents, Unretained(injector()), 2); |
| 211 | OnceClosure posted_task = |
| 212 | BindOnce(&PostMessageLoopTask, FROM_HERE, std::move(check_task)); |
| 213 | injector()->AddEventAsTask(0, std::move(posted_task)); |
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 214 | injector()->AddEventAsTask(0, DoNothing()); |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 215 | { |
| 216 | RunLoop run_loop; |
| 217 | injector()->AddEvent(0, run_loop.QuitClosure()); |
| 218 | run_loop.Run(); |
| 219 | } |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 220 | EXPECT_EQ(4, injector()->processed_events()); |
| 221 | |
| 222 | injector()->Reset(); |
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 223 | injector()->AddEventAsTask(0, DoNothing()); |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 224 | check_task = BindOnce(&ExpectProcessedEvents, Unretained(injector()), 2); |
| 225 | posted_task = |
| 226 | BindOnce(&PostMessageLoopTask, FROM_HERE, std::move(check_task)); |
| 227 | injector()->AddEventAsTask(0, std::move(posted_task)); |
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 228 | injector()->AddEventAsTask(10, DoNothing()); |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 229 | { |
| 230 | RunLoop run_loop; |
| 231 | injector()->AddEvent(0, run_loop.QuitClosure()); |
| 232 | run_loop.Run(); |
| 233 | } |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 234 | EXPECT_EQ(4, injector()->processed_events()); |
| 235 | } |
| 236 | |
| 237 | TEST_F(MessagePumpGLibTest, TestWorkWhileWaitingForEvents) { |
| 238 | int task_count = 0; |
| 239 | // Tests that we process tasks while waiting for new events. |
| 240 | // The event queue is empty at first. |
| 241 | for (int i = 0; i < 10; ++i) { |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 242 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
Carlos Caballero | f5b6f91c | 2019-12-20 14:07:38 | [diff] [blame] | 243 | FROM_HERE, BindOnce(&IncrementInt, &task_count)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 244 | } |
| 245 | // After all the previous tasks have executed, enqueue an event that will |
| 246 | // quit. |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 247 | { |
| 248 | RunLoop run_loop; |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 249 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 250 | FROM_HERE, BindOnce(&EventInjector::AddEvent, Unretained(injector()), 0, |
| 251 | run_loop.QuitClosure())); |
| 252 | run_loop.Run(); |
| 253 | } |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 254 | ASSERT_EQ(10, task_count); |
| 255 | EXPECT_EQ(1, injector()->processed_events()); |
| 256 | |
| 257 | // Tests that we process delayed tasks while waiting for new events. |
| 258 | injector()->Reset(); |
| 259 | task_count = 0; |
| 260 | for (int i = 0; i < 10; ++i) { |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 261 | SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask( |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 262 | FROM_HERE, BindOnce(&IncrementInt, &task_count), Milliseconds(10 * i)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 263 | } |
| 264 | // After all the previous tasks have executed, enqueue an event that will |
| 265 | // quit. |
| 266 | // This relies on the fact that delayed tasks are executed in delay order. |
| 267 | // That is verified in message_loop_unittest.cc. |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 268 | { |
| 269 | RunLoop run_loop; |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 270 | SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask( |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 271 | FROM_HERE, |
| 272 | BindOnce(&EventInjector::AddEvent, Unretained(injector()), 0, |
| 273 | run_loop.QuitClosure()), |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 274 | Milliseconds(150)); |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 275 | run_loop.Run(); |
| 276 | } |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 277 | ASSERT_EQ(10, task_count); |
| 278 | EXPECT_EQ(1, injector()->processed_events()); |
| 279 | } |
| 280 | |
| 281 | TEST_F(MessagePumpGLibTest, TestEventsWhileWaitingForWork) { |
| 282 | // Tests that we process events while waiting for work. |
| 283 | // The event queue is empty at first. |
| 284 | for (int i = 0; i < 10; ++i) { |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 285 | injector()->AddDummyEvent(0); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 286 | } |
| 287 | // After all the events have been processed, post a task that will check that |
| 288 | // the events have been processed (note: the task executes after the event |
| 289 | // that posted it has been handled, so we expect 11 at that point). |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 290 | OnceClosure check_task = |
| 291 | BindOnce(&ExpectProcessedEvents, Unretained(injector()), 11); |
| 292 | OnceClosure posted_task = |
| 293 | BindOnce(&PostMessageLoopTask, FROM_HERE, std::move(check_task)); |
| 294 | injector()->AddEventAsTask(10, std::move(posted_task)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 295 | |
| 296 | // And then quit (relies on the condition tested by TestEventTaskInterleave). |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 297 | RunLoop run_loop; |
| 298 | injector()->AddEvent(10, run_loop.QuitClosure()); |
| 299 | run_loop.Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 300 | |
| 301 | EXPECT_EQ(12, injector()->processed_events()); |
| 302 | } |
| 303 | |
| 304 | namespace { |
| 305 | |
| 306 | // This class is a helper for the concurrent events / posted tasks test below. |
| 307 | // It will quit the main loop once enough tasks and events have been processed, |
| 308 | // while making sure there is always work to do and events in the queue. |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 309 | class ConcurrentHelper : public RefCounted<ConcurrentHelper> { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 310 | public: |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 311 | ConcurrentHelper(EventInjector* injector, OnceClosure done_closure) |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 312 | : injector_(injector), |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 313 | done_closure_(std::move(done_closure)), |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 314 | event_count_(kStartingEventCount), |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 315 | task_count_(kStartingTaskCount) {} |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 316 | |
| 317 | void FromTask() { |
| 318 | if (task_count_ > 0) { |
| 319 | --task_count_; |
| 320 | } |
| 321 | if (task_count_ == 0 && event_count_ == 0) { |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 322 | std::move(done_closure_).Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 323 | } else { |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 324 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 325 | FROM_HERE, BindOnce(&ConcurrentHelper::FromTask, this)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | void FromEvent() { |
| 330 | if (event_count_ > 0) { |
| 331 | --event_count_; |
| 332 | } |
| 333 | if (task_count_ == 0 && event_count_ == 0) { |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 334 | std::move(done_closure_).Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 335 | } else { |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 336 | injector_->AddEventAsTask(0, |
| 337 | BindOnce(&ConcurrentHelper::FromEvent, this)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | int event_count() const { return event_count_; } |
| 342 | int task_count() const { return task_count_; } |
| 343 | |
| 344 | private: |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 345 | friend class RefCounted<ConcurrentHelper>; |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 346 | |
| 347 | ~ConcurrentHelper() {} |
| 348 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 349 | static const int kStartingEventCount = 20; |
| 350 | static const int kStartingTaskCount = 20; |
| 351 | |
Keishi Hattori | f28f4f8 | 2022-06-21 11:32:15 | [diff] [blame] | 352 | raw_ptr<EventInjector> injector_; |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 353 | OnceClosure done_closure_; |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 354 | int event_count_; |
| 355 | int task_count_; |
| 356 | }; |
| 357 | |
| 358 | } // namespace |
| 359 | |
| 360 | TEST_F(MessagePumpGLibTest, TestConcurrentEventPostedTask) { |
| 361 | // Tests that posted tasks don't starve events, nor the opposite. |
| 362 | // We use the helper class above. We keep both event and posted task queues |
| 363 | // full, the helper verifies that both tasks and events get processed. |
| 364 | // If that is not the case, either event_count_ or task_count_ will not get |
[email protected] | 91cae259 | 2013-01-10 14:56:17 | [diff] [blame] | 365 | // to 0, and MessageLoop::QuitWhenIdle() will never be called. |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 366 | RunLoop run_loop; |
| 367 | scoped_refptr<ConcurrentHelper> helper = |
| 368 | new ConcurrentHelper(injector(), run_loop.QuitClosure()); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 369 | |
| 370 | // Add 2 events to the queue to make sure it is always full (when we remove |
| 371 | // the event before processing it). |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 372 | injector()->AddEventAsTask(0, BindOnce(&ConcurrentHelper::FromEvent, helper)); |
| 373 | injector()->AddEventAsTask(0, BindOnce(&ConcurrentHelper::FromEvent, helper)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 374 | |
| 375 | // Similarly post 2 tasks. |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 376 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 377 | FROM_HERE, BindOnce(&ConcurrentHelper::FromTask, helper)); |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 378 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 379 | FROM_HERE, BindOnce(&ConcurrentHelper::FromTask, helper)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 380 | |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 381 | run_loop.Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 382 | EXPECT_EQ(0, helper->event_count()); |
| 383 | EXPECT_EQ(0, helper->task_count()); |
| 384 | } |
| 385 | |
| 386 | namespace { |
| 387 | |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 388 | void AddEventsAndDrainGLib(EventInjector* injector, OnceClosure on_drained) { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 389 | // Add a couple of dummy events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 390 | injector->AddDummyEvent(0); |
| 391 | injector->AddDummyEvent(0); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 392 | // Then add an event that will quit the main loop. |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 393 | injector->AddEvent(0, std::move(on_drained)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 394 | |
| 395 | // Post a couple of dummy tasks |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 396 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, DoNothing()); |
| 397 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, DoNothing()); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 398 | |
| 399 | // Drain the events |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 400 | while (g_main_context_pending(nullptr)) { |
| 401 | g_main_context_iteration(nullptr, FALSE); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | } // namespace |
| 406 | |
| 407 | TEST_F(MessagePumpGLibTest, TestDrainingGLib) { |
| 408 | // Tests that draining events using GLib works. |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 409 | RunLoop run_loop; |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 410 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 411 | FROM_HERE, BindOnce(&AddEventsAndDrainGLib, Unretained(injector()), |
| 412 | run_loop.QuitClosure())); |
| 413 | run_loop.Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 414 | |
| 415 | EXPECT_EQ(3, injector()->processed_events()); |
| 416 | } |
| 417 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 418 | namespace { |
| 419 | |
| 420 | // Helper class that lets us run the GLib message loop. |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 421 | class GLibLoopRunner : public RefCounted<GLibLoopRunner> { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 422 | public: |
| 423 | GLibLoopRunner() : quit_(false) { } |
| 424 | |
| 425 | void RunGLib() { |
| 426 | while (!quit_) { |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 427 | g_main_context_iteration(nullptr, TRUE); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
[email protected] | 258dca4 | 2011-09-21 00:17:19 | [diff] [blame] | 431 | void RunLoop() { |
[email protected] | 258dca4 | 2011-09-21 00:17:19 | [diff] [blame] | 432 | while (!quit_) { |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 433 | g_main_context_iteration(nullptr, TRUE); |
[email protected] | 258dca4 | 2011-09-21 00:17:19 | [diff] [blame] | 434 | } |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | void Quit() { |
| 438 | quit_ = true; |
| 439 | } |
| 440 | |
| 441 | void Reset() { |
| 442 | quit_ = false; |
| 443 | } |
| 444 | |
| 445 | private: |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 446 | friend class RefCounted<GLibLoopRunner>; |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 447 | |
| 448 | ~GLibLoopRunner() {} |
| 449 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 450 | bool quit_; |
| 451 | }; |
| 452 | |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 453 | void TestGLibLoopInternal(EventInjector* injector, OnceClosure done) { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 454 | scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); |
| 455 | |
| 456 | int task_count = 0; |
| 457 | // Add a couple of dummy events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 458 | injector->AddDummyEvent(0); |
| 459 | injector->AddDummyEvent(0); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 460 | // Post a couple of dummy tasks |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 461 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
| 462 | FROM_HERE, BindOnce(&IncrementInt, &task_count)); |
| 463 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
| 464 | FROM_HERE, BindOnce(&IncrementInt, &task_count)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 465 | // Delayed events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 466 | injector->AddDummyEvent(10); |
| 467 | injector->AddDummyEvent(10); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 468 | // Delayed work |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 469 | SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask( |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 470 | FROM_HERE, BindOnce(&IncrementInt, &task_count), Milliseconds(30)); |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 471 | SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask( |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 472 | FROM_HERE, BindOnce(&GLibLoopRunner::Quit, runner), Milliseconds(40)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 473 | |
| 474 | // Run a nested, straight GLib message loop. |
Gabriel Charette | bdbb58e7 | 2020-06-03 18:26:06 | [diff] [blame] | 475 | { |
Francois Doray | a06ee17 | 2022-11-24 21:09:18 | [diff] [blame] | 476 | CurrentThread::ScopedAllowApplicationTasksInNativeNestedLoop allow; |
Gabriel Charette | bdbb58e7 | 2020-06-03 18:26:06 | [diff] [blame] | 477 | runner->RunGLib(); |
| 478 | } |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 479 | |
| 480 | ASSERT_EQ(3, task_count); |
| 481 | EXPECT_EQ(4, injector->processed_events()); |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 482 | std::move(done).Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 483 | } |
| 484 | |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 485 | void TestGtkLoopInternal(EventInjector* injector, OnceClosure done) { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 486 | scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); |
| 487 | |
| 488 | int task_count = 0; |
| 489 | // Add a couple of dummy events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 490 | injector->AddDummyEvent(0); |
| 491 | injector->AddDummyEvent(0); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 492 | // Post a couple of dummy tasks |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 493 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
| 494 | FROM_HERE, BindOnce(&IncrementInt, &task_count)); |
| 495 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
| 496 | FROM_HERE, BindOnce(&IncrementInt, &task_count)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 497 | // Delayed events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 498 | injector->AddDummyEvent(10); |
| 499 | injector->AddDummyEvent(10); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 500 | // Delayed work |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 501 | SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask( |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 502 | FROM_HERE, BindOnce(&IncrementInt, &task_count), Milliseconds(30)); |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 503 | SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask( |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 504 | FROM_HERE, BindOnce(&GLibLoopRunner::Quit, runner), Milliseconds(40)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 505 | |
| 506 | // Run a nested, straight Gtk message loop. |
Gabriel Charette | bdbb58e7 | 2020-06-03 18:26:06 | [diff] [blame] | 507 | { |
Francois Doray | a06ee17 | 2022-11-24 21:09:18 | [diff] [blame] | 508 | CurrentThread::ScopedAllowApplicationTasksInNativeNestedLoop allow; |
Gabriel Charette | bdbb58e7 | 2020-06-03 18:26:06 | [diff] [blame] | 509 | runner->RunLoop(); |
| 510 | } |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 511 | |
| 512 | ASSERT_EQ(3, task_count); |
| 513 | EXPECT_EQ(4, injector->processed_events()); |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 514 | std::move(done).Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | } // namespace |
| 518 | |
| 519 | TEST_F(MessagePumpGLibTest, TestGLibLoop) { |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 520 | // Tests that events and posted tasks are correctly executed if the message |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 521 | // loop is not run by MessageLoop::Run() but by a straight GLib loop. |
| 522 | // Note that in this case we don't make strong guarantees about niceness |
| 523 | // between events and posted tasks. |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 524 | RunLoop run_loop; |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 525 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 526 | FROM_HERE, BindOnce(&TestGLibLoopInternal, Unretained(injector()), |
| 527 | run_loop.QuitClosure())); |
| 528 | run_loop.Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | TEST_F(MessagePumpGLibTest, TestGtkLoop) { |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 532 | // Tests that events and posted tasks are correctly executed if the message |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 533 | // loop is not run by MessageLoop::Run() but by a straight Gtk loop. |
| 534 | // Note that in this case we don't make strong guarantees about niceness |
| 535 | // between events and posted tasks. |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 536 | RunLoop run_loop; |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 537 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame] | 538 | FROM_HERE, BindOnce(&TestGtkLoopInternal, Unretained(injector()), |
| 539 | run_loop.QuitClosure())); |
| 540 | run_loop.Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 541 | } |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 542 | |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 543 | // Tests for WatchFileDescriptor API |
| 544 | class MessagePumpGLibFdWatchTest : public testing::Test { |
| 545 | protected: |
| 546 | MessagePumpGLibFdWatchTest() |
| 547 | : io_thread_("MessagePumpGLibFdWatchTestIOThread") {} |
| 548 | ~MessagePumpGLibFdWatchTest() override = default; |
| 549 | |
| 550 | void SetUp() override { |
| 551 | Thread::Options options(MessagePumpType::IO, 0); |
Olivier Li | d68d079 | 2021-05-17 20:51:41 | [diff] [blame] | 552 | ASSERT_TRUE(io_thread_.StartWithOptions(std::move(options))); |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 553 | int ret = pipe(pipefds_); |
| 554 | ASSERT_EQ(0, ret); |
| 555 | } |
| 556 | |
| 557 | void TearDown() override { |
Reilly Grant | 6e56f8c | 2022-01-12 02:45:55 | [diff] [blame] | 558 | // Wait for the IO thread to exit before closing FDs which may have been |
| 559 | // passed to it. |
| 560 | io_thread_.Stop(); |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 561 | if (IGNORE_EINTR(close(pipefds_[0])) < 0) |
| 562 | PLOG(ERROR) << "close"; |
| 563 | if (IGNORE_EINTR(close(pipefds_[1])) < 0) |
| 564 | PLOG(ERROR) << "close"; |
| 565 | } |
| 566 | |
| 567 | void WaitUntilIoThreadStarted() { |
| 568 | ASSERT_TRUE(io_thread_.WaitUntilThreadStarted()); |
| 569 | } |
| 570 | |
| 571 | scoped_refptr<SingleThreadTaskRunner> io_runner() const { |
| 572 | return io_thread_.task_runner(); |
| 573 | } |
| 574 | |
| 575 | void SimulateEvent(MessagePumpGlib* pump, |
| 576 | MessagePumpGlib::FdWatchController* controller) { |
| 577 | controller->poll_fd_->revents = G_IO_IN | G_IO_OUT; |
| 578 | pump->HandleFdWatchDispatch(controller); |
| 579 | } |
| 580 | |
| 581 | int pipefds_[2]; |
| 582 | |
| 583 | private: |
| 584 | Thread io_thread_; |
| 585 | }; |
| 586 | |
| 587 | namespace { |
| 588 | |
| 589 | class BaseWatcher : public MessagePumpGlib::FdWatcher { |
| 590 | public: |
| 591 | explicit BaseWatcher(MessagePumpGlib::FdWatchController* controller) |
| 592 | : controller_(controller) { |
| 593 | DCHECK(controller_); |
| 594 | } |
| 595 | ~BaseWatcher() override = default; |
| 596 | |
| 597 | // base:MessagePumpGlib::FdWatcher interface |
| 598 | void OnFileCanReadWithoutBlocking(int /* fd */) override { NOTREACHED(); } |
| 599 | void OnFileCanWriteWithoutBlocking(int /* fd */) override { NOTREACHED(); } |
| 600 | |
| 601 | protected: |
Keishi Hattori | f28f4f8 | 2022-06-21 11:32:15 | [diff] [blame] | 602 | raw_ptr<MessagePumpGlib::FdWatchController> controller_; |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 603 | }; |
| 604 | |
| 605 | class DeleteWatcher : public BaseWatcher { |
| 606 | public: |
| 607 | explicit DeleteWatcher( |
| 608 | std::unique_ptr<MessagePumpGlib::FdWatchController> controller) |
| 609 | : BaseWatcher(controller.get()), |
| 610 | owned_controller_(std::move(controller)) {} |
| 611 | |
| 612 | ~DeleteWatcher() override { DCHECK(!controller_); } |
| 613 | |
Tom Sepez | 2ba31a0 | 2023-01-05 18:39:43 | [diff] [blame] | 614 | bool HasController() const { return !!controller_; } |
| 615 | |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 616 | void OnFileCanWriteWithoutBlocking(int /* fd */) override { |
Tom Sepez | 2ba31a0 | 2023-01-05 18:39:43 | [diff] [blame] | 617 | ClearController(); |
| 618 | } |
| 619 | |
| 620 | protected: |
| 621 | void ClearController() { |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 622 | DCHECK(owned_controller_); |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 623 | controller_ = nullptr; |
Tom Sepez | 2a262d0c | 2022-12-22 02:01:19 | [diff] [blame] | 624 | owned_controller_.reset(); |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | private: |
| 628 | std::unique_ptr<MessagePumpGlib::FdWatchController> owned_controller_; |
| 629 | }; |
| 630 | |
| 631 | class StopWatcher : public BaseWatcher { |
| 632 | public: |
| 633 | explicit StopWatcher(MessagePumpGlib::FdWatchController* controller) |
| 634 | : BaseWatcher(controller) {} |
| 635 | |
| 636 | ~StopWatcher() override = default; |
| 637 | |
| 638 | void OnFileCanWriteWithoutBlocking(int /* fd */) override { |
| 639 | controller_->StopWatchingFileDescriptor(); |
| 640 | } |
| 641 | }; |
| 642 | |
| 643 | void QuitMessageLoopAndStart(OnceClosure quit_closure) { |
| 644 | std::move(quit_closure).Run(); |
| 645 | |
| 646 | RunLoop runloop(RunLoop::Type::kNestableTasksAllowed); |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 647 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, |
| 648 | runloop.QuitClosure()); |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 649 | runloop.Run(); |
| 650 | } |
| 651 | |
| 652 | class NestedPumpWatcher : public MessagePumpGlib::FdWatcher { |
| 653 | public: |
| 654 | NestedPumpWatcher() = default; |
| 655 | ~NestedPumpWatcher() override = default; |
| 656 | |
| 657 | void OnFileCanReadWithoutBlocking(int /* fd */) override { |
| 658 | RunLoop runloop; |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 659 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 660 | FROM_HERE, BindOnce(&QuitMessageLoopAndStart, runloop.QuitClosure())); |
| 661 | runloop.Run(); |
| 662 | } |
| 663 | |
| 664 | void OnFileCanWriteWithoutBlocking(int /* fd */) override {} |
| 665 | }; |
| 666 | |
Tom Sepez | 2ba31a0 | 2023-01-05 18:39:43 | [diff] [blame] | 667 | class QuitWatcher : public DeleteWatcher { |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 668 | public: |
Tom Sepez | 2ba31a0 | 2023-01-05 18:39:43 | [diff] [blame] | 669 | QuitWatcher(std::unique_ptr<MessagePumpGlib::FdWatchController> controller, |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 670 | base::OnceClosure quit_closure) |
Tom Sepez | 2ba31a0 | 2023-01-05 18:39:43 | [diff] [blame] | 671 | : DeleteWatcher(std::move(controller)), |
| 672 | quit_closure_(std::move(quit_closure)) {} |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 673 | |
Tom Sepez | 2ba31a0 | 2023-01-05 18:39:43 | [diff] [blame] | 674 | void OnFileCanReadWithoutBlocking(int fd) override { |
| 675 | ClearController(); |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 676 | if (quit_closure_) |
| 677 | std::move(quit_closure_).Run(); |
| 678 | } |
| 679 | |
| 680 | private: |
| 681 | base::OnceClosure quit_closure_; |
| 682 | }; |
| 683 | |
| 684 | void WriteFDWrapper(const int fd, |
| 685 | const char* buf, |
| 686 | int size, |
| 687 | WaitableEvent* event) { |
Lei Zhang | c9e8a16 | 2021-05-14 09:33:52 | [diff] [blame] | 688 | ASSERT_TRUE(WriteFileDescriptor(fd, StringPiece(buf, size))); |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | } // namespace |
| 692 | |
| 693 | // Tests that MessagePumpGlib::FdWatcher::OnFileCanReadWithoutBlocking is not |
Tom Sepez | 2ba31a0 | 2023-01-05 18:39:43 | [diff] [blame] | 694 | // called for a READ_WRITE event, and that the controller is destroyed in |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 695 | // OnFileCanWriteWithoutBlocking callback. |
| 696 | TEST_F(MessagePumpGLibFdWatchTest, DeleteWatcher) { |
| 697 | auto pump = std::make_unique<MessagePumpGlib>(); |
| 698 | auto controller_ptr = |
| 699 | std::make_unique<MessagePumpGlib::FdWatchController>(FROM_HERE); |
| 700 | auto* controller = controller_ptr.get(); |
| 701 | |
| 702 | DeleteWatcher watcher(std::move(controller_ptr)); |
| 703 | pump->WatchFileDescriptor(pipefds_[1], false, |
| 704 | MessagePumpGlib::WATCH_READ_WRITE, controller, |
| 705 | &watcher); |
| 706 | |
| 707 | SimulateEvent(pump.get(), controller); |
Tom Sepez | 2ba31a0 | 2023-01-05 18:39:43 | [diff] [blame] | 708 | EXPECT_FALSE(watcher.HasController()); |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | // Tests that MessagePumpGlib::FdWatcher::OnFileCanReadWithoutBlocking is not |
| 712 | // called for a READ_WRITE event, when the watcher calls |
| 713 | // StopWatchingFileDescriptor in OnFileCanWriteWithoutBlocking callback. |
| 714 | TEST_F(MessagePumpGLibFdWatchTest, StopWatcher) { |
| 715 | std::unique_ptr<MessagePumpGlib> pump(new MessagePumpGlib); |
| 716 | MessagePumpGlib::FdWatchController controller(FROM_HERE); |
| 717 | StopWatcher watcher(&controller); |
| 718 | pump->WatchFileDescriptor(pipefds_[1], false, |
| 719 | MessagePumpGlib::WATCH_READ_WRITE, &controller, |
| 720 | &watcher); |
| 721 | |
| 722 | SimulateEvent(pump.get(), &controller); |
| 723 | } |
| 724 | |
| 725 | // Tests that FdWatcher works properly with nested loops. |
| 726 | TEST_F(MessagePumpGLibFdWatchTest, NestedPumpWatcher) { |
Carlos Caballero | f5b6f91c | 2019-12-20 14:07:38 | [diff] [blame] | 727 | test::SingleThreadTaskEnvironment task_environment( |
| 728 | test::SingleThreadTaskEnvironment::MainThreadType::UI); |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 729 | std::unique_ptr<MessagePumpGlib> pump(new MessagePumpGlib); |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 730 | NestedPumpWatcher watcher; |
Tom Sepez | 2ba31a0 | 2023-01-05 18:39:43 | [diff] [blame] | 731 | MessagePumpGlib::FdWatchController controller(FROM_HERE); |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 732 | pump->WatchFileDescriptor(pipefds_[1], false, MessagePumpGlib::WATCH_READ, |
| 733 | &controller, &watcher); |
| 734 | |
| 735 | SimulateEvent(pump.get(), &controller); |
| 736 | } |
| 737 | |
| 738 | // Tests that MessagePumpGlib quits immediately when it is quit from |
| 739 | // libevent's event_base_loop(). |
| 740 | TEST_F(MessagePumpGLibFdWatchTest, QuitWatcher) { |
Carlos Caballero | f5b6f91c | 2019-12-20 14:07:38 | [diff] [blame] | 741 | MessagePumpGlib* pump = new MessagePumpGlib(); |
| 742 | SingleThreadTaskExecutor executor(WrapUnique(pump)); |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 743 | RunLoop run_loop; |
Tom Sepez | 2ba31a0 | 2023-01-05 18:39:43 | [diff] [blame] | 744 | |
| 745 | auto owned_controller = |
| 746 | std::make_unique<MessagePumpGlib::FdWatchController>(FROM_HERE); |
| 747 | MessagePumpGlib::FdWatchController* controller = owned_controller.get(); |
| 748 | QuitWatcher delegate(std::move(owned_controller), run_loop.QuitClosure()); |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 749 | |
| 750 | pump->WatchFileDescriptor(pipefds_[0], false, MessagePumpGlib::WATCH_READ, |
Tom Sepez | 2ba31a0 | 2023-01-05 18:39:43 | [diff] [blame] | 751 | controller, &delegate); |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 752 | |
| 753 | // Make the IO thread wait for |event| before writing to pipefds[1]. |
| 754 | const char buf = 0; |
Tom Sepez | 2ba31a0 | 2023-01-05 18:39:43 | [diff] [blame] | 755 | WaitableEvent event; |
| 756 | auto watcher = std::make_unique<WaitableEventWatcher>(); |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 757 | WaitableEventWatcher::EventCallback write_fd_task = |
| 758 | BindOnce(&WriteFDWrapper, pipefds_[1], &buf, 1); |
| 759 | io_runner()->PostTask( |
| 760 | FROM_HERE, BindOnce(IgnoreResult(&WaitableEventWatcher::StartWatching), |
| 761 | Unretained(watcher.get()), &event, |
| 762 | std::move(write_fd_task), io_runner())); |
| 763 | |
Carlos Caballero | b25fe847 | 2020-07-17 10:27:17 | [diff] [blame] | 764 | // Queue |event| to signal on |CurrentUIThread::Get()|. |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 765 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
Nick Diego Yamane | 5891419 | 2019-08-08 17:12:48 | [diff] [blame] | 766 | FROM_HERE, BindOnce(&WaitableEvent::Signal, Unretained(&event))); |
| 767 | |
| 768 | // Now run the MessageLoop. |
| 769 | run_loop.Run(); |
| 770 | |
| 771 | // StartWatching can move |watcher| to IO thread. Release on IO thread. |
| 772 | io_runner()->PostTask(FROM_HERE, BindOnce(&WaitableEventWatcher::StopWatching, |
| 773 | Owned(std::move(watcher)))); |
| 774 | } |
| 775 | |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 776 | } // namespace base |