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