[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" |
| 14 | #include "base/bind_helpers.h" |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 15 | #include "base/callback.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 16 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 17 | #include "base/memory/ref_counted.h" |
[email protected] | 495cad9 | 2013-07-18 08:12:40 | [diff] [blame] | 18 | #include "base/message_loop/message_loop.h" |
Gabriel Charette | 810f3f43 | 2018-04-27 22:28:14 | [diff] [blame] | 19 | #include "base/message_loop/message_loop_current.h" |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 20 | #include "base/run_loop.h" |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 21 | #include "base/single_thread_task_runner.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 22 | #include "base/threading/thread.h" |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 23 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 24 | #include "testing/gtest/include/gtest/gtest.h" |
| 25 | |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 26 | namespace base { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | // This class injects dummy "events" into the GLib loop. When "handled" these |
| 30 | // events can run tasks. This is intended to mock gtk events (the corresponding |
| 31 | // GLib source runs at the same priority). |
| 32 | class EventInjector { |
| 33 | public: |
| 34 | EventInjector() : processed_events_(0) { |
| 35 | source_ = static_cast<Source*>(g_source_new(&SourceFuncs, sizeof(Source))); |
| 36 | source_->injector = this; |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 37 | g_source_attach(source_, nullptr); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 38 | g_source_set_can_recurse(source_, TRUE); |
| 39 | } |
| 40 | |
| 41 | ~EventInjector() { |
| 42 | g_source_destroy(source_); |
| 43 | g_source_unref(source_); |
| 44 | } |
| 45 | |
| 46 | int HandlePrepare() { |
| 47 | // If the queue is empty, block. |
| 48 | if (events_.empty()) |
| 49 | return -1; |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 50 | TimeDelta delta = events_[0].time - Time::NowFromSystemTime(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 51 | return std::max(0, static_cast<int>(ceil(delta.InMillisecondsF()))); |
| 52 | } |
| 53 | |
| 54 | bool HandleCheck() { |
| 55 | if (events_.empty()) |
| 56 | return false; |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 57 | return events_[0].time <= Time::NowFromSystemTime(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void HandleDispatch() { |
| 61 | if (events_.empty()) |
| 62 | return; |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 63 | Event event = std::move(events_[0]); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 64 | events_.erase(events_.begin()); |
| 65 | ++processed_events_; |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 66 | if (!event.callback.is_null()) |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 67 | std::move(event.callback).Run(); |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 68 | else if (!event.task.is_null()) |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 69 | std::move(event.task).Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 72 | // Adds an event to the queue. When "handled", executes |callback|. |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 73 | // delay_ms is relative to the last event if any, or to Now() otherwise. |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 74 | void AddEvent(int delay_ms, OnceClosure callback) { |
| 75 | AddEventHelper(delay_ms, std::move(callback), OnceClosure()); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void AddDummyEvent(int delay_ms) { |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 79 | AddEventHelper(delay_ms, OnceClosure(), OnceClosure()); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 80 | } |
| 81 | |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 82 | void AddEventAsTask(int delay_ms, OnceClosure task) { |
| 83 | AddEventHelper(delay_ms, OnceClosure(), std::move(task)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void Reset() { |
| 87 | processed_events_ = 0; |
| 88 | events_.clear(); |
| 89 | } |
| 90 | |
| 91 | int processed_events() const { return processed_events_; } |
| 92 | |
| 93 | private: |
| 94 | struct Event { |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 95 | Time time; |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 96 | OnceClosure callback; |
| 97 | OnceClosure task; |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | struct Source : public GSource { |
| 101 | EventInjector* injector; |
| 102 | }; |
| 103 | |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 104 | void AddEventHelper(int delay_ms, OnceClosure callback, OnceClosure task) { |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 105 | Time last_time; |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 106 | if (!events_.empty()) |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 107 | last_time = (events_.end()-1)->time; |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 108 | else |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 109 | last_time = Time::NowFromSystemTime(); |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 110 | |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 111 | Time future = last_time + TimeDelta::FromMilliseconds(delay_ms); |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 112 | EventInjector::Event event = {future, std::move(callback), std::move(task)}; |
| 113 | events_.push_back(std::move(event)); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 114 | } |
| 115 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 116 | static gboolean Prepare(GSource* source, gint* timeout_ms) { |
| 117 | *timeout_ms = static_cast<Source*>(source)->injector->HandlePrepare(); |
| 118 | return FALSE; |
| 119 | } |
| 120 | |
| 121 | static gboolean Check(GSource* source) { |
| 122 | return static_cast<Source*>(source)->injector->HandleCheck(); |
| 123 | } |
| 124 | |
| 125 | static gboolean Dispatch(GSource* source, |
| 126 | GSourceFunc unused_func, |
| 127 | gpointer unused_data) { |
| 128 | static_cast<Source*>(source)->injector->HandleDispatch(); |
| 129 | return TRUE; |
| 130 | } |
| 131 | |
| 132 | Source* source_; |
| 133 | std::vector<Event> events_; |
| 134 | int processed_events_; |
| 135 | static GSourceFuncs SourceFuncs; |
| 136 | DISALLOW_COPY_AND_ASSIGN(EventInjector); |
| 137 | }; |
| 138 | |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 139 | GSourceFuncs EventInjector::SourceFuncs = {EventInjector::Prepare, |
| 140 | EventInjector::Check, |
| 141 | EventInjector::Dispatch, nullptr}; |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 142 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 143 | void IncrementInt(int *value) { |
| 144 | ++*value; |
| 145 | } |
| 146 | |
| 147 | // Checks how many events have been processed by the injector. |
| 148 | void ExpectProcessedEvents(EventInjector* injector, int count) { |
| 149 | EXPECT_EQ(injector->processed_events(), count); |
| 150 | } |
| 151 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 152 | // Posts a task on the current message loop. |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 153 | void PostMessageLoopTask(const Location& from_here, OnceClosure task) { |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame] | 154 | ThreadTaskRunnerHandle::Get()->PostTask(from_here, std::move(task)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Test fixture. |
| 158 | class MessagePumpGLibTest : public testing::Test { |
| 159 | public: |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 160 | MessagePumpGLibTest() : loop_(nullptr), injector_(nullptr) {} |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 161 | |
[email protected] | c19003d | 2012-07-31 01:38:18 | [diff] [blame] | 162 | // Overridden from testing::Test: |
dcheng | 8aef3761 | 2014-12-23 02:56:47 | [diff] [blame] | 163 | void SetUp() override { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 164 | loop_ = new MessageLoop(MessageLoop::TYPE_UI); |
| 165 | injector_ = new EventInjector(); |
| 166 | } |
dcheng | 8aef3761 | 2014-12-23 02:56:47 | [diff] [blame] | 167 | void TearDown() override { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 168 | delete injector_; |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 169 | injector_ = nullptr; |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 170 | delete loop_; |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 171 | loop_ = nullptr; |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | MessageLoop* loop() const { return loop_; } |
| 175 | EventInjector* injector() const { return injector_; } |
| 176 | |
| 177 | private: |
| 178 | MessageLoop* loop_; |
| 179 | EventInjector* injector_; |
| 180 | DISALLOW_COPY_AND_ASSIGN(MessagePumpGLibTest); |
| 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) { |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 242 | loop()->task_runner()->PostTask(FROM_HERE, |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 243 | 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; |
| 249 | loop()->task_runner()->PostTask( |
| 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) { |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 261 | loop()->task_runner()->PostDelayedTask(FROM_HERE, |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 262 | BindOnce(&IncrementInt, &task_count), |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 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; |
| 271 | loop()->task_runner()->PostDelayedTask( |
| 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. |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 377 | loop()->task_runner()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 378 | FROM_HERE, BindOnce(&ConcurrentHelper::FromTask, helper)); |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 379 | loop()->task_runner()->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; |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 411 | loop()->task_runner()->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 | // Allow tasks to be processed from 'native' event loops. |
Gabriel Charette | 810f3f43 | 2018-04-27 22:28:14 | [diff] [blame] | 456 | MessageLoopCurrent::Get()->SetNestableTasksAllowed(true); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 457 | scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); |
| 458 | |
| 459 | int task_count = 0; |
| 460 | // Add a couple of dummy events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 461 | injector->AddDummyEvent(0); |
| 462 | injector->AddDummyEvent(0); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 463 | // Post a couple of dummy tasks |
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)); |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 466 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 467 | BindOnce(&IncrementInt, &task_count)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 468 | // Delayed events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 469 | injector->AddDummyEvent(10); |
| 470 | injector->AddDummyEvent(10); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 471 | // Delayed work |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 472 | ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 473 | FROM_HERE, BindOnce(&IncrementInt, &task_count), |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 474 | TimeDelta::FromMilliseconds(30)); |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 475 | ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 476 | FROM_HERE, BindOnce(&GLibLoopRunner::Quit, runner), |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 477 | TimeDelta::FromMilliseconds(40)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 478 | |
| 479 | // Run a nested, straight GLib message loop. |
| 480 | runner->RunGLib(); |
| 481 | |
| 482 | ASSERT_EQ(3, task_count); |
| 483 | EXPECT_EQ(4, injector->processed_events()); |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame^] | 484 | std::move(done).Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 485 | } |
| 486 | |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame^] | 487 | void TestGtkLoopInternal(EventInjector* injector, OnceClosure done) { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 488 | // Allow tasks to be processed from 'native' event loops. |
Gabriel Charette | 810f3f43 | 2018-04-27 22:28:14 | [diff] [blame] | 489 | MessageLoopCurrent::Get()->SetNestableTasksAllowed(true); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 490 | scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); |
| 491 | |
| 492 | int task_count = 0; |
| 493 | // Add a couple of dummy events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 494 | injector->AddDummyEvent(0); |
| 495 | injector->AddDummyEvent(0); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 496 | // Post a couple of dummy tasks |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 497 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 498 | BindOnce(&IncrementInt, &task_count)); |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 499 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 500 | BindOnce(&IncrementInt, &task_count)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 501 | // Delayed events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 502 | injector->AddDummyEvent(10); |
| 503 | injector->AddDummyEvent(10); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 504 | // Delayed work |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 505 | ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 506 | FROM_HERE, BindOnce(&IncrementInt, &task_count), |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 507 | TimeDelta::FromMilliseconds(30)); |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 508 | ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 509 | FROM_HERE, BindOnce(&GLibLoopRunner::Quit, runner), |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 510 | TimeDelta::FromMilliseconds(40)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 511 | |
| 512 | // Run a nested, straight Gtk message loop. |
[email protected] | 258dca4 | 2011-09-21 00:17:19 | [diff] [blame] | 513 | runner->RunLoop(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 514 | |
| 515 | ASSERT_EQ(3, task_count); |
| 516 | EXPECT_EQ(4, injector->processed_events()); |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame^] | 517 | std::move(done).Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | } // namespace |
| 521 | |
| 522 | TEST_F(MessagePumpGLibTest, TestGLibLoop) { |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 523 | // Tests that events and posted tasks are correctly executed if the message |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 524 | // loop is not run by MessageLoop::Run() but by a straight GLib loop. |
| 525 | // Note that in this case we don't make strong guarantees about niceness |
| 526 | // between events and posted tasks. |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame^] | 527 | RunLoop run_loop; |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 528 | loop()->task_runner()->PostTask( |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame^] | 529 | FROM_HERE, BindOnce(&TestGLibLoopInternal, Unretained(injector()), |
| 530 | run_loop.QuitClosure())); |
| 531 | run_loop.Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | TEST_F(MessagePumpGLibTest, TestGtkLoop) { |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 535 | // Tests that events and posted tasks are correctly executed if the message |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 536 | // loop is not run by MessageLoop::Run() but by a straight Gtk loop. |
| 537 | // Note that in this case we don't make strong guarantees about niceness |
| 538 | // between events and posted tasks. |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame^] | 539 | RunLoop run_loop; |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 540 | loop()->task_runner()->PostTask( |
Wez | fda077b | 2018-06-07 21:18:11 | [diff] [blame^] | 541 | FROM_HERE, BindOnce(&TestGtkLoopInternal, Unretained(injector()), |
| 542 | run_loop.QuitClosure())); |
| 543 | run_loop.Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 544 | } |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 545 | |
| 546 | } // namespace base |