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

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

Issue 9369045: [net] HostResolverImpl + DnsTransaction + DnsConfigService = Asynchronous DNS ready for experiments. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Denitted. Created 8 years, 10 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 | « net/base/host_resolver_impl.cc ('k') | net/base/net_log_event_type_list.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 "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" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 HostResolverProc* resolver_proc) { 44 HostResolverProc* resolver_proc) {
45 return HostResolverImpl::ProcTaskParams(resolver_proc, 45 return HostResolverImpl::ProcTaskParams(resolver_proc,
46 kMaxRetryAttempts); 46 kMaxRetryAttempts);
47 } 47 }
48 48
49 HostResolverImpl* CreateHostResolverImpl(HostResolverProc* resolver_proc) { 49 HostResolverImpl* CreateHostResolverImpl(HostResolverProc* resolver_proc) {
50 return new HostResolverImpl( 50 return new HostResolverImpl(
51 HostCache::CreateDefaultCache(), 51 HostCache::CreateDefaultCache(),
52 DefaultLimits(), 52 DefaultLimits(),
53 DefaultParams(resolver_proc), 53 DefaultParams(resolver_proc),
54 scoped_ptr<DnsConfigService>(NULL),
54 NULL); 55 NULL);
55 } 56 }
56 57
57 // This HostResolverImpl will only allow 1 outstanding resolve at a time. 58 // This HostResolverImpl will only allow 1 outstanding resolve at a time.
58 HostResolverImpl* CreateSerialHostResolverImpl( 59 HostResolverImpl* CreateSerialHostResolverImpl(
59 HostResolverProc* resolver_proc) { 60 HostResolverProc* resolver_proc) {
60 HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc); 61 HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc);
61 params.max_retry_attempts = 0u; 62 params.max_retry_attempts = 0u;
62 63
63 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); 64 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
64 65
65 return new HostResolverImpl(HostCache::CreateDefaultCache(), 66 return new HostResolverImpl(HostCache::CreateDefaultCache(),
66 limits, 67 limits,
67 params, 68 params,
69 scoped_ptr<DnsConfigService>(NULL),
68 NULL); 70 NULL);
69 } 71 }
70 72
71 // Helper to create a HostResolver::RequestInfo. 73 // Helper to create a HostResolver::RequestInfo.
72 HostResolver::RequestInfo CreateResolverRequest( 74 HostResolver::RequestInfo CreateResolverRequest(
73 const std::string& hostname, 75 const std::string& hostname,
74 RequestPriority priority) { 76 RequestPriority priority) {
75 HostResolver::RequestInfo info(HostPortPair(hostname, 80)); 77 HostResolver::RequestInfo info(HostPortPair(hostname, 80));
76 info.set_priority(priority); 78 info.set_priority(priority);
77 return info; 79 return info;
(...skipping 29 matching lines...) Expand all
107 } 109 }
108 110
109 void Signal() { 111 void Signal() {
110 event_.Signal(); 112 event_.Signal();
111 } 113 }
112 114
113 virtual int Resolve(const std::string& hostname, 115 virtual int Resolve(const std::string& hostname,
114 AddressFamily address_family, 116 AddressFamily address_family,
115 HostResolverFlags host_resolver_flags, 117 HostResolverFlags host_resolver_flags,
116 AddressList* addrlist, 118 AddressList* addrlist,
117 int* os_error) { 119 int* os_error) OVERRIDE {
118 event_.Wait(); 120 event_.Wait();
119 { 121 {
120 base::AutoLock l(lock_); 122 base::AutoLock l(lock_);
121 capture_list_.push_back(CaptureEntry(hostname, address_family)); 123 capture_list_.push_back(CaptureEntry(hostname, address_family));
122 } 124 }
123 return ResolveUsingPrevious(hostname, address_family, 125 return ResolveUsingPrevious(hostname, address_family,
124 host_resolver_flags, addrlist, os_error); 126 host_resolver_flags, addrlist, os_error);
125 } 127 }
126 128
127 CaptureList GetCaptureList() const { 129 CaptureList GetCaptureList() const {
(...skipping 26 matching lines...) Expand all
154 // z = value of address_family 156 // z = value of address_family
155 // 157 //
156 class EchoingHostResolverProc : public HostResolverProc { 158 class EchoingHostResolverProc : public HostResolverProc {
157 public: 159 public:
158 EchoingHostResolverProc() : HostResolverProc(NULL) {} 160 EchoingHostResolverProc() : HostResolverProc(NULL) {}
159 161
160 virtual int Resolve(const std::string& hostname, 162 virtual int Resolve(const std::string& hostname,
161 AddressFamily address_family, 163 AddressFamily address_family,
162 HostResolverFlags host_resolver_flags, 164 HostResolverFlags host_resolver_flags,
163 AddressList* addrlist, 165 AddressList* addrlist,
164 int* os_error) { 166 int* os_error) OVERRIDE {
165 // Encode the request's hostname and address_family in the output address. 167 // Encode the request's hostname and address_family in the output address.
166 std::string ip_literal = base::StringPrintf("192.%d.%d.%d", 168 std::string ip_literal = base::StringPrintf("192.%d.%d.%d",
167 static_cast<int>(hostname.size()), 169 static_cast<int>(hostname.size()),
168 static_cast<int>(hostname[0]), 170 static_cast<int>(hostname[0]),
169 static_cast<int>(address_family)); 171 static_cast<int>(address_family));
170 172
171 return SystemHostResolverProc(ip_literal, 173 return SystemHostResolverProc(ip_literal,
172 ADDRESS_FAMILY_UNSPECIFIED, 174 ADDRESS_FAMILY_UNSPECIFIED,
173 host_resolver_flags, 175 host_resolver_flags,
174 addrlist, os_error); 176 addrlist, os_error);
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 int total_attempts_resolved() { return total_attempts_resolved_; } 222 int total_attempts_resolved() { return total_attempts_resolved_; }
221 223
222 // Returns the first attempt that that has resolved the host. 224 // Returns the first attempt that that has resolved the host.
223 int resolved_attempt_number() { return resolved_attempt_number_; } 225 int resolved_attempt_number() { return resolved_attempt_number_; }
224 226
225 // HostResolverProc methods. 227 // HostResolverProc methods.
226 virtual int Resolve(const std::string& host, 228 virtual int Resolve(const std::string& host,
227 AddressFamily address_family, 229 AddressFamily address_family,
228 HostResolverFlags host_resolver_flags, 230 HostResolverFlags host_resolver_flags,
229 AddressList* addrlist, 231 AddressList* addrlist,
230 int* os_error) { 232 int* os_error) OVERRIDE {
231 bool wait_for_right_attempt_to_complete = true; 233 bool wait_for_right_attempt_to_complete = true;
232 { 234 {
233 base::AutoLock auto_lock(lock_); 235 base::AutoLock auto_lock(lock_);
234 ++current_attempt_number_; 236 ++current_attempt_number_;
235 if (current_attempt_number_ == attempt_number_to_resolve_) { 237 if (current_attempt_number_ == attempt_number_to_resolve_) {
236 resolved_attempt_number_ = current_attempt_number_; 238 resolved_attempt_number_ = current_attempt_number_;
237 wait_for_right_attempt_to_complete = false; 239 wait_for_right_attempt_to_complete = false;
238 } 240 }
239 } 241 }
240 242
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 } 429 }
428 430
429 TEST_F(HostResolverImplTest, FailedAsynchronousLookup) { 431 TEST_F(HostResolverImplTest, FailedAsynchronousLookup) {
430 AddressList addrlist; 432 AddressList addrlist;
431 const int kPortnum = 80; 433 const int kPortnum = 80;
432 434
433 scoped_refptr<RuleBasedHostResolverProc> resolver_proc( 435 scoped_refptr<RuleBasedHostResolverProc> resolver_proc(
434 new RuleBasedHostResolverProc(NULL)); 436 new RuleBasedHostResolverProc(NULL));
435 resolver_proc->AddSimulatedFailure("just.testing"); 437 resolver_proc->AddSimulatedFailure("just.testing");
436 438
437 scoped_ptr<HostResolver> host_resolver( 439 scoped_ptr<HostResolver> host_resolver(CreateHostResolverImpl(resolver_proc));
438 CreateHostResolverImpl(resolver_proc));
439 440
440 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum)); 441 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum));
441 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); 442 CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
442 int err = host_resolver->Resolve(info, &addrlist, callback_, NULL, 443 int err = host_resolver->Resolve(info, &addrlist, callback_, NULL,
443 log.bound()); 444 log.bound());
444 EXPECT_EQ(ERR_IO_PENDING, err); 445 EXPECT_EQ(ERR_IO_PENDING, err);
445 446
446 CapturingNetLog::EntryList entries; 447 CapturingNetLog::EntryList entries;
447 log.GetEntries(&entries); 448 log.GetEntries(&entries);
448 449
(...skipping 14 matching lines...) Expand all
463 464
464 // Also test that the error is not cached! 465 // Also test that the error is not cached!
465 err = host_resolver->ResolveFromCache(info, &addrlist, log.bound()); 466 err = host_resolver->ResolveFromCache(info, &addrlist, log.bound());
466 EXPECT_EQ(ERR_DNS_CACHE_MISS, err); 467 EXPECT_EQ(ERR_DNS_CACHE_MISS, err);
467 } 468 }
468 469
469 // Using WaitingHostResolverProc you can simulate very long lookups. 470 // Using WaitingHostResolverProc you can simulate very long lookups.
470 class WaitingHostResolverProc : public HostResolverProc { 471 class WaitingHostResolverProc : public HostResolverProc {
471 public: 472 public:
472 explicit WaitingHostResolverProc(HostResolverProc* previous) 473 explicit WaitingHostResolverProc(HostResolverProc* previous)
473 : HostResolverProc(previous), 474 : HostResolverProc(previous),
474 is_waiting_(false, false), 475 is_waiting_(false, false),
475 is_signaled_(false, false) {} 476 is_signaled_(false, false) {}
477
478 // If |manual_reset| is true, once Signalled, it will let all Resolve calls
479 // proceed.
480 WaitingHostResolverProc(HostResolverProc* previous, bool manual_reset)
481 : HostResolverProc(previous),
482 is_waiting_(false, false),
483 is_signaled_(manual_reset, false) {}
476 484
477 // Waits until a call to |Resolve| is blocked. It is recommended to always 485 // Waits until a call to |Resolve| is blocked. It is recommended to always
478 // |Wait| before |Signal|, and required if issuing a series of two or more 486 // |Wait| before |Signal|, and required if issuing a series of two or more
479 // calls to |Signal|, because |WaitableEvent| does not count the number of 487 // calls to |Signal|, because |WaitableEvent| does not count the number of
480 // signals. 488 // signals.
481 void Wait() { 489 void Wait() {
482 is_waiting_.Wait(); 490 is_waiting_.Wait();
483 } 491 }
484 492
485 // Signals a waiting call to |Resolve|. 493 // Signals a waiting call to |Resolve|.
486 void Signal() { 494 void Signal() {
487 is_signaled_.Signal(); 495 is_signaled_.Signal();
488 } 496 }
489 497
490 // HostResolverProc methods: 498 // HostResolverProc methods:
491 virtual int Resolve(const std::string& host, 499 virtual int Resolve(const std::string& host,
492 AddressFamily address_family, 500 AddressFamily address_family,
493 HostResolverFlags host_resolver_flags, 501 HostResolverFlags host_resolver_flags,
494 AddressList* addrlist, 502 AddressList* addrlist,
495 int* os_error) { 503 int* os_error) OVERRIDE {
496 is_waiting_.Signal(); 504 is_waiting_.Signal();
497 is_signaled_.Wait(); 505 is_signaled_.Wait();
498 return ResolveUsingPrevious(host, address_family, host_resolver_flags, 506 return ResolveUsingPrevious(host, address_family, host_resolver_flags,
499 addrlist, os_error); 507 addrlist, os_error);
500 } 508 }
501 509
502 private: 510 private:
503 virtual ~WaitingHostResolverProc() {} 511 virtual ~WaitingHostResolverProc() {}
504
505 base::WaitableEvent is_waiting_; 512 base::WaitableEvent is_waiting_;
506 base::WaitableEvent is_signaled_; 513 base::WaitableEvent is_signaled_;
507 }; 514 };
508 515
509 TEST_F(HostResolverImplTest, AbortedAsynchronousLookup) { 516 TEST_F(HostResolverImplTest, AbortedAsynchronousLookup) {
510 scoped_refptr<WaitingHostResolverProc> resolver_proc( 517 scoped_refptr<WaitingHostResolverProc> resolver_proc(
511 new WaitingHostResolverProc(NULL)); 518 new WaitingHostResolverProc(NULL));
512 519
513 CapturingNetLog net_log(CapturingNetLog::kUnbounded); 520 CapturingNetLog net_log(CapturingNetLog::kUnbounded);
514 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); 521 CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
515 { 522 {
516 // This resolver will be destroyed while a lookup is running on WorkerPool. 523 // This resolver will be destroyed while a lookup is running on WorkerPool.
517 scoped_ptr<HostResolver> host_resolver( 524 scoped_ptr<HostResolver> host_resolver(
518 new HostResolverImpl(HostCache::CreateDefaultCache(), 525 new HostResolverImpl(HostCache::CreateDefaultCache(),
519 DefaultLimits(), 526 DefaultLimits(),
520 DefaultParams(resolver_proc), 527 DefaultParams(resolver_proc),
528 scoped_ptr<DnsConfigService>(NULL),
521 &net_log)); 529 &net_log));
522 AddressList addrlist; 530 AddressList addrlist;
523 const int kPortnum = 80; 531 const int kPortnum = 80;
524 532
525 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum)); 533 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum));
526 int err = host_resolver->Resolve(info, &addrlist, callback_, NULL, 534 int err = host_resolver->Resolve(info, &addrlist, callback_, NULL,
527 log.bound()); 535 log.bound());
528 EXPECT_EQ(ERR_IO_PENDING, err); 536 EXPECT_EQ(ERR_IO_PENDING, err);
529 537
530 resolver_proc->Wait(); 538 resolver_proc->Wait();
(...skipping 15 matching lines...) Expand all
546 554
547 int pos = ExpectLogContainsSomewhereAfter(net_log_entries, 0, 555 int pos = ExpectLogContainsSomewhereAfter(net_log_entries, 0,
548 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, 556 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST,
549 NetLog::PHASE_BEGIN); 557 NetLog::PHASE_BEGIN);
550 pos = ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, 558 pos = ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1,
551 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, 559 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
552 NetLog::PHASE_BEGIN); 560 NetLog::PHASE_BEGIN);
553 pos = ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, 561 pos = ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1,
554 NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK, 562 NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK,
555 NetLog::PHASE_BEGIN); 563 NetLog::PHASE_BEGIN);
556 // Both Request and ProcTask need to be cancelled. (The Job is "aborted".) 564
557 pos = ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, 565 // The Request needs to be cancelled. (The Job is "aborted".)
558 NetLog::TYPE_CANCELLED, 566 // Don't care about order in which Request, Job and ProcTask end.
559 NetLog::PHASE_NONE);
560 // Don't care about order in which Request, Job and ProcTask end, or when the
561 // other one is cancelled.
562 ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, 567 ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1,
563 NetLog::TYPE_CANCELLED, 568 NetLog::TYPE_CANCELLED,
564 NetLog::PHASE_NONE); 569 NetLog::PHASE_NONE);
565 ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, 570 ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1,
571 NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK,
572 NetLog::PHASE_END);
573 ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1,
566 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, 574 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST,
567 NetLog::PHASE_END); 575 NetLog::PHASE_END);
568 ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, 576 ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1,
569 NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK,
570 NetLog::PHASE_END);
571 ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1,
572 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, 577 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
573 NetLog::PHASE_END); 578 NetLog::PHASE_END);
574 579
575 EXPECT_FALSE(callback_called_); 580 EXPECT_FALSE(callback_called_);
576 } 581 }
577 582
578 TEST_F(HostResolverImplTest, NumericIPv4Address) { 583 TEST_F(HostResolverImplTest, NumericIPv4Address) {
579 // Stevens says dotted quads with AI_UNSPEC resolve to a single sockaddr_in. 584 // Stevens says dotted quads with AI_UNSPEC resolve to a single sockaddr_in.
580 585
581 scoped_refptr<RuleBasedHostResolverProc> resolver_proc( 586 scoped_refptr<RuleBasedHostResolverProc> resolver_proc(
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
672 677
673 // Helper class used by HostResolverImplTest.DeDupeRequests. It receives request 678 // Helper class used by HostResolverImplTest.DeDupeRequests. It receives request
674 // completion notifications for all the resolves, so it can tally up and 679 // completion notifications for all the resolves, so it can tally up and
675 // determine when we are done. 680 // determine when we are done.
676 class DeDupeRequestsVerifier : public ResolveRequest::Delegate { 681 class DeDupeRequestsVerifier : public ResolveRequest::Delegate {
677 public: 682 public:
678 explicit DeDupeRequestsVerifier(CapturingHostResolverProc* resolver_proc) 683 explicit DeDupeRequestsVerifier(CapturingHostResolverProc* resolver_proc)
679 : count_a_(0), count_b_(0), resolver_proc_(resolver_proc) {} 684 : count_a_(0), count_b_(0), resolver_proc_(resolver_proc) {}
680 685
681 // The test does 5 resolves (which can complete in any order). 686 // The test does 5 resolves (which can complete in any order).
682 virtual void OnCompleted(ResolveRequest* resolve) { 687 virtual void OnCompleted(ResolveRequest* resolve) OVERRIDE {
683 // Tally up how many requests we have seen. 688 // Tally up how many requests we have seen.
684 if (resolve->hostname() == "a") { 689 if (resolve->hostname() == "a") {
685 count_a_++; 690 count_a_++;
686 } else if (resolve->hostname() == "b") { 691 } else if (resolve->hostname() == "b") {
687 count_b_++; 692 count_b_++;
688 } else { 693 } else {
689 FAIL() << "Unexpected hostname: " << resolve->hostname(); 694 FAIL() << "Unexpected hostname: " << resolve->hostname();
690 } 695 }
691 696
692 // Check that the port was set correctly. 697 // Check that the port was set correctly.
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
745 // |verifier| will send quit message once all the requests have finished. 750 // |verifier| will send quit message once all the requests have finished.
746 MessageLoop::current()->Run(); 751 MessageLoop::current()->Run();
747 } 752 }
748 753
749 // Helper class used by HostResolverImplTest.CancelMultipleRequests. 754 // Helper class used by HostResolverImplTest.CancelMultipleRequests.
750 class CancelMultipleRequestsVerifier : public ResolveRequest::Delegate { 755 class CancelMultipleRequestsVerifier : public ResolveRequest::Delegate {
751 public: 756 public:
752 CancelMultipleRequestsVerifier() {} 757 CancelMultipleRequestsVerifier() {}
753 758
754 // The cancels kill all but one request. 759 // The cancels kill all but one request.
755 virtual void OnCompleted(ResolveRequest* resolve) { 760 virtual void OnCompleted(ResolveRequest* resolve) OVERRIDE {
756 EXPECT_EQ("a", resolve->hostname()); 761 EXPECT_EQ("a", resolve->hostname());
757 EXPECT_EQ(82, resolve->port()); 762 EXPECT_EQ(82, resolve->port());
758 763
759 // Check that the port was set correctly. 764 // Check that the port was set correctly.
760 EXPECT_EQ(resolve->port(), resolve->addrlist().GetPort()); 765 EXPECT_EQ(resolve->port(), resolve->addrlist().GetPort());
761 766
762 // End this test, we are done. 767 // End this test, we are done.
763 MessageLoop::current()->Quit(); 768 MessageLoop::current()->Quit();
764 } 769 }
765 770
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
803 MessageLoop::current()->Run(); 808 MessageLoop::current()->Run();
804 } 809 }
805 810
806 // Helper class used by HostResolverImplTest.CancelWithinCallback. 811 // Helper class used by HostResolverImplTest.CancelWithinCallback.
807 class CancelWithinCallbackVerifier : public ResolveRequest::Delegate { 812 class CancelWithinCallbackVerifier : public ResolveRequest::Delegate {
808 public: 813 public:
809 CancelWithinCallbackVerifier() 814 CancelWithinCallbackVerifier()
810 : req_to_cancel1_(NULL), req_to_cancel2_(NULL), num_completions_(0) { 815 : req_to_cancel1_(NULL), req_to_cancel2_(NULL), num_completions_(0) {
811 } 816 }
812 817
813 virtual void OnCompleted(ResolveRequest* resolve) { 818 virtual void OnCompleted(ResolveRequest* resolve) OVERRIDE {
814 num_completions_++; 819 num_completions_++;
815 820
816 // Port 80 is the first request that the callback will be invoked for. 821 // Port 80 is the first request that the callback will be invoked for.
817 // While we are executing within that callback, cancel the other requests 822 // While we are executing within that callback, cancel the other requests
818 // in the job and start another request. 823 // in the job and start another request.
819 if (80 == resolve->port()) { 824 if (80 == resolve->port()) {
820 EXPECT_EQ("a", resolve->hostname()); 825 EXPECT_EQ("a", resolve->hostname());
821 826
822 req_to_cancel1_->Cancel(); 827 req_to_cancel1_->Cancel();
823 req_to_cancel2_->Cancel(); 828 req_to_cancel2_->Cancel();
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
886 } 891 }
887 892
888 // Helper class used by HostResolverImplTest.DeleteWithinCallback. 893 // Helper class used by HostResolverImplTest.DeleteWithinCallback.
889 class DeleteWithinCallbackVerifier : public ResolveRequest::Delegate { 894 class DeleteWithinCallbackVerifier : public ResolveRequest::Delegate {
890 public: 895 public:
891 // |host_resolver| is the resolver that the the resolve requests were started 896 // |host_resolver| is the resolver that the the resolve requests were started
892 // with. 897 // with.
893 explicit DeleteWithinCallbackVerifier(HostResolver* host_resolver) 898 explicit DeleteWithinCallbackVerifier(HostResolver* host_resolver)
894 : host_resolver_(host_resolver) {} 899 : host_resolver_(host_resolver) {}
895 900
896 virtual void OnCompleted(ResolveRequest* resolve) { 901 virtual void OnCompleted(ResolveRequest* resolve) OVERRIDE {
897 EXPECT_EQ("a", resolve->hostname()); 902 EXPECT_EQ("a", resolve->hostname());
898 EXPECT_EQ(80, resolve->port()); 903 EXPECT_EQ(80, resolve->port());
899 904
900 // Deletes the host resolver. 905 // Deletes the host resolver.
901 host_resolver_.reset(); 906 host_resolver_.reset();
902 907
903 // Quit after returning from OnCompleted (to give it a chance at 908 // Quit after returning from OnCompleted (to give it a chance at
904 // incorrectly running the cancelled tasks). 909 // incorrectly running the cancelled tasks).
905 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 910 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
906 } 911 }
(...skipping 29 matching lines...) Expand all
936 941
937 // |verifier| will send quit message once all the requests have finished. 942 // |verifier| will send quit message once all the requests have finished.
938 MessageLoop::current()->Run(); 943 MessageLoop::current()->Run();
939 } 944 }
940 945
941 // Helper class used by HostResolverImplTest.StartWithinCallback. 946 // Helper class used by HostResolverImplTest.StartWithinCallback.
942 class StartWithinCallbackVerifier : public ResolveRequest::Delegate { 947 class StartWithinCallbackVerifier : public ResolveRequest::Delegate {
943 public: 948 public:
944 StartWithinCallbackVerifier() : num_requests_(0) {} 949 StartWithinCallbackVerifier() : num_requests_(0) {}
945 950
946 virtual void OnCompleted(ResolveRequest* resolve) { 951 virtual void OnCompleted(ResolveRequest* resolve) OVERRIDE {
947 EXPECT_EQ("a", resolve->hostname()); 952 EXPECT_EQ("a", resolve->hostname());
948 953
949 if (80 == resolve->port()) { 954 if (80 == resolve->port()) {
950 // On completing the first request, start another request for "a". 955 // On completing the first request, start another request for "a".
951 // Since caching is disabled, this will result in another async request. 956 // Since caching is disabled, this will result in another async request.
952 final_request_.reset(new ResolveRequest( 957 final_request_.reset(new ResolveRequest(
953 resolve->resolver(), "a", 70, this)); 958 resolve->resolver(), "a", 70, this));
954 } 959 }
955 if (++num_requests_ == 5) { 960 if (++num_requests_ == 5) {
956 // Test is done. 961 // Test is done.
957 MessageLoop::current()->Quit(); 962 MessageLoop::current()->Quit();
958 } 963 }
959 } 964 }
960 965
961 private: 966 private:
962 int num_requests_; 967 int num_requests_;
963 scoped_ptr<ResolveRequest> final_request_; 968 scoped_ptr<ResolveRequest> final_request_;
964 DISALLOW_COPY_AND_ASSIGN(StartWithinCallbackVerifier); 969 DISALLOW_COPY_AND_ASSIGN(StartWithinCallbackVerifier);
965 }; 970 };
966 971
967 TEST_F(HostResolverImplTest, StartWithinCallback) { 972 TEST_F(HostResolverImplTest, StartWithinCallback) {
968 // Use a capturing resolver_proc, since the verifier needs to know what calls 973 // Use a capturing resolver_proc, since the verifier needs to know what calls
969 // reached Resolver(). Also, the capturing resolver_proc is initially 974 // reached Resolver(). Also, the capturing resolver_proc is initially
970 // blocked. 975 // blocked.
971 scoped_refptr<CapturingHostResolverProc> resolver_proc( 976 scoped_refptr<CapturingHostResolverProc> resolver_proc(
972 new CapturingHostResolverProc(NULL)); 977 new CapturingHostResolverProc(NULL));
973 978
974 // Turn off caching for this host resolver. 979 // Turn off caching for this host resolver.
975 scoped_ptr<HostResolver> host_resolver(new HostResolverImpl( 980 scoped_ptr<HostResolver> host_resolver(new HostResolverImpl(
976 NULL, DefaultLimits(), DefaultParams(resolver_proc), NULL)); 981 NULL,
982 DefaultLimits(),
983 DefaultParams(resolver_proc),
984 scoped_ptr<DnsConfigService>(NULL),
985 NULL));
977 986
978 // The class will receive callbacks for when each resolve completes. It 987 // The class will receive callbacks for when each resolve completes. It
979 // checks that the right things happened. 988 // checks that the right things happened.
980 StartWithinCallbackVerifier verifier; 989 StartWithinCallbackVerifier verifier;
981 990
982 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is 991 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is
983 // blocked, these should all pile up until we signal it. 992 // blocked, these should all pile up until we signal it.
984 993
985 ResolveRequest req1(host_resolver.get(), "a", 80, &verifier); 994 ResolveRequest req1(host_resolver.get(), "a", 80, &verifier);
986 ResolveRequest req2(host_resolver.get(), "a", 81, &verifier); 995 ResolveRequest req2(host_resolver.get(), "a", 81, &verifier);
987 ResolveRequest req3(host_resolver.get(), "a", 82, &verifier); 996 ResolveRequest req3(host_resolver.get(), "a", 82, &verifier);
988 ResolveRequest req4(host_resolver.get(), "a", 83, &verifier); 997 ResolveRequest req4(host_resolver.get(), "a", 83, &verifier);
989 998
990 // Ready, Set, GO!!! 999 // Ready, Set, GO!!!
991 resolver_proc->Signal(); 1000 resolver_proc->Signal();
992 1001
993 // |verifier| will send quit message once all the requests have finished. 1002 // |verifier| will send quit message once all the requests have finished.
994 MessageLoop::current()->Run(); 1003 MessageLoop::current()->Run();
995 } 1004 }
996 1005
997 // Helper class used by HostResolverImplTest.BypassCache. 1006 // Helper class used by HostResolverImplTest.BypassCache.
998 class BypassCacheVerifier : public ResolveRequest::Delegate { 1007 class BypassCacheVerifier : public ResolveRequest::Delegate {
999 public: 1008 public:
1000 BypassCacheVerifier() {} 1009 BypassCacheVerifier() {}
1001 1010
1002 virtual void OnCompleted(ResolveRequest* resolve) { 1011 virtual void OnCompleted(ResolveRequest* resolve) OVERRIDE {
1003 EXPECT_EQ("a", resolve->hostname()); 1012 EXPECT_EQ("a", resolve->hostname());
1004 HostResolver* resolver = resolve->resolver(); 1013 HostResolver* resolver = resolve->resolver();
1005 1014
1006 if (80 == resolve->port()) { 1015 if (80 == resolve->port()) {
1007 // On completing the first request, start another request for "a". 1016 // On completing the first request, start another request for "a".
1008 // Since caching is enabled, this should complete synchronously. 1017 // Since caching is enabled, this should complete synchronously.
1009 1018
1010 // Note that |junk_callback| shouldn't be used since we are going to 1019 // Note that |junk_callback| shouldn't be used since we are going to
1011 // complete synchronously. 1020 // complete synchronously.
1012 TestCompletionCallback junk_callback; 1021 TestCompletionCallback junk_callback;
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
1134 EXPECT_EQ(ERR_ABORTED, callback.WaitForResult()); 1143 EXPECT_EQ(ERR_ABORTED, callback.WaitForResult());
1135 1144
1136 rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL, 1145 rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL,
1137 BoundNetLog()); 1146 BoundNetLog());
1138 EXPECT_EQ(ERR_IO_PENDING, rv); 1147 EXPECT_EQ(ERR_IO_PENDING, rv);
1139 resolver_proc->Wait(); 1148 resolver_proc->Wait();
1140 resolver_proc->Signal(); 1149 resolver_proc->Signal();
1141 EXPECT_EQ(OK, callback.WaitForResult()); 1150 EXPECT_EQ(OK, callback.WaitForResult());
1142 } 1151 }
1143 1152
1144 class ResolveWithinCallback { 1153 // Helper class used by AbortOnlyExistingRequestsOnIPAddressChange.
1154 class StartWithinAbortedCallbackVerifier : public ResolveRequest::Delegate {
1145 public: 1155 public:
1146 explicit ResolveWithinCallback(const HostResolver::RequestInfo& info) 1156 explicit StartWithinAbortedCallbackVerifier(const std::string& next_hostname)
1147 : info_(info) {} 1157 : next_hostname_(next_hostname) {}
1148 1158
1149 const CompletionCallback& callback() const { return callback_.callback(); } 1159 virtual void OnCompleted(ResolveRequest* resolve) OVERRIDE {
1150 1160 if (request_.get() == NULL) {
1151 int WaitForResult() { 1161 EXPECT_EQ(ERR_ABORTED, resolve->result());
1152 int result = callback_.WaitForResult(); 1162 // Start new request for a different hostname to ensure that the order
1153 // Ditch the WaitingHostResolverProc so that the subsequent request 1163 // of jobs in HostResolverImpl is not stable.
1154 // succeeds. 1164 request_.reset(new ResolveRequest(resolve->resolver(),
1155 host_resolver_.reset( 1165 next_hostname_,
1156 CreateHostResolverImpl(CreateCatchAllHostResolverProc())); 1166 resolve->port(),
1157 EXPECT_EQ(ERR_IO_PENDING, 1167 this));
1158 host_resolver_->Resolve(info_, &addrlist_, 1168 } else {
1159 nested_callback_.callback(), NULL, 1169 EXPECT_EQ(resolve, request_.get());
1160 BoundNetLog())); 1170 callback_.callback().Run(resolve->result());
1161 return result; 1171 }
1162 } 1172 }
1163 1173
1164 int WaitForNestedResult() { 1174 int WaitUntilDone() {
1165 return nested_callback_.WaitForResult(); 1175 return callback_.WaitForResult();
1166 } 1176 }
1167 1177
1168 private: 1178 private:
1169 const HostResolver::RequestInfo info_; 1179 std::string next_hostname_;
1170 AddressList addrlist_; 1180 scoped_ptr<ResolveRequest> request_;
1171 TestCompletionCallback callback_; 1181 TestCompletionCallback callback_;
1172 TestCompletionCallback nested_callback_; 1182 DISALLOW_COPY_AND_ASSIGN(StartWithinAbortedCallbackVerifier);
1173 scoped_ptr<HostResolver> host_resolver_;
1174 }; 1183 };
1175 1184
1176 TEST_F(HostResolverImplTest, OnlyAbortExistingRequestsOnIPAddressChange) { 1185 // Tests that a new Request made from the callback of a previously aborted one
1186 // will not be aborted.
1187 TEST_F(HostResolverImplTest, AbortOnlyExistingRequestsOnIPAddressChange) {
1188 // Setting |manual_reset| to true so that Signal unblocks all calls.
1177 scoped_refptr<WaitingHostResolverProc> resolver_proc( 1189 scoped_refptr<WaitingHostResolverProc> resolver_proc(
1178 new WaitingHostResolverProc(CreateCatchAllHostResolverProc())); 1190 new WaitingHostResolverProc(CreateCatchAllHostResolverProc(), true));
1179 scoped_ptr<HostResolver> host_resolver(CreateHostResolverImpl(resolver_proc)); 1191 scoped_ptr<HostResolver> host_resolver(CreateHostResolverImpl(resolver_proc));
1180 1192
1181 // Resolve "host1". 1193 StartWithinAbortedCallbackVerifier verifier1("zzz");
1182 HostResolver::RequestInfo info(HostPortPair("host1", 70)); 1194 StartWithinAbortedCallbackVerifier verifier2("aaa");
1183 ResolveWithinCallback callback(info); 1195 StartWithinAbortedCallbackVerifier verifier3("eee");
1184 AddressList addrlist;
1185 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL,
1186 BoundNetLog());
1187 EXPECT_EQ(ERR_IO_PENDING, rv);
1188 1196
1197 ResolveRequest req1(host_resolver.get(), "bbb", 40, &verifier1);
1198 ResolveRequest req2(host_resolver.get(), "eee", 80, &verifier2);
1199 ResolveRequest req3(host_resolver.get(), "ccc", 90, &verifier3);
1200 // The jobs start immediately.
1201 // Wait until at least one is blocked.
1189 resolver_proc->Wait(); 1202 resolver_proc->Wait();
1190 // Triggering an IP address change. 1203 // Trigger an IP address change.
1191 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); 1204 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
1192 MessageLoop::current()->RunAllPending(); // Notification happens async. 1205 // This should abort all running jobs.
1193 EXPECT_EQ(ERR_ABORTED, callback.WaitForResult()); 1206 MessageLoop::current()->RunAllPending();
1194 resolver_proc->Signal(); // release the thread from WorkerPool for cleanup 1207 EXPECT_EQ(ERR_ABORTED, req1.result());
1195 EXPECT_EQ(OK, callback.WaitForNestedResult()); 1208 EXPECT_EQ(ERR_ABORTED, req2.result());
1209 EXPECT_EQ(ERR_ABORTED, req3.result());
1210 // Unblock all calls to proc.
1211 resolver_proc->Signal();
1212 // Run until the re-started requests finish.
1213 EXPECT_EQ(OK, verifier1.WaitUntilDone());
1214 EXPECT_EQ(OK, verifier2.WaitUntilDone());
1215 EXPECT_EQ(OK, verifier3.WaitUntilDone());
1216 MessageLoop::current()->AssertIdle();
1196 } 1217 }
1197 1218
1198 // Tests that when the maximum threads is set to 1, requests are dequeued 1219 // Tests that when the maximum threads is set to 1, requests are dequeued
1199 // in order of priority. 1220 // in order of priority.
1200 TEST_F(HostResolverImplTest, HigherPriorityRequestsStartedFirst) { 1221 TEST_F(HostResolverImplTest, HigherPriorityRequestsStartedFirst) {
1201 scoped_refptr<CapturingHostResolverProc> resolver_proc( 1222 scoped_refptr<CapturingHostResolverProc> resolver_proc(
1202 new CapturingHostResolverProc(NULL)); 1223 new CapturingHostResolverProc(NULL));
1203 1224
1204 scoped_ptr<HostResolverImpl> host_resolver( 1225 scoped_ptr<HostResolverImpl> host_resolver(
1205 CreateSerialHostResolverImpl(resolver_proc)); 1226 CreateSerialHostResolverImpl(resolver_proc));
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after
1568 // the host. First and second attempt will be forced to sleep until they get 1589 // the host. First and second attempt will be forced to sleep until they get
1569 // word that a resolution has completed. The 3rd resolution attempt will try 1590 // word that a resolution has completed. The 3rd resolution attempt will try
1570 // to get done ASAP, and won't sleep.. 1591 // to get done ASAP, and won't sleep..
1571 int kAttemptNumberToResolve = 3; 1592 int kAttemptNumberToResolve = 3;
1572 int kTotalAttempts = 3; 1593 int kTotalAttempts = 3;
1573 1594
1574 scoped_refptr<LookupAttemptHostResolverProc> resolver_proc( 1595 scoped_refptr<LookupAttemptHostResolverProc> resolver_proc(
1575 new LookupAttemptHostResolverProc( 1596 new LookupAttemptHostResolverProc(
1576 NULL, kAttemptNumberToResolve, kTotalAttempts)); 1597 NULL, kAttemptNumberToResolve, kTotalAttempts));
1577 1598
1578 HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc); 1599 HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc.get());
1579 1600
1580 // Specify smaller interval for unresponsive_delay_ for HostResolverImpl so 1601 // Specify smaller interval for unresponsive_delay_ for HostResolverImpl so
1581 // that unit test runs faster. For example, this test finishes in 1.5 secs 1602 // that unit test runs faster. For example, this test finishes in 1.5 secs
1582 // (500ms * 3). 1603 // (500ms * 3).
1583 params.unresponsive_delay = TimeDelta::FromMilliseconds(500); 1604 params.unresponsive_delay = TimeDelta::FromMilliseconds(500);
1584 1605
1585 scoped_ptr<HostResolverImpl> host_resolver( 1606 scoped_ptr<HostResolverImpl> host_resolver(
1586 new HostResolverImpl(HostCache::CreateDefaultCache(), 1607 new HostResolverImpl(HostCache::CreateDefaultCache(),
1587 DefaultLimits(), 1608 DefaultLimits(),
1588 params, 1609 params,
1610 scoped_ptr<DnsConfigService>(NULL),
1589 NULL)); 1611 NULL));
1590 1612
1591 // Resolve "host1". 1613 // Resolve "host1".
1592 HostResolver::RequestInfo info(HostPortPair("host1", 70)); 1614 HostResolver::RequestInfo info(HostPortPair("host1", 70));
1593 TestCompletionCallback callback; 1615 TestCompletionCallback callback;
1594 AddressList addrlist; 1616 AddressList addrlist;
1595 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL, 1617 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL,
1596 BoundNetLog()); 1618 BoundNetLog());
1597 EXPECT_EQ(ERR_IO_PENDING, rv); 1619 EXPECT_EQ(ERR_IO_PENDING, rv);
1598 1620
1599 // Resolve returns -4 to indicate that 3rd attempt has resolved the host. 1621 // Resolve returns -4 to indicate that 3rd attempt has resolved the host.
1600 EXPECT_EQ(-4, callback.WaitForResult()); 1622 EXPECT_EQ(-4, callback.WaitForResult());
1601 1623
1602 resolver_proc->WaitForAllAttemptsToFinish(TimeDelta::FromMilliseconds(60000)); 1624 resolver_proc->WaitForAllAttemptsToFinish(TimeDelta::FromMilliseconds(60000));
1603 MessageLoop::current()->RunAllPending(); 1625 MessageLoop::current()->RunAllPending();
1604 1626
1605 EXPECT_EQ(resolver_proc->total_attempts_resolved(), kTotalAttempts); 1627 EXPECT_EQ(resolver_proc->total_attempts_resolved(), kTotalAttempts);
1606 EXPECT_EQ(resolver_proc->resolved_attempt_number(), kAttemptNumberToResolve); 1628 EXPECT_EQ(resolver_proc->resolved_attempt_number(), kAttemptNumberToResolve);
1607 } 1629 }
1608 1630
1609 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. 1631 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags.
1610 1632
1611 } // namespace net 1633 } // namespace net
OLDNEW
« no previous file with comments | « net/base/host_resolver_impl.cc ('k') | net/base/net_log_event_type_list.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698