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 enum Type { |
81 MessagePumpLibevent::FileDescriptorWatcher* controller) | 92 TYPE_DELETE, |
82 : controller_(controller) { | 93 TYPE_STOP |
94 }; | |
95 | |
96 explicit BaseWatcher( | |
jar (doing other things)
2012/07/10 17:02:10
[if you don't kill type_]...
nit: don't "explicit
Shouqun Liu
2012/07/11 00:49:37
Done.
| |
97 MessagePumpLibevent::FileDescriptorWatcher* controller, Type type) | |
98 : controller_(controller), type_(type) { | |
jar (doing other things)
2012/07/10 17:02:10
[if you don't kill type_]...
nit: one initializat
Shouqun Liu
2012/07/11 00:49:37
Remove the type, done.
| |
83 DCHECK(controller_); | 99 DCHECK(controller_); |
84 } | 100 } |
85 virtual ~DeleteWatcher() { | 101 virtual ~BaseWatcher() {} |
86 DCHECK(!controller_); | |
87 } | |
88 | 102 |
89 // base:MessagePumpLibevent::Watcher interface | 103 // base:MessagePumpLibevent::Watcher interface |
90 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { | 104 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { |
91 NOTREACHED(); | 105 NOTREACHED(); |
92 } | 106 } |
107 | |
108 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { | |
109 NOTREACHED(); | |
110 } | |
111 | |
112 protected: | |
113 MessagePumpLibevent::FileDescriptorWatcher* controller_; | |
114 Type type_; | |
115 }; | |
116 | |
117 class DeleteWatcher : public BaseWatcher { | |
118 public: | |
119 explicit DeleteWatcher( | |
120 MessagePumpLibevent::FileDescriptorWatcher* controller) | |
121 : BaseWatcher(controller, TYPE_DELETE) {} | |
122 | |
123 virtual ~DeleteWatcher() { | |
124 DCHECK(!controller_); | |
125 } | |
126 | |
93 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { | 127 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { |
94 DCHECK(controller_); | 128 DCHECK(controller_); |
95 delete controller_; | 129 delete controller_; |
96 controller_ = NULL; | 130 controller_ = NULL; |
97 } | 131 } |
98 | |
99 private: | |
100 MessagePumpLibevent::FileDescriptorWatcher* controller_; | |
101 }; | 132 }; |
102 | 133 |
103 TEST_F(MessagePumpLibeventTest, DeleteWatcher) { | 134 TEST_F(MessagePumpLibeventTest, DeleteWatcher) { |
104 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | 135 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
105 MessagePumpLibevent::FileDescriptorWatcher* watcher = | 136 MessagePumpLibevent::FileDescriptorWatcher* watcher = |
106 new MessagePumpLibevent::FileDescriptorWatcher; | 137 new MessagePumpLibevent::FileDescriptorWatcher; |
107 DeleteWatcher delegate(watcher); | 138 DeleteWatcher delegate(watcher); |
108 pump->WatchFileDescriptor( | 139 pump->WatchFileDescriptor(pipefds_[1], |
109 0, false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate); | 140 false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate); |
110 | 141 |
111 // Spoof a libevent notification. | 142 // Spoof a libevent notification. |
112 OnLibeventNotification(pump, watcher); | 143 OnLibeventNotification(pump, watcher); |
113 } | 144 } |
114 | 145 |
115 class StopWatcher : public MessagePumpLibevent::Watcher { | 146 class StopWatcher : public BaseWatcher { |
116 public: | 147 public: |
117 explicit StopWatcher( | 148 explicit StopWatcher( |
118 MessagePumpLibevent::FileDescriptorWatcher* controller) | 149 MessagePumpLibevent::FileDescriptorWatcher* controller) |
119 : controller_(controller) { | 150 : BaseWatcher(controller, TYPE_STOP) {} |
120 DCHECK(controller_); | 151 |
121 } | |
122 virtual ~StopWatcher() {} | 152 virtual ~StopWatcher() {} |
123 | 153 |
124 // base:MessagePumpLibevent::Watcher interface | |
125 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { | |
126 NOTREACHED(); | |
127 } | |
128 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { | 154 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { |
129 controller_->StopWatchingFileDescriptor(); | 155 controller_->StopWatchingFileDescriptor(); |
130 } | 156 } |
131 | |
132 private: | |
133 MessagePumpLibevent::FileDescriptorWatcher* const controller_; | |
134 }; | 157 }; |
135 | 158 |
136 TEST_F(MessagePumpLibeventTest, StopWatcher) { | 159 TEST_F(MessagePumpLibeventTest, StopWatcher) { |
137 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | 160 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
138 MessagePumpLibevent::FileDescriptorWatcher watcher; | 161 MessagePumpLibevent::FileDescriptorWatcher watcher; |
139 StopWatcher delegate(&watcher); | 162 StopWatcher delegate(&watcher); |
140 pump->WatchFileDescriptor( | 163 pump->WatchFileDescriptor(pipefds_[1], |
141 0, false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate); | 164 false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate); |
142 | 165 |
143 // Spoof a libevent notification. | 166 // Spoof a libevent notification. |
144 OnLibeventNotification(pump, &watcher); | 167 OnLibeventNotification(pump, &watcher); |
145 } | 168 } |
146 | 169 |
147 } // namespace | 170 } // namespace |
148 | 171 |
149 } // namespace base | 172 } // namespace base |
OLD | NEW |