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

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

Powered by Google App Engine
This is Rietveld 408576698