Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: base/message_loop/message_pump_libevent_unittest.cc

Issue 1059353003: Add missing check for quitting from MessagePumpLibevent (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use WaitableEventWatcher Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/message_loop/message_pump_libevent.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/bind.h" 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"
10 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
11 #include "base/posix/eintr_wrapper.h" 14 #include "base/posix/eintr_wrapper.h"
12 #include "base/run_loop.h" 15 #include "base/run_loop.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/synchronization/waitable_event_watcher.h"
13 #include "base/threading/thread.h" 18 #include "base/threading/thread.h"
14 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/libevent/event.h" 20 #include "third_party/libevent/event.h"
16 21
17 namespace base { 22 namespace base {
18 23
19 class MessagePumpLibeventTest : public testing::Test { 24 class MessagePumpLibeventTest : public testing::Test {
20 protected: 25 protected:
21 MessagePumpLibeventTest() 26 MessagePumpLibeventTest()
22 : ui_loop_(MessageLoop::TYPE_UI), 27 : ui_loop_(new MessageLoop(MessageLoop::TYPE_UI)),
23 io_thread_("MessagePumpLibeventTestIOThread") {} 28 io_thread_("MessagePumpLibeventTestIOThread") {}
24 ~MessagePumpLibeventTest() override {} 29 ~MessagePumpLibeventTest() override {}
25 30
26 void SetUp() override { 31 void SetUp() override {
27 Thread::Options options(MessageLoop::TYPE_IO, 0); 32 Thread::Options options(MessageLoop::TYPE_IO, 0);
28 ASSERT_TRUE(io_thread_.StartWithOptions(options)); 33 ASSERT_TRUE(io_thread_.StartWithOptions(options));
29 ASSERT_EQ(MessageLoop::TYPE_IO, io_thread_.message_loop()->type()); 34 ASSERT_EQ(MessageLoop::TYPE_IO, io_thread_.message_loop()->type());
30 int ret = pipe(pipefds_); 35 int ret = pipe(pipefds_);
31 ASSERT_EQ(0, ret); 36 ASSERT_EQ(0, ret);
32 } 37 }
33 38
34 void TearDown() override { 39 void TearDown() override {
35 if (IGNORE_EINTR(close(pipefds_[0])) < 0) 40 if (IGNORE_EINTR(close(pipefds_[0])) < 0)
36 PLOG(ERROR) << "close"; 41 PLOG(ERROR) << "close";
37 if (IGNORE_EINTR(close(pipefds_[1])) < 0) 42 if (IGNORE_EINTR(close(pipefds_[1])) < 0)
38 PLOG(ERROR) << "close"; 43 PLOG(ERROR) << "close";
39 } 44 }
40 45
41 MessageLoop* ui_loop() { return &ui_loop_; }
42 MessageLoopForIO* io_loop() const { 46 MessageLoopForIO* io_loop() const {
43 return static_cast<MessageLoopForIO*>(io_thread_.message_loop()); 47 return static_cast<MessageLoopForIO*>(io_thread_.message_loop());
44 } 48 }
45 49
46 void OnLibeventNotification( 50 void OnLibeventNotification(
47 MessagePumpLibevent* pump, 51 MessagePumpLibevent* pump,
48 MessagePumpLibevent::FileDescriptorWatcher* controller) { 52 MessagePumpLibevent::FileDescriptorWatcher* controller) {
49 pump->OnLibeventNotification(0, EV_WRITE | EV_READ, controller); 53 pump->OnLibeventNotification(0, EV_WRITE | EV_READ, controller);
50 } 54 }
51 55
52 int pipefds_[2]; 56 int pipefds_[2];
57 scoped_ptr<MessageLoop> ui_loop_;
53 58
54 private: 59 private:
55 MessageLoop ui_loop_;
56 Thread io_thread_; 60 Thread io_thread_;
57 }; 61 };
58 62
59 namespace { 63 namespace {
60 64
61 // Concrete implementation of MessagePumpLibevent::Watcher that does 65 // Concrete implementation of MessagePumpLibevent::Watcher that does
62 // nothing useful. 66 // nothing useful.
63 class StupidWatcher : public MessagePumpLibevent::Watcher { 67 class StupidWatcher : public MessagePumpLibevent::Watcher {
64 public: 68 public:
65 ~StupidWatcher() override {} 69 ~StupidWatcher() override {}
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); 191 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent);
188 MessagePumpLibevent::FileDescriptorWatcher watcher; 192 MessagePumpLibevent::FileDescriptorWatcher watcher;
189 NestedPumpWatcher delegate; 193 NestedPumpWatcher delegate;
190 pump->WatchFileDescriptor(pipefds_[1], 194 pump->WatchFileDescriptor(pipefds_[1],
191 false, MessagePumpLibevent::WATCH_READ, &watcher, &delegate); 195 false, MessagePumpLibevent::WATCH_READ, &watcher, &delegate);
192 196
193 // Spoof a libevent notification. 197 // Spoof a libevent notification.
194 OnLibeventNotification(pump.get(), &watcher); 198 OnLibeventNotification(pump.get(), &watcher);
195 } 199 }
196 200
201 void FatalClosure() {
202 FAIL() << "Reached fatal closure.";
203 }
204
205 class QuitWatcher : public BaseWatcher {
206 public:
207 QuitWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller,
208 RunLoop* run_loop)
209 : BaseWatcher(controller), run_loop_(run_loop) {}
210 ~QuitWatcher() override {}
211
212 void OnFileCanReadWithoutBlocking(int /* fd */) override {
213 // Post a fatal closure to the MessageLoop before we quit it.
214 MessageLoop::current()->PostTask(FROM_HERE, Bind(&FatalClosure));
215
216 // Now quit the MessageLoop.
217 run_loop_->Quit();
218 }
219
220 private:
221 RunLoop* run_loop_; // weak
222 };
223
224 void WriteFDWrapper(const int fd,
225 const char* buf,
226 int size,
227 WaitableEvent* event) {
228 ASSERT_TRUE(WriteFileDescriptor(fd, buf, size));
229 }
230
231 // Tests that MessagePumpLibevent quits immediately when it is quit from
232 // libevent's event_base_loop().
233 TEST_F(MessagePumpLibeventTest, QuitWatcher) {
234 // Delete the old MessageLoop so that we can manage our own one here.
235 ui_loop_.reset();
236
237 MessagePumpLibevent* pump = new MessagePumpLibevent; // owned by |loop|.
238 MessageLoop loop(make_scoped_ptr(pump));
239 RunLoop run_loop;
240 MessagePumpLibevent::FileDescriptorWatcher controller;
241 QuitWatcher delegate(&controller, &run_loop);
242 WaitableEvent event(false /* manual_reset */, false /* initially_signaled */);
243 WaitableEventWatcher watcher;
244
245 // Tell the pump to watch the pipe.
246 pump->WatchFileDescriptor(pipefds_[0], false, MessagePumpLibevent::WATCH_READ,
247 &controller, &delegate);
248
249 // Make the IO thread wait for |event| before writing to pipefds[1].
250 const char buf = 0;
251 const WaitableEventWatcher::EventCallback write_fd_task =
252 Bind(&WriteFDWrapper, pipefds_[1], &buf, 1);
253 io_loop()->PostTask(FROM_HERE,
254 Bind(IgnoreResult(&WaitableEventWatcher::StartWatching),
255 Unretained(&watcher), &event, write_fd_task));
256
257 // Queue |event| to signal on |loop|.
258 loop.PostTask(FROM_HERE, Bind(&WaitableEvent::Signal, Unretained(&event)));
259
260 // Now run the MessageLoop.
261 run_loop.Run();
262 }
263
197 } // namespace 264 } // namespace
198 265
199 } // namespace base 266 } // namespace base
OLDNEW
« no previous file with comments | « base/message_loop/message_pump_libevent.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698