[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" |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 19 | #include "base/run_loop.h" |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 20 | #include "base/single_thread_task_runner.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 21 | #include "base/threading/thread.h" |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 22 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 23 | #include "testing/gtest/include/gtest/gtest.h" |
| 24 | |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 25 | namespace base { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | // This class injects dummy "events" into the GLib loop. When "handled" these |
| 29 | // events can run tasks. This is intended to mock gtk events (the corresponding |
| 30 | // GLib source runs at the same priority). |
| 31 | class EventInjector { |
| 32 | public: |
| 33 | EventInjector() : processed_events_(0) { |
| 34 | source_ = static_cast<Source*>(g_source_new(&SourceFuncs, sizeof(Source))); |
| 35 | source_->injector = this; |
| 36 | g_source_attach(source_, NULL); |
| 37 | g_source_set_can_recurse(source_, TRUE); |
| 38 | } |
| 39 | |
| 40 | ~EventInjector() { |
| 41 | g_source_destroy(source_); |
| 42 | g_source_unref(source_); |
| 43 | } |
| 44 | |
| 45 | int HandlePrepare() { |
| 46 | // If the queue is empty, block. |
| 47 | if (events_.empty()) |
| 48 | return -1; |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 49 | TimeDelta delta = events_[0].time - Time::NowFromSystemTime(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 50 | return std::max(0, static_cast<int>(ceil(delta.InMillisecondsF()))); |
| 51 | } |
| 52 | |
| 53 | bool HandleCheck() { |
| 54 | if (events_.empty()) |
| 55 | return false; |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 56 | return events_[0].time <= Time::NowFromSystemTime(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void HandleDispatch() { |
| 60 | if (events_.empty()) |
| 61 | return; |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame^] | 62 | Event event = std::move(events_[0]); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 63 | events_.erase(events_.begin()); |
| 64 | ++processed_events_; |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 65 | if (!event.callback.is_null()) |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame^] | 66 | std::move(event.callback).Run(); |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 67 | else if (!event.task.is_null()) |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame^] | 68 | std::move(event.task).Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 71 | // Adds an event to the queue. When "handled", executes |callback|. |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 72 | // delay_ms is relative to the last event if any, or to Now() otherwise. |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame^] | 73 | void AddEvent(int delay_ms, OnceClosure callback) { |
| 74 | AddEventHelper(delay_ms, std::move(callback), OnceClosure()); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void AddDummyEvent(int delay_ms) { |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame^] | 78 | AddEventHelper(delay_ms, OnceClosure(), OnceClosure()); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 79 | } |
| 80 | |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame^] | 81 | void AddEventAsTask(int delay_ms, OnceClosure task) { |
| 82 | AddEventHelper(delay_ms, OnceClosure(), std::move(task)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void Reset() { |
| 86 | processed_events_ = 0; |
| 87 | events_.clear(); |
| 88 | } |
| 89 | |
| 90 | int processed_events() const { return processed_events_; } |
| 91 | |
| 92 | private: |
| 93 | struct Event { |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 94 | Time time; |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame^] | 95 | OnceClosure callback; |
| 96 | OnceClosure task; |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | struct Source : public GSource { |
| 100 | EventInjector* injector; |
| 101 | }; |
| 102 | |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame^] | 103 | void AddEventHelper(int delay_ms, OnceClosure callback, OnceClosure task) { |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 104 | Time last_time; |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 105 | if (!events_.empty()) |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 106 | last_time = (events_.end()-1)->time; |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 107 | else |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 108 | last_time = Time::NowFromSystemTime(); |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 109 | |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 110 | Time future = last_time + TimeDelta::FromMilliseconds(delay_ms); |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame^] | 111 | EventInjector::Event event = {future, std::move(callback), std::move(task)}; |
| 112 | events_.push_back(std::move(event)); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 113 | } |
| 114 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 115 | static gboolean Prepare(GSource* source, gint* timeout_ms) { |
| 116 | *timeout_ms = static_cast<Source*>(source)->injector->HandlePrepare(); |
| 117 | return FALSE; |
| 118 | } |
| 119 | |
| 120 | static gboolean Check(GSource* source) { |
| 121 | return static_cast<Source*>(source)->injector->HandleCheck(); |
| 122 | } |
| 123 | |
| 124 | static gboolean Dispatch(GSource* source, |
| 125 | GSourceFunc unused_func, |
| 126 | gpointer unused_data) { |
| 127 | static_cast<Source*>(source)->injector->HandleDispatch(); |
| 128 | return TRUE; |
| 129 | } |
| 130 | |
| 131 | Source* source_; |
| 132 | std::vector<Event> events_; |
| 133 | int processed_events_; |
| 134 | static GSourceFuncs SourceFuncs; |
| 135 | DISALLOW_COPY_AND_ASSIGN(EventInjector); |
| 136 | }; |
| 137 | |
| 138 | GSourceFuncs EventInjector::SourceFuncs = { |
| 139 | EventInjector::Prepare, |
| 140 | EventInjector::Check, |
| 141 | EventInjector::Dispatch, |
| 142 | NULL |
| 143 | }; |
| 144 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 145 | void IncrementInt(int *value) { |
| 146 | ++*value; |
| 147 | } |
| 148 | |
| 149 | // Checks how many events have been processed by the injector. |
| 150 | void ExpectProcessedEvents(EventInjector* injector, int count) { |
| 151 | EXPECT_EQ(injector->processed_events(), count); |
| 152 | } |
| 153 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 154 | // Posts a task on the current message loop. |
| 155 | void PostMessageLoopTask(const tracked_objects::Location& from_here, |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame^] | 156 | OnceClosure task) { |
| 157 | ThreadTaskRunnerHandle::Get()->PostTask(from_here, std::move(task)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // Test fixture. |
| 161 | class MessagePumpGLibTest : public testing::Test { |
| 162 | public: |
| 163 | MessagePumpGLibTest() : loop_(NULL), injector_(NULL) { } |
| 164 | |
[email protected] | c19003d | 2012-07-31 01:38:18 | [diff] [blame] | 165 | // Overridden from testing::Test: |
dcheng | 8aef3761 | 2014-12-23 02:56:47 | [diff] [blame] | 166 | void SetUp() override { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 167 | loop_ = new MessageLoop(MessageLoop::TYPE_UI); |
| 168 | injector_ = new EventInjector(); |
| 169 | } |
dcheng | 8aef3761 | 2014-12-23 02:56:47 | [diff] [blame] | 170 | void TearDown() override { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 171 | delete injector_; |
| 172 | injector_ = NULL; |
| 173 | delete loop_; |
| 174 | loop_ = NULL; |
| 175 | } |
| 176 | |
| 177 | MessageLoop* loop() const { return loop_; } |
| 178 | EventInjector* injector() const { return injector_; } |
| 179 | |
| 180 | private: |
| 181 | MessageLoop* loop_; |
| 182 | EventInjector* injector_; |
| 183 | DISALLOW_COPY_AND_ASSIGN(MessagePumpGLibTest); |
| 184 | }; |
| 185 | |
| 186 | } // namespace |
| 187 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 188 | TEST_F(MessagePumpGLibTest, TestQuit) { |
| 189 | // Checks that Quit works and that the basic infrastructure is working. |
| 190 | |
| 191 | // Quit from a task |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 192 | RunLoop().RunUntilIdle(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 193 | EXPECT_EQ(0, injector()->processed_events()); |
| 194 | |
| 195 | injector()->Reset(); |
| 196 | // Quit from an event |
[email protected] | a085953f | 2013-02-04 23:40:00 | [diff] [blame] | 197 | injector()->AddEvent(0, MessageLoop::QuitWhenIdleClosure()); |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 198 | RunLoop().Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 199 | EXPECT_EQ(1, injector()->processed_events()); |
| 200 | } |
| 201 | |
| 202 | TEST_F(MessagePumpGLibTest, TestEventTaskInterleave) { |
| 203 | // Checks that tasks posted by events are executed before the next event if |
| 204 | // the posted task queue is empty. |
| 205 | // MessageLoop doesn't make strong guarantees that it is the case, but the |
| 206 | // current implementation ensures it and the tests below rely on it. |
| 207 | // If changes cause this test to fail, it is reasonable to change it, but |
| 208 | // TestWorkWhileWaitingForEvents and TestEventsWhileWaitingForWork have to be |
| 209 | // changed accordingly, otherwise they can become flaky. |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame^] | 210 | injector()->AddEventAsTask(0, BindOnce(&DoNothing)); |
| 211 | OnceClosure check_task = |
| 212 | BindOnce(&ExpectProcessedEvents, Unretained(injector()), 2); |
| 213 | OnceClosure posted_task = |
| 214 | BindOnce(&PostMessageLoopTask, FROM_HERE, std::move(check_task)); |
| 215 | injector()->AddEventAsTask(0, std::move(posted_task)); |
| 216 | injector()->AddEventAsTask(0, BindOnce(&DoNothing)); |
[email protected] | a085953f | 2013-02-04 23:40:00 | [diff] [blame] | 217 | injector()->AddEvent(0, MessageLoop::QuitWhenIdleClosure()); |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 218 | RunLoop().Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 219 | EXPECT_EQ(4, injector()->processed_events()); |
| 220 | |
| 221 | injector()->Reset(); |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame^] | 222 | injector()->AddEventAsTask(0, BindOnce(&DoNothing)); |
| 223 | check_task = BindOnce(&ExpectProcessedEvents, Unretained(injector()), 2); |
| 224 | posted_task = |
| 225 | BindOnce(&PostMessageLoopTask, FROM_HERE, std::move(check_task)); |
| 226 | injector()->AddEventAsTask(0, std::move(posted_task)); |
| 227 | injector()->AddEventAsTask(10, BindOnce(&DoNothing)); |
[email protected] | a085953f | 2013-02-04 23:40:00 | [diff] [blame] | 228 | injector()->AddEvent(0, MessageLoop::QuitWhenIdleClosure()); |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 229 | RunLoop().Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 230 | EXPECT_EQ(4, injector()->processed_events()); |
| 231 | } |
| 232 | |
| 233 | TEST_F(MessagePumpGLibTest, TestWorkWhileWaitingForEvents) { |
| 234 | int task_count = 0; |
| 235 | // Tests that we process tasks while waiting for new events. |
| 236 | // The event queue is empty at first. |
| 237 | for (int i = 0; i < 10; ++i) { |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 238 | loop()->task_runner()->PostTask(FROM_HERE, |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 239 | BindOnce(&IncrementInt, &task_count)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 240 | } |
| 241 | // After all the previous tasks have executed, enqueue an event that will |
| 242 | // quit. |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 243 | loop()->task_runner()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 244 | FROM_HERE, BindOnce(&EventInjector::AddEvent, Unretained(injector()), 0, |
| 245 | MessageLoop::QuitWhenIdleClosure())); |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 246 | RunLoop().Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 247 | ASSERT_EQ(10, task_count); |
| 248 | EXPECT_EQ(1, injector()->processed_events()); |
| 249 | |
| 250 | // Tests that we process delayed tasks while waiting for new events. |
| 251 | injector()->Reset(); |
| 252 | task_count = 0; |
| 253 | for (int i = 0; i < 10; ++i) { |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 254 | loop()->task_runner()->PostDelayedTask(FROM_HERE, |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 255 | BindOnce(&IncrementInt, &task_count), |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 256 | TimeDelta::FromMilliseconds(10 * i)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 257 | } |
| 258 | // After all the previous tasks have executed, enqueue an event that will |
| 259 | // quit. |
| 260 | // This relies on the fact that delayed tasks are executed in delay order. |
| 261 | // That is verified in message_loop_unittest.cc. |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 262 | loop()->task_runner()->PostDelayedTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 263 | FROM_HERE, |
| 264 | BindOnce(&EventInjector::AddEvent, Unretained(injector()), 10, |
| 265 | MessageLoop::QuitWhenIdleClosure()), |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 266 | TimeDelta::FromMilliseconds(150)); |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 267 | RunLoop().Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 268 | ASSERT_EQ(10, task_count); |
| 269 | EXPECT_EQ(1, injector()->processed_events()); |
| 270 | } |
| 271 | |
| 272 | TEST_F(MessagePumpGLibTest, TestEventsWhileWaitingForWork) { |
| 273 | // Tests that we process events while waiting for work. |
| 274 | // The event queue is empty at first. |
| 275 | for (int i = 0; i < 10; ++i) { |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 276 | injector()->AddDummyEvent(0); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 277 | } |
| 278 | // After all the events have been processed, post a task that will check that |
| 279 | // the events have been processed (note: the task executes after the event |
| 280 | // that posted it has been handled, so we expect 11 at that point). |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame^] | 281 | OnceClosure check_task = |
| 282 | BindOnce(&ExpectProcessedEvents, Unretained(injector()), 11); |
| 283 | OnceClosure posted_task = |
| 284 | BindOnce(&PostMessageLoopTask, FROM_HERE, std::move(check_task)); |
| 285 | injector()->AddEventAsTask(10, std::move(posted_task)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 286 | |
| 287 | // And then quit (relies on the condition tested by TestEventTaskInterleave). |
[email protected] | a085953f | 2013-02-04 23:40:00 | [diff] [blame] | 288 | injector()->AddEvent(10, MessageLoop::QuitWhenIdleClosure()); |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 289 | RunLoop().Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 290 | |
| 291 | EXPECT_EQ(12, injector()->processed_events()); |
| 292 | } |
| 293 | |
| 294 | namespace { |
| 295 | |
| 296 | // This class is a helper for the concurrent events / posted tasks test below. |
| 297 | // It will quit the main loop once enough tasks and events have been processed, |
| 298 | // 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] | 299 | class ConcurrentHelper : public RefCounted<ConcurrentHelper> { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 300 | public: |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 301 | explicit ConcurrentHelper(EventInjector* injector) |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 302 | : injector_(injector), |
| 303 | event_count_(kStartingEventCount), |
| 304 | task_count_(kStartingTaskCount) { |
| 305 | } |
| 306 | |
| 307 | void FromTask() { |
| 308 | if (task_count_ > 0) { |
| 309 | --task_count_; |
| 310 | } |
| 311 | if (task_count_ == 0 && event_count_ == 0) { |
[email protected] | 91cae259 | 2013-01-10 14:56:17 | [diff] [blame] | 312 | MessageLoop::current()->QuitWhenIdle(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 313 | } else { |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 314 | ThreadTaskRunnerHandle::Get()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 315 | FROM_HERE, BindOnce(&ConcurrentHelper::FromTask, this)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
| 319 | void FromEvent() { |
| 320 | if (event_count_ > 0) { |
| 321 | --event_count_; |
| 322 | } |
| 323 | if (task_count_ == 0 && event_count_ == 0) { |
[email protected] | 91cae259 | 2013-01-10 14:56:17 | [diff] [blame] | 324 | MessageLoop::current()->QuitWhenIdle(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 325 | } else { |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame^] | 326 | injector_->AddEventAsTask(0, |
| 327 | BindOnce(&ConcurrentHelper::FromEvent, this)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | int event_count() const { return event_count_; } |
| 332 | int task_count() const { return task_count_; } |
| 333 | |
| 334 | private: |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 335 | friend class RefCounted<ConcurrentHelper>; |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 336 | |
| 337 | ~ConcurrentHelper() {} |
| 338 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 339 | static const int kStartingEventCount = 20; |
| 340 | static const int kStartingTaskCount = 20; |
| 341 | |
| 342 | EventInjector* injector_; |
| 343 | int event_count_; |
| 344 | int task_count_; |
| 345 | }; |
| 346 | |
| 347 | } // namespace |
| 348 | |
| 349 | TEST_F(MessagePumpGLibTest, TestConcurrentEventPostedTask) { |
| 350 | // Tests that posted tasks don't starve events, nor the opposite. |
| 351 | // We use the helper class above. We keep both event and posted task queues |
| 352 | // full, the helper verifies that both tasks and events get processed. |
| 353 | // 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] | 354 | // to 0, and MessageLoop::QuitWhenIdle() will never be called. |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 355 | scoped_refptr<ConcurrentHelper> helper = new ConcurrentHelper(injector()); |
| 356 | |
| 357 | // Add 2 events to the queue to make sure it is always full (when we remove |
| 358 | // the event before processing it). |
tzik | a8cc220 | 2017-04-18 07:01:15 | [diff] [blame^] | 359 | injector()->AddEventAsTask(0, BindOnce(&ConcurrentHelper::FromEvent, helper)); |
| 360 | injector()->AddEventAsTask(0, BindOnce(&ConcurrentHelper::FromEvent, helper)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 361 | |
| 362 | // Similarly post 2 tasks. |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 363 | loop()->task_runner()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 364 | FROM_HERE, BindOnce(&ConcurrentHelper::FromTask, helper)); |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 365 | loop()->task_runner()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 366 | FROM_HERE, BindOnce(&ConcurrentHelper::FromTask, helper)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 367 | |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 368 | RunLoop().Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 369 | EXPECT_EQ(0, helper->event_count()); |
| 370 | EXPECT_EQ(0, helper->task_count()); |
| 371 | } |
| 372 | |
| 373 | namespace { |
| 374 | |
| 375 | void AddEventsAndDrainGLib(EventInjector* injector) { |
| 376 | // Add a couple of dummy events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 377 | injector->AddDummyEvent(0); |
| 378 | injector->AddDummyEvent(0); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 379 | // Then add an event that will quit the main loop. |
[email protected] | a085953f | 2013-02-04 23:40:00 | [diff] [blame] | 380 | injector->AddEvent(0, MessageLoop::QuitWhenIdleClosure()); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 381 | |
| 382 | // Post a couple of dummy tasks |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 383 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, BindOnce(&DoNothing)); |
| 384 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, BindOnce(&DoNothing)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 385 | |
| 386 | // Drain the events |
| 387 | while (g_main_context_pending(NULL)) { |
| 388 | g_main_context_iteration(NULL, FALSE); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | } // namespace |
| 393 | |
| 394 | TEST_F(MessagePumpGLibTest, TestDrainingGLib) { |
| 395 | // Tests that draining events using GLib works. |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 396 | loop()->task_runner()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 397 | FROM_HERE, BindOnce(&AddEventsAndDrainGLib, Unretained(injector()))); |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 398 | RunLoop().Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 399 | |
| 400 | EXPECT_EQ(3, injector()->processed_events()); |
| 401 | } |
| 402 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 403 | namespace { |
| 404 | |
| 405 | // Helper class that lets us run the GLib message loop. |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 406 | class GLibLoopRunner : public RefCounted<GLibLoopRunner> { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 407 | public: |
| 408 | GLibLoopRunner() : quit_(false) { } |
| 409 | |
| 410 | void RunGLib() { |
| 411 | while (!quit_) { |
| 412 | g_main_context_iteration(NULL, TRUE); |
| 413 | } |
| 414 | } |
| 415 | |
[email protected] | 258dca4 | 2011-09-21 00:17:19 | [diff] [blame] | 416 | void RunLoop() { |
[email protected] | 258dca4 | 2011-09-21 00:17:19 | [diff] [blame] | 417 | while (!quit_) { |
| 418 | g_main_context_iteration(NULL, TRUE); |
| 419 | } |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | void Quit() { |
| 423 | quit_ = true; |
| 424 | } |
| 425 | |
| 426 | void Reset() { |
| 427 | quit_ = false; |
| 428 | } |
| 429 | |
| 430 | private: |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 431 | friend class RefCounted<GLibLoopRunner>; |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 432 | |
| 433 | ~GLibLoopRunner() {} |
| 434 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 435 | bool quit_; |
| 436 | }; |
| 437 | |
| 438 | void TestGLibLoopInternal(EventInjector* injector) { |
| 439 | // Allow tasks to be processed from 'native' event loops. |
| 440 | MessageLoop::current()->SetNestableTasksAllowed(true); |
| 441 | scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); |
| 442 | |
| 443 | int task_count = 0; |
| 444 | // Add a couple of dummy events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 445 | injector->AddDummyEvent(0); |
| 446 | injector->AddDummyEvent(0); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 447 | // Post a couple of dummy tasks |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 448 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 449 | BindOnce(&IncrementInt, &task_count)); |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 450 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 451 | BindOnce(&IncrementInt, &task_count)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 452 | // Delayed events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 453 | injector->AddDummyEvent(10); |
| 454 | injector->AddDummyEvent(10); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 455 | // Delayed work |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 456 | ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 457 | FROM_HERE, BindOnce(&IncrementInt, &task_count), |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 458 | TimeDelta::FromMilliseconds(30)); |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 459 | ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 460 | FROM_HERE, BindOnce(&GLibLoopRunner::Quit, runner), |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 461 | TimeDelta::FromMilliseconds(40)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 462 | |
| 463 | // Run a nested, straight GLib message loop. |
| 464 | runner->RunGLib(); |
| 465 | |
| 466 | ASSERT_EQ(3, task_count); |
| 467 | EXPECT_EQ(4, injector->processed_events()); |
[email protected] | 91cae259 | 2013-01-10 14:56:17 | [diff] [blame] | 468 | MessageLoop::current()->QuitWhenIdle(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void TestGtkLoopInternal(EventInjector* injector) { |
| 472 | // Allow tasks to be processed from 'native' event loops. |
| 473 | MessageLoop::current()->SetNestableTasksAllowed(true); |
| 474 | scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); |
| 475 | |
| 476 | int task_count = 0; |
| 477 | // Add a couple of dummy events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 478 | injector->AddDummyEvent(0); |
| 479 | injector->AddDummyEvent(0); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 480 | // Post a couple of dummy tasks |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 481 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 482 | BindOnce(&IncrementInt, &task_count)); |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 483 | ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 484 | BindOnce(&IncrementInt, &task_count)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 485 | // Delayed events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 486 | injector->AddDummyEvent(10); |
| 487 | injector->AddDummyEvent(10); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 488 | // Delayed work |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 489 | ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 490 | FROM_HERE, BindOnce(&IncrementInt, &task_count), |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 491 | TimeDelta::FromMilliseconds(30)); |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 492 | ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 493 | FROM_HERE, BindOnce(&GLibLoopRunner::Quit, runner), |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 494 | TimeDelta::FromMilliseconds(40)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 495 | |
| 496 | // Run a nested, straight Gtk message loop. |
[email protected] | 258dca4 | 2011-09-21 00:17:19 | [diff] [blame] | 497 | runner->RunLoop(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 498 | |
| 499 | ASSERT_EQ(3, task_count); |
| 500 | EXPECT_EQ(4, injector->processed_events()); |
[email protected] | 91cae259 | 2013-01-10 14:56:17 | [diff] [blame] | 501 | MessageLoop::current()->QuitWhenIdle(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | } // namespace |
| 505 | |
| 506 | TEST_F(MessagePumpGLibTest, TestGLibLoop) { |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 507 | // Tests that events and posted tasks are correctly executed if the message |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 508 | // loop is not run by MessageLoop::Run() but by a straight GLib loop. |
| 509 | // Note that in this case we don't make strong guarantees about niceness |
| 510 | // between events and posted tasks. |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 511 | loop()->task_runner()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 512 | FROM_HERE, BindOnce(&TestGLibLoopInternal, Unretained(injector()))); |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 513 | RunLoop().Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | TEST_F(MessagePumpGLibTest, TestGtkLoop) { |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 517 | // Tests that events and posted tasks are correctly executed if the message |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 518 | // loop is not run by MessageLoop::Run() but by a straight Gtk loop. |
| 519 | // Note that in this case we don't make strong guarantees about niceness |
| 520 | // between events and posted tasks. |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 521 | loop()->task_runner()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 522 | FROM_HERE, BindOnce(&TestGtkLoopInternal, Unretained(injector()))); |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 523 | RunLoop().Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 524 | } |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 525 | |
| 526 | } // namespace base |