1 // Copyright 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_io_ios.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"
16 class MessagePumpIOSForIOTest
: public testing::Test
{
18 MessagePumpIOSForIOTest()
19 : ui_loop_(MessageLoop::TYPE_UI
),
20 io_thread_("MessagePumpIOSForIOTestIOThread") {}
21 virtual ~MessagePumpIOSForIOTest() {}
23 virtual void SetUp() OVERRIDE
{
24 Thread::Options
options(MessageLoop::TYPE_IO
, 0);
25 ASSERT_TRUE(io_thread_
.StartWithOptions(options
));
26 ASSERT_EQ(MessageLoop::TYPE_IO
, io_thread_
.message_loop()->type());
27 int ret
= pipe(pipefds_
);
29 ret
= pipe(alternate_pipefds_
);
33 virtual void TearDown() OVERRIDE
{
34 if (IGNORE_EINTR(close(pipefds_
[0])) < 0)
35 PLOG(ERROR
) << "close";
36 if (IGNORE_EINTR(close(pipefds_
[1])) < 0)
37 PLOG(ERROR
) << "close";
40 MessageLoop
* ui_loop() { return &ui_loop_
; }
41 MessageLoopForIO
* io_loop() const {
42 return static_cast<MessageLoopForIO
*>(io_thread_
.message_loop());
45 void HandleFdIOEvent(MessageLoopForIO::FileDescriptorWatcher
* watcher
) {
46 MessagePumpIOSForIO::HandleFdIOEvent(watcher
->fdref_
,
47 kCFFileDescriptorReadCallBack
| kCFFileDescriptorWriteCallBack
,
52 int alternate_pipefds_
[2];
58 DISALLOW_COPY_AND_ASSIGN(MessagePumpIOSForIOTest
);
63 // Concrete implementation of MessagePumpIOSForIO::Watcher that does
65 class StupidWatcher
: public MessagePumpIOSForIO::Watcher
{
67 virtual ~StupidWatcher() {}
69 // base:MessagePumpIOSForIO::Watcher interface
70 virtual void OnFileCanReadWithoutBlocking(int fd
) OVERRIDE
{}
71 virtual void OnFileCanWriteWithoutBlocking(int fd
) OVERRIDE
{}
74 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
76 // Test to make sure that we catch calling WatchFileDescriptor off of the
78 TEST_F(MessagePumpIOSForIOTest
, TestWatchingFromBadThread
) {
79 MessagePumpIOSForIO::FileDescriptorWatcher watcher
;
80 StupidWatcher delegate
;
82 ASSERT_DEBUG_DEATH(io_loop()->WatchFileDescriptor(
83 STDOUT_FILENO
, false, MessageLoopForIO::WATCH_READ
, &watcher
, &delegate
),
85 "watch_file_descriptor_caller_checker_.CalledOnValidThread\\(\\)");
88 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
90 class BaseWatcher
: public MessagePumpIOSForIO::Watcher
{
92 BaseWatcher(MessagePumpIOSForIO::FileDescriptorWatcher
* controller
)
93 : controller_(controller
) {
96 virtual ~BaseWatcher() {}
98 // MessagePumpIOSForIO::Watcher interface
99 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE
{
103 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE
{
108 MessagePumpIOSForIO::FileDescriptorWatcher
* controller_
;
111 class DeleteWatcher
: public BaseWatcher
{
113 explicit DeleteWatcher(
114 MessagePumpIOSForIO::FileDescriptorWatcher
* controller
)
115 : BaseWatcher(controller
) {}
117 virtual ~DeleteWatcher() {
118 DCHECK(!controller_
);
121 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE
{
128 TEST_F(MessagePumpIOSForIOTest
, DeleteWatcher
) {
129 scoped_ptr
<MessagePumpIOSForIO
> pump(new MessagePumpIOSForIO
);
130 MessagePumpIOSForIO::FileDescriptorWatcher
* watcher
=
131 new MessagePumpIOSForIO::FileDescriptorWatcher
;
132 DeleteWatcher
delegate(watcher
);
133 pump
->WatchFileDescriptor(pipefds_
[1],
134 false, MessagePumpIOSForIO::WATCH_READ_WRITE
, watcher
, &delegate
);
137 HandleFdIOEvent(watcher
);
140 class StopWatcher
: public BaseWatcher
{
142 StopWatcher(MessagePumpIOSForIO::FileDescriptorWatcher
* controller
,
143 MessagePumpIOSForIO
* pump
,
144 int fd_to_start_watching
= -1)
145 : BaseWatcher(controller
),
147 fd_to_start_watching_(fd_to_start_watching
) {}
149 virtual ~StopWatcher() {}
151 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE
{
152 controller_
->StopWatchingFileDescriptor();
153 if (fd_to_start_watching_
>= 0) {
154 pump_
->WatchFileDescriptor(fd_to_start_watching_
,
155 false, MessagePumpIOSForIO::WATCH_READ_WRITE
, controller_
, this);
160 MessagePumpIOSForIO
* pump_
;
161 int fd_to_start_watching_
;
164 TEST_F(MessagePumpIOSForIOTest
, StopWatcher
) {
165 scoped_ptr
<MessagePumpIOSForIO
> pump(new MessagePumpIOSForIO
);
166 MessagePumpIOSForIO::FileDescriptorWatcher watcher
;
167 StopWatcher
delegate(&watcher
, pump
.get());
168 pump
->WatchFileDescriptor(pipefds_
[1],
169 false, MessagePumpIOSForIO::WATCH_READ_WRITE
, &watcher
, &delegate
);
172 HandleFdIOEvent(&watcher
);
175 TEST_F(MessagePumpIOSForIOTest
, StopWatcherAndWatchSomethingElse
) {
176 scoped_ptr
<MessagePumpIOSForIO
> pump(new MessagePumpIOSForIO
);
177 MessagePumpIOSForIO::FileDescriptorWatcher watcher
;
178 StopWatcher
delegate(&watcher
, pump
.get(), alternate_pipefds_
[1]);
179 pump
->WatchFileDescriptor(pipefds_
[1],
180 false, MessagePumpIOSForIO::WATCH_READ_WRITE
, &watcher
, &delegate
);
183 HandleFdIOEvent(&watcher
);