| OLD | NEW |
| 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" | 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 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 }; | 468 }; |
| 445 | 469 |
| 446 TEST_F(HostResolverImplTest, CanceledAsynchronousLookup) { | 470 TEST_F(HostResolverImplTest, CanceledAsynchronousLookup) { |
| 447 scoped_refptr<WaitingHostResolverProc> resolver_proc( | 471 scoped_refptr<WaitingHostResolverProc> resolver_proc( |
| 448 new WaitingHostResolverProc(NULL)); | 472 new WaitingHostResolverProc(NULL)); |
| 449 | 473 |
| 450 CapturingNetLog net_log(CapturingNetLog::kUnbounded); | 474 CapturingNetLog net_log(CapturingNetLog::kUnbounded); |
| 451 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); | 475 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 452 { | 476 { |
| 453 scoped_ptr<HostResolver> host_resolver( | 477 scoped_ptr<HostResolver> host_resolver( |
| 454 new HostResolverImpl(resolver_proc, | 478 new HostResolverImpl(HostCache::CreateDefaultCache(), |
| 455 HostCache::CreateDefaultCache(), | 479 DefaultLimits(), |
| 456 kMaxJobs, | 480 DefaultParams(resolver_proc), |
| 457 kMaxRetryAttempts, | |
| 458 &net_log)); | 481 &net_log)); |
| 459 AddressList addrlist; | 482 AddressList addrlist; |
| 460 const int kPortnum = 80; | 483 const int kPortnum = 80; |
| 461 | 484 |
| 462 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum)); | 485 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum)); |
| 463 int err = host_resolver->Resolve(info, &addrlist, callback_, NULL, | 486 int err = host_resolver->Resolve(info, &addrlist, callback_, NULL, |
| 464 log.bound()); | 487 log.bound()); |
| 465 EXPECT_EQ(ERR_IO_PENDING, err); | 488 EXPECT_EQ(ERR_IO_PENDING, err); |
| 466 | 489 |
| 467 resolver_proc->Wait(); | 490 resolver_proc->Wait(); |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 895 }; | 918 }; |
| 896 | 919 |
| 897 TEST_F(HostResolverImplTest, StartWithinCallback) { | 920 TEST_F(HostResolverImplTest, StartWithinCallback) { |
| 898 // Use a capturing resolver_proc, since the verifier needs to know what calls | 921 // Use a capturing resolver_proc, since the verifier needs to know what calls |
| 899 // reached Resolver(). Also, the capturing resolver_proc is initially | 922 // reached Resolver(). Also, the capturing resolver_proc is initially |
| 900 // blocked. | 923 // blocked. |
| 901 scoped_refptr<CapturingHostResolverProc> resolver_proc( | 924 scoped_refptr<CapturingHostResolverProc> resolver_proc( |
| 902 new CapturingHostResolverProc(NULL)); | 925 new CapturingHostResolverProc(NULL)); |
| 903 | 926 |
| 904 // Turn off caching for this host resolver. | 927 // Turn off caching for this host resolver. |
| 905 scoped_ptr<HostResolver> host_resolver( | 928 scoped_ptr<HostResolver> host_resolver(new HostResolverImpl( |
| 906 new HostResolverImpl(resolver_proc, NULL, kMaxJobs, kMaxRetryAttempts, | 929 NULL, DefaultLimits(), DefaultParams(resolver_proc), NULL)); |
| 907 NULL)); | |
| 908 | 930 |
| 909 // The class will receive callbacks for when each resolve completes. It | 931 // The class will receive callbacks for when each resolve completes. It |
| 910 // checks that the right things happened. | 932 // checks that the right things happened. |
| 911 StartWithinCallbackVerifier verifier; | 933 StartWithinCallbackVerifier verifier; |
| 912 | 934 |
| 913 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is | 935 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is |
| 914 // blocked, these should all pile up until we signal it. | 936 // blocked, these should all pile up until we signal it. |
| 915 | 937 |
| 916 ResolveRequest req1(host_resolver.get(), "a", 80, &verifier); | 938 ResolveRequest req1(host_resolver.get(), "a", 80, &verifier); |
| 917 ResolveRequest req2(host_resolver.get(), "a", 81, &verifier); | 939 ResolveRequest req2(host_resolver.get(), "a", 81, &verifier); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 977 // Start a request. | 999 // Start a request. |
| 978 ResolveRequest req1(host_resolver.get(), "a", 80, &verifier); | 1000 ResolveRequest req1(host_resolver.get(), "a", 80, &verifier); |
| 979 | 1001 |
| 980 // |verifier| will send quit message once all the requests have finished. | 1002 // |verifier| will send quit message once all the requests have finished. |
| 981 MessageLoop::current()->Run(); | 1003 MessageLoop::current()->Run(); |
| 982 } | 1004 } |
| 983 | 1005 |
| 984 // Test that IP address changes flush the cache. | 1006 // Test that IP address changes flush the cache. |
| 985 TEST_F(HostResolverImplTest, FlushCacheOnIPAddressChange) { | 1007 TEST_F(HostResolverImplTest, FlushCacheOnIPAddressChange) { |
| 986 scoped_ptr<HostResolver> host_resolver( | 1008 scoped_ptr<HostResolver> host_resolver( |
| 987 new HostResolverImpl(NULL, HostCache::CreateDefaultCache(), kMaxJobs, | 1009 CreateHostResolverImpl(NULL)); |
| 988 kMaxRetryAttempts, NULL)); | |
| 989 | 1010 |
| 990 AddressList addrlist; | 1011 AddressList addrlist; |
| 991 | 1012 |
| 992 // Resolve "host1". | 1013 // Resolve "host1". |
| 993 HostResolver::RequestInfo info1(HostPortPair("host1", 70)); | 1014 HostResolver::RequestInfo info1(HostPortPair("host1", 70)); |
| 994 TestCompletionCallback callback; | 1015 TestCompletionCallback callback; |
| 995 int rv = host_resolver->Resolve(info1, &addrlist, callback.callback(), NULL, | 1016 int rv = host_resolver->Resolve(info1, &addrlist, callback.callback(), NULL, |
| 996 BoundNetLog()); | 1017 BoundNetLog()); |
| 997 EXPECT_EQ(ERR_IO_PENDING, rv); | 1018 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 998 EXPECT_EQ(OK, callback.WaitForResult()); | 1019 EXPECT_EQ(OK, callback.WaitForResult()); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1036 resolver_proc->Signal(); | 1057 resolver_proc->Signal(); |
| 1037 | 1058 |
| 1038 EXPECT_EQ(ERR_ABORTED, callback.WaitForResult()); | 1059 EXPECT_EQ(ERR_ABORTED, callback.WaitForResult()); |
| 1039 EXPECT_EQ(0u, host_resolver->GetHostCache()->size()); | 1060 EXPECT_EQ(0u, host_resolver->GetHostCache()->size()); |
| 1040 } | 1061 } |
| 1041 | 1062 |
| 1042 // Obey pool constraints after IP address has changed. | 1063 // Obey pool constraints after IP address has changed. |
| 1043 TEST_F(HostResolverImplTest, ObeyPoolConstraintsAfterIPAddressChange) { | 1064 TEST_F(HostResolverImplTest, ObeyPoolConstraintsAfterIPAddressChange) { |
| 1044 scoped_refptr<WaitingHostResolverProc> resolver_proc( | 1065 scoped_refptr<WaitingHostResolverProc> resolver_proc( |
| 1045 new WaitingHostResolverProc(CreateCatchAllHostResolverProc())); | 1066 new WaitingHostResolverProc(CreateCatchAllHostResolverProc())); |
| 1067 |
| 1046 scoped_ptr<HostResolverImpl> host_resolver( | 1068 scoped_ptr<HostResolverImpl> host_resolver( |
| 1047 new HostResolverImpl(resolver_proc, HostCache::CreateDefaultCache(), | 1069 CreateSerialHostResolverImpl(resolver_proc)); |
| 1048 kMaxJobs, kMaxRetryAttempts, NULL)); | |
| 1049 | |
| 1050 const size_t kMaxOutstandingJobs = 1u; | |
| 1051 const size_t kMaxPendingRequests = 1000000u; // not relevant. | |
| 1052 host_resolver->SetPoolConstraints(HostResolverImpl::POOL_NORMAL, | |
| 1053 kMaxOutstandingJobs, | |
| 1054 kMaxPendingRequests); | |
| 1055 | 1070 |
| 1056 // Resolve "host1". | 1071 // Resolve "host1". |
| 1057 HostResolver::RequestInfo info(HostPortPair("host1", 70)); | 1072 HostResolver::RequestInfo info(HostPortPair("host1", 70)); |
| 1058 TestCompletionCallback callback; | 1073 TestCompletionCallback callback; |
| 1059 AddressList addrlist; | 1074 AddressList addrlist; |
| 1060 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL, | 1075 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL, |
| 1061 BoundNetLog()); | 1076 BoundNetLog()); |
| 1062 EXPECT_EQ(ERR_IO_PENDING, rv); | 1077 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1063 | 1078 |
| 1064 // Must wait before signal to ensure that the two signals don't get merged | 1079 // 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... |
| 1132 resolver_proc->Signal(); // release the thread from WorkerPool for cleanup | 1147 resolver_proc->Signal(); // release the thread from WorkerPool for cleanup |
| 1133 EXPECT_EQ(OK, callback.WaitForNestedResult()); | 1148 EXPECT_EQ(OK, callback.WaitForNestedResult()); |
| 1134 } | 1149 } |
| 1135 | 1150 |
| 1136 // Tests that when the maximum threads is set to 1, requests are dequeued | 1151 // Tests that when the maximum threads is set to 1, requests are dequeued |
| 1137 // in order of priority. | 1152 // in order of priority. |
| 1138 TEST_F(HostResolverImplTest, HigherPriorityRequestsStartedFirst) { | 1153 TEST_F(HostResolverImplTest, HigherPriorityRequestsStartedFirst) { |
| 1139 scoped_refptr<CapturingHostResolverProc> resolver_proc( | 1154 scoped_refptr<CapturingHostResolverProc> resolver_proc( |
| 1140 new CapturingHostResolverProc(NULL)); | 1155 new CapturingHostResolverProc(NULL)); |
| 1141 | 1156 |
| 1142 // This HostResolverImpl will only allow 1 outstanding resolve at a time. | 1157 scoped_ptr<HostResolverImpl> host_resolver( |
| 1143 size_t kMaxJobs = 1u; | 1158 CreateSerialHostResolverImpl(resolver_proc)); |
| 1144 const size_t kRetryAttempts = 0u; | |
| 1145 scoped_ptr<HostResolver> host_resolver( | |
| 1146 new HostResolverImpl(resolver_proc, HostCache::CreateDefaultCache(), | |
| 1147 kMaxJobs, kRetryAttempts, NULL)); | |
| 1148 | 1159 |
| 1149 // Note that at this point the CapturingHostResolverProc is blocked, so any | 1160 // Note that at this point the CapturingHostResolverProc is blocked, so any |
| 1150 // requests we make will not complete. | 1161 // requests we make will not complete. |
| 1151 | 1162 |
| 1152 HostResolver::RequestInfo req[] = { | 1163 HostResolver::RequestInfo req[] = { |
| 1153 CreateResolverRequest("req0", LOW), | 1164 CreateResolverRequest("req0", LOW), |
| 1154 CreateResolverRequest("req1", MEDIUM), | 1165 CreateResolverRequest("req1", MEDIUM), |
| 1155 CreateResolverRequest("req2", MEDIUM), | 1166 CreateResolverRequest("req2", MEDIUM), |
| 1156 CreateResolverRequest("req3", LOW), | 1167 CreateResolverRequest("req3", LOW), |
| 1157 CreateResolverRequest("req4", HIGHEST), | 1168 CreateResolverRequest("req4", HIGHEST), |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1194 EXPECT_EQ("req2", capture_list[4].hostname); | 1205 EXPECT_EQ("req2", capture_list[4].hostname); |
| 1195 EXPECT_EQ("req3", capture_list[5].hostname); | 1206 EXPECT_EQ("req3", capture_list[5].hostname); |
| 1196 EXPECT_EQ("req6", capture_list[6].hostname); | 1207 EXPECT_EQ("req6", capture_list[6].hostname); |
| 1197 } | 1208 } |
| 1198 | 1209 |
| 1199 // Try cancelling a request which has not been attached to a job yet. | 1210 // Try cancelling a request which has not been attached to a job yet. |
| 1200 TEST_F(HostResolverImplTest, CancelPendingRequest) { | 1211 TEST_F(HostResolverImplTest, CancelPendingRequest) { |
| 1201 scoped_refptr<CapturingHostResolverProc> resolver_proc( | 1212 scoped_refptr<CapturingHostResolverProc> resolver_proc( |
| 1202 new CapturingHostResolverProc(NULL)); | 1213 new CapturingHostResolverProc(NULL)); |
| 1203 | 1214 |
| 1204 // This HostResolverImpl will only allow 1 outstanding resolve at a time. | 1215 scoped_ptr<HostResolverImpl> host_resolver( |
| 1205 const size_t kMaxJobs = 1u; | 1216 CreateSerialHostResolverImpl(resolver_proc)); |
| 1206 const size_t kRetryAttempts = 0u; | |
| 1207 scoped_ptr<HostResolver> host_resolver( | |
| 1208 new HostResolverImpl(resolver_proc, HostCache::CreateDefaultCache(), | |
| 1209 kMaxJobs, kRetryAttempts, NULL)); | |
| 1210 | 1217 |
| 1211 // Note that at this point the CapturingHostResolverProc is blocked, so any | 1218 // Note that at this point the CapturingHostResolverProc is blocked, so any |
| 1212 // requests we make will not complete. | 1219 // requests we make will not complete. |
| 1213 | 1220 |
| 1214 HostResolver::RequestInfo req[] = { | 1221 HostResolver::RequestInfo req[] = { |
| 1215 CreateResolverRequest("req0", LOWEST), | 1222 CreateResolverRequest("req0", LOWEST), |
| 1216 CreateResolverRequest("req1", HIGHEST), // Will cancel. | 1223 CreateResolverRequest("req1", HIGHEST), // Will cancel. |
| 1217 CreateResolverRequest("req2", MEDIUM), | 1224 CreateResolverRequest("req2", MEDIUM), |
| 1218 CreateResolverRequest("req3", LOW), | 1225 CreateResolverRequest("req3", LOW), |
| 1219 CreateResolverRequest("req4", HIGHEST), // Will cancel. | 1226 CreateResolverRequest("req4", HIGHEST), // Will cancel. |
| 1220 CreateResolverRequest("req5", LOWEST), // Will cancel. | 1227 CreateResolverRequest("req5", LOWEST), // Will cancel. |
| 1221 CreateResolverRequest("req6", MEDIUM), | 1228 CreateResolverRequest("req6", MEDIUM), |
| 1222 }; | 1229 }; |
| 1223 | 1230 |
| 1224 TestCompletionCallback callback[arraysize(req)]; | 1231 TestCompletionCallback callback[arraysize(req)]; |
| 1225 AddressList addrlist[arraysize(req)]; | 1232 AddressList addrlist[arraysize(req)]; |
| 1226 HostResolver::RequestHandle handle[arraysize(req)]; | 1233 HostResolver::RequestHandle handle[arraysize(req)]; |
| 1227 | 1234 |
| 1228 // Start all of the requests. | 1235 // Start all of the requests. |
| 1229 for (size_t i = 0; i < arraysize(req); ++i) { | 1236 for (size_t i = 0; i < arraysize(req); ++i) { |
| 1230 int rv = host_resolver->Resolve(req[i], &addrlist[i], | 1237 int rv = host_resolver->Resolve(req[i], &addrlist[i], |
| (...skipping 28 matching lines...) Expand all Loading... |
| 1259 EXPECT_EQ("req2", capture_list[1].hostname); | 1266 EXPECT_EQ("req2", capture_list[1].hostname); |
| 1260 EXPECT_EQ("req6", capture_list[2].hostname); | 1267 EXPECT_EQ("req6", capture_list[2].hostname); |
| 1261 EXPECT_EQ("req3", capture_list[3].hostname); | 1268 EXPECT_EQ("req3", capture_list[3].hostname); |
| 1262 } | 1269 } |
| 1263 | 1270 |
| 1264 // Test that when too many requests are enqueued, old ones start to be aborted. | 1271 // Test that when too many requests are enqueued, old ones start to be aborted. |
| 1265 TEST_F(HostResolverImplTest, QueueOverflow) { | 1272 TEST_F(HostResolverImplTest, QueueOverflow) { |
| 1266 scoped_refptr<CapturingHostResolverProc> resolver_proc( | 1273 scoped_refptr<CapturingHostResolverProc> resolver_proc( |
| 1267 new CapturingHostResolverProc(NULL)); | 1274 new CapturingHostResolverProc(NULL)); |
| 1268 | 1275 |
| 1269 // This HostResolverImpl will only allow 1 outstanding resolve at a time. | 1276 scoped_ptr<HostResolverImpl> host_resolver( |
| 1270 const size_t kMaxOutstandingJobs = 1u; | 1277 CreateSerialHostResolverImpl(resolver_proc)); |
| 1271 const size_t kRetryAttempts = 0u; | |
| 1272 scoped_ptr<HostResolverImpl> host_resolver(new HostResolverImpl( | |
| 1273 resolver_proc, HostCache::CreateDefaultCache(), kMaxOutstandingJobs, | |
| 1274 kRetryAttempts, NULL)); | |
| 1275 | 1278 |
| 1276 // Only allow up to 3 requests to be enqueued at a time. | 1279 // Allow only 3 queued jobs. |
| 1277 const size_t kMaxPendingRequests = 3u; | 1280 const size_t kMaxPendingJobs = 3u; |
| 1278 host_resolver->SetPoolConstraints(HostResolverImpl::POOL_NORMAL, | 1281 host_resolver->SetMaxQueuedJobs(kMaxPendingJobs); |
| 1279 kMaxOutstandingJobs, | |
| 1280 kMaxPendingRequests); | |
| 1281 | 1282 |
| 1282 // Note that at this point the CapturingHostResolverProc is blocked, so any | 1283 // Note that at this point the CapturingHostResolverProc is blocked, so any |
| 1283 // requests we make will not complete. | 1284 // requests we make will not complete. |
| 1284 | 1285 |
| 1285 HostResolver::RequestInfo req[] = { | 1286 HostResolver::RequestInfo req[] = { |
| 1286 CreateResolverRequest("req0", LOWEST), | 1287 CreateResolverRequest("req0", LOWEST), |
| 1287 CreateResolverRequest("req1", HIGHEST), | 1288 CreateResolverRequest("req1", HIGHEST), |
| 1288 CreateResolverRequest("req2", MEDIUM), | 1289 CreateResolverRequest("req2", MEDIUM), |
| 1289 CreateResolverRequest("req3", MEDIUM), | 1290 CreateResolverRequest("req3", MEDIUM), |
| 1290 | 1291 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1341 EXPECT_EQ("req7", capture_list[3].hostname); | 1342 EXPECT_EQ("req7", capture_list[3].hostname); |
| 1342 } | 1343 } |
| 1343 | 1344 |
| 1344 // Tests that after changing the default AddressFamily to IPV4, requests | 1345 // Tests that after changing the default AddressFamily to IPV4, requests |
| 1345 // with UNSPECIFIED address family map to IPV4. | 1346 // with UNSPECIFIED address family map to IPV4. |
| 1346 TEST_F(HostResolverImplTest, SetDefaultAddressFamily_IPv4) { | 1347 TEST_F(HostResolverImplTest, SetDefaultAddressFamily_IPv4) { |
| 1347 scoped_refptr<CapturingHostResolverProc> resolver_proc( | 1348 scoped_refptr<CapturingHostResolverProc> resolver_proc( |
| 1348 new CapturingHostResolverProc(new EchoingHostResolverProc)); | 1349 new CapturingHostResolverProc(new EchoingHostResolverProc)); |
| 1349 | 1350 |
| 1350 // This HostResolverImpl will only allow 1 outstanding resolve at a time. | 1351 // This HostResolverImpl will only allow 1 outstanding resolve at a time. |
| 1351 const size_t kMaxOutstandingJobs = 1u; | 1352 scoped_ptr<HostResolverImpl> host_resolver( |
| 1352 const size_t kRetryAttempts = 0u; | 1353 CreateSerialHostResolverImpl(resolver_proc)); |
| 1353 scoped_ptr<HostResolverImpl> host_resolver(new HostResolverImpl( | |
| 1354 resolver_proc, HostCache::CreateDefaultCache(), kMaxOutstandingJobs, | |
| 1355 kRetryAttempts, NULL)); | |
| 1356 | 1354 |
| 1357 host_resolver->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); | 1355 host_resolver->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); |
| 1358 | 1356 |
| 1359 // Note that at this point the CapturingHostResolverProc is blocked, so any | 1357 // Note that at this point the CapturingHostResolverProc is blocked, so any |
| 1360 // requests we make will not complete. | 1358 // requests we make will not complete. |
| 1361 | 1359 |
| 1362 HostResolver::RequestInfo req[] = { | 1360 HostResolver::RequestInfo req[] = { |
| 1363 CreateResolverRequestForAddressFamily("h1", MEDIUM, | 1361 CreateResolverRequestForAddressFamily("h1", MEDIUM, |
| 1364 ADDRESS_FAMILY_UNSPECIFIED), | 1362 ADDRESS_FAMILY_UNSPECIFIED), |
| 1365 CreateResolverRequestForAddressFamily("h1", MEDIUM, ADDRESS_FAMILY_IPV4), | 1363 CreateResolverRequestForAddressFamily("h1", MEDIUM, ADDRESS_FAMILY_IPV4), |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1411 EXPECT_EQ("192.2.104.2", NetAddressToString(addrlist[2].head())); | 1409 EXPECT_EQ("192.2.104.2", NetAddressToString(addrlist[2].head())); |
| 1412 } | 1410 } |
| 1413 | 1411 |
| 1414 // This is the exact same test as SetDefaultAddressFamily_IPv4, except the order | 1412 // This is the exact same test as SetDefaultAddressFamily_IPv4, except the order |
| 1415 // of requests 0 and 1 is flipped, and the default is set to IPv6 in place of | 1413 // of requests 0 and 1 is flipped, and the default is set to IPv6 in place of |
| 1416 // IPv4. | 1414 // IPv4. |
| 1417 TEST_F(HostResolverImplTest, SetDefaultAddressFamily_IPv6) { | 1415 TEST_F(HostResolverImplTest, SetDefaultAddressFamily_IPv6) { |
| 1418 scoped_refptr<CapturingHostResolverProc> resolver_proc( | 1416 scoped_refptr<CapturingHostResolverProc> resolver_proc( |
| 1419 new CapturingHostResolverProc(new EchoingHostResolverProc)); | 1417 new CapturingHostResolverProc(new EchoingHostResolverProc)); |
| 1420 | 1418 |
| 1421 // This HostResolverImpl will only allow 1 outstanding resolve at a time. | 1419 scoped_ptr<HostResolverImpl> host_resolver( |
| 1422 const size_t kMaxOutstandingJobs = 1u; | 1420 CreateSerialHostResolverImpl(resolver_proc)); |
| 1423 const size_t kRetryAttempts = 0u; | |
| 1424 scoped_ptr<HostResolverImpl> host_resolver(new HostResolverImpl( | |
| 1425 resolver_proc, HostCache::CreateDefaultCache(), kMaxOutstandingJobs, | |
| 1426 kRetryAttempts, NULL)); | |
| 1427 | 1421 |
| 1428 host_resolver->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV6); | 1422 host_resolver->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV6); |
| 1429 | 1423 |
| 1430 // Note that at this point the CapturingHostResolverProc is blocked, so any | 1424 // Note that at this point the CapturingHostResolverProc is blocked, so any |
| 1431 // requests we make will not complete. | 1425 // requests we make will not complete. |
| 1432 | 1426 |
| 1433 HostResolver::RequestInfo req[] = { | 1427 HostResolver::RequestInfo req[] = { |
| 1434 CreateResolverRequestForAddressFamily("h1", MEDIUM, ADDRESS_FAMILY_IPV6), | 1428 CreateResolverRequestForAddressFamily("h1", MEDIUM, ADDRESS_FAMILY_IPV6), |
| 1435 CreateResolverRequestForAddressFamily("h1", MEDIUM, | 1429 CreateResolverRequestForAddressFamily("h1", MEDIUM, |
| 1436 ADDRESS_FAMILY_UNSPECIFIED), | 1430 ADDRESS_FAMILY_UNSPECIFIED), |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1526 // Total number of attempts would be 3 and we want the 3rd attempt to resolve | 1520 // Total number of attempts would be 3 and we want the 3rd attempt to resolve |
| 1527 // the host. First and second attempt will be forced to sleep until they get | 1521 // the host. First and second attempt will be forced to sleep until they get |
| 1528 // word that a resolution has completed. The 3rd resolution attempt will try | 1522 // word that a resolution has completed. The 3rd resolution attempt will try |
| 1529 // to get done ASAP, and won't sleep.. | 1523 // to get done ASAP, and won't sleep.. |
| 1530 int kAttemptNumberToResolve = 3; | 1524 int kAttemptNumberToResolve = 3; |
| 1531 int kTotalAttempts = 3; | 1525 int kTotalAttempts = 3; |
| 1532 | 1526 |
| 1533 scoped_refptr<LookupAttemptHostResolverProc> resolver_proc( | 1527 scoped_refptr<LookupAttemptHostResolverProc> resolver_proc( |
| 1534 new LookupAttemptHostResolverProc( | 1528 new LookupAttemptHostResolverProc( |
| 1535 NULL, kAttemptNumberToResolve, kTotalAttempts)); | 1529 NULL, kAttemptNumberToResolve, kTotalAttempts)); |
| 1536 HostCache* cache = HostCache::CreateDefaultCache(); | 1530 |
| 1537 scoped_ptr<HostResolverImpl> host_resolver( | 1531 HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc); |
| 1538 new HostResolverImpl(resolver_proc, cache, kMaxJobs, kMaxRetryAttempts, | |
| 1539 NULL)); | |
| 1540 | 1532 |
| 1541 // Specify smaller interval for unresponsive_delay_ for HostResolverImpl so | 1533 // Specify smaller interval for unresponsive_delay_ for HostResolverImpl so |
| 1542 // that unit test runs faster. For example, this test finishes in 1.5 secs | 1534 // that unit test runs faster. For example, this test finishes in 1.5 secs |
| 1543 // (500ms * 3). | 1535 // (500ms * 3). |
| 1544 TimeDelta kUnresponsiveTime = TimeDelta::FromMilliseconds(500); | 1536 params.unresponsive_delay = TimeDelta::FromMilliseconds(500); |
| 1545 host_resolver->set_unresponsive_delay(kUnresponsiveTime); | 1537 |
| 1538 scoped_ptr<HostResolverImpl> host_resolver( |
| 1539 new HostResolverImpl(HostCache::CreateDefaultCache(), |
| 1540 DefaultLimits(), |
| 1541 params, |
| 1542 NULL)); |
| 1546 | 1543 |
| 1547 // Resolve "host1". | 1544 // Resolve "host1". |
| 1548 HostResolver::RequestInfo info(HostPortPair("host1", 70)); | 1545 HostResolver::RequestInfo info(HostPortPair("host1", 70)); |
| 1549 TestCompletionCallback callback; | 1546 TestCompletionCallback callback; |
| 1550 AddressList addrlist; | 1547 AddressList addrlist; |
| 1551 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL, | 1548 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL, |
| 1552 BoundNetLog()); | 1549 BoundNetLog()); |
| 1553 EXPECT_EQ(ERR_IO_PENDING, rv); | 1550 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1554 | 1551 |
| 1555 // Resolve returns -4 to indicate that 3rd attempt has resolved the host. | 1552 // Resolve returns -4 to indicate that 3rd attempt has resolved the host. |
| 1556 EXPECT_EQ(-4, callback.WaitForResult()); | 1553 EXPECT_EQ(-4, callback.WaitForResult()); |
| 1557 | 1554 |
| 1558 resolver_proc->WaitForAllAttemptsToFinish(TimeDelta::FromMilliseconds(60000)); | 1555 resolver_proc->WaitForAllAttemptsToFinish(TimeDelta::FromMilliseconds(60000)); |
| 1559 MessageLoop::current()->RunAllPending(); | 1556 MessageLoop::current()->RunAllPending(); |
| 1560 | 1557 |
| 1561 EXPECT_EQ(resolver_proc->total_attempts_resolved(), kTotalAttempts); | 1558 EXPECT_EQ(resolver_proc->total_attempts_resolved(), kTotalAttempts); |
| 1562 EXPECT_EQ(resolver_proc->resolved_attempt_number(), kAttemptNumberToResolve); | 1559 EXPECT_EQ(resolver_proc->resolved_attempt_number(), kAttemptNumberToResolve); |
| 1563 } | 1560 } |
| 1564 | 1561 |
| 1565 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. | 1562 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. |
| 1566 | 1563 |
| 1567 } // namespace net | 1564 } // namespace net |
| OLD | NEW |