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