Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: content/common/net/url_fetcher_impl_unittest.cc

Issue 10203002: Make URLRequestThrottlerManager a member of URLRequestContext. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge LKGR (pure merge) Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "content/common/net/url_fetcher_impl.h" 5 #include "content/common/net/url_fetcher_impl.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 18 matching lines...) Expand all
29 using base::Time; 29 using base::Time;
30 using base::TimeDelta; 30 using base::TimeDelta;
31 31
32 // TODO(eroman): Add a regression test for http://crbug.com/40505. 32 // TODO(eroman): Add a regression test for http://crbug.com/40505.
33 33
34 namespace { 34 namespace {
35 35
36 const FilePath::CharType kDocRoot[] = FILE_PATH_LITERAL("chrome/test/data"); 36 const FilePath::CharType kDocRoot[] = FILE_PATH_LITERAL("chrome/test/data");
37 const char kTestServerFilePrefix[] = "files/"; 37 const char kTestServerFilePrefix[] = "files/";
38 38
39 class ThrottlingTestURLRequestContextGetter
40 : public TestURLRequestContextGetter {
41 public:
42 ThrottlingTestURLRequestContextGetter(
43 base::MessageLoopProxy* io_message_loop_proxy,
44 TestURLRequestContext* request_context)
45 : TestURLRequestContextGetter(io_message_loop_proxy),
46 context_(request_context) {
47 }
48
49 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE {
50 return context_.get();
51 }
52
53 protected:
54 virtual ~ThrottlingTestURLRequestContextGetter() {}
55
56 scoped_refptr<TestURLRequestContext> context_;
57 };
58
39 } // namespace 59 } // namespace
40 60
41 class URLFetcherTest : public testing::Test, 61 class URLFetcherTest : public testing::Test,
42 public content::URLFetcherDelegate { 62 public content::URLFetcherDelegate {
43 public: 63 public:
44 URLFetcherTest() : fetcher_(NULL) { } 64 URLFetcherTest()
65 : fetcher_(NULL),
66 context_(new TestURLRequestContext()) {
67 }
45 68
46 static int GetNumFetcherCores() { 69 static int GetNumFetcherCores() {
47 return URLFetcherImpl::GetNumFetcherCores(); 70 return URLFetcherImpl::GetNumFetcherCores();
48 } 71 }
49 72
50 // Creates a URLFetcher, using the program's main thread to do IO. 73 // Creates a URLFetcher, using the program's main thread to do IO.
51 virtual void CreateFetcher(const GURL& url); 74 virtual void CreateFetcher(const GURL& url);
52 75
53 // content::URLFetcherDelegate 76 // content::URLFetcherDelegate
54 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; 77 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE;
55 78
56 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { 79 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() {
57 return io_message_loop_proxy_; 80 return io_message_loop_proxy_;
58 } 81 }
59 82
83 TestURLRequestContext* request_context() {
84 return context_.get();
85 }
86
60 protected: 87 protected:
61 virtual void SetUp() OVERRIDE { 88 virtual void SetUp() OVERRIDE {
62 testing::Test::SetUp(); 89 testing::Test::SetUp();
63 90
64 io_message_loop_proxy_ = base::MessageLoopProxy::current(); 91 io_message_loop_proxy_ = base::MessageLoopProxy::current();
65 92
66 #if defined(USE_NSS) 93 #if defined(USE_NSS)
67 crypto::EnsureNSSInit(); 94 crypto::EnsureNSSInit();
68 net::EnsureNSSHttpIOInit(); 95 net::EnsureNSSHttpIOInit();
69 #endif 96 #endif
70 } 97 }
71 98
72 virtual void TearDown() OVERRIDE { 99 virtual void TearDown() OVERRIDE {
73 #if defined(USE_NSS) 100 #if defined(USE_NSS)
74 net::ShutdownNSSHttpIO(); 101 net::ShutdownNSSHttpIO();
75 #endif 102 #endif
76 } 103 }
77 104
78 // URLFetcher is designed to run on the main UI thread, but in our tests 105 // URLFetcher is designed to run on the main UI thread, but in our tests
79 // we assume that the current thread is the IO thread where the URLFetcher 106 // we assume that the current thread is the IO thread where the URLFetcher
80 // dispatches its requests to. When we wish to simulate being used from 107 // dispatches its requests to. When we wish to simulate being used from
81 // a UI thread, we dispatch a worker thread to do so. 108 // a UI thread, we dispatch a worker thread to do so.
82 MessageLoopForIO io_loop_; 109 MessageLoopForIO io_loop_;
83 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; 110 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
84 111
85 URLFetcherImpl* fetcher_; 112 URLFetcherImpl* fetcher_;
113 scoped_refptr<TestURLRequestContext> context_;
86 }; 114 };
87 115
88 void URLFetcherTest::CreateFetcher(const GURL& url) { 116 void URLFetcherTest::CreateFetcher(const GURL& url) {
89 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); 117 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this);
90 fetcher_->SetRequestContext(new TestURLRequestContextGetter( 118 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
91 io_message_loop_proxy())); 119 io_message_loop_proxy(), request_context()));
92 fetcher_->Start(); 120 fetcher_->Start();
93 } 121 }
94 122
95 void URLFetcherTest::OnURLFetchComplete(const content::URLFetcher* source) { 123 void URLFetcherTest::OnURLFetchComplete(const content::URLFetcher* source) {
96 EXPECT_TRUE(source->GetStatus().is_success()); 124 EXPECT_TRUE(source->GetStatus().is_success());
97 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK 125 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK
98 126
99 std::string data; 127 std::string data;
100 EXPECT_TRUE(source->GetResponseAsString(&data)); 128 EXPECT_TRUE(source->GetResponseAsString(&data));
101 EXPECT_FALSE(data.empty()); 129 EXPECT_FALSE(data.empty());
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 virtual void CreateFetcher(const GURL& url) OVERRIDE; 250 virtual void CreateFetcher(const GURL& url) OVERRIDE;
223 // content::URLFetcherDelegate 251 // content::URLFetcherDelegate
224 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; 252 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE;
225 253
226 void CancelRequest(); 254 void CancelRequest();
227 }; 255 };
228 256
229 // Version of TestURLRequestContext that posts a Quit task to the IO 257 // Version of TestURLRequestContext that posts a Quit task to the IO
230 // thread once it is deleted. 258 // thread once it is deleted.
231 class CancelTestURLRequestContext : public TestURLRequestContext { 259 class CancelTestURLRequestContext : public TestURLRequestContext {
260 public:
261 explicit CancelTestURLRequestContext() {
262 }
263
264 private:
232 virtual ~CancelTestURLRequestContext() { 265 virtual ~CancelTestURLRequestContext() {
233 // The d'tor should execute in the IO thread. Post the quit task to the 266 // The d'tor should execute in the IO thread. Post the quit task to the
234 // current thread. 267 // current thread.
235 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 268 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
236 } 269 }
237 }; 270 };
238 271
239 class CancelTestURLRequestContextGetter : public net::URLRequestContextGetter { 272 class CancelTestURLRequestContextGetter
273 : public TestURLRequestContextGetter {
240 public: 274 public:
241 explicit CancelTestURLRequestContextGetter( 275 CancelTestURLRequestContextGetter(
242 base::MessageLoopProxy* io_message_loop_proxy) 276 base::MessageLoopProxy* io_message_loop_proxy,
243 : io_message_loop_proxy_(io_message_loop_proxy), 277 const GURL& throttle_for_url)
244 context_created_(false, false) { 278 : TestURLRequestContextGetter(io_message_loop_proxy),
279 io_message_loop_proxy_(io_message_loop_proxy),
280 context_created_(false, false),
281 throttle_for_url_(throttle_for_url) {
245 } 282 }
246 virtual net::URLRequestContext* GetURLRequestContext() { 283 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE {
247 if (!context_) { 284 if (!context_) {
248 context_ = new CancelTestURLRequestContext(); 285 context_ = new CancelTestURLRequestContext();
286 DCHECK(context_->throttler_manager());
287
288 // Registers an entry for test url. The backoff time is calculated by:
289 // new_backoff = 2.0 * old_backoff + 0
290 // The initial backoff is 2 seconds and maximum backoff is 4 seconds.
291 // Maximum retries allowed is set to 2.
292 scoped_refptr<net::URLRequestThrottlerEntry> entry(
293 new net::URLRequestThrottlerEntry(
294 context_->throttler_manager(),
295 "", 200, 3, 2000, 2.0, 0.0, 4000));
296 context_->throttler_manager()->OverrideEntryForTests(
297 throttle_for_url_, entry);
298
249 context_created_.Signal(); 299 context_created_.Signal();
250 } 300 }
251 return context_; 301 return context_;
252 } 302 }
253 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { 303 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const {
254 return io_message_loop_proxy_; 304 return io_message_loop_proxy_;
255 } 305 }
256 void WaitForContextCreation() { 306 void WaitForContextCreation() {
257 context_created_.Wait(); 307 context_created_.Wait();
258 } 308 }
259 309
260 protected: 310 protected:
261 virtual ~CancelTestURLRequestContextGetter() {} 311 virtual ~CancelTestURLRequestContextGetter() {}
262 312
263 private: 313 private:
314 scoped_refptr<TestURLRequestContext> context_;
264 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; 315 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
265 base::WaitableEvent context_created_; 316 base::WaitableEvent context_created_;
266 scoped_refptr<net::URLRequestContext> context_; 317 GURL throttle_for_url_;
267 }; 318 };
268 319
269 // Version of URLFetcherTest that tests retying the same request twice. 320 // Version of URLFetcherTest that tests retying the same request twice.
270 class URLFetcherMultipleAttemptTest : public URLFetcherTest { 321 class URLFetcherMultipleAttemptTest : public URLFetcherTest {
271 public: 322 public:
272 // content::URLFetcherDelegate 323 // content::URLFetcherDelegate
273 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; 324 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE;
274 private: 325 private:
275 std::string data_; 326 std::string data_;
276 }; 327 };
(...skipping 18 matching lines...) Expand all
295 // disowning prevents the file from being deleted. 346 // disowning prevents the file from being deleted.
296 bool take_ownership_of_file_; 347 bool take_ownership_of_file_;
297 348
298 // Expected file error code for the test. 349 // Expected file error code for the test.
299 // PLATFORM_FILE_OK when expecting success. 350 // PLATFORM_FILE_OK when expecting success.
300 base::PlatformFileError expected_file_error_; 351 base::PlatformFileError expected_file_error_;
301 }; 352 };
302 353
303 void URLFetcherPostTest::CreateFetcher(const GURL& url) { 354 void URLFetcherPostTest::CreateFetcher(const GURL& url) {
304 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this); 355 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this);
305 fetcher_->SetRequestContext(new TestURLRequestContextGetter( 356 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
306 io_message_loop_proxy())); 357 io_message_loop_proxy(), request_context()));
307 fetcher_->SetUploadData("application/x-www-form-urlencoded", 358 fetcher_->SetUploadData("application/x-www-form-urlencoded",
308 "bobsyeruncle"); 359 "bobsyeruncle");
309 fetcher_->Start(); 360 fetcher_->Start();
310 } 361 }
311 362
312 void URLFetcherPostTest::OnURLFetchComplete(const content::URLFetcher* source) { 363 void URLFetcherPostTest::OnURLFetchComplete(const content::URLFetcher* source) {
313 std::string data; 364 std::string data;
314 EXPECT_TRUE(source->GetResponseAsString(&data)); 365 EXPECT_TRUE(source->GetResponseAsString(&data));
315 EXPECT_EQ(std::string("bobsyeruncle"), data); 366 EXPECT_EQ(std::string("bobsyeruncle"), data);
316 URLFetcherTest::OnURLFetchComplete(source); 367 URLFetcherTest::OnURLFetchComplete(source);
317 } 368 }
318 369
319 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) { 370 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) {
320 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); 371 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this);
321 fetcher_->SetRequestContext(new TestURLRequestContextGetter( 372 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
322 io_message_loop_proxy())); 373 io_message_loop_proxy(), request_context()));
323 previous_progress_ = 0; 374 previous_progress_ = 0;
324 fetcher_->Start(); 375 fetcher_->Start();
325 } 376 }
326 377
327 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress( 378 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress(
328 const content::URLFetcher* source, int64 current, int64 total) { 379 const content::URLFetcher* source, int64 current, int64 total) {
329 // Increasing between 0 and total. 380 // Increasing between 0 and total.
330 EXPECT_LE(0, current); 381 EXPECT_LE(0, current);
331 EXPECT_GE(total, current); 382 EXPECT_GE(total, current);
332 EXPECT_LE(previous_progress_, current); 383 EXPECT_LE(previous_progress_, current);
333 previous_progress_ = current; 384 previous_progress_ = current;
334 EXPECT_EQ(expected_total_, total); 385 EXPECT_EQ(expected_total_, total);
335 } 386 }
336 387
337 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) { 388 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) {
338 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); 389 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this);
339 fetcher_->SetRequestContext(new TestURLRequestContextGetter( 390 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
340 io_message_loop_proxy())); 391 io_message_loop_proxy(), request_context()));
341 cancelled_ = false; 392 cancelled_ = false;
342 fetcher_->Start(); 393 fetcher_->Start();
343 } 394 }
344 395
345 void URLFetcherDownloadProgressCancelTest::OnURLFetchDownloadProgress( 396 void URLFetcherDownloadProgressCancelTest::OnURLFetchDownloadProgress(
346 const content::URLFetcher* source, int64 current, int64 total) { 397 const content::URLFetcher* source, int64 current, int64 total) {
347 EXPECT_FALSE(cancelled_); 398 EXPECT_FALSE(cancelled_);
348 if (!cancelled_) { 399 if (!cancelled_) {
349 delete fetcher_; 400 delete fetcher_;
350 cancelled_ = true; 401 cancelled_ = true;
351 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 402 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
352 } 403 }
353 } 404 }
354 405
355 void URLFetcherDownloadProgressCancelTest::OnURLFetchComplete( 406 void URLFetcherDownloadProgressCancelTest::OnURLFetchComplete(
356 const content::URLFetcher* source) { 407 const content::URLFetcher* source) {
357 // Should have been cancelled. 408 // Should have been cancelled.
358 ADD_FAILURE(); 409 ADD_FAILURE();
359 delete fetcher_; 410 delete fetcher_;
360 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 411 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
361 } 412 }
362 413
363 void URLFetcherUploadProgressTest::CreateFetcher(const GURL& url) { 414 void URLFetcherUploadProgressTest::CreateFetcher(const GURL& url) {
364 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this); 415 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this);
365 fetcher_->SetRequestContext(new TestURLRequestContextGetter( 416 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
366 io_message_loop_proxy())); 417 io_message_loop_proxy(), request_context()));
367 previous_progress_ = 0; 418 previous_progress_ = 0;
368 // Large enough data to require more than one read from UploadDataStream. 419 // Large enough data to require more than one read from UploadDataStream.
369 chunk_.assign(1<<16, 'a'); 420 chunk_.assign(1<<16, 'a');
370 // Use chunked upload to wait for a timer event of progress notification. 421 // Use chunked upload to wait for a timer event of progress notification.
371 fetcher_->SetChunkedUpload("application/x-www-form-urlencoded"); 422 fetcher_->SetChunkedUpload("application/x-www-form-urlencoded");
372 fetcher_->Start(); 423 fetcher_->Start();
373 number_of_chunks_added_ = 1; 424 number_of_chunks_added_ = 1;
374 fetcher_->AppendChunkToUpload(chunk_, false); 425 fetcher_->AppendChunkToUpload(chunk_, false);
375 } 426 }
376 427
(...skipping 24 matching lines...) Expand all
401 452
402 void URLFetcherSocketAddressTest::OnURLFetchComplete( 453 void URLFetcherSocketAddressTest::OnURLFetchComplete(
403 const content::URLFetcher* source) { 454 const content::URLFetcher* source) {
404 EXPECT_EQ("127.0.0.1", source->GetSocketAddress().host()); 455 EXPECT_EQ("127.0.0.1", source->GetSocketAddress().host());
405 EXPECT_EQ(expected_port_, source->GetSocketAddress().port()); 456 EXPECT_EQ(expected_port_, source->GetSocketAddress().port());
406 URLFetcherTest::OnURLFetchComplete(source); 457 URLFetcherTest::OnURLFetchComplete(source);
407 } 458 }
408 459
409 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { 460 void URLFetcherProtectTest::CreateFetcher(const GURL& url) {
410 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); 461 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this);
411 fetcher_->SetRequestContext(new TestURLRequestContextGetter( 462 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
412 io_message_loop_proxy())); 463 io_message_loop_proxy(), request_context()));
413 start_time_ = Time::Now(); 464 start_time_ = Time::Now();
414 fetcher_->SetMaxRetries(11); 465 fetcher_->SetMaxRetries(11);
415 fetcher_->Start(); 466 fetcher_->Start();
416 } 467 }
417 468
418 void URLFetcherProtectTest::OnURLFetchComplete( 469 void URLFetcherProtectTest::OnURLFetchComplete(
419 const content::URLFetcher* source) { 470 const content::URLFetcher* source) {
420 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); 471 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000);
421 if (source->GetResponseCode() >= 500) { 472 if (source->GetResponseCode() >= 500) {
422 // Now running ServerUnavailable test. 473 // Now running ServerUnavailable test.
423 // It takes more than 1 second to finish all 11 requests. 474 // It takes more than 1 second to finish all 11 requests.
424 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); 475 EXPECT_TRUE(Time::Now() - start_time_ >= one_second);
425 EXPECT_TRUE(source->GetStatus().is_success()); 476 EXPECT_TRUE(source->GetStatus().is_success());
426 std::string data; 477 std::string data;
427 EXPECT_TRUE(source->GetResponseAsString(&data)); 478 EXPECT_TRUE(source->GetResponseAsString(&data));
428 EXPECT_FALSE(data.empty()); 479 EXPECT_FALSE(data.empty());
429 delete fetcher_; 480 delete fetcher_;
430 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 481 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
431 } else { 482 } else {
432 // Now running Overload test. 483 // Now running Overload test.
433 static int count = 0; 484 static int count = 0;
434 count++; 485 count++;
435 if (count < 20) { 486 if (count < 20) {
436 fetcher_->SetRequestContext( 487 fetcher_->SetRequestContext(
437 new TestURLRequestContextGetter(io_message_loop_proxy())); 488 new ThrottlingTestURLRequestContextGetter(
489 io_message_loop_proxy(), request_context()));
438 fetcher_->Start(); 490 fetcher_->Start();
439 } else { 491 } else {
440 // We have already sent 20 requests continuously. And we expect that 492 // We have already sent 20 requests continuously. And we expect that
441 // it takes more than 1 second due to the overload protection settings. 493 // it takes more than 1 second due to the overload protection settings.
442 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); 494 EXPECT_TRUE(Time::Now() - start_time_ >= one_second);
443 URLFetcherTest::OnURLFetchComplete(source); 495 URLFetcherTest::OnURLFetchComplete(source);
444 } 496 }
445 } 497 }
446 } 498 }
447 499
448 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) { 500 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) {
449 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); 501 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this);
450 fetcher_->SetRequestContext(new TestURLRequestContextGetter( 502 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
451 io_message_loop_proxy())); 503 io_message_loop_proxy(), request_context()));
452 fetcher_->SetAutomaticallyRetryOn5xx(false); 504 fetcher_->SetAutomaticallyRetryOn5xx(false);
453 start_time_ = Time::Now(); 505 start_time_ = Time::Now();
454 fetcher_->SetMaxRetries(11); 506 fetcher_->SetMaxRetries(11);
455 fetcher_->Start(); 507 fetcher_->Start();
456 } 508 }
457 509
458 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete( 510 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete(
459 const content::URLFetcher* source) { 511 const content::URLFetcher* source) {
460 const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000); 512 const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000);
461 if (source->GetResponseCode() >= 500) { 513 if (source->GetResponseCode() >= 500) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 EXPECT_TRUE(data.empty()); 556 EXPECT_TRUE(data.empty());
505 557
506 // The rest is the same as URLFetcherTest::OnURLFetchComplete. 558 // The rest is the same as URLFetcherTest::OnURLFetchComplete.
507 delete fetcher_; 559 delete fetcher_;
508 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 560 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
509 } 561 }
510 562
511 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { 563 void URLFetcherCancelTest::CreateFetcher(const GURL& url) {
512 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); 564 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this);
513 CancelTestURLRequestContextGetter* context_getter = 565 CancelTestURLRequestContextGetter* context_getter =
514 new CancelTestURLRequestContextGetter(io_message_loop_proxy()); 566 new CancelTestURLRequestContextGetter(io_message_loop_proxy(),
567 url);
515 fetcher_->SetRequestContext(context_getter); 568 fetcher_->SetRequestContext(context_getter);
516 fetcher_->SetMaxRetries(2); 569 fetcher_->SetMaxRetries(2);
517 fetcher_->Start(); 570 fetcher_->Start();
518 // We need to wait for the creation of the net::URLRequestContext, since we 571 // We need to wait for the creation of the net::URLRequestContext, since we
519 // rely on it being destroyed as a signal to end the test. 572 // rely on it being destroyed as a signal to end the test.
520 context_getter->WaitForContextCreation(); 573 context_getter->WaitForContextCreation();
521 CancelRequest(); 574 CancelRequest();
522 } 575 }
523 576
524 void URLFetcherCancelTest::OnURLFetchComplete( 577 void URLFetcherCancelTest::OnURLFetchComplete(
(...skipping 13 matching lines...) Expand all
538 591
539 void URLFetcherMultipleAttemptTest::OnURLFetchComplete( 592 void URLFetcherMultipleAttemptTest::OnURLFetchComplete(
540 const content::URLFetcher* source) { 593 const content::URLFetcher* source) {
541 EXPECT_TRUE(source->GetStatus().is_success()); 594 EXPECT_TRUE(source->GetStatus().is_success());
542 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK 595 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK
543 std::string data; 596 std::string data;
544 EXPECT_TRUE(source->GetResponseAsString(&data)); 597 EXPECT_TRUE(source->GetResponseAsString(&data));
545 EXPECT_FALSE(data.empty()); 598 EXPECT_FALSE(data.empty());
546 if (!data.empty() && data_.empty()) { 599 if (!data.empty() && data_.empty()) {
547 data_ = data; 600 data_ = data;
548 fetcher_->SetRequestContext( 601 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
549 new TestURLRequestContextGetter(io_message_loop_proxy())); 602 io_message_loop_proxy(), request_context()));
550 fetcher_->Start(); 603 fetcher_->Start();
551 } else { 604 } else {
552 EXPECT_EQ(data, data_); 605 EXPECT_EQ(data, data_);
553 delete fetcher_; // Have to delete this here and not in the destructor, 606 delete fetcher_; // Have to delete this here and not in the destructor,
554 // because the destructor won't necessarily run on the 607 // because the destructor won't necessarily run on the
555 // same thread that CreateFetcher() did. 608 // same thread that CreateFetcher() did.
556 609
557 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 610 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
558 // If the current message loop is not the IO loop, it will be shut down when 611 // If the current message loop is not the IO loop, it will be shut down when
559 // the main loop returns and this thread subsequently goes out of scope. 612 // the main loop returns and this thread subsequently goes out of scope.
560 } 613 }
561 } 614 }
562 615
563 void URLFetcherFileTest::CreateFetcherForFile(const GURL& url, 616 void URLFetcherFileTest::CreateFetcherForFile(const GURL& url,
564 const FilePath& file_path) { 617 const FilePath& file_path) {
565 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); 618 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this);
566 fetcher_->SetRequestContext(new TestURLRequestContextGetter( 619 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
567 io_message_loop_proxy())); 620 io_message_loop_proxy(), request_context()));
568 621
569 // Use the IO message loop to do the file operations in this test. 622 // Use the IO message loop to do the file operations in this test.
570 fetcher_->SaveResponseToFileAtPath(file_path, io_message_loop_proxy()); 623 fetcher_->SaveResponseToFileAtPath(file_path, io_message_loop_proxy());
571 fetcher_->Start(); 624 fetcher_->Start();
572 } 625 }
573 626
574 void URLFetcherFileTest::CreateFetcherForTempFile(const GURL& url) { 627 void URLFetcherFileTest::CreateFetcherForTempFile(const GURL& url) {
575 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); 628 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this);
576 fetcher_->SetRequestContext(new TestURLRequestContextGetter( 629 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
577 io_message_loop_proxy())); 630 io_message_loop_proxy(), request_context()));
578 631
579 // Use the IO message loop to do the file operations in this test. 632 // Use the IO message loop to do the file operations in this test.
580 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); 633 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy());
581 fetcher_->Start(); 634 fetcher_->Start();
582 } 635 }
583 636
584 void URLFetcherFileTest::OnURLFetchComplete(const content::URLFetcher* source) { 637 void URLFetcherFileTest::OnURLFetchComplete(const content::URLFetcher* source) {
585 if (expected_file_error_ == base::PLATFORM_FILE_OK) { 638 if (expected_file_error_ == base::PLATFORM_FILE_OK) {
586 EXPECT_TRUE(source->GetStatus().is_success()); 639 EXPECT_TRUE(source->GetStatus().is_success());
587 EXPECT_EQ(source->GetResponseCode(), 200); 640 EXPECT_EQ(source->GetResponseCode(), 200);
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
759 TEST_F(URLFetcherProtectTest, Overload) { 812 TEST_F(URLFetcherProtectTest, Overload) {
760 net::TestServer test_server(net::TestServer::TYPE_HTTP, 813 net::TestServer test_server(net::TestServer::TYPE_HTTP,
761 net::TestServer::kLocalhost, 814 net::TestServer::kLocalhost,
762 FilePath(kDocRoot)); 815 FilePath(kDocRoot));
763 ASSERT_TRUE(test_server.Start()); 816 ASSERT_TRUE(test_server.Start());
764 817
765 GURL url(test_server.GetURL("defaultresponse")); 818 GURL url(test_server.GetURL("defaultresponse"));
766 819
767 // Registers an entry for test url. It only allows 3 requests to be sent 820 // Registers an entry for test url. It only allows 3 requests to be sent
768 // in 200 milliseconds. 821 // in 200 milliseconds.
769 net::URLRequestThrottlerManager* manager =
770 net::URLRequestThrottlerManager::GetInstance();
771 scoped_refptr<net::URLRequestThrottlerEntry> entry( 822 scoped_refptr<net::URLRequestThrottlerEntry> entry(
772 new net::URLRequestThrottlerEntry(manager, "", 200, 3, 1, 2.0, 0.0, 256)); 823 new net::URLRequestThrottlerEntry(
773 manager->OverrideEntryForTests(url, entry); 824 request_context()->throttler_manager(),
825 "", 200, 3, 1, 2.0, 0.0, 256));
826 request_context()->throttler_manager()->OverrideEntryForTests(url, entry);
774 827
775 CreateFetcher(url); 828 CreateFetcher(url);
776 829
777 MessageLoop::current()->Run(); 830 MessageLoop::current()->Run();
778
779 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
780 } 831 }
781 832
782 TEST_F(URLFetcherProtectTest, ServerUnavailable) { 833 TEST_F(URLFetcherProtectTest, ServerUnavailable) {
783 net::TestServer test_server(net::TestServer::TYPE_HTTP, 834 net::TestServer test_server(net::TestServer::TYPE_HTTP,
784 net::TestServer::kLocalhost, 835 net::TestServer::kLocalhost,
785 FilePath(kDocRoot)); 836 FilePath(kDocRoot));
786 ASSERT_TRUE(test_server.Start()); 837 ASSERT_TRUE(test_server.Start());
787 838
788 GURL url(test_server.GetURL("files/server-unavailable.html")); 839 GURL url(test_server.GetURL("files/server-unavailable.html"));
789 840
790 // Registers an entry for test url. The backoff time is calculated by: 841 // Registers an entry for test url. The backoff time is calculated by:
791 // new_backoff = 2.0 * old_backoff + 0 842 // new_backoff = 2.0 * old_backoff + 0
792 // and maximum backoff time is 256 milliseconds. 843 // and maximum backoff time is 256 milliseconds.
793 // Maximum retries allowed is set to 11. 844 // Maximum retries allowed is set to 11.
794 net::URLRequestThrottlerManager* manager =
795 net::URLRequestThrottlerManager::GetInstance();
796 scoped_refptr<net::URLRequestThrottlerEntry> entry( 845 scoped_refptr<net::URLRequestThrottlerEntry> entry(
797 new net::URLRequestThrottlerEntry(manager, "", 200, 3, 1, 2.0, 0.0, 256)); 846 new net::URLRequestThrottlerEntry(
798 manager->OverrideEntryForTests(url, entry); 847 request_context()->throttler_manager(),
848 "", 200, 3, 1, 2.0, 0.0, 256));
849 request_context()->throttler_manager()->OverrideEntryForTests(url, entry);
799 850
800 CreateFetcher(url); 851 CreateFetcher(url);
801 852
802 MessageLoop::current()->Run(); 853 MessageLoop::current()->Run();
803
804 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
805 } 854 }
806 855
807 TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) { 856 TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) {
808 net::TestServer test_server(net::TestServer::TYPE_HTTP, 857 net::TestServer test_server(net::TestServer::TYPE_HTTP,
809 net::TestServer::kLocalhost, 858 net::TestServer::kLocalhost,
810 FilePath(kDocRoot)); 859 FilePath(kDocRoot));
811 ASSERT_TRUE(test_server.Start()); 860 ASSERT_TRUE(test_server.Start());
812 861
813 GURL url(test_server.GetURL("files/server-unavailable.html")); 862 GURL url(test_server.GetURL("files/server-unavailable.html"));
814 863
815 // Registers an entry for test url. The backoff time is calculated by: 864 // Registers an entry for test url. The backoff time is calculated by:
816 // new_backoff = 2.0 * old_backoff + 0 865 // new_backoff = 2.0 * old_backoff + 0
817 // and maximum backoff time is 150000 milliseconds. 866 // and maximum backoff time is 150000 milliseconds.
818 // Maximum retries allowed is set to 11. 867 // Maximum retries allowed is set to 11.
819 net::URLRequestThrottlerManager* manager =
820 net::URLRequestThrottlerManager::GetInstance();
821 scoped_refptr<net::URLRequestThrottlerEntry> entry( 868 scoped_refptr<net::URLRequestThrottlerEntry> entry(
822 new net::URLRequestThrottlerEntry( 869 new net::URLRequestThrottlerEntry(
823 manager, "", 200, 3, 100, 2.0, 0.0, 150000)); 870 request_context()->throttler_manager(),
871 "", 200, 3, 100, 2.0, 0.0, 150000));
824 // Total time if *not* for not doing automatic backoff would be 150s. 872 // Total time if *not* for not doing automatic backoff would be 150s.
825 // In reality it should be "as soon as server responds". 873 // In reality it should be "as soon as server responds".
826 manager->OverrideEntryForTests(url, entry); 874 request_context()->throttler_manager()->OverrideEntryForTests(url, entry);
827 875
828 CreateFetcher(url); 876 CreateFetcher(url);
829 877
830 MessageLoop::current()->Run(); 878 MessageLoop::current()->Run();
831
832 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
833 } 879 }
834 880
835 #if defined(OS_MACOSX) 881 #if defined(OS_MACOSX)
836 // SIGSEGV on Mac: http://crbug.com/60426 882 // SIGSEGV on Mac: http://crbug.com/60426
837 TEST_F(URLFetcherBadHTTPSTest, DISABLED_BadHTTPSTest) { 883 TEST_F(URLFetcherBadHTTPSTest, DISABLED_BadHTTPSTest) {
838 #else 884 #else
839 TEST_F(URLFetcherBadHTTPSTest, BadHTTPSTest) { 885 TEST_F(URLFetcherBadHTTPSTest, BadHTTPSTest) {
840 #endif 886 #endif
841 net::TestServer::HTTPSOptions https_options( 887 net::TestServer::HTTPSOptions https_options(
842 net::TestServer::HTTPSOptions::CERT_EXPIRED); 888 net::TestServer::HTTPSOptions::CERT_EXPIRED);
(...skipping 10 matching lines...) Expand all
853 #else 899 #else
854 TEST_F(URLFetcherCancelTest, ReleasesContext) { 900 TEST_F(URLFetcherCancelTest, ReleasesContext) {
855 #endif 901 #endif
856 net::TestServer test_server(net::TestServer::TYPE_HTTP, 902 net::TestServer test_server(net::TestServer::TYPE_HTTP,
857 net::TestServer::kLocalhost, 903 net::TestServer::kLocalhost,
858 FilePath(kDocRoot)); 904 FilePath(kDocRoot));
859 ASSERT_TRUE(test_server.Start()); 905 ASSERT_TRUE(test_server.Start());
860 906
861 GURL url(test_server.GetURL("files/server-unavailable.html")); 907 GURL url(test_server.GetURL("files/server-unavailable.html"));
862 908
863 // Registers an entry for test url. The backoff time is calculated by:
864 // new_backoff = 2.0 * old_backoff + 0
865 // The initial backoff is 2 seconds and maximum backoff is 4 seconds.
866 // Maximum retries allowed is set to 2.
867 net::URLRequestThrottlerManager* manager =
868 net::URLRequestThrottlerManager::GetInstance();
869 scoped_refptr<net::URLRequestThrottlerEntry> entry(
870 new net::URLRequestThrottlerEntry(
871 manager, "", 200, 3, 2000, 2.0, 0.0, 4000));
872 manager->OverrideEntryForTests(url, entry);
873
874 // Create a separate thread that will create the URLFetcher. The current 909 // Create a separate thread that will create the URLFetcher. The current
875 // (main) thread will do the IO, and when the fetch is complete it will 910 // (main) thread will do the IO, and when the fetch is complete it will
876 // terminate the main thread's message loop; then the other thread's 911 // terminate the main thread's message loop; then the other thread's
877 // message loop will be shut down automatically as the thread goes out of 912 // message loop will be shut down automatically as the thread goes out of
878 // scope. 913 // scope.
879 base::Thread t("URLFetcher test thread"); 914 base::Thread t("URLFetcher test thread");
880 ASSERT_TRUE(t.Start()); 915 ASSERT_TRUE(t.Start());
881 t.message_loop()->PostTask( 916 t.message_loop()->PostTask(
882 FROM_HERE, 917 FROM_HERE,
883 base::Bind(&URLFetcherTest::CreateFetcher, base::Unretained(this), url)); 918 base::Bind(&URLFetcherCancelTest::CreateFetcher,
919 base::Unretained(this), url));
884 920
885 MessageLoop::current()->Run(); 921 MessageLoop::current()->Run();
886
887 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
888 } 922 }
889 923
890 TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) { 924 TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) {
891 net::TestServer test_server(net::TestServer::TYPE_HTTP, 925 net::TestServer test_server(net::TestServer::TYPE_HTTP,
892 net::TestServer::kLocalhost, 926 net::TestServer::kLocalhost,
893 FilePath(kDocRoot)); 927 FilePath(kDocRoot));
894 ASSERT_TRUE(test_server.Start()); 928 ASSERT_TRUE(test_server.Start());
895 929
896 GURL url(test_server.GetURL("files/server-unavailable.html")); 930 GURL url(test_server.GetURL("files/server-unavailable.html"));
897 931
898 // Register an entry for test url. 932 // Register an entry for test url.
899 // Using a sliding window of 4 seconds, and max of 1 request, under a fast 933 // Using a sliding window of 4 seconds, and max of 1 request, under a fast
900 // run we expect to have a 4 second delay when posting the Start task. 934 // run we expect to have a 4 second delay when posting the Start task.
901 net::URLRequestThrottlerManager* manager =
902 net::URLRequestThrottlerManager::GetInstance();
903 scoped_refptr<net::URLRequestThrottlerEntry> entry( 935 scoped_refptr<net::URLRequestThrottlerEntry> entry(
904 new net::URLRequestThrottlerEntry( 936 new net::URLRequestThrottlerEntry(
905 manager, "", 4000, 1, 2000, 2.0, 0.0, 4000)); 937 request_context()->throttler_manager(),
906 manager->OverrideEntryForTests(url, entry); 938 "", 4000, 1, 2000, 2.0, 0.0, 4000));
939 request_context()->throttler_manager()->OverrideEntryForTests(url, entry);
907 // Fake that a request has just started. 940 // Fake that a request has just started.
908 entry->ReserveSendingTimeForNextRequest(base::TimeTicks()); 941 entry->ReserveSendingTimeForNextRequest(base::TimeTicks());
909 942
910 // The next request we try to send will be delayed by ~4 seconds. 943 // The next request we try to send will be delayed by ~4 seconds.
911 // The slower the test runs, the less the delay will be (since it takes the 944 // The slower the test runs, the less the delay will be (since it takes the
912 // time difference from now). 945 // time difference from now).
913 946
914 base::Thread t("URLFetcher test thread"); 947 base::Thread t("URLFetcher test thread");
915 ASSERT_TRUE(t.Start()); 948 ASSERT_TRUE(t.Start());
916 t.message_loop()->PostTask( 949 t.message_loop()->PostTask(
917 FROM_HERE, 950 FROM_HERE,
918 base::Bind(&URLFetcherTest::CreateFetcher, base::Unretained(this), url)); 951 base::Bind(&URLFetcherTest::CreateFetcher, base::Unretained(this), url));
919 952
920 MessageLoop::current()->Run(); 953 MessageLoop::current()->Run();
921
922 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
923 } 954 }
924 955
925 TEST_F(URLFetcherMultipleAttemptTest, SameData) { 956 TEST_F(URLFetcherMultipleAttemptTest, SameData) {
926 net::TestServer test_server(net::TestServer::TYPE_HTTP, 957 net::TestServer test_server(net::TestServer::TYPE_HTTP,
927 net::TestServer::kLocalhost, 958 net::TestServer::kLocalhost,
928 FilePath(kDocRoot)); 959 FilePath(kDocRoot));
929 ASSERT_TRUE(test_server.Start()); 960 ASSERT_TRUE(test_server.Start());
930 961
931 // Create the fetcher on the main thread. Since IO will happen on the main 962 // Create the fetcher on the main thread. Since IO will happen on the main
932 // thread, this will test URLFetcher's ability to do everything on one 963 // thread, this will test URLFetcher's ability to do everything on one
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
1103 std::string(kTestServerFilePrefix) + kFileToFetch)); 1134 std::string(kTestServerFilePrefix) + kFileToFetch));
1104 1135
1105 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). 1136 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1106 1137
1107 MessageLoop::current()->RunAllPending(); 1138 MessageLoop::current()->RunAllPending();
1108 ASSERT_FALSE(file_util::PathExists(file_path_)) 1139 ASSERT_FALSE(file_util::PathExists(file_path_))
1109 << file_path_.value() << " not removed."; 1140 << file_path_.value() << " not removed.";
1110 } 1141 }
1111 1142
1112 } // namespace. 1143 } // namespace.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698