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"
9 #include "base/message_loop/message_loop.h"
10 #include "base/posix/eintr_wrapper.h"
11 #include "base/threading/thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/libevent/event.h"
17 class MessagePumpLibeventTest
: public testing::Test
{
19 MessagePumpLibeventTest()
20 : ui_loop_(MessageLoop::TYPE_UI
),
21 io_thread_("MessagePumpLibeventTestIOThread") {}
22 virtual ~MessagePumpLibeventTest() {}
24 virtual void SetUp() OVERRIDE
{
25 Thread::Options
options(MessageLoop::TYPE_IO
, 0);
26 ASSERT_TRUE(io_thread_
.StartWithOptions(options
));
27 ASSERT_EQ(MessageLoop::TYPE_IO
, io_thread_
.message_loop()->type());
28 int ret
= pipe(pipefds_
);
32 virtual void TearDown() OVERRIDE
{
33 if (HANDLE_EINTR(close(pipefds_
[0])) < 0)
34 PLOG(ERROR
) << "close";
35 if (HANDLE_EINTR(close(pipefds_
[1])) < 0)
36 PLOG(ERROR
) << "close";
39 MessageLoop
* ui_loop() { return &ui_loop_
; }
40 MessageLoopForIO
* io_loop() const {
41 return static_cast<MessageLoopForIO
*>(io_thread_
.message_loop());
44 void OnLibeventNotification(
45 MessagePumpLibevent
* pump
,
46 MessagePumpLibevent::FileDescriptorWatcher
* controller
) {
47 pump
->OnLibeventNotification(0, EV_WRITE
| EV_READ
, controller
);
59 // Concrete implementation of MessagePumpLibevent::Watcher that does
61 class StupidWatcher
: public MessagePumpLibevent::Watcher
{
63 virtual ~StupidWatcher() {}
65 // base:MessagePumpLibevent::Watcher interface
66 virtual void OnFileCanReadWithoutBlocking(int fd
) OVERRIDE
{}
67 virtual void OnFileCanWriteWithoutBlocking(int fd
) OVERRIDE
{}
70 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
72 // Test to make sure that we catch calling WatchFileDescriptor off of the
74 TEST_F(MessagePumpLibeventTest
, TestWatchingFromBadThread
) {
75 MessagePumpLibevent::FileDescriptorWatcher watcher
;
76 StupidWatcher delegate
;
78 ASSERT_DEATH(io_loop()->WatchFileDescriptor(
79 STDOUT_FILENO
, false, MessageLoopForIO::WATCH_READ
, &watcher
, &delegate
),
81 "watch_file_descriptor_caller_checker_.CalledOnValidThread()");
84 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
86 class BaseWatcher
: public MessagePumpLibevent::Watcher
{
88 explicit BaseWatcher(MessagePumpLibevent::FileDescriptorWatcher
* controller
)
89 : controller_(controller
) {
92 virtual ~BaseWatcher() {}
94 // base:MessagePumpLibevent::Watcher interface
95 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE
{
99 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE
{
104 MessagePumpLibevent::FileDescriptorWatcher
* controller_
;
107 class DeleteWatcher
: public BaseWatcher
{
109 explicit DeleteWatcher(
110 MessagePumpLibevent::FileDescriptorWatcher
* controller
)
111 : BaseWatcher(controller
) {}
113 virtual ~DeleteWatcher() {
114 DCHECK(!controller_
);
117 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE
{
124 TEST_F(MessagePumpLibeventTest
, DeleteWatcher
) {
125 scoped_refptr
<MessagePumpLibevent
> pump(new MessagePumpLibevent
);
126 MessagePumpLibevent::FileDescriptorWatcher
* watcher
=
127 new MessagePumpLibevent::FileDescriptorWatcher
;
128 DeleteWatcher
delegate(watcher
);
129 pump
->WatchFileDescriptor(pipefds_
[1],
130 false, MessagePumpLibevent::WATCH_READ_WRITE
, watcher
, &delegate
);
132 // Spoof a libevent notification.
133 OnLibeventNotification(pump
.get(), watcher
);
136 class StopWatcher
: public BaseWatcher
{
138 explicit StopWatcher(
139 MessagePumpLibevent::FileDescriptorWatcher
* controller
)
140 : BaseWatcher(controller
) {}
142 virtual ~StopWatcher() {}
144 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE
{
145 controller_
->StopWatchingFileDescriptor();
149 TEST_F(MessagePumpLibeventTest
, StopWatcher
) {
150 scoped_refptr
<MessagePumpLibevent
> pump(new MessagePumpLibevent
);
151 MessagePumpLibevent::FileDescriptorWatcher watcher
;
152 StopWatcher
delegate(&watcher
);
153 pump
->WatchFileDescriptor(pipefds_
[1],
154 false, MessagePumpLibevent::WATCH_READ_WRITE
, &watcher
, &delegate
);
156 // Spoof a libevent notification.
157 OnLibeventNotification(pump
.get(), &watcher
);