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

Side by Side Diff: net/base/host_resolver_impl_unittest.cc

Issue 8549004: base::Bind: Convert HostResolver::Resolve. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Created 9 years, 1 month 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 | « net/base/host_resolver_impl.cc ('k') | net/base/mapped_host_resolver.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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "net/base/host_resolver_impl.h" 5 #include "net/base/host_resolver_impl.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
10 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
11 #include "base/message_loop.h" 13 #include "base/message_loop.h"
12 #include "base/string_util.h" 14 #include "base/string_util.h"
13 #include "base/stringprintf.h" 15 #include "base/stringprintf.h"
14 #include "base/synchronization/condition_variable.h" 16 #include "base/synchronization/condition_variable.h"
15 #include "base/synchronization/lock.h" 17 #include "base/synchronization/lock.h"
16 #include "base/time.h" 18 #include "base/time.h"
17 #include "net/base/address_list.h" 19 #include "net/base/address_list.h"
18 #include "net/base/completion_callback.h" 20 #include "net/base/completion_callback.h"
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 virtual ~Delegate() {} 262 virtual ~Delegate() {}
261 virtual void OnCompleted(ResolveRequest* resolve) = 0; 263 virtual void OnCompleted(ResolveRequest* resolve) = 0;
262 }; 264 };
263 265
264 ResolveRequest(HostResolver* resolver, 266 ResolveRequest(HostResolver* resolver,
265 const std::string& hostname, 267 const std::string& hostname,
266 int port, 268 int port,
267 Delegate* delegate) 269 Delegate* delegate)
268 : info_(HostPortPair(hostname, port)), 270 : info_(HostPortPair(hostname, port)),
269 resolver_(resolver), 271 resolver_(resolver),
270 delegate_(delegate), 272 delegate_(delegate) {
271 ALLOW_THIS_IN_INITIALIZER_LIST(
272 callback_(this, &ResolveRequest::OnLookupFinished)) {
273 // Start the request. 273 // Start the request.
274 int err = resolver->Resolve(info_, &addrlist_, &callback_, &req_, 274 int err = resolver->Resolve(
275 BoundNetLog()); 275 info_, &addrlist_,
276 base::Bind(&ResolveRequest::OnLookupFinished, base::Unretained(this)),
277 &req_, BoundNetLog());
276 EXPECT_EQ(ERR_IO_PENDING, err); 278 EXPECT_EQ(ERR_IO_PENDING, err);
277 } 279 }
278 280
279 ResolveRequest(HostResolver* resolver, 281 ResolveRequest(HostResolver* resolver,
280 const HostResolver::RequestInfo& info, 282 const HostResolver::RequestInfo& info,
281 Delegate* delegate) 283 Delegate* delegate)
282 : info_(info), resolver_(resolver), delegate_(delegate), 284 : info_(info), resolver_(resolver), delegate_(delegate) {
283 ALLOW_THIS_IN_INITIALIZER_LIST(
284 callback_(this, &ResolveRequest::OnLookupFinished)) {
285 // Start the request. 285 // Start the request.
286 int err = resolver->Resolve(info, &addrlist_, &callback_, &req_, 286 int err = resolver->Resolve(
287 BoundNetLog()); 287 info, &addrlist_,
288 base::Bind(&ResolveRequest::OnLookupFinished,
289 base::Unretained(this)),
290 &req_, BoundNetLog());
288 EXPECT_EQ(ERR_IO_PENDING, err); 291 EXPECT_EQ(ERR_IO_PENDING, err);
289 } 292 }
290 293
291 void Cancel() { 294 void Cancel() {
292 resolver_->CancelRequest(req_); 295 resolver_->CancelRequest(req_);
293 } 296 }
294 297
295 const std::string& hostname() const { 298 const std::string& hostname() const {
296 return info_.hostname(); 299 return info_.hostname();
297 } 300 }
(...skipping 24 matching lines...) Expand all
322 HostResolver::RequestInfo info_; 325 HostResolver::RequestInfo info_;
323 HostResolver::RequestHandle req_; 326 HostResolver::RequestHandle req_;
324 327
325 // The result of the resolve. 328 // The result of the resolve.
326 int result_; 329 int result_;
327 AddressList addrlist_; 330 AddressList addrlist_;
328 331
329 HostResolver* resolver_; 332 HostResolver* resolver_;
330 333
331 Delegate* delegate_; 334 Delegate* delegate_;
332 OldCompletionCallbackImpl<ResolveRequest> callback_;
333 335
334 DISALLOW_COPY_AND_ASSIGN(ResolveRequest); 336 DISALLOW_COPY_AND_ASSIGN(ResolveRequest);
335 }; 337 };
336 338
337 class HostResolverImplTest : public testing::Test { 339 class HostResolverImplTest : public testing::Test {
338 public: 340 public:
339 HostResolverImplTest() 341 HostResolverImplTest()
340 : callback_called_(false), 342 : callback_called_(false),
341 ALLOW_THIS_IN_INITIALIZER_LIST( 343 ALLOW_THIS_IN_INITIALIZER_LIST(callback_(
342 callback_(this, &HostResolverImplTest::OnLookupFinished)) { 344 base::Bind(&HostResolverImplTest::OnLookupFinished,
345 base::Unretained(this)))) {
343 } 346 }
344 347
345 protected: 348 protected:
346 bool callback_called_;
347 int callback_result_;
348 OldCompletionCallbackImpl<HostResolverImplTest> callback_;
349
350 private:
351 void OnLookupFinished(int result) { 349 void OnLookupFinished(int result) {
352 callback_called_ = true; 350 callback_called_ = true;
353 callback_result_ = result; 351 callback_result_ = result;
354 MessageLoop::current()->Quit(); 352 MessageLoop::current()->Quit();
355 } 353 }
354
355 bool callback_called_;
356 int callback_result_;
357 CompletionCallback callback_;
356 }; 358 };
357 359
358 TEST_F(HostResolverImplTest, AsynchronousLookup) { 360 TEST_F(HostResolverImplTest, AsynchronousLookup) {
359 AddressList addrlist; 361 AddressList addrlist;
360 const int kPortnum = 80; 362 const int kPortnum = 80;
361 363
362 scoped_refptr<RuleBasedHostResolverProc> resolver_proc( 364 scoped_refptr<RuleBasedHostResolverProc> resolver_proc(
363 new RuleBasedHostResolverProc(NULL)); 365 new RuleBasedHostResolverProc(NULL));
364 resolver_proc->AddRule("just.testing", "192.168.1.42"); 366 resolver_proc->AddRule("just.testing", "192.168.1.42");
365 367
366 scoped_ptr<HostResolver> host_resolver( 368 scoped_ptr<HostResolver> host_resolver(
367 CreateHostResolverImpl(resolver_proc)); 369 CreateHostResolverImpl(resolver_proc));
368 370
369 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum)); 371 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum));
370 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); 372 CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
371 int err = host_resolver->Resolve(info, &addrlist, &callback_, NULL, 373 int err = host_resolver->Resolve(info, &addrlist, callback_, NULL,
372 log.bound()); 374 log.bound());
373 EXPECT_EQ(ERR_IO_PENDING, err); 375 EXPECT_EQ(ERR_IO_PENDING, err);
374 376
375 CapturingNetLog::EntryList entries; 377 CapturingNetLog::EntryList entries;
376 log.GetEntries(&entries); 378 log.GetEntries(&entries);
377 379
378 EXPECT_EQ(1u, entries.size()); 380 EXPECT_EQ(1u, entries.size());
379 EXPECT_TRUE(LogContainsBeginEvent( 381 EXPECT_TRUE(LogContainsBeginEvent(
380 entries, 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); 382 entries, 0, NetLog::TYPE_HOST_RESOLVER_IMPL));
381 383
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 scoped_ptr<HostResolver> host_resolver( 453 scoped_ptr<HostResolver> host_resolver(
452 new HostResolverImpl(resolver_proc, 454 new HostResolverImpl(resolver_proc,
453 HostCache::CreateDefaultCache(), 455 HostCache::CreateDefaultCache(),
454 kMaxJobs, 456 kMaxJobs,
455 kMaxRetryAttempts, 457 kMaxRetryAttempts,
456 &net_log)); 458 &net_log));
457 AddressList addrlist; 459 AddressList addrlist;
458 const int kPortnum = 80; 460 const int kPortnum = 80;
459 461
460 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum)); 462 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum));
461 int err = host_resolver->Resolve(info, &addrlist, &callback_, NULL, 463 int err = host_resolver->Resolve(info, &addrlist, callback_, NULL,
462 log.bound()); 464 log.bound());
463 EXPECT_EQ(ERR_IO_PENDING, err); 465 EXPECT_EQ(ERR_IO_PENDING, err);
464 466
465 resolver_proc->Wait(); 467 resolver_proc->Wait();
466 } 468 }
467 469
468 resolver_proc->Signal(); 470 resolver_proc->Signal();
469 471
470 CapturingNetLog::EntryList entries; 472 CapturingNetLog::EntryList entries;
471 log.GetEntries(&entries); 473 log.GetEntries(&entries);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 // Stevens says dotted quads with AI_UNSPEC resolve to a single sockaddr_in. 510 // Stevens says dotted quads with AI_UNSPEC resolve to a single sockaddr_in.
509 511
510 scoped_refptr<RuleBasedHostResolverProc> resolver_proc( 512 scoped_refptr<RuleBasedHostResolverProc> resolver_proc(
511 new RuleBasedHostResolverProc(NULL)); 513 new RuleBasedHostResolverProc(NULL));
512 resolver_proc->AllowDirectLookup("*"); 514 resolver_proc->AllowDirectLookup("*");
513 515
514 scoped_ptr<HostResolver> host_resolver( 516 scoped_ptr<HostResolver> host_resolver(
515 CreateHostResolverImpl(resolver_proc)); 517 CreateHostResolverImpl(resolver_proc));
516 AddressList addrlist; 518 AddressList addrlist;
517 const int kPortnum = 5555; 519 const int kPortnum = 5555;
518 TestOldCompletionCallback callback; 520 TestCompletionCallback callback;
519 HostResolver::RequestInfo info(HostPortPair("127.1.2.3", kPortnum)); 521 HostResolver::RequestInfo info(HostPortPair("127.1.2.3", kPortnum));
520 int err = host_resolver->Resolve(info, &addrlist, &callback, NULL, 522 int err = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL,
521 BoundNetLog()); 523 BoundNetLog());
522 EXPECT_EQ(OK, err); 524 EXPECT_EQ(OK, err);
523 525
524 const struct addrinfo* ainfo = addrlist.head(); 526 const struct addrinfo* ainfo = addrlist.head();
525 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); 527 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next);
526 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); 528 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen);
527 529
528 const struct sockaddr* sa = ainfo->ai_addr; 530 const struct sockaddr* sa = ainfo->ai_addr;
529 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; 531 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa;
530 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); 532 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port);
531 EXPECT_TRUE(htonl(0x7f010203) == sa_in->sin_addr.s_addr); 533 EXPECT_TRUE(htonl(0x7f010203) == sa_in->sin_addr.s_addr);
532 } 534 }
533 535
534 TEST_F(HostResolverImplTest, NumericIPv6Address) { 536 TEST_F(HostResolverImplTest, NumericIPv6Address) {
535 scoped_refptr<RuleBasedHostResolverProc> resolver_proc( 537 scoped_refptr<RuleBasedHostResolverProc> resolver_proc(
536 new RuleBasedHostResolverProc(NULL)); 538 new RuleBasedHostResolverProc(NULL));
537 resolver_proc->AllowDirectLookup("*"); 539 resolver_proc->AllowDirectLookup("*");
538 540
539 // Resolve a plain IPv6 address. Don't worry about [brackets], because 541 // Resolve a plain IPv6 address. Don't worry about [brackets], because
540 // the caller should have removed them. 542 // the caller should have removed them.
541 scoped_ptr<HostResolver> host_resolver( 543 scoped_ptr<HostResolver> host_resolver(
542 CreateHostResolverImpl(resolver_proc)); 544 CreateHostResolverImpl(resolver_proc));
543 AddressList addrlist; 545 AddressList addrlist;
544 const int kPortnum = 5555; 546 const int kPortnum = 5555;
545 TestOldCompletionCallback callback; 547 TestCompletionCallback callback;
546 HostResolver::RequestInfo info(HostPortPair("2001:db8::1", kPortnum)); 548 HostResolver::RequestInfo info(HostPortPair("2001:db8::1", kPortnum));
547 int err = host_resolver->Resolve(info, &addrlist, &callback, NULL, 549 int err = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL,
548 BoundNetLog()); 550 BoundNetLog());
549 EXPECT_EQ(OK, err); 551 EXPECT_EQ(OK, err);
550 552
551 const struct addrinfo* ainfo = addrlist.head(); 553 const struct addrinfo* ainfo = addrlist.head();
552 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); 554 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next);
553 EXPECT_EQ(sizeof(struct sockaddr_in6), ainfo->ai_addrlen); 555 EXPECT_EQ(sizeof(struct sockaddr_in6), ainfo->ai_addrlen);
554 556
555 const struct sockaddr* sa = ainfo->ai_addr; 557 const struct sockaddr* sa = ainfo->ai_addr;
556 const struct sockaddr_in6* sa_in6 = (const struct sockaddr_in6*) sa; 558 const struct sockaddr_in6* sa_in6 = (const struct sockaddr_in6*) sa;
557 EXPECT_TRUE(htons(kPortnum) == sa_in6->sin6_port); 559 EXPECT_TRUE(htons(kPortnum) == sa_in6->sin6_port);
558 560
559 const uint8 expect_addr[] = { 561 const uint8 expect_addr[] = {
560 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 562 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
561 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 563 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
562 }; 564 };
563 for (int i = 0; i < 16; i++) { 565 for (int i = 0; i < 16; i++) {
564 EXPECT_EQ(expect_addr[i], sa_in6->sin6_addr.s6_addr[i]); 566 EXPECT_EQ(expect_addr[i], sa_in6->sin6_addr.s6_addr[i]);
565 } 567 }
566 } 568 }
567 569
568 TEST_F(HostResolverImplTest, EmptyHost) { 570 TEST_F(HostResolverImplTest, EmptyHost) {
569 scoped_refptr<RuleBasedHostResolverProc> resolver_proc( 571 scoped_refptr<RuleBasedHostResolverProc> resolver_proc(
570 new RuleBasedHostResolverProc(NULL)); 572 new RuleBasedHostResolverProc(NULL));
571 resolver_proc->AllowDirectLookup("*"); 573 resolver_proc->AllowDirectLookup("*");
572 574
573 scoped_ptr<HostResolver> host_resolver( 575 scoped_ptr<HostResolver> host_resolver(
574 CreateHostResolverImpl(resolver_proc)); 576 CreateHostResolverImpl(resolver_proc));
575 AddressList addrlist; 577 AddressList addrlist;
576 const int kPortnum = 5555; 578 const int kPortnum = 5555;
577 TestOldCompletionCallback callback; 579 TestCompletionCallback callback;
578 HostResolver::RequestInfo info(HostPortPair("", kPortnum)); 580 HostResolver::RequestInfo info(HostPortPair("", kPortnum));
579 int err = host_resolver->Resolve(info, &addrlist, &callback, NULL, 581 int err = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL,
580 BoundNetLog()); 582 BoundNetLog());
581 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, err); 583 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, err);
582 } 584 }
583 585
584 TEST_F(HostResolverImplTest, LongHost) { 586 TEST_F(HostResolverImplTest, LongHost) {
585 scoped_refptr<RuleBasedHostResolverProc> resolver_proc( 587 scoped_refptr<RuleBasedHostResolverProc> resolver_proc(
586 new RuleBasedHostResolverProc(NULL)); 588 new RuleBasedHostResolverProc(NULL));
587 resolver_proc->AllowDirectLookup("*"); 589 resolver_proc->AllowDirectLookup("*");
588 590
589 scoped_ptr<HostResolver> host_resolver( 591 scoped_ptr<HostResolver> host_resolver(
590 CreateHostResolverImpl(resolver_proc)); 592 CreateHostResolverImpl(resolver_proc));
591 AddressList addrlist; 593 AddressList addrlist;
592 const int kPortnum = 5555; 594 const int kPortnum = 5555;
593 std::string hostname(4097, 'a'); 595 std::string hostname(4097, 'a');
594 TestOldCompletionCallback callback; 596 TestCompletionCallback callback;
595 HostResolver::RequestInfo info(HostPortPair(hostname, kPortnum)); 597 HostResolver::RequestInfo info(HostPortPair(hostname, kPortnum));
596 int err = host_resolver->Resolve(info, &addrlist, &callback, NULL, 598 int err = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL,
597 BoundNetLog()); 599 BoundNetLog());
598 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, err); 600 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, err);
599 } 601 }
600 602
601 // Helper class used by HostResolverImplTest.DeDupeRequests. It receives request 603 // Helper class used by HostResolverImplTest.DeDupeRequests. It receives request
602 // completion notifications for all the resolves, so it can tally up and 604 // completion notifications for all the resolves, so it can tally up and
603 // determine when we are done. 605 // determine when we are done.
604 class DeDupeRequestsVerifier : public ResolveRequest::Delegate { 606 class DeDupeRequestsVerifier : public ResolveRequest::Delegate {
605 public: 607 public:
606 explicit DeDupeRequestsVerifier(CapturingHostResolverProc* resolver_proc) 608 explicit DeDupeRequestsVerifier(CapturingHostResolverProc* resolver_proc)
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
930 932
931 virtual void OnCompleted(ResolveRequest* resolve) { 933 virtual void OnCompleted(ResolveRequest* resolve) {
932 EXPECT_EQ("a", resolve->hostname()); 934 EXPECT_EQ("a", resolve->hostname());
933 HostResolver* resolver = resolve->resolver(); 935 HostResolver* resolver = resolve->resolver();
934 936
935 if (80 == resolve->port()) { 937 if (80 == resolve->port()) {
936 // On completing the first request, start another request for "a". 938 // On completing the first request, start another request for "a".
937 // Since caching is enabled, this should complete synchronously. 939 // Since caching is enabled, this should complete synchronously.
938 940
939 // Note that |junk_callback| shouldn't be used since we are going to 941 // Note that |junk_callback| shouldn't be used since we are going to
940 // complete synchronously. We can't specify NULL though since that would 942 // complete synchronously.
941 // mean synchronous mode so we give it a value of 1. 943 TestCompletionCallback junk_callback;
942 OldCompletionCallback* junk_callback =
943 reinterpret_cast<OldCompletionCallback*> (1);
944 AddressList addrlist; 944 AddressList addrlist;
945 945
946 HostResolver::RequestInfo info(HostPortPair("a", 70)); 946 HostResolver::RequestInfo info(HostPortPair("a", 70));
947 int error = resolver->Resolve(info, &addrlist, junk_callback, NULL, 947 int error = resolver->Resolve(info, &addrlist, junk_callback.callback(),
948 BoundNetLog()); 948 NULL, BoundNetLog());
949 EXPECT_EQ(OK, error); 949 EXPECT_EQ(OK, error);
950 950
951 // Ok good. Now make sure that if we ask to bypass the cache, it can no 951 // Ok good. Now make sure that if we ask to bypass the cache, it can no
952 // longer service the request synchronously. 952 // longer service the request synchronously.
953 info = HostResolver::RequestInfo(HostPortPair("a", 71)); 953 info = HostResolver::RequestInfo(HostPortPair("a", 71));
954 info.set_allow_cached_response(false); 954 info.set_allow_cached_response(false);
955 final_request_.reset(new ResolveRequest(resolver, info, this)); 955 final_request_.reset(new ResolveRequest(resolver, info, this));
956 } else if (71 == resolve->port()) { 956 } else if (71 == resolve->port()) {
957 // Test is done. 957 // Test is done.
958 MessageLoop::current()->Quit(); 958 MessageLoop::current()->Quit();
(...skipping 25 matching lines...) Expand all
984 // Test that IP address changes flush the cache. 984 // Test that IP address changes flush the cache.
985 TEST_F(HostResolverImplTest, FlushCacheOnIPAddressChange) { 985 TEST_F(HostResolverImplTest, FlushCacheOnIPAddressChange) {
986 scoped_ptr<HostResolver> host_resolver( 986 scoped_ptr<HostResolver> host_resolver(
987 new HostResolverImpl(NULL, HostCache::CreateDefaultCache(), kMaxJobs, 987 new HostResolverImpl(NULL, HostCache::CreateDefaultCache(), kMaxJobs,
988 kMaxRetryAttempts, NULL)); 988 kMaxRetryAttempts, NULL));
989 989
990 AddressList addrlist; 990 AddressList addrlist;
991 991
992 // Resolve "host1". 992 // Resolve "host1".
993 HostResolver::RequestInfo info1(HostPortPair("host1", 70)); 993 HostResolver::RequestInfo info1(HostPortPair("host1", 70));
994 TestOldCompletionCallback callback; 994 TestCompletionCallback callback;
995 int rv = host_resolver->Resolve(info1, &addrlist, &callback, NULL, 995 int rv = host_resolver->Resolve(info1, &addrlist, callback.callback(), NULL,
996 BoundNetLog()); 996 BoundNetLog());
997 EXPECT_EQ(ERR_IO_PENDING, rv); 997 EXPECT_EQ(ERR_IO_PENDING, rv);
998 EXPECT_EQ(OK, callback.WaitForResult()); 998 EXPECT_EQ(OK, callback.WaitForResult());
999 999
1000 // Resolve "host1" again -- this time it will be served from cache, but it 1000 // Resolve "host1" again -- this time it will be served from cache, but it
1001 // should still notify of completion. 1001 // should still notify of completion.
1002 rv = host_resolver->Resolve(info1, &addrlist, &callback, NULL, BoundNetLog()); 1002 rv = host_resolver->Resolve(info1, &addrlist, callback.callback(), NULL,
1003 BoundNetLog());
1003 ASSERT_EQ(OK, rv); // Should complete synchronously. 1004 ASSERT_EQ(OK, rv); // Should complete synchronously.
1004 1005
1005 // Flush cache by triggering an IP address change. 1006 // Flush cache by triggering an IP address change.
1006 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); 1007 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
1007 MessageLoop::current()->RunAllPending(); // Notification happens async. 1008 MessageLoop::current()->RunAllPending(); // Notification happens async.
1008 1009
1009 // Resolve "host1" again -- this time it won't be served from cache, so it 1010 // Resolve "host1" again -- this time it won't be served from cache, so it
1010 // will complete asynchronously. 1011 // will complete asynchronously.
1011 rv = host_resolver->Resolve(info1, &addrlist, &callback, NULL, BoundNetLog()); 1012 rv = host_resolver->Resolve(info1, &addrlist, callback.callback(), NULL,
1013 BoundNetLog());
1012 ASSERT_EQ(ERR_IO_PENDING, rv); // Should complete asynchronously. 1014 ASSERT_EQ(ERR_IO_PENDING, rv); // Should complete asynchronously.
1013 EXPECT_EQ(OK, callback.WaitForResult()); 1015 EXPECT_EQ(OK, callback.WaitForResult());
1014 } 1016 }
1015 1017
1016 // Test that IP address changes send ERR_ABORTED to pending requests. 1018 // Test that IP address changes send ERR_ABORTED to pending requests.
1017 TEST_F(HostResolverImplTest, AbortOnIPAddressChanged) { 1019 TEST_F(HostResolverImplTest, AbortOnIPAddressChanged) {
1018 scoped_refptr<WaitingHostResolverProc> resolver_proc( 1020 scoped_refptr<WaitingHostResolverProc> resolver_proc(
1019 new WaitingHostResolverProc(NULL)); 1021 new WaitingHostResolverProc(NULL));
1020 scoped_ptr<HostResolver> host_resolver(CreateHostResolverImpl(resolver_proc)); 1022 scoped_ptr<HostResolver> host_resolver(CreateHostResolverImpl(resolver_proc));
1021 1023
1022 // Resolve "host1". 1024 // Resolve "host1".
1023 HostResolver::RequestInfo info(HostPortPair("host1", 70)); 1025 HostResolver::RequestInfo info(HostPortPair("host1", 70));
1024 TestOldCompletionCallback callback; 1026 TestCompletionCallback callback;
1025 AddressList addrlist; 1027 AddressList addrlist;
1026 int rv = host_resolver->Resolve(info, &addrlist, &callback, NULL, 1028 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL,
1027 BoundNetLog()); 1029 BoundNetLog());
1028 EXPECT_EQ(ERR_IO_PENDING, rv); 1030 EXPECT_EQ(ERR_IO_PENDING, rv);
1029 1031
1030 resolver_proc->Wait(); 1032 resolver_proc->Wait();
1031 // Triggering an IP address change. 1033 // Triggering an IP address change.
1032 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); 1034 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
1033 MessageLoop::current()->RunAllPending(); // Notification happens async. 1035 MessageLoop::current()->RunAllPending(); // Notification happens async.
1034 resolver_proc->Signal(); 1036 resolver_proc->Signal();
1035 1037
1036 EXPECT_EQ(ERR_ABORTED, callback.WaitForResult()); 1038 EXPECT_EQ(ERR_ABORTED, callback.WaitForResult());
1037 EXPECT_EQ(0u, host_resolver->GetHostCache()->size()); 1039 EXPECT_EQ(0u, host_resolver->GetHostCache()->size());
1038 } 1040 }
1039 1041
1040 // Obey pool constraints after IP address has changed. 1042 // Obey pool constraints after IP address has changed.
1041 TEST_F(HostResolverImplTest, ObeyPoolConstraintsAfterIPAddressChange) { 1043 TEST_F(HostResolverImplTest, ObeyPoolConstraintsAfterIPAddressChange) {
1042 scoped_refptr<WaitingHostResolverProc> resolver_proc( 1044 scoped_refptr<WaitingHostResolverProc> resolver_proc(
1043 new WaitingHostResolverProc(CreateCatchAllHostResolverProc())); 1045 new WaitingHostResolverProc(CreateCatchAllHostResolverProc()));
1044 scoped_ptr<HostResolverImpl> host_resolver( 1046 scoped_ptr<HostResolverImpl> host_resolver(
1045 new HostResolverImpl(resolver_proc, HostCache::CreateDefaultCache(), 1047 new HostResolverImpl(resolver_proc, HostCache::CreateDefaultCache(),
1046 kMaxJobs, kMaxRetryAttempts, NULL)); 1048 kMaxJobs, kMaxRetryAttempts, NULL));
1047 1049
1048 const size_t kMaxOutstandingJobs = 1u; 1050 const size_t kMaxOutstandingJobs = 1u;
1049 const size_t kMaxPendingRequests = 1000000u; // not relevant. 1051 const size_t kMaxPendingRequests = 1000000u; // not relevant.
1050 host_resolver->SetPoolConstraints(HostResolverImpl::POOL_NORMAL, 1052 host_resolver->SetPoolConstraints(HostResolverImpl::POOL_NORMAL,
1051 kMaxOutstandingJobs, 1053 kMaxOutstandingJobs,
1052 kMaxPendingRequests); 1054 kMaxPendingRequests);
1053 1055
1054 // Resolve "host1". 1056 // Resolve "host1".
1055 HostResolver::RequestInfo info(HostPortPair("host1", 70)); 1057 HostResolver::RequestInfo info(HostPortPair("host1", 70));
1056 TestOldCompletionCallback callback; 1058 TestCompletionCallback callback;
1057 AddressList addrlist; 1059 AddressList addrlist;
1058 int rv = host_resolver->Resolve(info, &addrlist, &callback, NULL, 1060 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL,
1059 BoundNetLog()); 1061 BoundNetLog());
1060 EXPECT_EQ(ERR_IO_PENDING, rv); 1062 EXPECT_EQ(ERR_IO_PENDING, rv);
1061 1063
1062 // Must wait before signal to ensure that the two signals don't get merged 1064 // Must wait before signal to ensure that the two signals don't get merged
1063 // together. (Worker threads might not start until the last WaitForResult.) 1065 // together. (Worker threads might not start until the last WaitForResult.)
1064 resolver_proc->Wait(); 1066 resolver_proc->Wait();
1065 // Triggering an IP address change. 1067 // Triggering an IP address change.
1066 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); 1068 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
1067 MessageLoop::current()->RunAllPending(); // Notification happens async. 1069 MessageLoop::current()->RunAllPending(); // Notification happens async.
1068 resolver_proc->Signal(); 1070 resolver_proc->Signal();
1069 1071
1070 EXPECT_EQ(ERR_ABORTED, callback.WaitForResult()); 1072 EXPECT_EQ(ERR_ABORTED, callback.WaitForResult());
1071 1073
1072 rv = host_resolver->Resolve(info, &addrlist, &callback, NULL, 1074 rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL,
1073 BoundNetLog()); 1075 BoundNetLog());
1074 EXPECT_EQ(ERR_IO_PENDING, rv); 1076 EXPECT_EQ(ERR_IO_PENDING, rv);
1075 resolver_proc->Wait(); 1077 resolver_proc->Wait();
1076 resolver_proc->Signal(); 1078 resolver_proc->Signal();
1077 EXPECT_EQ(OK, callback.WaitForResult()); 1079 EXPECT_EQ(OK, callback.WaitForResult());
1078 } 1080 }
1079 1081
1080 class ResolveWithinCallback : public CallbackRunner< Tuple1<int> > { 1082 class ResolveWithinCallback {
1081 public: 1083 public:
1082 explicit ResolveWithinCallback(const HostResolver::RequestInfo& info) 1084 explicit ResolveWithinCallback(const HostResolver::RequestInfo& info)
1083 : info_(info) {} 1085 : info_(info) {}
1084 1086
1085 virtual void RunWithParams(const Tuple1<int>& params) { 1087 const CompletionCallback& callback() const { return callback_.callback(); }
1088
1089 int WaitForResult() {
1090 int result = callback_.WaitForResult();
1086 // Ditch the WaitingHostResolverProc so that the subsequent request 1091 // Ditch the WaitingHostResolverProc so that the subsequent request
1087 // succeeds. 1092 // succeeds.
1088 callback_.RunWithParams(params);
1089 host_resolver_.reset( 1093 host_resolver_.reset(
1090 CreateHostResolverImpl(CreateCatchAllHostResolverProc())); 1094 CreateHostResolverImpl(CreateCatchAllHostResolverProc()));
1091 EXPECT_EQ(ERR_IO_PENDING, 1095 EXPECT_EQ(ERR_IO_PENDING,
1092 host_resolver_->Resolve(info_, &addrlist_, &nested_callback_, 1096 host_resolver_->Resolve(info_, &addrlist_,
1093 NULL, BoundNetLog())); 1097 nested_callback_.callback(), NULL,
1094 } 1098 BoundNetLog()));
1095 1099 return result;
1096 int WaitForResult() {
1097 return callback_.WaitForResult();
1098 } 1100 }
1099 1101
1100 int WaitForNestedResult() { 1102 int WaitForNestedResult() {
1101 return nested_callback_.WaitForResult(); 1103 return nested_callback_.WaitForResult();
1102 } 1104 }
1103 1105
1104 private: 1106 private:
1105 const HostResolver::RequestInfo info_; 1107 const HostResolver::RequestInfo info_;
1106 AddressList addrlist_; 1108 AddressList addrlist_;
1107 TestOldCompletionCallback callback_; 1109 TestCompletionCallback callback_;
1108 TestOldCompletionCallback nested_callback_; 1110 TestCompletionCallback nested_callback_;
1109 scoped_ptr<HostResolver> host_resolver_; 1111 scoped_ptr<HostResolver> host_resolver_;
1110 }; 1112 };
1111 1113
1112 TEST_F(HostResolverImplTest, OnlyAbortExistingRequestsOnIPAddressChange) { 1114 TEST_F(HostResolverImplTest, OnlyAbortExistingRequestsOnIPAddressChange) {
1113 scoped_refptr<WaitingHostResolverProc> resolver_proc( 1115 scoped_refptr<WaitingHostResolverProc> resolver_proc(
1114 new WaitingHostResolverProc(CreateCatchAllHostResolverProc())); 1116 new WaitingHostResolverProc(CreateCatchAllHostResolverProc()));
1115 scoped_ptr<HostResolver> host_resolver(CreateHostResolverImpl(resolver_proc)); 1117 scoped_ptr<HostResolver> host_resolver(CreateHostResolverImpl(resolver_proc));
1116 1118
1117 // Resolve "host1". 1119 // Resolve "host1".
1118 HostResolver::RequestInfo info(HostPortPair("host1", 70)); 1120 HostResolver::RequestInfo info(HostPortPair("host1", 70));
1119 ResolveWithinCallback callback(info); 1121 ResolveWithinCallback callback(info);
1120 AddressList addrlist; 1122 AddressList addrlist;
1121 int rv = host_resolver->Resolve(info, &addrlist, &callback, NULL, 1123 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL,
1122 BoundNetLog()); 1124 BoundNetLog());
1123 EXPECT_EQ(ERR_IO_PENDING, rv); 1125 EXPECT_EQ(ERR_IO_PENDING, rv);
1124 1126
1125 resolver_proc->Wait(); 1127 resolver_proc->Wait();
1126 // Triggering an IP address change. 1128 // Triggering an IP address change.
1127 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); 1129 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
1128 MessageLoop::current()->RunAllPending(); // Notification happens async. 1130 MessageLoop::current()->RunAllPending(); // Notification happens async.
1129 EXPECT_EQ(ERR_ABORTED, callback.WaitForResult()); 1131 EXPECT_EQ(ERR_ABORTED, callback.WaitForResult());
1130 resolver_proc->Signal(); // release the thread from WorkerPool for cleanup 1132 resolver_proc->Signal(); // release the thread from WorkerPool for cleanup
1131 EXPECT_EQ(OK, callback.WaitForNestedResult()); 1133 EXPECT_EQ(OK, callback.WaitForNestedResult());
(...skipping 19 matching lines...) Expand all
1151 CreateResolverRequest("req0", LOW), 1153 CreateResolverRequest("req0", LOW),
1152 CreateResolverRequest("req1", MEDIUM), 1154 CreateResolverRequest("req1", MEDIUM),
1153 CreateResolverRequest("req2", MEDIUM), 1155 CreateResolverRequest("req2", MEDIUM),
1154 CreateResolverRequest("req3", LOW), 1156 CreateResolverRequest("req3", LOW),
1155 CreateResolverRequest("req4", HIGHEST), 1157 CreateResolverRequest("req4", HIGHEST),
1156 CreateResolverRequest("req5", LOW), 1158 CreateResolverRequest("req5", LOW),
1157 CreateResolverRequest("req6", LOW), 1159 CreateResolverRequest("req6", LOW),
1158 CreateResolverRequest("req5", HIGHEST), 1160 CreateResolverRequest("req5", HIGHEST),
1159 }; 1161 };
1160 1162
1161 TestOldCompletionCallback callback[arraysize(req)]; 1163 TestCompletionCallback callback[arraysize(req)];
1162 AddressList addrlist[arraysize(req)]; 1164 AddressList addrlist[arraysize(req)];
1163 1165
1164 // Start all of the requests. 1166 // Start all of the requests.
1165 for (size_t i = 0; i < arraysize(req); ++i) { 1167 for (size_t i = 0; i < arraysize(req); ++i) {
1166 int rv = host_resolver->Resolve(req[i], &addrlist[i], 1168 int rv = host_resolver->Resolve(req[i], &addrlist[i],
1167 &callback[i], NULL, BoundNetLog()); 1169 callback[i].callback(), NULL,
1170 BoundNetLog());
1168 EXPECT_EQ(ERR_IO_PENDING, rv); 1171 EXPECT_EQ(ERR_IO_PENDING, rv);
1169 } 1172 }
1170 1173
1171 // Unblock the resolver thread so the requests can run. 1174 // Unblock the resolver thread so the requests can run.
1172 resolver_proc->Signal(); 1175 resolver_proc->Signal();
1173 1176
1174 // Wait for all the requests to complete succesfully. 1177 // Wait for all the requests to complete succesfully.
1175 for (size_t i = 0; i < arraysize(req); ++i) { 1178 for (size_t i = 0; i < arraysize(req); ++i) {
1176 EXPECT_EQ(OK, callback[i].WaitForResult()) << "i=" << i; 1179 EXPECT_EQ(OK, callback[i].WaitForResult()) << "i=" << i;
1177 } 1180 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1211 HostResolver::RequestInfo req[] = { 1214 HostResolver::RequestInfo req[] = {
1212 CreateResolverRequest("req0", LOWEST), 1215 CreateResolverRequest("req0", LOWEST),
1213 CreateResolverRequest("req1", HIGHEST), // Will cancel. 1216 CreateResolverRequest("req1", HIGHEST), // Will cancel.
1214 CreateResolverRequest("req2", MEDIUM), 1217 CreateResolverRequest("req2", MEDIUM),
1215 CreateResolverRequest("req3", LOW), 1218 CreateResolverRequest("req3", LOW),
1216 CreateResolverRequest("req4", HIGHEST), // Will cancel. 1219 CreateResolverRequest("req4", HIGHEST), // Will cancel.
1217 CreateResolverRequest("req5", LOWEST), // Will cancel. 1220 CreateResolverRequest("req5", LOWEST), // Will cancel.
1218 CreateResolverRequest("req6", MEDIUM), 1221 CreateResolverRequest("req6", MEDIUM),
1219 }; 1222 };
1220 1223
1221 TestOldCompletionCallback callback[arraysize(req)]; 1224 TestCompletionCallback callback[arraysize(req)];
1222 AddressList addrlist[arraysize(req)]; 1225 AddressList addrlist[arraysize(req)];
1223 HostResolver::RequestHandle handle[arraysize(req)]; 1226 HostResolver::RequestHandle handle[arraysize(req)];
1224 1227
1225 // Start all of the requests. 1228 // Start all of the requests.
1226 for (size_t i = 0; i < arraysize(req); ++i) { 1229 for (size_t i = 0; i < arraysize(req); ++i) {
1227 int rv = host_resolver->Resolve(req[i], &addrlist[i], 1230 int rv = host_resolver->Resolve(req[i], &addrlist[i],
1228 &callback[i], &handle[i], BoundNetLog()); 1231 callback[i].callback(), &handle[i],
1232 BoundNetLog());
1229 EXPECT_EQ(ERR_IO_PENDING, rv); 1233 EXPECT_EQ(ERR_IO_PENDING, rv);
1230 } 1234 }
1231 1235
1232 // Cancel some requests 1236 // Cancel some requests
1233 host_resolver->CancelRequest(handle[1]); 1237 host_resolver->CancelRequest(handle[1]);
1234 host_resolver->CancelRequest(handle[4]); 1238 host_resolver->CancelRequest(handle[4]);
1235 host_resolver->CancelRequest(handle[5]); 1239 host_resolver->CancelRequest(handle[5]);
1236 handle[1] = handle[4] = handle[5] = NULL; 1240 handle[1] = handle[4] = handle[5] = NULL;
1237 1241
1238 // Unblock the resolver thread so the requests can run. 1242 // Unblock the resolver thread so the requests can run.
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1287 // At this point, there are 3 enqueued requests. 1291 // At this point, there are 3 enqueued requests.
1288 // Insertion of subsequent requests will cause evictions 1292 // Insertion of subsequent requests will cause evictions
1289 // based on priority. 1293 // based on priority.
1290 1294
1291 CreateResolverRequest("req4", LOW), // Evicts itself! 1295 CreateResolverRequest("req4", LOW), // Evicts itself!
1292 CreateResolverRequest("req5", MEDIUM), // Evicts req3 1296 CreateResolverRequest("req5", MEDIUM), // Evicts req3
1293 CreateResolverRequest("req6", HIGHEST), // Evicts req5. 1297 CreateResolverRequest("req6", HIGHEST), // Evicts req5.
1294 CreateResolverRequest("req7", MEDIUM), // Evicts req2. 1298 CreateResolverRequest("req7", MEDIUM), // Evicts req2.
1295 }; 1299 };
1296 1300
1297 TestOldCompletionCallback callback[arraysize(req)]; 1301 TestCompletionCallback callback[arraysize(req)];
1298 AddressList addrlist[arraysize(req)]; 1302 AddressList addrlist[arraysize(req)];
1299 HostResolver::RequestHandle handle[arraysize(req)]; 1303 HostResolver::RequestHandle handle[arraysize(req)];
1300 1304
1301 // Start all of the requests. 1305 // Start all of the requests.
1302 for (size_t i = 0; i < arraysize(req); ++i) { 1306 for (size_t i = 0; i < arraysize(req); ++i) {
1303 int rv = host_resolver->Resolve(req[i], &addrlist[i], 1307 int rv = host_resolver->Resolve(req[i], &addrlist[i],
1304 &callback[i], &handle[i], BoundNetLog()); 1308 callback[i].callback(), &handle[i],
1309 BoundNetLog());
1305 if (i == 4u) 1310 if (i == 4u)
1306 EXPECT_EQ(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE, rv); 1311 EXPECT_EQ(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE, rv);
1307 else 1312 else
1308 EXPECT_EQ(ERR_IO_PENDING, rv) << i; 1313 EXPECT_EQ(ERR_IO_PENDING, rv) << i;
1309 } 1314 }
1310 1315
1311 // Unblock the resolver thread so the requests can run. 1316 // Unblock the resolver thread so the requests can run.
1312 resolver_proc->Signal(); 1317 resolver_proc->Signal();
1313 1318
1314 // Requests 3, 5, 2 will have been evicted due to queue overflow. 1319 // Requests 3, 5, 2 will have been evicted due to queue overflow.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1354 // Note that at this point the CapturingHostResolverProc is blocked, so any 1359 // Note that at this point the CapturingHostResolverProc is blocked, so any
1355 // requests we make will not complete. 1360 // requests we make will not complete.
1356 1361
1357 HostResolver::RequestInfo req[] = { 1362 HostResolver::RequestInfo req[] = {
1358 CreateResolverRequestForAddressFamily("h1", MEDIUM, 1363 CreateResolverRequestForAddressFamily("h1", MEDIUM,
1359 ADDRESS_FAMILY_UNSPECIFIED), 1364 ADDRESS_FAMILY_UNSPECIFIED),
1360 CreateResolverRequestForAddressFamily("h1", MEDIUM, ADDRESS_FAMILY_IPV4), 1365 CreateResolverRequestForAddressFamily("h1", MEDIUM, ADDRESS_FAMILY_IPV4),
1361 CreateResolverRequestForAddressFamily("h1", MEDIUM, ADDRESS_FAMILY_IPV6), 1366 CreateResolverRequestForAddressFamily("h1", MEDIUM, ADDRESS_FAMILY_IPV6),
1362 }; 1367 };
1363 1368
1364 TestOldCompletionCallback callback[arraysize(req)]; 1369 TestCompletionCallback callback[arraysize(req)];
1365 AddressList addrlist[arraysize(req)]; 1370 AddressList addrlist[arraysize(req)];
1366 HostResolver::RequestHandle handle[arraysize(req)]; 1371 HostResolver::RequestHandle handle[arraysize(req)];
1367 1372
1368 // Start all of the requests. 1373 // Start all of the requests.
1369 for (size_t i = 0; i < arraysize(req); ++i) { 1374 for (size_t i = 0; i < arraysize(req); ++i) {
1370 int rv = host_resolver->Resolve(req[i], &addrlist[i], 1375 int rv = host_resolver->Resolve(req[i], &addrlist[i],
1371 &callback[i], &handle[i], BoundNetLog()); 1376 callback[i].callback(), &handle[i],
1377 BoundNetLog());
1372 EXPECT_EQ(ERR_IO_PENDING, rv) << i; 1378 EXPECT_EQ(ERR_IO_PENDING, rv) << i;
1373 } 1379 }
1374 1380
1375 // Unblock the resolver thread so the requests can run. 1381 // Unblock the resolver thread so the requests can run.
1376 resolver_proc->Signal(); 1382 resolver_proc->Signal();
1377 1383
1378 // Wait for all the requests to complete. 1384 // Wait for all the requests to complete.
1379 for (size_t i = 0u; i < arraysize(req); ++i) { 1385 for (size_t i = 0u; i < arraysize(req); ++i) {
1380 EXPECT_EQ(OK, callback[i].WaitForResult()); 1386 EXPECT_EQ(OK, callback[i].WaitForResult());
1381 } 1387 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1424 // Note that at this point the CapturingHostResolverProc is blocked, so any 1430 // Note that at this point the CapturingHostResolverProc is blocked, so any
1425 // requests we make will not complete. 1431 // requests we make will not complete.
1426 1432
1427 HostResolver::RequestInfo req[] = { 1433 HostResolver::RequestInfo req[] = {
1428 CreateResolverRequestForAddressFamily("h1", MEDIUM, ADDRESS_FAMILY_IPV6), 1434 CreateResolverRequestForAddressFamily("h1", MEDIUM, ADDRESS_FAMILY_IPV6),
1429 CreateResolverRequestForAddressFamily("h1", MEDIUM, 1435 CreateResolverRequestForAddressFamily("h1", MEDIUM,
1430 ADDRESS_FAMILY_UNSPECIFIED), 1436 ADDRESS_FAMILY_UNSPECIFIED),
1431 CreateResolverRequestForAddressFamily("h1", MEDIUM, ADDRESS_FAMILY_IPV4), 1437 CreateResolverRequestForAddressFamily("h1", MEDIUM, ADDRESS_FAMILY_IPV4),
1432 }; 1438 };
1433 1439
1434 TestOldCompletionCallback callback[arraysize(req)]; 1440 TestCompletionCallback callback[arraysize(req)];
1435 AddressList addrlist[arraysize(req)]; 1441 AddressList addrlist[arraysize(req)];
1436 HostResolver::RequestHandle handle[arraysize(req)]; 1442 HostResolver::RequestHandle handle[arraysize(req)];
1437 1443
1438 // Start all of the requests. 1444 // Start all of the requests.
1439 for (size_t i = 0; i < arraysize(req); ++i) { 1445 for (size_t i = 0; i < arraysize(req); ++i) {
1440 int rv = host_resolver->Resolve(req[i], &addrlist[i], 1446 int rv = host_resolver->Resolve(req[i], &addrlist[i],
1441 &callback[i], &handle[i], BoundNetLog()); 1447 callback[i].callback(), &handle[i],
1448 BoundNetLog());
1442 EXPECT_EQ(ERR_IO_PENDING, rv) << i; 1449 EXPECT_EQ(ERR_IO_PENDING, rv) << i;
1443 } 1450 }
1444 1451
1445 // Unblock the resolver thread so the requests can run. 1452 // Unblock the resolver thread so the requests can run.
1446 resolver_proc->Signal(); 1453 resolver_proc->Signal();
1447 1454
1448 // Wait for all the requests to complete. 1455 // Wait for all the requests to complete.
1449 for (size_t i = 0u; i < arraysize(req); ++i) { 1456 for (size_t i = 0u; i < arraysize(req); ++i) {
1450 EXPECT_EQ(OK, callback[i].WaitForResult()); 1457 EXPECT_EQ(OK, callback[i].WaitForResult());
1451 } 1458 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1486 scoped_ptr<HostResolver> host_resolver( 1493 scoped_ptr<HostResolver> host_resolver(
1487 CreateHostResolverImpl(resolver_proc)); 1494 CreateHostResolverImpl(resolver_proc));
1488 1495
1489 // First hit will miss the cache. 1496 // First hit will miss the cache.
1490 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum)); 1497 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum));
1491 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); 1498 CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
1492 int err = host_resolver->ResolveFromCache(info, &addrlist, log.bound()); 1499 int err = host_resolver->ResolveFromCache(info, &addrlist, log.bound());
1493 EXPECT_EQ(ERR_DNS_CACHE_MISS, err); 1500 EXPECT_EQ(ERR_DNS_CACHE_MISS, err);
1494 1501
1495 // This time, we fetch normally. 1502 // This time, we fetch normally.
1496 TestOldCompletionCallback callback; 1503 TestCompletionCallback callback;
1497 err = host_resolver->Resolve(info, &addrlist, &callback, NULL, log.bound()); 1504 err = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL,
1505 log.bound());
1498 EXPECT_EQ(ERR_IO_PENDING, err); 1506 EXPECT_EQ(ERR_IO_PENDING, err);
1499 err = callback.WaitForResult(); 1507 err = callback.WaitForResult();
1500 EXPECT_EQ(OK, err); 1508 EXPECT_EQ(OK, err);
1501 1509
1502 // Now we should be able to fetch from the cache. 1510 // Now we should be able to fetch from the cache.
1503 err = host_resolver->ResolveFromCache(info, &addrlist, log.bound()); 1511 err = host_resolver->ResolveFromCache(info, &addrlist, log.bound());
1504 EXPECT_EQ(OK, err); 1512 EXPECT_EQ(OK, err);
1505 1513
1506 const struct addrinfo* ainfo = addrlist.head(); 1514 const struct addrinfo* ainfo = addrlist.head();
1507 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); 1515 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next);
(...skipping 23 matching lines...) Expand all
1531 NULL)); 1539 NULL));
1532 1540
1533 // Specify smaller interval for unresponsive_delay_ for HostResolverImpl so 1541 // Specify smaller interval for unresponsive_delay_ for HostResolverImpl so
1534 // that unit test runs faster. For example, this test finishes in 1.5 secs 1542 // that unit test runs faster. For example, this test finishes in 1.5 secs
1535 // (500ms * 3). 1543 // (500ms * 3).
1536 TimeDelta kUnresponsiveTime = TimeDelta::FromMilliseconds(500); 1544 TimeDelta kUnresponsiveTime = TimeDelta::FromMilliseconds(500);
1537 host_resolver->set_unresponsive_delay(kUnresponsiveTime); 1545 host_resolver->set_unresponsive_delay(kUnresponsiveTime);
1538 1546
1539 // Resolve "host1". 1547 // Resolve "host1".
1540 HostResolver::RequestInfo info(HostPortPair("host1", 70)); 1548 HostResolver::RequestInfo info(HostPortPair("host1", 70));
1541 TestOldCompletionCallback callback; 1549 TestCompletionCallback callback;
1542 AddressList addrlist; 1550 AddressList addrlist;
1543 int rv = host_resolver->Resolve(info, &addrlist, &callback, NULL, 1551 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL,
1544 BoundNetLog()); 1552 BoundNetLog());
1545 EXPECT_EQ(ERR_IO_PENDING, rv); 1553 EXPECT_EQ(ERR_IO_PENDING, rv);
1546 1554
1547 // Resolve returns -4 to indicate that 3rd attempt has resolved the host. 1555 // Resolve returns -4 to indicate that 3rd attempt has resolved the host.
1548 EXPECT_EQ(-4, callback.WaitForResult()); 1556 EXPECT_EQ(-4, callback.WaitForResult());
1549 1557
1550 resolver_proc->WaitForAllAttemptsToFinish(TimeDelta::FromMilliseconds(60000)); 1558 resolver_proc->WaitForAllAttemptsToFinish(TimeDelta::FromMilliseconds(60000));
1551 MessageLoop::current()->RunAllPending(); 1559 MessageLoop::current()->RunAllPending();
1552 1560
1553 EXPECT_EQ(resolver_proc->total_attempts_resolved(), kTotalAttempts); 1561 EXPECT_EQ(resolver_proc->total_attempts_resolved(), kTotalAttempts);
1554 EXPECT_EQ(resolver_proc->resolved_attempt_number(), kAttemptNumberToResolve); 1562 EXPECT_EQ(resolver_proc->resolved_attempt_number(), kAttemptNumberToResolve);
1555 } 1563 }
1556 1564
1557 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. 1565 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags.
1558 1566
1559 } // namespace net 1567 } // namespace net
OLDNEW
« no previous file with comments | « net/base/host_resolver_impl.cc ('k') | net/base/mapped_host_resolver.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698