OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop/message_pump_libevent.h" | 5 #include "base/message_loop/message_pump_libevent.h" |
6 | 6 |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/posix/eintr_wrapper.h" | 16 #include "base/posix/eintr_wrapper.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
19 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
20 #include "base/synchronization/waitable_event_watcher.h" | 20 #include "base/synchronization/waitable_event_watcher.h" |
21 #include "base/test/gtest_util.h" | 21 #include "base/test/gtest_util.h" |
22 #include "base/third_party/libevent/event.h" | 22 #include "base/third_party/libevent/event.h" |
23 #include "base/threading/thread.h" | 23 #include "base/threading/thread.h" |
| 24 #include "base/threading/thread_task_runner_handle.h" |
24 #include "build/build_config.h" | 25 #include "build/build_config.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 | 27 |
27 namespace base { | 28 namespace base { |
28 | 29 |
29 class MessagePumpLibeventTest : public testing::Test { | 30 class MessagePumpLibeventTest : public testing::Test { |
30 protected: | 31 protected: |
31 MessagePumpLibeventTest() | 32 MessagePumpLibeventTest() |
32 : ui_loop_(new MessageLoop(MessageLoop::TYPE_UI)), | 33 : ui_loop_(new MessageLoop(MessageLoop::TYPE_UI)), |
33 io_thread_("MessagePumpLibeventTestIOThread") {} | 34 io_thread_("MessagePumpLibeventTestIOThread") {} |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 168 |
168 // Spoof a libevent notification. | 169 // Spoof a libevent notification. |
169 OnLibeventNotification(pump.get(), &watcher); | 170 OnLibeventNotification(pump.get(), &watcher); |
170 } | 171 } |
171 | 172 |
172 void QuitMessageLoopAndStart(const Closure& quit_closure) { | 173 void QuitMessageLoopAndStart(const Closure& quit_closure) { |
173 quit_closure.Run(); | 174 quit_closure.Run(); |
174 | 175 |
175 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | 176 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
176 RunLoop runloop; | 177 RunLoop runloop; |
177 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, | 178 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, runloop.QuitClosure()); |
178 runloop.QuitClosure()); | |
179 runloop.Run(); | 179 runloop.Run(); |
180 } | 180 } |
181 | 181 |
182 class NestedPumpWatcher : public MessagePumpLibevent::Watcher { | 182 class NestedPumpWatcher : public MessagePumpLibevent::Watcher { |
183 public: | 183 public: |
184 NestedPumpWatcher() {} | 184 NestedPumpWatcher() {} |
185 ~NestedPumpWatcher() override {} | 185 ~NestedPumpWatcher() override {} |
186 | 186 |
187 void OnFileCanReadWithoutBlocking(int /* fd */) override { | 187 void OnFileCanReadWithoutBlocking(int /* fd */) override { |
188 RunLoop runloop; | 188 RunLoop runloop; |
189 MessageLoop::current()->task_runner()->PostTask( | 189 ThreadTaskRunnerHandle::Get()->PostTask( |
190 FROM_HERE, Bind(&QuitMessageLoopAndStart, runloop.QuitClosure())); | 190 FROM_HERE, Bind(&QuitMessageLoopAndStart, runloop.QuitClosure())); |
191 runloop.Run(); | 191 runloop.Run(); |
192 } | 192 } |
193 | 193 |
194 void OnFileCanWriteWithoutBlocking(int /* fd */) override {} | 194 void OnFileCanWriteWithoutBlocking(int /* fd */) override {} |
195 }; | 195 }; |
196 | 196 |
197 TEST_F(MessagePumpLibeventTest, NestedPumpWatcher) { | 197 TEST_F(MessagePumpLibeventTest, NestedPumpWatcher) { |
198 std::unique_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | 198 std::unique_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
199 MessagePumpLibevent::FileDescriptorWatcher watcher; | 199 MessagePumpLibevent::FileDescriptorWatcher watcher; |
(...skipping 11 matching lines...) Expand all Loading... |
211 | 211 |
212 class QuitWatcher : public BaseWatcher { | 212 class QuitWatcher : public BaseWatcher { |
213 public: | 213 public: |
214 QuitWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller, | 214 QuitWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller, |
215 RunLoop* run_loop) | 215 RunLoop* run_loop) |
216 : BaseWatcher(controller), run_loop_(run_loop) {} | 216 : BaseWatcher(controller), run_loop_(run_loop) {} |
217 ~QuitWatcher() override {} | 217 ~QuitWatcher() override {} |
218 | 218 |
219 void OnFileCanReadWithoutBlocking(int /* fd */) override { | 219 void OnFileCanReadWithoutBlocking(int /* fd */) override { |
220 // Post a fatal closure to the MessageLoop before we quit it. | 220 // Post a fatal closure to the MessageLoop before we quit it. |
221 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, | 221 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, Bind(&FatalClosure)); |
222 Bind(&FatalClosure)); | |
223 | 222 |
224 // Now quit the MessageLoop. | 223 // Now quit the MessageLoop. |
225 run_loop_->Quit(); | 224 run_loop_->Quit(); |
226 } | 225 } |
227 | 226 |
228 private: | 227 private: |
229 RunLoop* run_loop_; // weak | 228 RunLoop* run_loop_; // weak |
230 }; | 229 }; |
231 | 230 |
232 void WriteFDWrapper(const int fd, | 231 void WriteFDWrapper(const int fd, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 | 271 |
273 // StartWatching can move |watcher| to IO thread. Release on IO thread. | 272 // StartWatching can move |watcher| to IO thread. Release on IO thread. |
274 io_loop()->task_runner()->PostTask( | 273 io_loop()->task_runner()->PostTask( |
275 FROM_HERE, | 274 FROM_HERE, |
276 Bind(&WaitableEventWatcher::StopWatching, Owned(watcher.release()))); | 275 Bind(&WaitableEventWatcher::StopWatching, Owned(watcher.release()))); |
277 } | 276 } |
278 | 277 |
279 } // namespace | 278 } // namespace |
280 | 279 |
281 } // namespace base | 280 } // namespace base |
OLD | NEW |