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

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

Powered by Google App Engine
This is Rietveld 408576698