| OLD | NEW |
| 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_pump_libevent.h" | 5 #include "base/message_pump_libevent.h" |
| 6 | 6 |
| 7 #include <unistd.h> | 7 #include <unistd.h> |
| 8 | 8 |
| 9 #include "base/eintr_wrapper.h" |
| 9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 10 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 #if defined(USE_SYSTEM_LIBEVENT) | 14 #if defined(USE_SYSTEM_LIBEVENT) |
| 14 #include <event.h> | 15 #include <event.h> |
| 15 #else | 16 #else |
| 16 #include "third_party/libevent/event.h" | 17 #include "third_party/libevent/event.h" |
| 17 #endif | 18 #endif |
| 18 | 19 |
| 19 namespace base { | 20 namespace base { |
| 20 | 21 |
| 21 class MessagePumpLibeventTest : public testing::Test { | 22 class MessagePumpLibeventTest : public testing::Test { |
| 22 protected: | 23 protected: |
| 23 MessagePumpLibeventTest() | 24 MessagePumpLibeventTest() |
| 24 : ui_loop_(MessageLoop::TYPE_UI), | 25 : ui_loop_(MessageLoop::TYPE_UI), |
| 25 io_thread_("MessagePumpLibeventTestIOThread") {} | 26 io_thread_("MessagePumpLibeventTestIOThread") {} |
| 26 virtual ~MessagePumpLibeventTest() {} | 27 virtual ~MessagePumpLibeventTest() {} |
| 27 | 28 |
| 28 virtual void SetUp() OVERRIDE { | 29 virtual void SetUp() OVERRIDE { |
| 29 Thread::Options options(MessageLoop::TYPE_IO, 0); | 30 Thread::Options options(MessageLoop::TYPE_IO, 0); |
| 30 ASSERT_TRUE(io_thread_.StartWithOptions(options)); | 31 ASSERT_TRUE(io_thread_.StartWithOptions(options)); |
| 31 ASSERT_EQ(MessageLoop::TYPE_IO, io_thread_.message_loop()->type()); | 32 ASSERT_EQ(MessageLoop::TYPE_IO, io_thread_.message_loop()->type()); |
| 33 int err = pipe(pipefds_); |
| 34 ASSERT_EQ(0, err); |
| 35 } |
| 36 |
| 37 virtual void TearDown() OVERRIDE { |
| 38 if (HANDLE_EINTR(close(pipefds_[0])) < 0) |
| 39 PLOG(ERROR) << "close"; |
| 40 if (HANDLE_EINTR(close(pipefds_[1])) < 0) |
| 41 PLOG(ERROR) << "close"; |
| 32 } | 42 } |
| 33 | 43 |
| 34 MessageLoop* ui_loop() { return &ui_loop_; } | 44 MessageLoop* ui_loop() { return &ui_loop_; } |
| 35 MessageLoopForIO* io_loop() const { | 45 MessageLoopForIO* io_loop() const { |
| 36 return static_cast<MessageLoopForIO*>(io_thread_.message_loop()); | 46 return static_cast<MessageLoopForIO*>(io_thread_.message_loop()); |
| 37 } | 47 } |
| 38 | 48 |
| 39 void OnLibeventNotification( | 49 void OnLibeventNotification( |
| 40 MessagePumpLibevent* pump, | 50 MessagePumpLibevent* pump, |
| 41 MessagePumpLibevent::FileDescriptorWatcher* controller) { | 51 MessagePumpLibevent::FileDescriptorWatcher* controller) { |
| 42 pump->OnLibeventNotification(0, EV_WRITE | EV_READ, controller); | 52 pump->OnLibeventNotification(0, EV_WRITE | EV_READ, controller); |
| 43 } | 53 } |
| 44 | 54 |
| 45 MessageLoop ui_loop_; | 55 MessageLoop ui_loop_; |
| 46 Thread io_thread_; | 56 Thread io_thread_; |
| 57 int pipefds_[2]; |
| 47 }; | 58 }; |
| 48 | 59 |
| 49 namespace { | 60 namespace { |
| 50 | 61 |
| 51 // Concrete implementation of MessagePumpLibevent::Watcher that does | 62 // Concrete implementation of MessagePumpLibevent::Watcher that does |
| 52 // nothing useful. | 63 // nothing useful. |
| 53 class StupidWatcher : public MessagePumpLibevent::Watcher { | 64 class StupidWatcher : public MessagePumpLibevent::Watcher { |
| 54 public: | 65 public: |
| 55 virtual ~StupidWatcher() {} | 66 virtual ~StupidWatcher() {} |
| 56 | 67 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 68 StupidWatcher delegate; | 79 StupidWatcher delegate; |
| 69 | 80 |
| 70 ASSERT_DEATH(io_loop()->WatchFileDescriptor( | 81 ASSERT_DEATH(io_loop()->WatchFileDescriptor( |
| 71 STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate), | 82 STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate), |
| 72 "Check failed: " | 83 "Check failed: " |
| 73 "watch_file_descriptor_caller_checker_.CalledOnValidThread()"); | 84 "watch_file_descriptor_caller_checker_.CalledOnValidThread()"); |
| 74 } | 85 } |
| 75 | 86 |
| 76 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | 87 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
| 77 | 88 |
| 78 class DeleteWatcher : public MessagePumpLibevent::Watcher { | 89 class BaseWatcher : public MessagePumpLibevent::Watcher { |
| 79 public: | 90 public: |
| 80 explicit DeleteWatcher( | 91 BaseWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller) |
| 81 MessagePumpLibevent::FileDescriptorWatcher* controller) | |
| 82 : controller_(controller) { | 92 : controller_(controller) { |
| 83 DCHECK(controller_); | 93 DCHECK(controller_); |
| 84 } | 94 } |
| 85 virtual ~DeleteWatcher() { | 95 virtual ~BaseWatcher() {} |
| 86 DCHECK(!controller_); | |
| 87 } | |
| 88 | 96 |
| 89 // base:MessagePumpLibevent::Watcher interface | 97 // base:MessagePumpLibevent::Watcher interface |
| 90 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { | 98 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { |
| 91 NOTREACHED(); | 99 NOTREACHED(); |
| 92 } | 100 } |
| 101 |
| 102 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { |
| 103 NOTREACHED(); |
| 104 } |
| 105 |
| 106 protected: |
| 107 MessagePumpLibevent::FileDescriptorWatcher* controller_; |
| 108 }; |
| 109 |
| 110 class DeleteWatcher : public BaseWatcher { |
| 111 public: |
| 112 explicit DeleteWatcher( |
| 113 MessagePumpLibevent::FileDescriptorWatcher* controller) |
| 114 : BaseWatcher(controller) {} |
| 115 |
| 116 virtual ~DeleteWatcher() { |
| 117 DCHECK(!controller_); |
| 118 } |
| 119 |
| 93 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { | 120 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { |
| 94 DCHECK(controller_); | 121 DCHECK(controller_); |
| 95 delete controller_; | 122 delete controller_; |
| 96 controller_ = NULL; | 123 controller_ = NULL; |
| 97 } | 124 } |
| 98 | |
| 99 private: | |
| 100 MessagePumpLibevent::FileDescriptorWatcher* controller_; | |
| 101 }; | 125 }; |
| 102 | 126 |
| 103 TEST_F(MessagePumpLibeventTest, DeleteWatcher) { | 127 TEST_F(MessagePumpLibeventTest, DeleteWatcher) { |
| 104 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | 128 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
| 105 MessagePumpLibevent::FileDescriptorWatcher* watcher = | 129 MessagePumpLibevent::FileDescriptorWatcher* watcher = |
| 106 new MessagePumpLibevent::FileDescriptorWatcher; | 130 new MessagePumpLibevent::FileDescriptorWatcher; |
| 107 DeleteWatcher delegate(watcher); | 131 DeleteWatcher delegate(watcher); |
| 108 pump->WatchFileDescriptor( | 132 pump->WatchFileDescriptor(pipefds_[1], |
| 109 0, false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate); | 133 false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate); |
| 110 | 134 |
| 111 // Spoof a libevent notification. | 135 // Spoof a libevent notification. |
| 112 OnLibeventNotification(pump, watcher); | 136 OnLibeventNotification(pump, watcher); |
| 113 } | 137 } |
| 114 | 138 |
| 115 class StopWatcher : public MessagePumpLibevent::Watcher { | 139 class StopWatcher : public BaseWatcher { |
| 116 public: | 140 public: |
| 117 explicit StopWatcher( | 141 explicit StopWatcher( |
| 118 MessagePumpLibevent::FileDescriptorWatcher* controller) | 142 MessagePumpLibevent::FileDescriptorWatcher* controller) |
| 119 : controller_(controller) { | 143 : BaseWatcher(controller) {} |
| 120 DCHECK(controller_); | 144 |
| 121 } | |
| 122 virtual ~StopWatcher() {} | 145 virtual ~StopWatcher() {} |
| 123 | 146 |
| 124 // base:MessagePumpLibevent::Watcher interface | |
| 125 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { | |
| 126 NOTREACHED(); | |
| 127 } | |
| 128 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { | 147 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { |
| 129 controller_->StopWatchingFileDescriptor(); | 148 controller_->StopWatchingFileDescriptor(); |
| 130 } | 149 } |
| 131 | |
| 132 private: | |
| 133 MessagePumpLibevent::FileDescriptorWatcher* const controller_; | |
| 134 }; | 150 }; |
| 135 | 151 |
| 136 TEST_F(MessagePumpLibeventTest, StopWatcher) { | 152 TEST_F(MessagePumpLibeventTest, StopWatcher) { |
| 137 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | 153 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
| 138 MessagePumpLibevent::FileDescriptorWatcher watcher; | 154 MessagePumpLibevent::FileDescriptorWatcher watcher; |
| 139 StopWatcher delegate(&watcher); | 155 StopWatcher delegate(&watcher); |
| 140 pump->WatchFileDescriptor( | 156 pump->WatchFileDescriptor(pipefds_[1], |
| 141 0, false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate); | 157 false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate); |
| 142 | 158 |
| 143 // Spoof a libevent notification. | 159 // Spoof a libevent notification. |
| 144 OnLibeventNotification(pump, &watcher); | 160 OnLibeventNotification(pump, &watcher); |
| 145 } | 161 } |
| 146 | 162 |
| 147 } // namespace | 163 } // namespace |
| 148 | 164 |
| 149 } // namespace base | 165 } // namespace base |
| OLD | NEW |