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/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/location.h" | 6 #include "base/location.h" |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 int response_code, | 248 int response_code, |
249 const net::ResponseCookies& cookies, | 249 const net::ResponseCookies& cookies, |
250 const std::string& data) { | 250 const std::string& data) { |
251 EXPECT_TRUE(status.is_success()); | 251 EXPECT_TRUE(status.is_success()); |
252 EXPECT_EQ(200, response_code); // HTTP OK | 252 EXPECT_EQ(200, response_code); // HTTP OK |
253 EXPECT_FALSE(data.empty()); | 253 EXPECT_FALSE(data.empty()); |
254 | 254 |
255 if (handle_raw_response_) { | 255 if (handle_raw_response_) { |
256 // If the current message loop is not the IO loop, it will be shut down when | 256 // If the current message loop is not the IO loop, it will be shut down when |
257 // the main loop returns and this thread subsequently goes out of scope. | 257 // the main loop returns and this thread subsequently goes out of scope. |
258 io_task_runner()->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | 258 io_task_runner()->PostTask(FROM_HERE, |
| 259 base::MessageLoop::QuitWhenIdleClosure()); |
259 return CloudPrintURLFetcher::STOP_PROCESSING; | 260 return CloudPrintURLFetcher::STOP_PROCESSING; |
260 } | 261 } |
261 return CloudPrintURLFetcher::CONTINUE_PROCESSING; | 262 return CloudPrintURLFetcher::CONTINUE_PROCESSING; |
262 } | 263 } |
263 | 264 |
264 CloudPrintURLFetcher::ResponseAction | 265 CloudPrintURLFetcher::ResponseAction |
265 CloudPrintURLFetcherBasicTest::HandleRawData( | 266 CloudPrintURLFetcherBasicTest::HandleRawData( |
266 const net::URLFetcher* source, | 267 const net::URLFetcher* source, |
267 const GURL& url, | 268 const GURL& url, |
268 const std::string& data) { | 269 const std::string& data) { |
269 // We should never get here if we returned true in HandleRawResponse | 270 // We should never get here if we returned true in HandleRawResponse |
270 EXPECT_FALSE(handle_raw_response_); | 271 EXPECT_FALSE(handle_raw_response_); |
271 if (handle_raw_data_) { | 272 if (handle_raw_data_) { |
272 io_task_runner()->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | 273 io_task_runner()->PostTask(FROM_HERE, |
| 274 base::MessageLoop::QuitWhenIdleClosure()); |
273 return CloudPrintURLFetcher::STOP_PROCESSING; | 275 return CloudPrintURLFetcher::STOP_PROCESSING; |
274 } | 276 } |
275 return CloudPrintURLFetcher::CONTINUE_PROCESSING; | 277 return CloudPrintURLFetcher::CONTINUE_PROCESSING; |
276 } | 278 } |
277 | 279 |
278 CloudPrintURLFetcher::ResponseAction | 280 CloudPrintURLFetcher::ResponseAction |
279 CloudPrintURLFetcherBasicTest::HandleJSONData( | 281 CloudPrintURLFetcherBasicTest::HandleJSONData( |
280 const net::URLFetcher* source, | 282 const net::URLFetcher* source, |
281 const GURL& url, | 283 const GURL& url, |
282 base::DictionaryValue* json_data, | 284 base::DictionaryValue* json_data, |
283 bool succeeded) { | 285 bool succeeded) { |
284 // We should never get here if we returned true in one of the above methods. | 286 // We should never get here if we returned true in one of the above methods. |
285 EXPECT_FALSE(handle_raw_response_); | 287 EXPECT_FALSE(handle_raw_response_); |
286 EXPECT_FALSE(handle_raw_data_); | 288 EXPECT_FALSE(handle_raw_data_); |
287 io_task_runner()->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | 289 io_task_runner()->PostTask(FROM_HERE, |
| 290 base::MessageLoop::QuitWhenIdleClosure()); |
288 return CloudPrintURLFetcher::STOP_PROCESSING; | 291 return CloudPrintURLFetcher::STOP_PROCESSING; |
289 } | 292 } |
290 | 293 |
291 CloudPrintURLFetcher::ResponseAction | 294 CloudPrintURLFetcher::ResponseAction |
292 CloudPrintURLFetcherOverloadTest::HandleRawData( | 295 CloudPrintURLFetcherOverloadTest::HandleRawData( |
293 const net::URLFetcher* source, | 296 const net::URLFetcher* source, |
294 const GURL& url, | 297 const GURL& url, |
295 const std::string& data) { | 298 const std::string& data) { |
296 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); | 299 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); |
297 response_count_++; | 300 response_count_++; |
298 if (response_count_ < 20) { | 301 if (response_count_ < 20) { |
299 fetcher_->StartGetRequest(CloudPrintURLFetcher::REQUEST_MAX, url, this, | 302 fetcher_->StartGetRequest(CloudPrintURLFetcher::REQUEST_MAX, url, this, |
300 max_retries_, std::string()); | 303 max_retries_, std::string()); |
301 } else { | 304 } else { |
302 // We have already sent 20 requests continuously. And we expect that | 305 // We have already sent 20 requests continuously. And we expect that |
303 // it takes more than 1 second due to the overload protection settings. | 306 // it takes more than 1 second due to the overload protection settings. |
304 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | 307 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); |
305 io_task_runner()->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | 308 io_task_runner()->PostTask(FROM_HERE, |
| 309 base::MessageLoop::QuitWhenIdleClosure()); |
306 } | 310 } |
307 return CloudPrintURLFetcher::STOP_PROCESSING; | 311 return CloudPrintURLFetcher::STOP_PROCESSING; |
308 } | 312 } |
309 | 313 |
310 CloudPrintURLFetcher::ResponseAction | 314 CloudPrintURLFetcher::ResponseAction |
311 CloudPrintURLFetcherRetryBackoffTest::HandleRawData( | 315 CloudPrintURLFetcherRetryBackoffTest::HandleRawData( |
312 const net::URLFetcher* source, | 316 const net::URLFetcher* source, |
313 const GURL& url, | 317 const GURL& url, |
314 const std::string& data) { | 318 const std::string& data) { |
315 response_count_++; | 319 response_count_++; |
316 // First attempt + 11 retries = 12 total responses. | 320 // First attempt + 11 retries = 12 total responses. |
317 EXPECT_LE(response_count_, 12); | 321 EXPECT_LE(response_count_, 12); |
318 return CloudPrintURLFetcher::RETRY_REQUEST; | 322 return CloudPrintURLFetcher::RETRY_REQUEST; |
319 } | 323 } |
320 | 324 |
321 void CloudPrintURLFetcherRetryBackoffTest::OnRequestGiveUp() { | 325 void CloudPrintURLFetcherRetryBackoffTest::OnRequestGiveUp() { |
322 // It takes more than 200 ms to finish all 11 requests. | 326 // It takes more than 200 ms to finish all 11 requests. |
323 EXPECT_TRUE(Time::Now() - start_time_ >= TimeDelta::FromMilliseconds(200)); | 327 EXPECT_TRUE(Time::Now() - start_time_ >= TimeDelta::FromMilliseconds(200)); |
324 io_task_runner()->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | 328 io_task_runner()->PostTask(FROM_HERE, |
| 329 base::MessageLoop::QuitWhenIdleClosure()); |
325 } | 330 } |
326 | 331 |
327 TEST_F(CloudPrintURLFetcherBasicTest, HandleRawResponse) { | 332 TEST_F(CloudPrintURLFetcherBasicTest, HandleRawResponse) { |
328 net::SpawnedTestServer test_server(net::SpawnedTestServer::TYPE_HTTP, | 333 net::SpawnedTestServer test_server(net::SpawnedTestServer::TYPE_HTTP, |
329 net::SpawnedTestServer::kLocalhost, | 334 net::SpawnedTestServer::kLocalhost, |
330 base::FilePath(kDocRoot)); | 335 base::FilePath(kDocRoot)); |
331 ASSERT_TRUE(test_server.Start()); | 336 ASSERT_TRUE(test_server.Start()); |
332 SetHandleRawResponse(true); | 337 SetHandleRawResponse(true); |
333 | 338 |
334 CreateFetcher(test_server.GetURL("echo"), 0); | 339 CreateFetcher(test_server.GetURL("echo"), 0); |
(...skipping 29 matching lines...) Expand all Loading... |
364 base::FilePath(kDocRoot)); | 369 base::FilePath(kDocRoot)); |
365 ASSERT_TRUE(test_server.Start()); | 370 ASSERT_TRUE(test_server.Start()); |
366 | 371 |
367 GURL url(test_server.GetURL("defaultresponse")); | 372 GURL url(test_server.GetURL("defaultresponse")); |
368 CreateFetcher(url, 11); | 373 CreateFetcher(url, 11); |
369 | 374 |
370 base::MessageLoop::current()->Run(); | 375 base::MessageLoop::current()->Run(); |
371 } | 376 } |
372 | 377 |
373 } // namespace cloud_print | 378 } // namespace cloud_print |
OLD | NEW |