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.
5 #include "base/message_loop/message_pump_libevent.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"
24 class MessagePumpLibeventTest
: public testing::Test
{
26 MessagePumpLibeventTest()
27 : ui_loop_(new MessageLoop(MessageLoop::TYPE_UI
)),
28 io_thread_("MessagePumpLibeventTestIOThread") {}
29 ~MessagePumpLibeventTest() override
{}
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_
);
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";
46 MessageLoopForIO
* io_loop() const {
47 return static_cast<MessageLoopForIO
*>(io_thread_
.message_loop());
50 void OnLibeventNotification(
51 MessagePumpLibevent
* pump
,
52 MessagePumpLibevent::FileDescriptorWatcher
* controller
) {
53 pump
->OnLibeventNotification(0, EV_WRITE
| EV_READ
, controller
);
57 scoped_ptr
<MessageLoop
> ui_loop_
;
65 // Concrete implementation of MessagePumpLibevent::Watcher that does
67 class StupidWatcher
: public MessagePumpLibevent::Watcher
{
69 ~StupidWatcher() override
{}
71 // base:MessagePumpLibevent::Watcher interface
72 void OnFileCanReadWithoutBlocking(int fd
) override
{}
73 void OnFileCanWriteWithoutBlocking(int fd
) override
{}
76 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
78 // Test to make sure that we catch calling WatchFileDescriptor off of the
80 #if defined(OS_CHROMEOS) || defined(OS_LINUX)
81 // Flaky on Chrome OS and Linux: crbug.com/138845.
82 #define MAYBE_TestWatchingFromBadThread DISABLED_TestWatchingFromBadThread
84 #define MAYBE_TestWatchingFromBadThread TestWatchingFromBadThread
86 TEST_F(MessagePumpLibeventTest
, MAYBE_TestWatchingFromBadThread
) {
87 MessagePumpLibevent::FileDescriptorWatcher watcher
;
88 StupidWatcher delegate
;
90 ASSERT_DEATH(io_loop()->WatchFileDescriptor(
91 STDOUT_FILENO
, false, MessageLoopForIO::WATCH_READ
, &watcher
, &delegate
),
93 "watch_file_descriptor_caller_checker_.CalledOnValidThread\\(\\)");
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!");
102 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
104 class BaseWatcher
: public MessagePumpLibevent::Watcher
{
106 explicit BaseWatcher(MessagePumpLibevent::FileDescriptorWatcher
* controller
)
107 : controller_(controller
) {
110 ~BaseWatcher() override
{}
112 // base:MessagePumpLibevent::Watcher interface
113 void OnFileCanReadWithoutBlocking(int /* fd */) override
{ NOTREACHED(); }
115 void OnFileCanWriteWithoutBlocking(int /* fd */) override
{ NOTREACHED(); }
118 MessagePumpLibevent::FileDescriptorWatcher
* controller_
;
121 class DeleteWatcher
: public BaseWatcher
{
123 explicit DeleteWatcher(
124 MessagePumpLibevent::FileDescriptorWatcher
* controller
)
125 : BaseWatcher(controller
) {}
127 ~DeleteWatcher() override
{ DCHECK(!controller_
); }
129 void OnFileCanWriteWithoutBlocking(int /* fd */) override
{
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
);
144 // Spoof a libevent notification.
145 OnLibeventNotification(pump
.get(), watcher
);
148 class StopWatcher
: public BaseWatcher
{
150 explicit StopWatcher(
151 MessagePumpLibevent::FileDescriptorWatcher
* controller
)
152 : BaseWatcher(controller
) {}
154 ~StopWatcher() override
{}
156 void OnFileCanWriteWithoutBlocking(int /* fd */) override
{
157 controller_
->StopWatchingFileDescriptor();
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
);
168 // Spoof a libevent notification.
169 OnLibeventNotification(pump
.get(), &watcher
);
172 void QuitMessageLoopAndStart(const Closure
& quit_closure
) {
175 MessageLoop::ScopedNestableTaskAllower
allow(MessageLoop::current());
177 MessageLoop::current()->PostTask(FROM_HERE
, runloop
.QuitClosure());
181 class NestedPumpWatcher
: public MessagePumpLibevent::Watcher
{
183 NestedPumpWatcher() {}
184 ~NestedPumpWatcher() override
{}
186 void OnFileCanReadWithoutBlocking(int /* fd */) override
{
188 MessageLoop::current()->PostTask(FROM_HERE
, Bind(&QuitMessageLoopAndStart
,
189 runloop
.QuitClosure()));
193 void OnFileCanWriteWithoutBlocking(int /* fd */) override
{}
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
);
203 // Spoof a libevent notification.
204 OnLibeventNotification(pump
.get(), &watcher
);
207 void FatalClosure() {
208 FAIL() << "Reached fatal closure.";
211 class QuitWatcher
: public BaseWatcher
{
213 QuitWatcher(MessagePumpLibevent::FileDescriptorWatcher
* controller
,
215 : BaseWatcher(controller
), run_loop_(run_loop
) {}
216 ~QuitWatcher() override
{}
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
));
222 // Now quit the MessageLoop.
227 RunLoop
* run_loop_
; // weak
230 void WriteFDWrapper(const int fd
,
233 WaitableEvent
* event
) {
234 ASSERT_TRUE(WriteFileDescriptor(fd
, buf
, size
));
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.
243 MessagePumpLibevent
* pump
= new MessagePumpLibevent
; // owned by |loop|.
244 MessageLoop
loop(make_scoped_ptr(pump
));
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
);
251 // Tell the pump to watch the pipe.
252 pump
->WatchFileDescriptor(pipefds_
[0], false, MessagePumpLibevent::WATCH_READ
,
253 &controller
, &delegate
);
255 // Make the IO thread wait for |event| before writing to pipefds[1].
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
));
263 // Queue |event| to signal on |loop|.
264 loop
.PostTask(FROM_HERE
, Bind(&WaitableEvent::Signal
, Unretained(&event
)));
266 // Now run the MessageLoop.
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())));