| OLD | NEW |
| 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" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 16 #include "base/synchronization/condition_variable.h" | 16 #include "base/synchronization/condition_variable.h" |
| 17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 18 #include "base/time.h" | 18 #include "base/time.h" |
| 19 #include "net/base/address_list.h" | 19 #include "net/base/address_list.h" |
| 20 #include "net/base/completion_callback.h" | 20 #include "net/base/completion_callback.h" |
| 21 #include "net/base/host_cache.h" | 21 #include "net/base/host_cache.h" |
| 22 #include "net/base/mock_host_resolver.h" | 22 #include "net/base/mock_host_resolver.h" |
| 23 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
| 24 #include "net/base/net_log_unittest.h" | 24 #include "net/base/net_log_unittest.h" |
| 25 #include "net/base/net_util.h" | 25 #include "net/base/net_util.h" |
| 26 #include "net/base/sys_addrinfo.h" | 26 #include "net/base/sys_addrinfo.h" |
| 27 #include "net/base/test_completion_callback.h" | 27 #include "net/base/test_completion_callback.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 29 |
| 30 // TODO(eroman): | |
| 31 // - Test mixing async with sync (in particular how does sync update the | |
| 32 // cache while an async is already pending). | |
| 33 | |
| 34 namespace net { | 30 namespace net { |
| 35 | 31 |
| 36 using base::TimeDelta; | 32 using base::TimeDelta; |
| 37 using base::TimeTicks; | 33 using base::TimeTicks; |
| 38 | 34 |
| 39 static const size_t kMaxJobs = 10u; | 35 static const size_t kMaxJobs = 10u; |
| 40 static const size_t kMaxRetryAttempts = 4u; | 36 static const size_t kMaxRetryAttempts = 4u; |
| 41 | 37 |
| 38 PrioritizedDispatcher::Limits DefaultLimits() { |
| 39 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, kMaxJobs); |
| 40 return limits; |
| 41 } |
| 42 |
| 43 HostResolverImpl::ProcTaskParams DefaultParams( |
| 44 HostResolverProc* resolver_proc) { |
| 45 return HostResolverImpl::ProcTaskParams(resolver_proc, |
| 46 kMaxRetryAttempts); |
| 47 } |
| 48 |
| 42 HostResolverImpl* CreateHostResolverImpl(HostResolverProc* resolver_proc) { | 49 HostResolverImpl* CreateHostResolverImpl(HostResolverProc* resolver_proc) { |
| 43 return new HostResolverImpl(resolver_proc, HostCache::CreateDefaultCache(), | 50 return new HostResolverImpl( |
| 44 kMaxJobs, kMaxRetryAttempts, NULL); | 51 HostCache::CreateDefaultCache(), |
| 52 DefaultLimits(), |
| 53 DefaultParams(resolver_proc), |
| 54 NULL); |
| 55 } |
| 56 |
| 57 // This HostResolverImpl will only allow 1 outstanding resolve at a time. |
| 58 HostResolverImpl* CreateSerialHostResolverImpl( |
| 59 HostResolverProc* resolver_proc) { |
| 60 HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc); |
| 61 params.max_retry_attempts = 0u; |
| 62 |
| 63 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 64 |
| 65 return new HostResolverImpl(HostCache::CreateDefaultCache(), |
| 66 limits, |
| 67 params, |
| 68 NULL); |
| 45 } | 69 } |
| 46 | 70 |
| 47 // Helper to create a HostResolver::RequestInfo. | 71 // Helper to create a HostResolver::RequestInfo. |
| 48 HostResolver::RequestInfo CreateResolverRequest( | 72 HostResolver::RequestInfo CreateResolverRequest( |
| 49 const std::string& hostname, | 73 const std::string& hostname, |
| 50 RequestPriority priority) { | 74 RequestPriority priority) { |
| 51 HostResolver::RequestInfo info(HostPortPair(hostname, 80)); | 75 HostResolver::RequestInfo info(HostPortPair(hostname, 80)); |
| 52 info.set_priority(priority); | 76 info.set_priority(priority); |
| 53 return info; | 77 return info; |
| 54 } | 78 } |
| (...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 addrlist, os_error); | 499 addrlist, os_error); |
| 476 } | 500 } |
| 477 | 501 |
| 478 private: | 502 private: |
| 479 virtual ~WaitingHostResolverProc() {} | 503 virtual ~WaitingHostResolverProc() {} |
| 480 | 504 |
| 481 base::WaitableEvent is_waiting_; | 505 base::WaitableEvent is_waiting_; |
| 482 base::WaitableEvent is_signaled_; | 506 base::WaitableEvent is_signaled_; |
| 483 }; | 507 }; |
| 484 | 508 |
| 485 TEST_F(HostResolverImplTest, CanceledAsynchronousLookup) { | 509 TEST_F(HostResolverImplTest, AbortedAsynchronousLookup) { |
| 486 scoped_refptr<WaitingHostResolverProc> resolver_proc( | 510 scoped_refptr<WaitingHostResolverProc> resolver_proc( |
| 487 new WaitingHostResolverProc(NULL)); | 511 new WaitingHostResolverProc(NULL)); |
| 488 | 512 |
| 489 CapturingNetLog net_log(CapturingNetLog::kUnbounded); | 513 CapturingNetLog net_log(CapturingNetLog::kUnbounded); |
| 490 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); | 514 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 491 { | 515 { |
| 516 // This resolver will be destroyed while a lookup is running on WorkerPool. |
| 492 scoped_ptr<HostResolver> host_resolver( | 517 scoped_ptr<HostResolver> host_resolver( |
| 493 new HostResolverImpl(resolver_proc, | 518 new HostResolverImpl(HostCache::CreateDefaultCache(), |
| 494 HostCache::CreateDefaultCache(), | 519 DefaultLimits(), |
| 495 kMaxJobs, | 520 DefaultParams(resolver_proc), |
| 496 kMaxRetryAttempts, | |
| 497 &net_log)); | 521 &net_log)); |
| 498 AddressList addrlist; | 522 AddressList addrlist; |
| 499 const int kPortnum = 80; | 523 const int kPortnum = 80; |
| 500 | 524 |
| 501 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum)); | 525 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum)); |
| 502 int err = host_resolver->Resolve(info, &addrlist, callback_, NULL, | 526 int err = host_resolver->Resolve(info, &addrlist, callback_, NULL, |
| 503 log.bound()); | 527 log.bound()); |
| 504 EXPECT_EQ(ERR_IO_PENDING, err); | 528 EXPECT_EQ(ERR_IO_PENDING, err); |
| 505 | 529 |
| 506 resolver_proc->Wait(); | 530 resolver_proc->Wait(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 519 | 543 |
| 520 CapturingNetLog::EntryList net_log_entries; | 544 CapturingNetLog::EntryList net_log_entries; |
| 521 net_log.GetEntries(&net_log_entries); | 545 net_log.GetEntries(&net_log_entries); |
| 522 | 546 |
| 523 int pos = ExpectLogContainsSomewhereAfter(net_log_entries, 0, | 547 int pos = ExpectLogContainsSomewhereAfter(net_log_entries, 0, |
| 524 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, | 548 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, |
| 525 NetLog::PHASE_BEGIN); | 549 NetLog::PHASE_BEGIN); |
| 526 pos = ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, | 550 pos = ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, |
| 527 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, | 551 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, |
| 528 NetLog::PHASE_BEGIN); | 552 NetLog::PHASE_BEGIN); |
| 529 // Both Job and Request need to be cancelled. | 553 pos = ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, |
| 554 NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK, |
| 555 NetLog::PHASE_BEGIN); |
| 556 // Both Request and ProcTask need to be cancelled. (The Job is "aborted".) |
| 530 pos = ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, | 557 pos = ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, |
| 531 NetLog::TYPE_CANCELLED, | 558 NetLog::TYPE_CANCELLED, |
| 532 NetLog::PHASE_NONE); | 559 NetLog::PHASE_NONE); |
| 533 // Don't care about order in which they end, or when the other one is | 560 // Don't care about order in which Request, Job and ProcTask end, or when the |
| 534 // cancelled. | 561 // other one is cancelled. |
| 535 ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, | 562 ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, |
| 536 NetLog::TYPE_CANCELLED, | 563 NetLog::TYPE_CANCELLED, |
| 537 NetLog::PHASE_NONE); | 564 NetLog::PHASE_NONE); |
| 538 ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, | 565 ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, |
| 539 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, | 566 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, |
| 540 NetLog::PHASE_END); | 567 NetLog::PHASE_END); |
| 541 ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, | 568 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, |
| 542 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, | 572 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, |
| 543 NetLog::PHASE_END); | 573 NetLog::PHASE_END); |
| 544 | 574 |
| 545 EXPECT_FALSE(callback_called_); | 575 EXPECT_FALSE(callback_called_); |
| 546 } | 576 } |
| 547 | 577 |
| 548 TEST_F(HostResolverImplTest, NumericIPv4Address) { | 578 TEST_F(HostResolverImplTest, NumericIPv4Address) { |
| 549 // Stevens says dotted quads with AI_UNSPEC resolve to a single sockaddr_in. | 579 // Stevens says dotted quads with AI_UNSPEC resolve to a single sockaddr_in. |
| 550 | 580 |
| 551 scoped_refptr<RuleBasedHostResolverProc> resolver_proc( | 581 scoped_refptr<RuleBasedHostResolverProc> resolver_proc( |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 935 }; | 965 }; |
| 936 | 966 |
| 937 TEST_F(HostResolverImplTest, StartWithinCallback) { | 967 TEST_F(HostResolverImplTest, StartWithinCallback) { |
| 938 // Use a capturing resolver_proc, since the verifier needs to know what calls | 968 // Use a capturing resolver_proc, since the verifier needs to know what calls |
| 939 // reached Resolver(). Also, the capturing resolver_proc is initially | 969 // reached Resolver(). Also, the capturing resolver_proc is initially |
| 940 // blocked. | 970 // blocked. |
| 941 scoped_refptr<CapturingHostResolverProc> resolver_proc( | 971 scoped_refptr<CapturingHostResolverProc> resolver_proc( |
| 942 new CapturingHostResolverProc(NULL)); | 972 new CapturingHostResolverProc(NULL)); |
| 943 | 973 |
| 944 // Turn off caching for this host resolver. | 974 // Turn off caching for this host resolver. |
| 945 scoped_ptr<HostResolver> host_resolver( | 975 scoped_ptr<HostResolver> host_resolver(new HostResolverImpl( |
| 946 new HostResolverImpl(resolver_proc, NULL, kMaxJobs, kMaxRetryAttempts, | 976 NULL, DefaultLimits(), DefaultParams(resolver_proc), NULL)); |
| 947 NULL)); | |
| 948 | 977 |
| 949 // The class will receive callbacks for when each resolve completes. It | 978 // The class will receive callbacks for when each resolve completes. It |
| 950 // checks that the right things happened. | 979 // checks that the right things happened. |
| 951 StartWithinCallbackVerifier verifier; | 980 StartWithinCallbackVerifier verifier; |
| 952 | 981 |
| 953 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is | 982 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is |
| 954 // blocked, these should all pile up until we signal it. | 983 // blocked, these should all pile up until we signal it. |
| 955 | 984 |
| 956 ResolveRequest req1(host_resolver.get(), "a", 80, &verifier); | 985 ResolveRequest req1(host_resolver.get(), "a", 80, &verifier); |
| 957 ResolveRequest req2(host_resolver.get(), "a", 81, &verifier); | 986 ResolveRequest req2(host_resolver.get(), "a", 81, &verifier); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1017 // Start a request. | 1046 // Start a request. |
| 1018 ResolveRequest req1(host_resolver.get(), "a", 80, &verifier); | 1047 ResolveRequest req1(host_resolver.get(), "a", 80, &verifier); |
| 1019 | 1048 |
| 1020 // |verifier| will send quit message once all the requests have finished. | 1049 // |verifier| will send quit message once all the requests have finished. |
| 1021 MessageLoop::current()->Run(); | 1050 MessageLoop::current()->Run(); |
| 1022 } | 1051 } |
| 1023 | 1052 |
| 1024 // Test that IP address changes flush the cache. | 1053 // Test that IP address changes flush the cache. |
| 1025 TEST_F(HostResolverImplTest, FlushCacheOnIPAddressChange) { | 1054 TEST_F(HostResolverImplTest, FlushCacheOnIPAddressChange) { |
| 1026 scoped_ptr<HostResolver> host_resolver( | 1055 scoped_ptr<HostResolver> host_resolver( |
| 1027 new HostResolverImpl(NULL, HostCache::CreateDefaultCache(), kMaxJobs, | 1056 CreateHostResolverImpl(NULL)); |
| 1028 kMaxRetryAttempts, NULL)); | |
| 1029 | 1057 |
| 1030 AddressList addrlist; | 1058 AddressList addrlist; |
| 1031 | 1059 |
| 1032 // Resolve "host1". | 1060 // Resolve "host1". Assume that ScopedDefaultHostResolverProc resolves all. |
| 1033 HostResolver::RequestInfo info1(HostPortPair("host1", 70)); | 1061 HostResolver::RequestInfo info1(HostPortPair("host1", 70)); |
| 1034 TestCompletionCallback callback; | 1062 TestCompletionCallback callback; |
| 1035 int rv = host_resolver->Resolve(info1, &addrlist, callback.callback(), NULL, | 1063 int rv = host_resolver->Resolve(info1, &addrlist, callback.callback(), NULL, |
| 1036 BoundNetLog()); | 1064 BoundNetLog()); |
| 1037 EXPECT_EQ(ERR_IO_PENDING, rv); | 1065 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1038 EXPECT_EQ(OK, callback.WaitForResult()); | 1066 EXPECT_EQ(OK, callback.WaitForResult()); |
| 1039 | 1067 |
| 1040 // Resolve "host1" again -- this time it will be served from cache, but it | 1068 // Resolve "host1" again -- this time it will be served from cache, but it |
| 1041 // should still notify of completion. | 1069 // should still notify of completion. |
| 1042 rv = host_resolver->Resolve(info1, &addrlist, callback.callback(), NULL, | 1070 rv = host_resolver->Resolve(info1, &addrlist, callback.callback(), NULL, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1076 resolver_proc->Signal(); | 1104 resolver_proc->Signal(); |
| 1077 | 1105 |
| 1078 EXPECT_EQ(ERR_ABORTED, callback.WaitForResult()); | 1106 EXPECT_EQ(ERR_ABORTED, callback.WaitForResult()); |
| 1079 EXPECT_EQ(0u, host_resolver->GetHostCache()->size()); | 1107 EXPECT_EQ(0u, host_resolver->GetHostCache()->size()); |
| 1080 } | 1108 } |
| 1081 | 1109 |
| 1082 // Obey pool constraints after IP address has changed. | 1110 // Obey pool constraints after IP address has changed. |
| 1083 TEST_F(HostResolverImplTest, ObeyPoolConstraintsAfterIPAddressChange) { | 1111 TEST_F(HostResolverImplTest, ObeyPoolConstraintsAfterIPAddressChange) { |
| 1084 scoped_refptr<WaitingHostResolverProc> resolver_proc( | 1112 scoped_refptr<WaitingHostResolverProc> resolver_proc( |
| 1085 new WaitingHostResolverProc(CreateCatchAllHostResolverProc())); | 1113 new WaitingHostResolverProc(CreateCatchAllHostResolverProc())); |
| 1114 |
| 1086 scoped_ptr<HostResolverImpl> host_resolver( | 1115 scoped_ptr<HostResolverImpl> host_resolver( |
| 1087 new HostResolverImpl(resolver_proc, HostCache::CreateDefaultCache(), | 1116 CreateSerialHostResolverImpl(resolver_proc)); |
| 1088 kMaxJobs, kMaxRetryAttempts, NULL)); | |
| 1089 | |
| 1090 const size_t kMaxOutstandingJobs = 1u; | |
| 1091 const size_t kMaxPendingRequests = 1000000u; // not relevant. | |
| 1092 host_resolver->SetPoolConstraints(HostResolverImpl::POOL_NORMAL, | |
| 1093 kMaxOutstandingJobs, | |
| 1094 kMaxPendingRequests); | |
| 1095 | 1117 |
| 1096 // Resolve "host1". | 1118 // Resolve "host1". |
| 1097 HostResolver::RequestInfo info(HostPortPair("host1", 70)); | 1119 HostResolver::RequestInfo info(HostPortPair("host1", 70)); |
| 1098 TestCompletionCallback callback; | 1120 TestCompletionCallback callback; |
| 1099 AddressList addrlist; | 1121 AddressList addrlist; |
| 1100 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL, | 1122 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL, |
| 1101 BoundNetLog()); | 1123 BoundNetLog()); |
| 1102 EXPECT_EQ(ERR_IO_PENDING, rv); | 1124 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1103 | 1125 |
| 1104 // Must wait before signal to ensure that the two signals don't get merged | 1126 // Must wait before signal to ensure that the two signals don't get merged |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1172 resolver_proc->Signal(); // release the thread from WorkerPool for cleanup | 1194 resolver_proc->Signal(); // release the thread from WorkerPool for cleanup |
| 1173 EXPECT_EQ(OK, callback.WaitForNestedResult()); | 1195 EXPECT_EQ(OK, callback.WaitForNestedResult()); |
| 1174 } | 1196 } |
| 1175 | 1197 |
| 1176 // Tests that when the maximum threads is set to 1, requests are dequeued | 1198 // Tests that when the maximum threads is set to 1, requests are dequeued |
| 1177 // in order of priority. | 1199 // in order of priority. |
| 1178 TEST_F(HostResolverImplTest, HigherPriorityRequestsStartedFirst) { | 1200 TEST_F(HostResolverImplTest, HigherPriorityRequestsStartedFirst) { |
| 1179 scoped_refptr<CapturingHostResolverProc> resolver_proc( | 1201 scoped_refptr<CapturingHostResolverProc> resolver_proc( |
| 1180 new CapturingHostResolverProc(NULL)); | 1202 new CapturingHostResolverProc(NULL)); |
| 1181 | 1203 |
| 1182 // This HostResolverImpl will only allow 1 outstanding resolve at a time. | 1204 scoped_ptr<HostResolverImpl> host_resolver( |
| 1183 size_t kMaxJobs = 1u; | 1205 CreateSerialHostResolverImpl(resolver_proc)); |
| 1184 const size_t kRetryAttempts = 0u; | |
| 1185 scoped_ptr<HostResolver> host_resolver( | |
| 1186 new HostResolverImpl(resolver_proc, HostCache::CreateDefaultCache(), | |
| 1187 kMaxJobs, kRetryAttempts, NULL)); | |
| 1188 | 1206 |
| 1189 // Note that at this point the CapturingHostResolverProc is blocked, so any | 1207 // Note that at this point the CapturingHostResolverProc is blocked, so any |
| 1190 // requests we make will not complete. | 1208 // requests we make will not complete. |
| 1191 | 1209 |
| 1192 HostResolver::RequestInfo req[] = { | 1210 HostResolver::RequestInfo req[] = { |
| 1193 CreateResolverRequest("req0", LOW), | 1211 CreateResolverRequest("req0", LOW), |
| 1194 CreateResolverRequest("req1", MEDIUM), | 1212 CreateResolverRequest("req1", MEDIUM), |
| 1195 CreateResolverRequest("req2", MEDIUM), | 1213 CreateResolverRequest("req2", MEDIUM), |
| 1196 CreateResolverRequest("req3", LOW), | 1214 CreateResolverRequest("req3", LOW), |
| 1197 CreateResolverRequest("req4", HIGHEST), | 1215 CreateResolverRequest("req4", HIGHEST), |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1234 EXPECT_EQ("req2", capture_list[4].hostname); | 1252 EXPECT_EQ("req2", capture_list[4].hostname); |
| 1235 EXPECT_EQ("req3", capture_list[5].hostname); | 1253 EXPECT_EQ("req3", capture_list[5].hostname); |
| 1236 EXPECT_EQ("req6", capture_list[6].hostname); | 1254 EXPECT_EQ("req6", capture_list[6].hostname); |
| 1237 } | 1255 } |
| 1238 | 1256 |
| 1239 // Try cancelling a request which has not been attached to a job yet. | 1257 // Try cancelling a request which has not been attached to a job yet. |
| 1240 TEST_F(HostResolverImplTest, CancelPendingRequest) { | 1258 TEST_F(HostResolverImplTest, CancelPendingRequest) { |
| 1241 scoped_refptr<CapturingHostResolverProc> resolver_proc( | 1259 scoped_refptr<CapturingHostResolverProc> resolver_proc( |
| 1242 new CapturingHostResolverProc(NULL)); | 1260 new CapturingHostResolverProc(NULL)); |
| 1243 | 1261 |
| 1244 // This HostResolverImpl will only allow 1 outstanding resolve at a time. | 1262 scoped_ptr<HostResolverImpl> host_resolver( |
| 1245 const size_t kMaxJobs = 1u; | 1263 CreateSerialHostResolverImpl(resolver_proc)); |
| 1246 const size_t kRetryAttempts = 0u; | |
| 1247 scoped_ptr<HostResolver> host_resolver( | |
| 1248 new HostResolverImpl(resolver_proc, HostCache::CreateDefaultCache(), | |
| 1249 kMaxJobs, kRetryAttempts, NULL)); | |
| 1250 | 1264 |
| 1251 // Note that at this point the CapturingHostResolverProc is blocked, so any | 1265 // Note that at this point the CapturingHostResolverProc is blocked, so any |
| 1252 // requests we make will not complete. | 1266 // requests we make will not complete. |
| 1253 | 1267 |
| 1254 HostResolver::RequestInfo req[] = { | 1268 HostResolver::RequestInfo req[] = { |
| 1255 CreateResolverRequest("req0", LOWEST), | 1269 CreateResolverRequest("req0", LOWEST), |
| 1256 CreateResolverRequest("req1", HIGHEST), // Will cancel. | 1270 CreateResolverRequest("req1", HIGHEST), // Will cancel. |
| 1257 CreateResolverRequest("req2", MEDIUM), | 1271 CreateResolverRequest("req2", MEDIUM), |
| 1258 CreateResolverRequest("req3", LOW), | 1272 CreateResolverRequest("req3", LOW), |
| 1259 CreateResolverRequest("req4", HIGHEST), // Will cancel. | 1273 CreateResolverRequest("req4", HIGHEST), // Will cancel. |
| 1260 CreateResolverRequest("req5", LOWEST), // Will cancel. | 1274 CreateResolverRequest("req5", LOWEST), // Will cancel. |
| 1261 CreateResolverRequest("req6", MEDIUM), | 1275 CreateResolverRequest("req6", MEDIUM), |
| 1262 }; | 1276 }; |
| 1263 | 1277 |
| 1264 TestCompletionCallback callback[arraysize(req)]; | 1278 TestCompletionCallback callback[arraysize(req)]; |
| 1265 AddressList addrlist[arraysize(req)]; | 1279 AddressList addrlist[arraysize(req)]; |
| 1266 HostResolver::RequestHandle handle[arraysize(req)]; | 1280 HostResolver::RequestHandle handle[arraysize(req)]; |
| 1267 | 1281 |
| 1268 // Start all of the requests. | 1282 // Start all of the requests. |
| 1269 for (size_t i = 0; i < arraysize(req); ++i) { | 1283 for (size_t i = 0; i < arraysize(req); ++i) { |
| 1270 int rv = host_resolver->Resolve(req[i], &addrlist[i], | 1284 int rv = host_resolver->Resolve(req[i], &addrlist[i], |
| (...skipping 28 matching lines...) Expand all Loading... |
| 1299 EXPECT_EQ("req2", capture_list[1].hostname); | 1313 EXPECT_EQ("req2", capture_list[1].hostname); |
| 1300 EXPECT_EQ("req6", capture_list[2].hostname); | 1314 EXPECT_EQ("req6", capture_list[2].hostname); |
| 1301 EXPECT_EQ("req3", capture_list[3].hostname); | 1315 EXPECT_EQ("req3", capture_list[3].hostname); |
| 1302 } | 1316 } |
| 1303 | 1317 |
| 1304 // Test that when too many requests are enqueued, old ones start to be aborted. | 1318 // Test that when too many requests are enqueued, old ones start to be aborted. |
| 1305 TEST_F(HostResolverImplTest, QueueOverflow) { | 1319 TEST_F(HostResolverImplTest, QueueOverflow) { |
| 1306 scoped_refptr<CapturingHostResolverProc> resolver_proc( | 1320 scoped_refptr<CapturingHostResolverProc> resolver_proc( |
| 1307 new CapturingHostResolverProc(NULL)); | 1321 new CapturingHostResolverProc(NULL)); |
| 1308 | 1322 |
| 1309 // This HostResolverImpl will only allow 1 outstanding resolve at a time. | 1323 scoped_ptr<HostResolverImpl> host_resolver( |
| 1310 const size_t kMaxOutstandingJobs = 1u; | 1324 CreateSerialHostResolverImpl(resolver_proc)); |
| 1311 const size_t kRetryAttempts = 0u; | |
| 1312 scoped_ptr<HostResolverImpl> host_resolver(new HostResolverImpl( | |
| 1313 resolver_proc, HostCache::CreateDefaultCache(), kMaxOutstandingJobs, | |
| 1314 kRetryAttempts, NULL)); | |
| 1315 | 1325 |
| 1316 // Only allow up to 3 requests to be enqueued at a time. | 1326 // Allow only 3 queued jobs. |
| 1317 const size_t kMaxPendingRequests = 3u; | 1327 const size_t kMaxPendingJobs = 3u; |
| 1318 host_resolver->SetPoolConstraints(HostResolverImpl::POOL_NORMAL, | 1328 host_resolver->SetMaxQueuedJobs(kMaxPendingJobs); |
| 1319 kMaxOutstandingJobs, | |
| 1320 kMaxPendingRequests); | |
| 1321 | 1329 |
| 1322 // Note that at this point the CapturingHostResolverProc is blocked, so any | 1330 // Note that at this point the CapturingHostResolverProc is blocked, so any |
| 1323 // requests we make will not complete. | 1331 // requests we make will not complete. |
| 1324 | 1332 |
| 1325 HostResolver::RequestInfo req[] = { | 1333 HostResolver::RequestInfo req[] = { |
| 1326 CreateResolverRequest("req0", LOWEST), | 1334 CreateResolverRequest("req0", LOWEST), |
| 1327 CreateResolverRequest("req1", HIGHEST), | 1335 CreateResolverRequest("req1", HIGHEST), |
| 1328 CreateResolverRequest("req2", MEDIUM), | 1336 CreateResolverRequest("req2", MEDIUM), |
| 1329 CreateResolverRequest("req3", MEDIUM), | 1337 CreateResolverRequest("req3", MEDIUM), |
| 1330 | 1338 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1381 EXPECT_EQ("req7", capture_list[3].hostname); | 1389 EXPECT_EQ("req7", capture_list[3].hostname); |
| 1382 } | 1390 } |
| 1383 | 1391 |
| 1384 // Tests that after changing the default AddressFamily to IPV4, requests | 1392 // Tests that after changing the default AddressFamily to IPV4, requests |
| 1385 // with UNSPECIFIED address family map to IPV4. | 1393 // with UNSPECIFIED address family map to IPV4. |
| 1386 TEST_F(HostResolverImplTest, SetDefaultAddressFamily_IPv4) { | 1394 TEST_F(HostResolverImplTest, SetDefaultAddressFamily_IPv4) { |
| 1387 scoped_refptr<CapturingHostResolverProc> resolver_proc( | 1395 scoped_refptr<CapturingHostResolverProc> resolver_proc( |
| 1388 new CapturingHostResolverProc(new EchoingHostResolverProc)); | 1396 new CapturingHostResolverProc(new EchoingHostResolverProc)); |
| 1389 | 1397 |
| 1390 // This HostResolverImpl will only allow 1 outstanding resolve at a time. | 1398 // This HostResolverImpl will only allow 1 outstanding resolve at a time. |
| 1391 const size_t kMaxOutstandingJobs = 1u; | 1399 scoped_ptr<HostResolverImpl> host_resolver( |
| 1392 const size_t kRetryAttempts = 0u; | 1400 CreateSerialHostResolverImpl(resolver_proc)); |
| 1393 scoped_ptr<HostResolverImpl> host_resolver(new HostResolverImpl( | |
| 1394 resolver_proc, HostCache::CreateDefaultCache(), kMaxOutstandingJobs, | |
| 1395 kRetryAttempts, NULL)); | |
| 1396 | 1401 |
| 1397 host_resolver->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); | 1402 host_resolver->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); |
| 1398 | 1403 |
| 1399 // Note that at this point the CapturingHostResolverProc is blocked, so any | 1404 // Note that at this point the CapturingHostResolverProc is blocked, so any |
| 1400 // requests we make will not complete. | 1405 // requests we make will not complete. |
| 1401 | 1406 |
| 1402 HostResolver::RequestInfo req[] = { | 1407 HostResolver::RequestInfo req[] = { |
| 1403 CreateResolverRequestForAddressFamily("h1", MEDIUM, | 1408 CreateResolverRequestForAddressFamily("h1", MEDIUM, |
| 1404 ADDRESS_FAMILY_UNSPECIFIED), | 1409 ADDRESS_FAMILY_UNSPECIFIED), |
| 1405 CreateResolverRequestForAddressFamily("h1", MEDIUM, ADDRESS_FAMILY_IPV4), | 1410 CreateResolverRequestForAddressFamily("h1", MEDIUM, ADDRESS_FAMILY_IPV4), |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1451 EXPECT_EQ("192.2.104.2", NetAddressToString(addrlist[2].head())); | 1456 EXPECT_EQ("192.2.104.2", NetAddressToString(addrlist[2].head())); |
| 1452 } | 1457 } |
| 1453 | 1458 |
| 1454 // This is the exact same test as SetDefaultAddressFamily_IPv4, except the order | 1459 // This is the exact same test as SetDefaultAddressFamily_IPv4, except the order |
| 1455 // of requests 0 and 1 is flipped, and the default is set to IPv6 in place of | 1460 // of requests 0 and 1 is flipped, and the default is set to IPv6 in place of |
| 1456 // IPv4. | 1461 // IPv4. |
| 1457 TEST_F(HostResolverImplTest, SetDefaultAddressFamily_IPv6) { | 1462 TEST_F(HostResolverImplTest, SetDefaultAddressFamily_IPv6) { |
| 1458 scoped_refptr<CapturingHostResolverProc> resolver_proc( | 1463 scoped_refptr<CapturingHostResolverProc> resolver_proc( |
| 1459 new CapturingHostResolverProc(new EchoingHostResolverProc)); | 1464 new CapturingHostResolverProc(new EchoingHostResolverProc)); |
| 1460 | 1465 |
| 1461 // This HostResolverImpl will only allow 1 outstanding resolve at a time. | 1466 scoped_ptr<HostResolverImpl> host_resolver( |
| 1462 const size_t kMaxOutstandingJobs = 1u; | 1467 CreateSerialHostResolverImpl(resolver_proc)); |
| 1463 const size_t kRetryAttempts = 0u; | |
| 1464 scoped_ptr<HostResolverImpl> host_resolver(new HostResolverImpl( | |
| 1465 resolver_proc, HostCache::CreateDefaultCache(), kMaxOutstandingJobs, | |
| 1466 kRetryAttempts, NULL)); | |
| 1467 | 1468 |
| 1468 host_resolver->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV6); | 1469 host_resolver->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV6); |
| 1469 | 1470 |
| 1470 // Note that at this point the CapturingHostResolverProc is blocked, so any | 1471 // Note that at this point the CapturingHostResolverProc is blocked, so any |
| 1471 // requests we make will not complete. | 1472 // requests we make will not complete. |
| 1472 | 1473 |
| 1473 HostResolver::RequestInfo req[] = { | 1474 HostResolver::RequestInfo req[] = { |
| 1474 CreateResolverRequestForAddressFamily("h1", MEDIUM, ADDRESS_FAMILY_IPV6), | 1475 CreateResolverRequestForAddressFamily("h1", MEDIUM, ADDRESS_FAMILY_IPV6), |
| 1475 CreateResolverRequestForAddressFamily("h1", MEDIUM, | 1476 CreateResolverRequestForAddressFamily("h1", MEDIUM, |
| 1476 ADDRESS_FAMILY_UNSPECIFIED), | 1477 ADDRESS_FAMILY_UNSPECIFIED), |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1566 // Total number of attempts would be 3 and we want the 3rd attempt to resolve | 1567 // Total number of attempts would be 3 and we want the 3rd attempt to resolve |
| 1567 // the host. First and second attempt will be forced to sleep until they get | 1568 // the host. First and second attempt will be forced to sleep until they get |
| 1568 // word that a resolution has completed. The 3rd resolution attempt will try | 1569 // word that a resolution has completed. The 3rd resolution attempt will try |
| 1569 // to get done ASAP, and won't sleep.. | 1570 // to get done ASAP, and won't sleep.. |
| 1570 int kAttemptNumberToResolve = 3; | 1571 int kAttemptNumberToResolve = 3; |
| 1571 int kTotalAttempts = 3; | 1572 int kTotalAttempts = 3; |
| 1572 | 1573 |
| 1573 scoped_refptr<LookupAttemptHostResolverProc> resolver_proc( | 1574 scoped_refptr<LookupAttemptHostResolverProc> resolver_proc( |
| 1574 new LookupAttemptHostResolverProc( | 1575 new LookupAttemptHostResolverProc( |
| 1575 NULL, kAttemptNumberToResolve, kTotalAttempts)); | 1576 NULL, kAttemptNumberToResolve, kTotalAttempts)); |
| 1576 HostCache* cache = HostCache::CreateDefaultCache(); | 1577 |
| 1577 scoped_ptr<HostResolverImpl> host_resolver( | 1578 HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc); |
| 1578 new HostResolverImpl(resolver_proc, cache, kMaxJobs, kMaxRetryAttempts, | |
| 1579 NULL)); | |
| 1580 | 1579 |
| 1581 // Specify smaller interval for unresponsive_delay_ for HostResolverImpl so | 1580 // Specify smaller interval for unresponsive_delay_ for HostResolverImpl so |
| 1582 // that unit test runs faster. For example, this test finishes in 1.5 secs | 1581 // that unit test runs faster. For example, this test finishes in 1.5 secs |
| 1583 // (500ms * 3). | 1582 // (500ms * 3). |
| 1584 TimeDelta kUnresponsiveTime = TimeDelta::FromMilliseconds(500); | 1583 params.unresponsive_delay = TimeDelta::FromMilliseconds(500); |
| 1585 host_resolver->set_unresponsive_delay(kUnresponsiveTime); | 1584 |
| 1585 scoped_ptr<HostResolverImpl> host_resolver( |
| 1586 new HostResolverImpl(HostCache::CreateDefaultCache(), |
| 1587 DefaultLimits(), |
| 1588 params, |
| 1589 NULL)); |
| 1586 | 1590 |
| 1587 // Resolve "host1". | 1591 // Resolve "host1". |
| 1588 HostResolver::RequestInfo info(HostPortPair("host1", 70)); | 1592 HostResolver::RequestInfo info(HostPortPair("host1", 70)); |
| 1589 TestCompletionCallback callback; | 1593 TestCompletionCallback callback; |
| 1590 AddressList addrlist; | 1594 AddressList addrlist; |
| 1591 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL, | 1595 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL, |
| 1592 BoundNetLog()); | 1596 BoundNetLog()); |
| 1593 EXPECT_EQ(ERR_IO_PENDING, rv); | 1597 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1594 | 1598 |
| 1595 // Resolve returns -4 to indicate that 3rd attempt has resolved the host. | 1599 // Resolve returns -4 to indicate that 3rd attempt has resolved the host. |
| 1596 EXPECT_EQ(-4, callback.WaitForResult()); | 1600 EXPECT_EQ(-4, callback.WaitForResult()); |
| 1597 | 1601 |
| 1598 resolver_proc->WaitForAllAttemptsToFinish(TimeDelta::FromMilliseconds(60000)); | 1602 resolver_proc->WaitForAllAttemptsToFinish(TimeDelta::FromMilliseconds(60000)); |
| 1599 MessageLoop::current()->RunAllPending(); | 1603 MessageLoop::current()->RunAllPending(); |
| 1600 | 1604 |
| 1601 EXPECT_EQ(resolver_proc->total_attempts_resolved(), kTotalAttempts); | 1605 EXPECT_EQ(resolver_proc->total_attempts_resolved(), kTotalAttempts); |
| 1602 EXPECT_EQ(resolver_proc->resolved_attempt_number(), kAttemptNumberToResolve); | 1606 EXPECT_EQ(resolver_proc->resolved_attempt_number(), kAttemptNumberToResolve); |
| 1603 } | 1607 } |
| 1604 | 1608 |
| 1605 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. | 1609 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. |
| 1606 | 1610 |
| 1607 } // namespace net | 1611 } // namespace net |
| OLD | NEW |