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