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