OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "mojo/common/handle_watcher.h" | 5 #include "mojo/common/handle_watcher.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/test/simple_test_tick_clock.h" | 14 #include "base/test/simple_test_tick_clock.h" |
15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
16 #include "mojo/common/message_pump_mojo.h" | |
16 #include "mojo/common/time_helper.h" | 17 #include "mojo/common/time_helper.h" |
17 #include "mojo/public/cpp/system/core.h" | 18 #include "mojo/public/cpp/system/core.h" |
18 #include "mojo/public/cpp/test_support/test_utils.h" | 19 #include "mojo/public/cpp/test_support/test_utils.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
21 namespace mojo { | 22 namespace mojo { |
22 namespace common { | 23 namespace common { |
23 namespace test { | 24 namespace test { |
24 | 25 |
26 enum MessageLoopConfig { | |
27 MESSAGE_LOOP_CONFIG_DEFAULT = 0, | |
28 MESSAGE_LOOP_CONFIG_MOJO = 1, | |
29 MESSAGE_LOOP_CONFIG_COUNT = 2 | |
30 }; | |
31 | |
25 void ObserveCallback(bool* was_signaled, | 32 void ObserveCallback(bool* was_signaled, |
26 MojoResult* result_observed, | 33 MojoResult* result_observed, |
27 MojoResult result) { | 34 MojoResult result) { |
28 *was_signaled = true; | 35 *was_signaled = true; |
29 *result_observed = result; | 36 *result_observed = result; |
30 } | 37 } |
31 | 38 |
32 void RunUntilIdle() { | 39 void RunUntilIdle() { |
33 base::RunLoop run_loop; | 40 base::RunLoop run_loop; |
34 run_loop.RunUntilIdle(); | 41 run_loop.RunUntilIdle(); |
35 } | 42 } |
36 | 43 |
37 void DeleteWatcherAndForwardResult( | 44 void DeleteWatcherAndForwardResult( |
38 HandleWatcher* watcher, | 45 HandleWatcher* watcher, |
39 base::Callback<void(MojoResult)> next_callback, | 46 base::Callback<void(MojoResult)> next_callback, |
40 MojoResult result) { | 47 MojoResult result) { |
41 delete watcher; | 48 delete watcher; |
42 next_callback.Run(result); | 49 next_callback.Run(result); |
43 } | 50 } |
44 | 51 |
52 scoped_ptr<base::MessageLoop> CreateMessageLoop(MessageLoopConfig config) { | |
53 scoped_ptr<base::MessageLoop> loop; | |
54 switch (config) { | |
55 case MESSAGE_LOOP_CONFIG_DEFAULT: | |
56 loop.reset(new base::MessageLoop()); | |
57 break; | |
58 case MESSAGE_LOOP_CONFIG_MOJO: | |
59 loop.reset(new base::MessageLoop(MessagePumpMojo::Create())); | |
60 break; | |
61 case MESSAGE_LOOP_CONFIG_COUNT: | |
62 break; | |
sky
2014/08/28 23:12:19
NOTREACHED
yzshen1
2014/08/29 00:23:45
This 'case' is removed entirely.
| |
63 } | |
64 return loop.Pass(); | |
65 } | |
66 | |
45 // Helper class to manage the callback and running the message loop waiting for | 67 // Helper class to manage the callback and running the message loop waiting for |
46 // message to be received. Typical usage is something like: | 68 // message to be received. Typical usage is something like: |
47 // Schedule callback returned from GetCallback(). | 69 // Schedule callback returned from GetCallback(). |
48 // RunUntilGotCallback(); | 70 // RunUntilGotCallback(); |
49 // EXPECT_TRUE(got_callback()); | 71 // EXPECT_TRUE(got_callback()); |
50 // clear_callback(); | 72 // clear_callback(); |
51 class CallbackHelper { | 73 class CallbackHelper { |
52 public: | 74 public: |
53 CallbackHelper() | 75 CallbackHelper() |
54 : got_callback_(false), | 76 : got_callback_(false), |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 base::RunLoop* run_loop_; | 120 base::RunLoop* run_loop_; |
99 | 121 |
100 base::WeakPtrFactory<CallbackHelper> weak_factory_; | 122 base::WeakPtrFactory<CallbackHelper> weak_factory_; |
101 | 123 |
102 private: | 124 private: |
103 DISALLOW_COPY_AND_ASSIGN(CallbackHelper); | 125 DISALLOW_COPY_AND_ASSIGN(CallbackHelper); |
104 }; | 126 }; |
105 | 127 |
106 class HandleWatcherTest : public testing::Test { | 128 class HandleWatcherTest : public testing::Test { |
107 public: | 129 public: |
108 HandleWatcherTest() {} | 130 HandleWatcherTest() : next_message_loop_config_(MESSAGE_LOOP_CONFIG_DEFAULT) { |
131 } | |
109 virtual ~HandleWatcherTest() { | 132 virtual ~HandleWatcherTest() { |
110 test::SetTickClockForTest(NULL); | 133 test::SetTickClockForTest(NULL); |
111 } | 134 } |
112 | 135 |
113 protected: | 136 protected: |
137 bool NextTestConfig() { | |
138 if (next_message_loop_config_ == MESSAGE_LOOP_CONFIG_COUNT) | |
139 return false; | |
140 | |
141 // We have to destroy the current message loop before we create a new one. | |
142 message_loop_.reset(); | |
143 | |
144 message_loop_ = CreateMessageLoop(next_message_loop_config_); | |
145 next_message_loop_config_ = static_cast<MessageLoopConfig>( | |
146 next_message_loop_config_ + 1); | |
147 return true; | |
148 } | |
149 | |
114 void InstallTickClock() { | 150 void InstallTickClock() { |
115 test::SetTickClockForTest(&tick_clock_); | 151 test::SetTickClockForTest(&tick_clock_); |
116 } | 152 } |
117 | 153 |
118 base::SimpleTestTickClock tick_clock_; | 154 base::SimpleTestTickClock tick_clock_; |
119 | 155 |
120 private: | 156 private: |
121 base::ShadowingAtExitManager at_exit_; | 157 base::ShadowingAtExitManager at_exit_; |
122 base::MessageLoop message_loop_; | 158 scoped_ptr<base::MessageLoop> message_loop_; |
159 MessageLoopConfig next_message_loop_config_; | |
123 | 160 |
124 DISALLOW_COPY_AND_ASSIGN(HandleWatcherTest); | 161 DISALLOW_COPY_AND_ASSIGN(HandleWatcherTest); |
125 }; | 162 }; |
126 | 163 |
127 // Trivial test case with a single handle to watch. | 164 // Trivial test case with a single handle to watch. |
128 TEST_F(HandleWatcherTest, SingleHandler) { | 165 TEST_F(HandleWatcherTest, SingleHandler) { |
129 MessagePipe test_pipe; | 166 while (NextTestConfig()) { |
130 ASSERT_TRUE(test_pipe.handle0.is_valid()); | 167 MessagePipe test_pipe; |
131 CallbackHelper callback_helper; | 168 ASSERT_TRUE(test_pipe.handle0.is_valid()); |
132 HandleWatcher watcher; | 169 CallbackHelper callback_helper; |
133 callback_helper.Start(&watcher, test_pipe.handle0.get()); | 170 HandleWatcher watcher; |
134 RunUntilIdle(); | 171 callback_helper.Start(&watcher, test_pipe.handle0.get()); |
135 EXPECT_FALSE(callback_helper.got_callback()); | 172 RunUntilIdle(); |
136 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle1.get(), | 173 EXPECT_FALSE(callback_helper.got_callback()); |
137 std::string())); | 174 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle1.get(), |
138 callback_helper.RunUntilGotCallback(); | 175 std::string())); |
139 EXPECT_TRUE(callback_helper.got_callback()); | 176 callback_helper.RunUntilGotCallback(); |
177 EXPECT_TRUE(callback_helper.got_callback()); | |
178 } | |
140 } | 179 } |
141 | 180 |
142 // Creates three handles and notfies them in reverse order ensuring each one is | 181 // Creates three handles and notfies them in reverse order ensuring each one is |
143 // notified appropriately. | 182 // notified appropriately. |
144 TEST_F(HandleWatcherTest, ThreeHandles) { | 183 TEST_F(HandleWatcherTest, ThreeHandles) { |
145 MessagePipe test_pipe1; | 184 while (NextTestConfig()) { |
sky
2014/08/28 23:12:19
Did you look into making these parameterized tests
yzshen1
2014/08/29 00:23:45
Great suggestion!
| |
146 MessagePipe test_pipe2; | 185 MessagePipe test_pipe1; |
147 MessagePipe test_pipe3; | 186 MessagePipe test_pipe2; |
148 CallbackHelper callback_helper1; | 187 MessagePipe test_pipe3; |
149 CallbackHelper callback_helper2; | 188 CallbackHelper callback_helper1; |
150 CallbackHelper callback_helper3; | 189 CallbackHelper callback_helper2; |
151 ASSERT_TRUE(test_pipe1.handle0.is_valid()); | 190 CallbackHelper callback_helper3; |
152 ASSERT_TRUE(test_pipe2.handle0.is_valid()); | 191 ASSERT_TRUE(test_pipe1.handle0.is_valid()); |
153 ASSERT_TRUE(test_pipe3.handle0.is_valid()); | 192 ASSERT_TRUE(test_pipe2.handle0.is_valid()); |
154 | 193 ASSERT_TRUE(test_pipe3.handle0.is_valid()); |
155 HandleWatcher watcher1; | 194 |
156 callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); | 195 HandleWatcher watcher1; |
157 RunUntilIdle(); | 196 callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); |
158 EXPECT_FALSE(callback_helper1.got_callback()); | 197 RunUntilIdle(); |
159 EXPECT_FALSE(callback_helper2.got_callback()); | 198 EXPECT_FALSE(callback_helper1.got_callback()); |
160 EXPECT_FALSE(callback_helper3.got_callback()); | 199 EXPECT_FALSE(callback_helper2.got_callback()); |
161 | 200 EXPECT_FALSE(callback_helper3.got_callback()); |
162 HandleWatcher watcher2; | 201 |
163 callback_helper2.Start(&watcher2, test_pipe2.handle0.get()); | 202 HandleWatcher watcher2; |
164 RunUntilIdle(); | 203 callback_helper2.Start(&watcher2, test_pipe2.handle0.get()); |
165 EXPECT_FALSE(callback_helper1.got_callback()); | 204 RunUntilIdle(); |
166 EXPECT_FALSE(callback_helper2.got_callback()); | 205 EXPECT_FALSE(callback_helper1.got_callback()); |
167 EXPECT_FALSE(callback_helper3.got_callback()); | 206 EXPECT_FALSE(callback_helper2.got_callback()); |
168 | 207 EXPECT_FALSE(callback_helper3.got_callback()); |
169 HandleWatcher watcher3; | 208 |
170 callback_helper3.Start(&watcher3, test_pipe3.handle0.get()); | 209 HandleWatcher watcher3; |
171 RunUntilIdle(); | 210 callback_helper3.Start(&watcher3, test_pipe3.handle0.get()); |
172 EXPECT_FALSE(callback_helper1.got_callback()); | 211 RunUntilIdle(); |
173 EXPECT_FALSE(callback_helper2.got_callback()); | 212 EXPECT_FALSE(callback_helper1.got_callback()); |
174 EXPECT_FALSE(callback_helper3.got_callback()); | 213 EXPECT_FALSE(callback_helper2.got_callback()); |
175 | 214 EXPECT_FALSE(callback_helper3.got_callback()); |
176 // Write to 3 and make sure it's notified. | 215 |
177 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe3.handle1.get(), | 216 // Write to 3 and make sure it's notified. |
178 std::string())); | 217 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe3.handle1.get(), |
179 callback_helper3.RunUntilGotCallback(); | 218 std::string())); |
180 EXPECT_FALSE(callback_helper1.got_callback()); | 219 callback_helper3.RunUntilGotCallback(); |
181 EXPECT_FALSE(callback_helper2.got_callback()); | 220 EXPECT_FALSE(callback_helper1.got_callback()); |
182 EXPECT_TRUE(callback_helper3.got_callback()); | 221 EXPECT_FALSE(callback_helper2.got_callback()); |
183 callback_helper3.clear_callback(); | 222 EXPECT_TRUE(callback_helper3.got_callback()); |
184 | 223 callback_helper3.clear_callback(); |
185 // Write to 1 and 3. Only 1 should be notified since 3 was is no longer | 224 |
186 // running. | 225 // Write to 1 and 3. Only 1 should be notified since 3 was is no longer |
187 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(), | 226 // running. |
188 std::string())); | 227 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(), |
189 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe3.handle1.get(), | 228 std::string())); |
190 std::string())); | 229 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe3.handle1.get(), |
191 callback_helper1.RunUntilGotCallback(); | 230 std::string())); |
192 EXPECT_TRUE(callback_helper1.got_callback()); | 231 callback_helper1.RunUntilGotCallback(); |
193 EXPECT_FALSE(callback_helper2.got_callback()); | 232 EXPECT_TRUE(callback_helper1.got_callback()); |
194 EXPECT_FALSE(callback_helper3.got_callback()); | 233 EXPECT_FALSE(callback_helper2.got_callback()); |
195 callback_helper1.clear_callback(); | 234 EXPECT_FALSE(callback_helper3.got_callback()); |
196 | 235 callback_helper1.clear_callback(); |
197 // Write to 1 and 2. Only 2 should be notified (since 1 was already notified). | 236 |
198 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(), | 237 // Write to 1 and 2. Only 2 should be notified (since 1 was already |
199 std::string())); | 238 // notified). |
200 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe2.handle1.get(), | 239 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(), |
201 std::string())); | 240 std::string())); |
202 callback_helper2.RunUntilGotCallback(); | 241 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe2.handle1.get(), |
203 EXPECT_FALSE(callback_helper1.got_callback()); | 242 std::string())); |
204 EXPECT_TRUE(callback_helper2.got_callback()); | 243 callback_helper2.RunUntilGotCallback(); |
205 EXPECT_FALSE(callback_helper3.got_callback()); | 244 EXPECT_FALSE(callback_helper1.got_callback()); |
245 EXPECT_TRUE(callback_helper2.got_callback()); | |
246 EXPECT_FALSE(callback_helper3.got_callback()); | |
247 } | |
206 } | 248 } |
207 | 249 |
208 // Verifies Start() invoked a second time works. | 250 // Verifies Start() invoked a second time works. |
209 TEST_F(HandleWatcherTest, Restart) { | 251 TEST_F(HandleWatcherTest, Restart) { |
210 MessagePipe test_pipe1; | 252 while (NextTestConfig()) { |
211 MessagePipe test_pipe2; | 253 MessagePipe test_pipe1; |
212 CallbackHelper callback_helper1; | 254 MessagePipe test_pipe2; |
213 CallbackHelper callback_helper2; | 255 CallbackHelper callback_helper1; |
214 ASSERT_TRUE(test_pipe1.handle0.is_valid()); | 256 CallbackHelper callback_helper2; |
215 ASSERT_TRUE(test_pipe2.handle0.is_valid()); | 257 ASSERT_TRUE(test_pipe1.handle0.is_valid()); |
216 | 258 ASSERT_TRUE(test_pipe2.handle0.is_valid()); |
217 HandleWatcher watcher1; | 259 |
218 callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); | 260 HandleWatcher watcher1; |
219 RunUntilIdle(); | 261 callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); |
220 EXPECT_FALSE(callback_helper1.got_callback()); | 262 RunUntilIdle(); |
221 EXPECT_FALSE(callback_helper2.got_callback()); | 263 EXPECT_FALSE(callback_helper1.got_callback()); |
222 | 264 EXPECT_FALSE(callback_helper2.got_callback()); |
223 HandleWatcher watcher2; | 265 |
224 callback_helper2.Start(&watcher2, test_pipe2.handle0.get()); | 266 HandleWatcher watcher2; |
225 RunUntilIdle(); | 267 callback_helper2.Start(&watcher2, test_pipe2.handle0.get()); |
226 EXPECT_FALSE(callback_helper1.got_callback()); | 268 RunUntilIdle(); |
227 EXPECT_FALSE(callback_helper2.got_callback()); | 269 EXPECT_FALSE(callback_helper1.got_callback()); |
228 | 270 EXPECT_FALSE(callback_helper2.got_callback()); |
229 // Write to 1 and make sure it's notified. | 271 |
230 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(), | 272 // Write to 1 and make sure it's notified. |
231 std::string())); | 273 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(), |
232 callback_helper1.RunUntilGotCallback(); | 274 std::string())); |
233 EXPECT_TRUE(callback_helper1.got_callback()); | 275 callback_helper1.RunUntilGotCallback(); |
234 EXPECT_FALSE(callback_helper2.got_callback()); | 276 EXPECT_TRUE(callback_helper1.got_callback()); |
235 callback_helper1.clear_callback(); | 277 EXPECT_FALSE(callback_helper2.got_callback()); |
236 EXPECT_TRUE(mojo::test::DiscardMessage(test_pipe1.handle0.get())); | 278 callback_helper1.clear_callback(); |
237 | 279 EXPECT_TRUE(mojo::test::DiscardMessage(test_pipe1.handle0.get())); |
238 // Write to 2 and make sure it's notified. | 280 |
239 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe2.handle1.get(), | 281 // Write to 2 and make sure it's notified. |
240 std::string())); | 282 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe2.handle1.get(), |
241 callback_helper2.RunUntilGotCallback(); | 283 std::string())); |
242 EXPECT_FALSE(callback_helper1.got_callback()); | 284 callback_helper2.RunUntilGotCallback(); |
243 EXPECT_TRUE(callback_helper2.got_callback()); | 285 EXPECT_FALSE(callback_helper1.got_callback()); |
244 callback_helper2.clear_callback(); | 286 EXPECT_TRUE(callback_helper2.got_callback()); |
245 | 287 callback_helper2.clear_callback(); |
246 // Listen on 1 again. | 288 |
247 callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); | 289 // Listen on 1 again. |
248 RunUntilIdle(); | 290 callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); |
249 EXPECT_FALSE(callback_helper1.got_callback()); | 291 RunUntilIdle(); |
250 EXPECT_FALSE(callback_helper2.got_callback()); | 292 EXPECT_FALSE(callback_helper1.got_callback()); |
251 | 293 EXPECT_FALSE(callback_helper2.got_callback()); |
252 // Write to 1 and make sure it's notified. | 294 |
253 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(), | 295 // Write to 1 and make sure it's notified. |
254 std::string())); | 296 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(), |
255 callback_helper1.RunUntilGotCallback(); | 297 std::string())); |
256 EXPECT_TRUE(callback_helper1.got_callback()); | 298 callback_helper1.RunUntilGotCallback(); |
257 EXPECT_FALSE(callback_helper2.got_callback()); | 299 EXPECT_TRUE(callback_helper1.got_callback()); |
300 EXPECT_FALSE(callback_helper2.got_callback()); | |
301 } | |
258 } | 302 } |
259 | 303 |
260 // Verifies deadline is honored. | 304 // Verifies deadline is honored. |
261 TEST_F(HandleWatcherTest, Deadline) { | 305 TEST_F(HandleWatcherTest, Deadline) { |
262 InstallTickClock(); | 306 while (NextTestConfig()) { |
263 | 307 InstallTickClock(); |
264 MessagePipe test_pipe1; | 308 |
265 MessagePipe test_pipe2; | 309 MessagePipe test_pipe1; |
266 MessagePipe test_pipe3; | 310 MessagePipe test_pipe2; |
267 CallbackHelper callback_helper1; | 311 MessagePipe test_pipe3; |
268 CallbackHelper callback_helper2; | 312 CallbackHelper callback_helper1; |
269 CallbackHelper callback_helper3; | 313 CallbackHelper callback_helper2; |
270 ASSERT_TRUE(test_pipe1.handle0.is_valid()); | 314 CallbackHelper callback_helper3; |
271 ASSERT_TRUE(test_pipe2.handle0.is_valid()); | 315 ASSERT_TRUE(test_pipe1.handle0.is_valid()); |
272 ASSERT_TRUE(test_pipe3.handle0.is_valid()); | 316 ASSERT_TRUE(test_pipe2.handle0.is_valid()); |
273 | 317 ASSERT_TRUE(test_pipe3.handle0.is_valid()); |
274 // Add a watcher with an infinite timeout. | 318 |
275 HandleWatcher watcher1; | 319 // Add a watcher with an infinite timeout. |
276 callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); | 320 HandleWatcher watcher1; |
277 RunUntilIdle(); | 321 callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); |
278 EXPECT_FALSE(callback_helper1.got_callback()); | 322 RunUntilIdle(); |
279 EXPECT_FALSE(callback_helper2.got_callback()); | 323 EXPECT_FALSE(callback_helper1.got_callback()); |
280 EXPECT_FALSE(callback_helper3.got_callback()); | 324 EXPECT_FALSE(callback_helper2.got_callback()); |
281 | 325 EXPECT_FALSE(callback_helper3.got_callback()); |
282 // Add another watcher wth a timeout of 500 microseconds. | 326 |
283 HandleWatcher watcher2; | 327 // Add another watcher wth a timeout of 500 microseconds. |
284 watcher2.Start(test_pipe2.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, 500, | 328 HandleWatcher watcher2; |
285 callback_helper2.GetCallback()); | 329 watcher2.Start(test_pipe2.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, 500, |
286 RunUntilIdle(); | 330 callback_helper2.GetCallback()); |
287 EXPECT_FALSE(callback_helper1.got_callback()); | 331 RunUntilIdle(); |
288 EXPECT_FALSE(callback_helper2.got_callback()); | 332 EXPECT_FALSE(callback_helper1.got_callback()); |
289 EXPECT_FALSE(callback_helper3.got_callback()); | 333 EXPECT_FALSE(callback_helper2.got_callback()); |
290 | 334 EXPECT_FALSE(callback_helper3.got_callback()); |
291 // Advance the clock passed the deadline. We also have to start another | 335 |
292 // watcher to wake up the background thread. | 336 // Advance the clock passed the deadline. We also have to start another |
293 tick_clock_.Advance(base::TimeDelta::FromMicroseconds(501)); | 337 // watcher to wake up the background thread. |
294 | 338 tick_clock_.Advance(base::TimeDelta::FromMicroseconds(501)); |
295 HandleWatcher watcher3; | 339 |
296 callback_helper3.Start(&watcher3, test_pipe3.handle0.get()); | 340 HandleWatcher watcher3; |
297 | 341 callback_helper3.Start(&watcher3, test_pipe3.handle0.get()); |
298 callback_helper2.RunUntilGotCallback(); | 342 |
299 EXPECT_FALSE(callback_helper1.got_callback()); | 343 callback_helper2.RunUntilGotCallback(); |
300 EXPECT_TRUE(callback_helper2.got_callback()); | 344 EXPECT_FALSE(callback_helper1.got_callback()); |
301 EXPECT_FALSE(callback_helper3.got_callback()); | 345 EXPECT_TRUE(callback_helper2.got_callback()); |
346 EXPECT_FALSE(callback_helper3.got_callback()); | |
347 } | |
302 } | 348 } |
303 | 349 |
304 TEST_F(HandleWatcherTest, DeleteInCallback) { | 350 TEST_F(HandleWatcherTest, DeleteInCallback) { |
305 MessagePipe test_pipe; | 351 while (NextTestConfig()) { |
306 CallbackHelper callback_helper; | 352 MessagePipe test_pipe; |
307 | 353 CallbackHelper callback_helper; |
308 HandleWatcher* watcher = new HandleWatcher(); | 354 |
309 callback_helper.StartWithCallback(watcher, test_pipe.handle1.get(), | 355 HandleWatcher* watcher = new HandleWatcher(); |
310 base::Bind(&DeleteWatcherAndForwardResult, | 356 callback_helper.StartWithCallback( |
311 watcher, | 357 watcher, test_pipe.handle1.get(), |
312 callback_helper.GetCallback())); | 358 base::Bind(&DeleteWatcherAndForwardResult, watcher, |
313 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle0.get(), | 359 callback_helper.GetCallback())); |
314 std::string())); | 360 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle0.get(), |
315 callback_helper.RunUntilGotCallback(); | 361 std::string())); |
316 EXPECT_TRUE(callback_helper.got_callback()); | 362 callback_helper.RunUntilGotCallback(); |
363 EXPECT_TRUE(callback_helper.got_callback()); | |
364 } | |
317 } | 365 } |
318 | 366 |
319 TEST(HandleWatcherCleanEnvironmentTest, AbortedOnMessageLoopDestruction) { | 367 TEST(HandleWatcherCleanEnvironmentTest, AbortedOnMessageLoopDestruction) { |
320 bool was_signaled = false; | 368 for (int i = 0; i < MESSAGE_LOOP_CONFIG_COUNT; ++i) { |
321 MojoResult result = MOJO_RESULT_OK; | 369 bool was_signaled = false; |
322 | 370 MojoResult result = MOJO_RESULT_OK; |
323 base::ShadowingAtExitManager at_exit; | 371 |
324 MessagePipe pipe; | 372 base::ShadowingAtExitManager at_exit; |
325 HandleWatcher watcher; | 373 MessagePipe pipe; |
326 { | 374 HandleWatcher watcher; |
327 base::MessageLoop loop; | 375 { |
328 | 376 scoped_ptr<base::MessageLoop> loop( |
329 watcher.Start(pipe.handle0.get(), | 377 CreateMessageLoop(static_cast<MessageLoopConfig>(i))); |
330 MOJO_HANDLE_SIGNAL_READABLE, | 378 |
331 MOJO_DEADLINE_INDEFINITE, | 379 watcher.Start(pipe.handle0.get(), |
332 base::Bind(&ObserveCallback, &was_signaled, &result)); | 380 MOJO_HANDLE_SIGNAL_READABLE, |
333 | 381 MOJO_DEADLINE_INDEFINITE, |
334 // Now, let the MessageLoop get torn down. We expect our callback to run. | 382 base::Bind(&ObserveCallback, &was_signaled, &result)); |
335 } | 383 |
336 | 384 // Now, let the MessageLoop get torn down. We expect our callback to run. |
337 EXPECT_TRUE(was_signaled); | 385 } |
338 EXPECT_EQ(MOJO_RESULT_ABORTED, result); | 386 |
387 EXPECT_TRUE(was_signaled); | |
388 EXPECT_EQ(MOJO_RESULT_ABORTED, result); | |
389 } | |
339 } | 390 } |
340 | 391 |
341 void NeverReached(MojoResult result) { | 392 void NeverReached(MojoResult result) { |
342 FAIL() << "Callback should never be invoked " << result; | 393 FAIL() << "Callback should never be invoked " << result; |
343 } | 394 } |
344 | 395 |
345 // Called on the main thread when a thread is done. Decrements |active_count| | 396 // Called on the main thread when a thread is done. Decrements |active_count| |
346 // and if |active_count| is zero quits |run_loop|. | 397 // and if |active_count| is zero quits |run_loop|. |
347 void StressThreadDone(base::RunLoop* run_loop, int* active_count) { | 398 void StressThreadDone(base::RunLoop* run_loop, int* active_count) { |
348 (*active_count)--; | 399 (*active_count)--; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
416 | 467 |
417 base::ShadowingAtExitManager at_exit; | 468 base::ShadowingAtExitManager at_exit; |
418 base::MessageLoop message_loop; | 469 base::MessageLoop message_loop; |
419 base::RunLoop run_loop; | 470 base::RunLoop run_loop; |
420 ScopedVector<base::Thread> threads; | 471 ScopedVector<base::Thread> threads; |
421 int threads_active_counter = kThreadCount; | 472 int threads_active_counter = kThreadCount; |
422 // Starts the threads first and then post the task in hopes of having more | 473 // Starts the threads first and then post the task in hopes of having more |
423 // threads running at once. | 474 // threads running at once. |
424 for (int i = 0; i < kThreadCount; ++i) { | 475 for (int i = 0; i < kThreadCount; ++i) { |
425 scoped_ptr<base::Thread> thread(new base::Thread("test thread")); | 476 scoped_ptr<base::Thread> thread(new base::Thread("test thread")); |
426 thread->Start(); | 477 if (i % 2) { |
478 base::Thread::Options thread_options; | |
479 thread_options.message_pump_factory = | |
480 base::Bind(&MessagePumpMojo::Create); | |
481 thread->StartWithOptions(thread_options); | |
482 } else { | |
483 thread->Start(); | |
484 } | |
427 threads.push_back(thread.release()); | 485 threads.push_back(thread.release()); |
428 } | 486 } |
429 for (int i = 0; i < kThreadCount; ++i) { | 487 for (int i = 0; i < kThreadCount; ++i) { |
430 threads[i]->task_runner()->PostTask( | 488 threads[i]->task_runner()->PostTask( |
431 FROM_HERE, base::Bind(&RunStressTest, kWatchCount, | 489 FROM_HERE, base::Bind(&RunStressTest, kWatchCount, |
432 message_loop.task_runner(), | 490 message_loop.task_runner(), |
433 &run_loop, &threads_active_counter)); | 491 &run_loop, &threads_active_counter)); |
434 } | 492 } |
435 run_loop.Run(); | 493 run_loop.Run(); |
436 ASSERT_EQ(0, threads_active_counter); | 494 ASSERT_EQ(0, threads_active_counter); |
437 } | 495 } |
438 | 496 |
439 } // namespace test | 497 } // namespace test |
440 } // namespace common | 498 } // namespace common |
441 } // namespace mojo | 499 } // namespace mojo |
OLD | NEW |