| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "base/message_loop/message_pump_io_ios.h" | |
| 6 | |
| 7 #include <unistd.h> | |
| 8 | |
| 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 | |
| 14 namespace base { | |
| 15 | |
| 16 class MessagePumpIOSForIOTest : public testing::Test { | |
| 17 protected: | |
| 18 MessagePumpIOSForIOTest() | |
| 19 : ui_loop_(MessageLoop::TYPE_UI), | |
| 20 io_thread_("MessagePumpIOSForIOTestIOThread") {} | |
| 21 ~MessagePumpIOSForIOTest() override {} | |
| 22 | |
| 23 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_); | |
| 28 ASSERT_EQ(0, ret); | |
| 29 ret = pipe(alternate_pipefds_); | |
| 30 ASSERT_EQ(0, ret); | |
| 31 } | |
| 32 | |
| 33 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"; | |
| 38 } | |
| 39 | |
| 40 MessageLoop* ui_loop() { return &ui_loop_; } | |
| 41 MessageLoopForIO* io_loop() const { | |
| 42 return static_cast<MessageLoopForIO*>(io_thread_.message_loop()); | |
| 43 } | |
| 44 | |
| 45 void HandleFdIOEvent(MessageLoopForIO::FileDescriptorWatcher* watcher) { | |
| 46 MessagePumpIOSForIO::HandleFdIOEvent(watcher->fdref_, | |
| 47 kCFFileDescriptorReadCallBack | kCFFileDescriptorWriteCallBack, | |
| 48 watcher); | |
| 49 } | |
| 50 | |
| 51 int pipefds_[2]; | |
| 52 int alternate_pipefds_[2]; | |
| 53 | |
| 54 private: | |
| 55 MessageLoop ui_loop_; | |
| 56 Thread io_thread_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(MessagePumpIOSForIOTest); | |
| 59 }; | |
| 60 | |
| 61 namespace { | |
| 62 | |
| 63 // Concrete implementation of MessagePumpIOSForIO::Watcher that does | |
| 64 // nothing useful. | |
| 65 class StupidWatcher : public MessagePumpIOSForIO::Watcher { | |
| 66 public: | |
| 67 ~StupidWatcher() override {} | |
| 68 | |
| 69 // base:MessagePumpIOSForIO::Watcher interface | |
| 70 void OnFileCanReadWithoutBlocking(int fd) override {} | |
| 71 void OnFileCanWriteWithoutBlocking(int fd) override {} | |
| 72 }; | |
| 73 | |
| 74 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | |
| 75 | |
| 76 // Test to make sure that we catch calling WatchFileDescriptor off of the | |
| 77 // wrong thread. | |
| 78 TEST_F(MessagePumpIOSForIOTest, TestWatchingFromBadThread) { | |
| 79 MessagePumpIOSForIO::FileDescriptorWatcher watcher; | |
| 80 StupidWatcher delegate; | |
| 81 | |
| 82 ASSERT_DEBUG_DEATH(io_loop()->WatchFileDescriptor( | |
| 83 STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate), | |
| 84 "Check failed: " | |
| 85 "watch_file_descriptor_caller_checker_.CalledOnValidThread\\(\\)"); | |
| 86 } | |
| 87 | |
| 88 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | |
| 89 | |
| 90 class BaseWatcher : public MessagePumpIOSForIO::Watcher { | |
| 91 public: | |
| 92 BaseWatcher(MessagePumpIOSForIO::FileDescriptorWatcher* controller) | |
| 93 : controller_(controller) { | |
| 94 DCHECK(controller_); | |
| 95 } | |
| 96 ~BaseWatcher() override {} | |
| 97 | |
| 98 // MessagePumpIOSForIO::Watcher interface | |
| 99 void OnFileCanReadWithoutBlocking(int /* fd */) override { NOTREACHED(); } | |
| 100 | |
| 101 void OnFileCanWriteWithoutBlocking(int /* fd */) override { NOTREACHED(); } | |
| 102 | |
| 103 protected: | |
| 104 MessagePumpIOSForIO::FileDescriptorWatcher* controller_; | |
| 105 }; | |
| 106 | |
| 107 class DeleteWatcher : public BaseWatcher { | |
| 108 public: | |
| 109 explicit DeleteWatcher( | |
| 110 MessagePumpIOSForIO::FileDescriptorWatcher* controller) | |
| 111 : BaseWatcher(controller) {} | |
| 112 | |
| 113 ~DeleteWatcher() override { DCHECK(!controller_); } | |
| 114 | |
| 115 void OnFileCanWriteWithoutBlocking(int /* fd */) override { | |
| 116 DCHECK(controller_); | |
| 117 delete controller_; | |
| 118 controller_ = NULL; | |
| 119 } | |
| 120 }; | |
| 121 | |
| 122 TEST_F(MessagePumpIOSForIOTest, DeleteWatcher) { | |
| 123 scoped_ptr<MessagePumpIOSForIO> pump(new MessagePumpIOSForIO); | |
| 124 MessagePumpIOSForIO::FileDescriptorWatcher* watcher = | |
| 125 new MessagePumpIOSForIO::FileDescriptorWatcher; | |
| 126 DeleteWatcher delegate(watcher); | |
| 127 pump->WatchFileDescriptor(pipefds_[1], | |
| 128 false, MessagePumpIOSForIO::WATCH_READ_WRITE, watcher, &delegate); | |
| 129 | |
| 130 // Spoof a callback. | |
| 131 HandleFdIOEvent(watcher); | |
| 132 } | |
| 133 | |
| 134 class StopWatcher : public BaseWatcher { | |
| 135 public: | |
| 136 StopWatcher(MessagePumpIOSForIO::FileDescriptorWatcher* controller, | |
| 137 MessagePumpIOSForIO* pump, | |
| 138 int fd_to_start_watching = -1) | |
| 139 : BaseWatcher(controller), | |
| 140 pump_(pump), | |
| 141 fd_to_start_watching_(fd_to_start_watching) {} | |
| 142 | |
| 143 ~StopWatcher() override {} | |
| 144 | |
| 145 void OnFileCanWriteWithoutBlocking(int /* fd */) override { | |
| 146 controller_->StopWatchingFileDescriptor(); | |
| 147 if (fd_to_start_watching_ >= 0) { | |
| 148 pump_->WatchFileDescriptor(fd_to_start_watching_, | |
| 149 false, MessagePumpIOSForIO::WATCH_READ_WRITE, controller_, this); | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 private: | |
| 154 MessagePumpIOSForIO* pump_; | |
| 155 int fd_to_start_watching_; | |
| 156 }; | |
| 157 | |
| 158 TEST_F(MessagePumpIOSForIOTest, StopWatcher) { | |
| 159 scoped_ptr<MessagePumpIOSForIO> pump(new MessagePumpIOSForIO); | |
| 160 MessagePumpIOSForIO::FileDescriptorWatcher watcher; | |
| 161 StopWatcher delegate(&watcher, pump.get()); | |
| 162 pump->WatchFileDescriptor(pipefds_[1], | |
| 163 false, MessagePumpIOSForIO::WATCH_READ_WRITE, &watcher, &delegate); | |
| 164 | |
| 165 // Spoof a callback. | |
| 166 HandleFdIOEvent(&watcher); | |
| 167 } | |
| 168 | |
| 169 TEST_F(MessagePumpIOSForIOTest, StopWatcherAndWatchSomethingElse) { | |
| 170 scoped_ptr<MessagePumpIOSForIO> pump(new MessagePumpIOSForIO); | |
| 171 MessagePumpIOSForIO::FileDescriptorWatcher watcher; | |
| 172 StopWatcher delegate(&watcher, pump.get(), alternate_pipefds_[1]); | |
| 173 pump->WatchFileDescriptor(pipefds_[1], | |
| 174 false, MessagePumpIOSForIO::WATCH_READ_WRITE, &watcher, &delegate); | |
| 175 | |
| 176 // Spoof a callback. | |
| 177 HandleFdIOEvent(&watcher); | |
| 178 } | |
| 179 | |
| 180 } // namespace | |
| 181 | |
| 182 } // namespace base | |
| OLD | NEW |