| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/message_loop/message_pump_libevent.h" | |
| 6 | |
| 7 #include <unistd.h> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/files/file_util.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "base/posix/eintr_wrapper.h" | |
| 15 #include "base/run_loop.h" | |
| 16 #include "base/synchronization/waitable_event.h" | |
| 17 #include "base/synchronization/waitable_event_watcher.h" | |
| 18 #include "base/threading/thread.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 #include "third_party/libevent/event.h" | |
| 21 | |
| 22 namespace base { | |
| 23 | |
| 24 class MessagePumpLibeventTest : public testing::Test { | |
| 25 protected: | |
| 26 MessagePumpLibeventTest() | |
| 27 : ui_loop_(new MessageLoop(MessageLoop::TYPE_UI)), | |
| 28 io_thread_("MessagePumpLibeventTestIOThread") {} | |
| 29 ~MessagePumpLibeventTest() override {} | |
| 30 | |
| 31 void SetUp() override { | |
| 32 Thread::Options options(MessageLoop::TYPE_IO, 0); | |
| 33 ASSERT_TRUE(io_thread_.StartWithOptions(options)); | |
| 34 ASSERT_EQ(MessageLoop::TYPE_IO, io_thread_.message_loop()->type()); | |
| 35 int ret = pipe(pipefds_); | |
| 36 ASSERT_EQ(0, ret); | |
| 37 } | |
| 38 | |
| 39 void TearDown() override { | |
| 40 if (IGNORE_EINTR(close(pipefds_[0])) < 0) | |
| 41 PLOG(ERROR) << "close"; | |
| 42 if (IGNORE_EINTR(close(pipefds_[1])) < 0) | |
| 43 PLOG(ERROR) << "close"; | |
| 44 } | |
| 45 | |
| 46 MessageLoopForIO* io_loop() const { | |
| 47 return static_cast<MessageLoopForIO*>(io_thread_.message_loop()); | |
| 48 } | |
| 49 | |
| 50 void OnLibeventNotification( | |
| 51 MessagePumpLibevent* pump, | |
| 52 MessagePumpLibevent::FileDescriptorWatcher* controller) { | |
| 53 pump->OnLibeventNotification(0, EV_WRITE | EV_READ, controller); | |
| 54 } | |
| 55 | |
| 56 int pipefds_[2]; | |
| 57 scoped_ptr<MessageLoop> ui_loop_; | |
| 58 | |
| 59 private: | |
| 60 Thread io_thread_; | |
| 61 }; | |
| 62 | |
| 63 namespace { | |
| 64 | |
| 65 // Concrete implementation of MessagePumpLibevent::Watcher that does | |
| 66 // nothing useful. | |
| 67 class StupidWatcher : public MessagePumpLibevent::Watcher { | |
| 68 public: | |
| 69 ~StupidWatcher() override {} | |
| 70 | |
| 71 // base:MessagePumpLibevent::Watcher interface | |
| 72 void OnFileCanReadWithoutBlocking(int fd) override {} | |
| 73 void OnFileCanWriteWithoutBlocking(int fd) override {} | |
| 74 }; | |
| 75 | |
| 76 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | |
| 77 | |
| 78 // Test to make sure that we catch calling WatchFileDescriptor off of the | |
| 79 // wrong thread. | |
| 80 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | |
| 81 // Flaky on Chrome OS and Linux: crbug.com/138845. | |
| 82 #define MAYBE_TestWatchingFromBadThread DISABLED_TestWatchingFromBadThread | |
| 83 #else | |
| 84 #define MAYBE_TestWatchingFromBadThread TestWatchingFromBadThread | |
| 85 #endif | |
| 86 TEST_F(MessagePumpLibeventTest, MAYBE_TestWatchingFromBadThread) { | |
| 87 MessagePumpLibevent::FileDescriptorWatcher watcher; | |
| 88 StupidWatcher delegate; | |
| 89 | |
| 90 ASSERT_DEATH(io_loop()->WatchFileDescriptor( | |
| 91 STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate), | |
| 92 "Check failed: " | |
| 93 "watch_file_descriptor_caller_checker_.CalledOnValidThread\\(\\)"); | |
| 94 } | |
| 95 | |
| 96 TEST_F(MessagePumpLibeventTest, QuitOutsideOfRun) { | |
| 97 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | |
| 98 ASSERT_DEATH(pump->Quit(), "Check failed: in_run_. " | |
| 99 "Quit was called outside of Run!"); | |
| 100 } | |
| 101 | |
| 102 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | |
| 103 | |
| 104 class BaseWatcher : public MessagePumpLibevent::Watcher { | |
| 105 public: | |
| 106 explicit BaseWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller) | |
| 107 : controller_(controller) { | |
| 108 DCHECK(controller_); | |
| 109 } | |
| 110 ~BaseWatcher() override {} | |
| 111 | |
| 112 // base:MessagePumpLibevent::Watcher interface | |
| 113 void OnFileCanReadWithoutBlocking(int /* fd */) override { NOTREACHED(); } | |
| 114 | |
| 115 void OnFileCanWriteWithoutBlocking(int /* fd */) override { NOTREACHED(); } | |
| 116 | |
| 117 protected: | |
| 118 MessagePumpLibevent::FileDescriptorWatcher* controller_; | |
| 119 }; | |
| 120 | |
| 121 class DeleteWatcher : public BaseWatcher { | |
| 122 public: | |
| 123 explicit DeleteWatcher( | |
| 124 MessagePumpLibevent::FileDescriptorWatcher* controller) | |
| 125 : BaseWatcher(controller) {} | |
| 126 | |
| 127 ~DeleteWatcher() override { DCHECK(!controller_); } | |
| 128 | |
| 129 void OnFileCanWriteWithoutBlocking(int /* fd */) override { | |
| 130 DCHECK(controller_); | |
| 131 delete controller_; | |
| 132 controller_ = NULL; | |
| 133 } | |
| 134 }; | |
| 135 | |
| 136 TEST_F(MessagePumpLibeventTest, DeleteWatcher) { | |
| 137 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | |
| 138 MessagePumpLibevent::FileDescriptorWatcher* watcher = | |
| 139 new MessagePumpLibevent::FileDescriptorWatcher; | |
| 140 DeleteWatcher delegate(watcher); | |
| 141 pump->WatchFileDescriptor(pipefds_[1], | |
| 142 false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate); | |
| 143 | |
| 144 // Spoof a libevent notification. | |
| 145 OnLibeventNotification(pump.get(), watcher); | |
| 146 } | |
| 147 | |
| 148 class StopWatcher : public BaseWatcher { | |
| 149 public: | |
| 150 explicit StopWatcher( | |
| 151 MessagePumpLibevent::FileDescriptorWatcher* controller) | |
| 152 : BaseWatcher(controller) {} | |
| 153 | |
| 154 ~StopWatcher() override {} | |
| 155 | |
| 156 void OnFileCanWriteWithoutBlocking(int /* fd */) override { | |
| 157 controller_->StopWatchingFileDescriptor(); | |
| 158 } | |
| 159 }; | |
| 160 | |
| 161 TEST_F(MessagePumpLibeventTest, StopWatcher) { | |
| 162 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | |
| 163 MessagePumpLibevent::FileDescriptorWatcher watcher; | |
| 164 StopWatcher delegate(&watcher); | |
| 165 pump->WatchFileDescriptor(pipefds_[1], | |
| 166 false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate); | |
| 167 | |
| 168 // Spoof a libevent notification. | |
| 169 OnLibeventNotification(pump.get(), &watcher); | |
| 170 } | |
| 171 | |
| 172 void QuitMessageLoopAndStart(const Closure& quit_closure) { | |
| 173 quit_closure.Run(); | |
| 174 | |
| 175 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | |
| 176 RunLoop runloop; | |
| 177 MessageLoop::current()->PostTask(FROM_HERE, runloop.QuitClosure()); | |
| 178 runloop.Run(); | |
| 179 } | |
| 180 | |
| 181 class NestedPumpWatcher : public MessagePumpLibevent::Watcher { | |
| 182 public: | |
| 183 NestedPumpWatcher() {} | |
| 184 ~NestedPumpWatcher() override {} | |
| 185 | |
| 186 void OnFileCanReadWithoutBlocking(int /* fd */) override { | |
| 187 RunLoop runloop; | |
| 188 MessageLoop::current()->PostTask(FROM_HERE, Bind(&QuitMessageLoopAndStart, | |
| 189 runloop.QuitClosure())); | |
| 190 runloop.Run(); | |
| 191 } | |
| 192 | |
| 193 void OnFileCanWriteWithoutBlocking(int /* fd */) override {} | |
| 194 }; | |
| 195 | |
| 196 TEST_F(MessagePumpLibeventTest, NestedPumpWatcher) { | |
| 197 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | |
| 198 MessagePumpLibevent::FileDescriptorWatcher watcher; | |
| 199 NestedPumpWatcher delegate; | |
| 200 pump->WatchFileDescriptor(pipefds_[1], | |
| 201 false, MessagePumpLibevent::WATCH_READ, &watcher, &delegate); | |
| 202 | |
| 203 // Spoof a libevent notification. | |
| 204 OnLibeventNotification(pump.get(), &watcher); | |
| 205 } | |
| 206 | |
| 207 void FatalClosure() { | |
| 208 FAIL() << "Reached fatal closure."; | |
| 209 } | |
| 210 | |
| 211 class QuitWatcher : public BaseWatcher { | |
| 212 public: | |
| 213 QuitWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller, | |
| 214 RunLoop* run_loop) | |
| 215 : BaseWatcher(controller), run_loop_(run_loop) {} | |
| 216 ~QuitWatcher() override {} | |
| 217 | |
| 218 void OnFileCanReadWithoutBlocking(int /* fd */) override { | |
| 219 // Post a fatal closure to the MessageLoop before we quit it. | |
| 220 MessageLoop::current()->PostTask(FROM_HERE, Bind(&FatalClosure)); | |
| 221 | |
| 222 // Now quit the MessageLoop. | |
| 223 run_loop_->Quit(); | |
| 224 } | |
| 225 | |
| 226 private: | |
| 227 RunLoop* run_loop_; // weak | |
| 228 }; | |
| 229 | |
| 230 void WriteFDWrapper(const int fd, | |
| 231 const char* buf, | |
| 232 int size, | |
| 233 WaitableEvent* event) { | |
| 234 ASSERT_TRUE(WriteFileDescriptor(fd, buf, size)); | |
| 235 } | |
| 236 | |
| 237 // Tests that MessagePumpLibevent quits immediately when it is quit from | |
| 238 // libevent's event_base_loop(). | |
| 239 TEST_F(MessagePumpLibeventTest, QuitWatcher) { | |
| 240 // Delete the old MessageLoop so that we can manage our own one here. | |
| 241 ui_loop_.reset(); | |
| 242 | |
| 243 MessagePumpLibevent* pump = new MessagePumpLibevent; // owned by |loop|. | |
| 244 MessageLoop loop(make_scoped_ptr(pump)); | |
| 245 RunLoop run_loop; | |
| 246 MessagePumpLibevent::FileDescriptorWatcher controller; | |
| 247 QuitWatcher delegate(&controller, &run_loop); | |
| 248 WaitableEvent event(false /* manual_reset */, false /* initially_signaled */); | |
| 249 scoped_ptr<WaitableEventWatcher> watcher(new WaitableEventWatcher); | |
| 250 | |
| 251 // Tell the pump to watch the pipe. | |
| 252 pump->WatchFileDescriptor(pipefds_[0], false, MessagePumpLibevent::WATCH_READ, | |
| 253 &controller, &delegate); | |
| 254 | |
| 255 // Make the IO thread wait for |event| before writing to pipefds[1]. | |
| 256 const char buf = 0; | |
| 257 const WaitableEventWatcher::EventCallback write_fd_task = | |
| 258 Bind(&WriteFDWrapper, pipefds_[1], &buf, 1); | |
| 259 io_loop()->PostTask(FROM_HERE, | |
| 260 Bind(IgnoreResult(&WaitableEventWatcher::StartWatching), | |
| 261 Unretained(watcher.get()), &event, write_fd_task)); | |
| 262 | |
| 263 // Queue |event| to signal on |loop|. | |
| 264 loop.PostTask(FROM_HERE, Bind(&WaitableEvent::Signal, Unretained(&event))); | |
| 265 | |
| 266 // Now run the MessageLoop. | |
| 267 run_loop.Run(); | |
| 268 | |
| 269 // StartWatching can move |watcher| to IO thread. Release on IO thread. | |
| 270 io_loop()->PostTask(FROM_HERE, Bind(&WaitableEventWatcher::StopWatching, | |
| 271 Owned(watcher.release()))); | |
| 272 } | |
| 273 | |
| 274 } // namespace | |
| 275 | |
| 276 } // namespace base | |
| OLD | NEW |