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_loop/message_pump_libevent.h" | 5 #include "base/message_loop/message_pump_libevent.h" |
6 | 6 |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 MessageLoop ui_loop_; | 55 MessageLoop ui_loop_; |
56 Thread io_thread_; | 56 Thread io_thread_; |
57 }; | 57 }; |
58 | 58 |
59 namespace { | 59 namespace { |
60 | 60 |
61 // Concrete implementation of MessagePumpLibevent::Watcher that does | 61 // Concrete implementation of MessagePumpLibevent::Watcher that does |
62 // nothing useful. | 62 // nothing useful. |
63 class StupidWatcher : public MessagePumpLibevent::Watcher { | 63 class StupidWatcher : public MessagePumpLibevent::Watcher { |
64 public: | 64 public: |
65 virtual ~StupidWatcher() {} | 65 ~StupidWatcher() override {} |
66 | 66 |
67 // base:MessagePumpLibevent::Watcher interface | 67 // base:MessagePumpLibevent::Watcher interface |
68 virtual void OnFileCanReadWithoutBlocking(int fd) override {} | 68 void OnFileCanReadWithoutBlocking(int fd) override {} |
69 virtual void OnFileCanWriteWithoutBlocking(int fd) override {} | 69 void OnFileCanWriteWithoutBlocking(int fd) override {} |
70 }; | 70 }; |
71 | 71 |
72 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | 72 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
73 | 73 |
74 // Test to make sure that we catch calling WatchFileDescriptor off of the | 74 // Test to make sure that we catch calling WatchFileDescriptor off of the |
75 // wrong thread. | 75 // wrong thread. |
76 TEST_F(MessagePumpLibeventTest, TestWatchingFromBadThread) { | 76 TEST_F(MessagePumpLibeventTest, TestWatchingFromBadThread) { |
77 MessagePumpLibevent::FileDescriptorWatcher watcher; | 77 MessagePumpLibevent::FileDescriptorWatcher watcher; |
78 StupidWatcher delegate; | 78 StupidWatcher delegate; |
79 | 79 |
(...skipping 10 matching lines...) Expand all Loading... |
90 } | 90 } |
91 | 91 |
92 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | 92 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
93 | 93 |
94 class BaseWatcher : public MessagePumpLibevent::Watcher { | 94 class BaseWatcher : public MessagePumpLibevent::Watcher { |
95 public: | 95 public: |
96 explicit BaseWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller) | 96 explicit BaseWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller) |
97 : controller_(controller) { | 97 : controller_(controller) { |
98 DCHECK(controller_); | 98 DCHECK(controller_); |
99 } | 99 } |
100 virtual ~BaseWatcher() {} | 100 ~BaseWatcher() override {} |
101 | 101 |
102 // base:MessagePumpLibevent::Watcher interface | 102 // base:MessagePumpLibevent::Watcher interface |
103 virtual void OnFileCanReadWithoutBlocking(int /* fd */) override { | 103 void OnFileCanReadWithoutBlocking(int /* fd */) override { NOTREACHED(); } |
104 NOTREACHED(); | |
105 } | |
106 | 104 |
107 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) override { | 105 void OnFileCanWriteWithoutBlocking(int /* fd */) override { NOTREACHED(); } |
108 NOTREACHED(); | |
109 } | |
110 | 106 |
111 protected: | 107 protected: |
112 MessagePumpLibevent::FileDescriptorWatcher* controller_; | 108 MessagePumpLibevent::FileDescriptorWatcher* controller_; |
113 }; | 109 }; |
114 | 110 |
115 class DeleteWatcher : public BaseWatcher { | 111 class DeleteWatcher : public BaseWatcher { |
116 public: | 112 public: |
117 explicit DeleteWatcher( | 113 explicit DeleteWatcher( |
118 MessagePumpLibevent::FileDescriptorWatcher* controller) | 114 MessagePumpLibevent::FileDescriptorWatcher* controller) |
119 : BaseWatcher(controller) {} | 115 : BaseWatcher(controller) {} |
120 | 116 |
121 virtual ~DeleteWatcher() { | 117 ~DeleteWatcher() override { DCHECK(!controller_); } |
122 DCHECK(!controller_); | |
123 } | |
124 | 118 |
125 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) override { | 119 void OnFileCanWriteWithoutBlocking(int /* fd */) override { |
126 DCHECK(controller_); | 120 DCHECK(controller_); |
127 delete controller_; | 121 delete controller_; |
128 controller_ = NULL; | 122 controller_ = NULL; |
129 } | 123 } |
130 }; | 124 }; |
131 | 125 |
132 TEST_F(MessagePumpLibeventTest, DeleteWatcher) { | 126 TEST_F(MessagePumpLibeventTest, DeleteWatcher) { |
133 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | 127 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
134 MessagePumpLibevent::FileDescriptorWatcher* watcher = | 128 MessagePumpLibevent::FileDescriptorWatcher* watcher = |
135 new MessagePumpLibevent::FileDescriptorWatcher; | 129 new MessagePumpLibevent::FileDescriptorWatcher; |
136 DeleteWatcher delegate(watcher); | 130 DeleteWatcher delegate(watcher); |
137 pump->WatchFileDescriptor(pipefds_[1], | 131 pump->WatchFileDescriptor(pipefds_[1], |
138 false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate); | 132 false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate); |
139 | 133 |
140 // Spoof a libevent notification. | 134 // Spoof a libevent notification. |
141 OnLibeventNotification(pump.get(), watcher); | 135 OnLibeventNotification(pump.get(), watcher); |
142 } | 136 } |
143 | 137 |
144 class StopWatcher : public BaseWatcher { | 138 class StopWatcher : public BaseWatcher { |
145 public: | 139 public: |
146 explicit StopWatcher( | 140 explicit StopWatcher( |
147 MessagePumpLibevent::FileDescriptorWatcher* controller) | 141 MessagePumpLibevent::FileDescriptorWatcher* controller) |
148 : BaseWatcher(controller) {} | 142 : BaseWatcher(controller) {} |
149 | 143 |
150 virtual ~StopWatcher() {} | 144 ~StopWatcher() override {} |
151 | 145 |
152 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) override { | 146 void OnFileCanWriteWithoutBlocking(int /* fd */) override { |
153 controller_->StopWatchingFileDescriptor(); | 147 controller_->StopWatchingFileDescriptor(); |
154 } | 148 } |
155 }; | 149 }; |
156 | 150 |
157 TEST_F(MessagePumpLibeventTest, StopWatcher) { | 151 TEST_F(MessagePumpLibeventTest, StopWatcher) { |
158 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | 152 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
159 MessagePumpLibevent::FileDescriptorWatcher watcher; | 153 MessagePumpLibevent::FileDescriptorWatcher watcher; |
160 StopWatcher delegate(&watcher); | 154 StopWatcher delegate(&watcher); |
161 pump->WatchFileDescriptor(pipefds_[1], | 155 pump->WatchFileDescriptor(pipefds_[1], |
162 false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate); | 156 false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate); |
163 | 157 |
164 // Spoof a libevent notification. | 158 // Spoof a libevent notification. |
165 OnLibeventNotification(pump.get(), &watcher); | 159 OnLibeventNotification(pump.get(), &watcher); |
166 } | 160 } |
167 | 161 |
168 void QuitMessageLoopAndStart(const Closure& quit_closure) { | 162 void QuitMessageLoopAndStart(const Closure& quit_closure) { |
169 quit_closure.Run(); | 163 quit_closure.Run(); |
170 | 164 |
171 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | 165 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
172 RunLoop runloop; | 166 RunLoop runloop; |
173 MessageLoop::current()->PostTask(FROM_HERE, runloop.QuitClosure()); | 167 MessageLoop::current()->PostTask(FROM_HERE, runloop.QuitClosure()); |
174 runloop.Run(); | 168 runloop.Run(); |
175 } | 169 } |
176 | 170 |
177 class NestedPumpWatcher : public MessagePumpLibevent::Watcher { | 171 class NestedPumpWatcher : public MessagePumpLibevent::Watcher { |
178 public: | 172 public: |
179 NestedPumpWatcher() {} | 173 NestedPumpWatcher() {} |
180 virtual ~NestedPumpWatcher() {} | 174 ~NestedPumpWatcher() override {} |
181 | 175 |
182 virtual void OnFileCanReadWithoutBlocking(int /* fd */) override { | 176 void OnFileCanReadWithoutBlocking(int /* fd */) override { |
183 RunLoop runloop; | 177 RunLoop runloop; |
184 MessageLoop::current()->PostTask(FROM_HERE, Bind(&QuitMessageLoopAndStart, | 178 MessageLoop::current()->PostTask(FROM_HERE, Bind(&QuitMessageLoopAndStart, |
185 runloop.QuitClosure())); | 179 runloop.QuitClosure())); |
186 runloop.Run(); | 180 runloop.Run(); |
187 } | 181 } |
188 | 182 |
189 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) override {} | 183 void OnFileCanWriteWithoutBlocking(int /* fd */) override {} |
190 }; | 184 }; |
191 | 185 |
192 TEST_F(MessagePumpLibeventTest, NestedPumpWatcher) { | 186 TEST_F(MessagePumpLibeventTest, NestedPumpWatcher) { |
193 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | 187 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
194 MessagePumpLibevent::FileDescriptorWatcher watcher; | 188 MessagePumpLibevent::FileDescriptorWatcher watcher; |
195 NestedPumpWatcher delegate; | 189 NestedPumpWatcher delegate; |
196 pump->WatchFileDescriptor(pipefds_[1], | 190 pump->WatchFileDescriptor(pipefds_[1], |
197 false, MessagePumpLibevent::WATCH_READ, &watcher, &delegate); | 191 false, MessagePumpLibevent::WATCH_READ, &watcher, &delegate); |
198 | 192 |
199 // Spoof a libevent notification. | 193 // Spoof a libevent notification. |
200 OnLibeventNotification(pump.get(), &watcher); | 194 OnLibeventNotification(pump.get(), &watcher); |
201 } | 195 } |
202 | 196 |
203 } // namespace | 197 } // namespace |
204 | 198 |
205 } // namespace base | 199 } // namespace base |
OLD | NEW |