| 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" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 HostResolverImpl::ProcTaskParams DefaultParams( | 43 HostResolverImpl::ProcTaskParams DefaultParams( |
| 44 HostResolverProc* resolver_proc) { | 44 HostResolverProc* resolver_proc) { |
| 45 return HostResolverImpl::ProcTaskParams(resolver_proc, | 45 return HostResolverImpl::ProcTaskParams(resolver_proc, |
| 46 kMaxRetryAttempts); | 46 kMaxRetryAttempts); |
| 47 } | 47 } |
| 48 | 48 |
| 49 HostResolverImpl* CreateHostResolverImpl(HostResolverProc* resolver_proc) { | 49 HostResolverImpl* CreateHostResolverImpl(HostResolverProc* resolver_proc) { |
| 50 return new HostResolverImpl( | 50 return new HostResolverImpl( |
| 51 HostCache::CreateDefaultCache(), | 51 HostCache::CreateDefaultCache(), |
| 52 DefaultLimits(), | 52 DefaultLimits(), |
| 53 DefaultLimits(), |
| 53 DefaultParams(resolver_proc), | 54 DefaultParams(resolver_proc), |
| 55 scoped_ptr<DnsConfigService>(NULL), |
| 54 NULL); | 56 NULL); |
| 55 } | 57 } |
| 56 | 58 |
| 57 // This HostResolverImpl will only allow 1 outstanding resolve at a time. | 59 // This HostResolverImpl will only allow 1 outstanding resolve at a time. |
| 58 HostResolverImpl* CreateSerialHostResolverImpl( | 60 HostResolverImpl* CreateSerialHostResolverImpl( |
| 59 HostResolverProc* resolver_proc) { | 61 HostResolverProc* resolver_proc) { |
| 60 HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc); | 62 HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc); |
| 61 params.max_retry_attempts = 0u; | 63 params.max_retry_attempts = 0u; |
| 62 | 64 |
| 63 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 65 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 64 | 66 |
| 65 return new HostResolverImpl(HostCache::CreateDefaultCache(), | 67 return new HostResolverImpl(HostCache::CreateDefaultCache(), |
| 66 limits, | 68 limits, |
| 69 limits, |
| 67 params, | 70 params, |
| 71 scoped_ptr<DnsConfigService>(NULL), |
| 68 NULL); | 72 NULL); |
| 69 } | 73 } |
| 70 | 74 |
| 71 // Helper to create a HostResolver::RequestInfo. | 75 // Helper to create a HostResolver::RequestInfo. |
| 72 HostResolver::RequestInfo CreateResolverRequest( | 76 HostResolver::RequestInfo CreateResolverRequest( |
| 73 const std::string& hostname, | 77 const std::string& hostname, |
| 74 RequestPriority priority) { | 78 RequestPriority priority) { |
| 75 HostResolver::RequestInfo info(HostPortPair(hostname, 80)); | 79 HostResolver::RequestInfo info(HostPortPair(hostname, 80)); |
| 76 info.set_priority(priority); | 80 info.set_priority(priority); |
| 77 return info; | 81 return info; |
| (...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 scoped_refptr<WaitingHostResolverProc> resolver_proc( | 514 scoped_refptr<WaitingHostResolverProc> resolver_proc( |
| 511 new WaitingHostResolverProc(NULL)); | 515 new WaitingHostResolverProc(NULL)); |
| 512 | 516 |
| 513 CapturingNetLog net_log(CapturingNetLog::kUnbounded); | 517 CapturingNetLog net_log(CapturingNetLog::kUnbounded); |
| 514 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); | 518 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 515 { | 519 { |
| 516 // This resolver will be destroyed while a lookup is running on WorkerPool. | 520 // This resolver will be destroyed while a lookup is running on WorkerPool. |
| 517 scoped_ptr<HostResolver> host_resolver( | 521 scoped_ptr<HostResolver> host_resolver( |
| 518 new HostResolverImpl(HostCache::CreateDefaultCache(), | 522 new HostResolverImpl(HostCache::CreateDefaultCache(), |
| 519 DefaultLimits(), | 523 DefaultLimits(), |
| 524 DefaultLimits(), |
| 520 DefaultParams(resolver_proc), | 525 DefaultParams(resolver_proc), |
| 526 scoped_ptr<DnsConfigService>(NULL), |
| 521 &net_log)); | 527 &net_log)); |
| 522 AddressList addrlist; | 528 AddressList addrlist; |
| 523 const int kPortnum = 80; | 529 const int kPortnum = 80; |
| 524 | 530 |
| 525 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum)); | 531 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum)); |
| 526 int err = host_resolver->Resolve(info, &addrlist, callback_, NULL, | 532 int err = host_resolver->Resolve(info, &addrlist, callback_, NULL, |
| 527 log.bound()); | 533 log.bound()); |
| 528 EXPECT_EQ(ERR_IO_PENDING, err); | 534 EXPECT_EQ(ERR_IO_PENDING, err); |
| 529 | 535 |
| 530 resolver_proc->Wait(); | 536 resolver_proc->Wait(); |
| (...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 966 | 972 |
| 967 TEST_F(HostResolverImplTest, StartWithinCallback) { | 973 TEST_F(HostResolverImplTest, StartWithinCallback) { |
| 968 // Use a capturing resolver_proc, since the verifier needs to know what calls | 974 // Use a capturing resolver_proc, since the verifier needs to know what calls |
| 969 // reached Resolver(). Also, the capturing resolver_proc is initially | 975 // reached Resolver(). Also, the capturing resolver_proc is initially |
| 970 // blocked. | 976 // blocked. |
| 971 scoped_refptr<CapturingHostResolverProc> resolver_proc( | 977 scoped_refptr<CapturingHostResolverProc> resolver_proc( |
| 972 new CapturingHostResolverProc(NULL)); | 978 new CapturingHostResolverProc(NULL)); |
| 973 | 979 |
| 974 // Turn off caching for this host resolver. | 980 // Turn off caching for this host resolver. |
| 975 scoped_ptr<HostResolver> host_resolver(new HostResolverImpl( | 981 scoped_ptr<HostResolver> host_resolver(new HostResolverImpl( |
| 976 NULL, DefaultLimits(), DefaultParams(resolver_proc), NULL)); | 982 NULL, |
| 983 DefaultLimits(), |
| 984 DefaultLimits(), |
| 985 DefaultParams(resolver_proc), |
| 986 scoped_ptr<DnsConfigService>(NULL), |
| 987 NULL)); |
| 977 | 988 |
| 978 // The class will receive callbacks for when each resolve completes. It | 989 // The class will receive callbacks for when each resolve completes. It |
| 979 // checks that the right things happened. | 990 // checks that the right things happened. |
| 980 StartWithinCallbackVerifier verifier; | 991 StartWithinCallbackVerifier verifier; |
| 981 | 992 |
| 982 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is | 993 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is |
| 983 // blocked, these should all pile up until we signal it. | 994 // blocked, these should all pile up until we signal it. |
| 984 | 995 |
| 985 ResolveRequest req1(host_resolver.get(), "a", 80, &verifier); | 996 ResolveRequest req1(host_resolver.get(), "a", 80, &verifier); |
| 986 ResolveRequest req2(host_resolver.get(), "a", 81, &verifier); | 997 ResolveRequest req2(host_resolver.get(), "a", 81, &verifier); |
| (...skipping 591 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1578 HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc); | 1589 HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc); |
| 1579 | 1590 |
| 1580 // Specify smaller interval for unresponsive_delay_ for HostResolverImpl so | 1591 // Specify smaller interval for unresponsive_delay_ for HostResolverImpl so |
| 1581 // that unit test runs faster. For example, this test finishes in 1.5 secs | 1592 // that unit test runs faster. For example, this test finishes in 1.5 secs |
| 1582 // (500ms * 3). | 1593 // (500ms * 3). |
| 1583 params.unresponsive_delay = TimeDelta::FromMilliseconds(500); | 1594 params.unresponsive_delay = TimeDelta::FromMilliseconds(500); |
| 1584 | 1595 |
| 1585 scoped_ptr<HostResolverImpl> host_resolver( | 1596 scoped_ptr<HostResolverImpl> host_resolver( |
| 1586 new HostResolverImpl(HostCache::CreateDefaultCache(), | 1597 new HostResolverImpl(HostCache::CreateDefaultCache(), |
| 1587 DefaultLimits(), | 1598 DefaultLimits(), |
| 1599 DefaultLimits(), |
| 1588 params, | 1600 params, |
| 1601 scoped_ptr<DnsConfigService>(NULL), |
| 1589 NULL)); | 1602 NULL)); |
| 1590 | 1603 |
| 1591 // Resolve "host1". | 1604 // Resolve "host1". |
| 1592 HostResolver::RequestInfo info(HostPortPair("host1", 70)); | 1605 HostResolver::RequestInfo info(HostPortPair("host1", 70)); |
| 1593 TestCompletionCallback callback; | 1606 TestCompletionCallback callback; |
| 1594 AddressList addrlist; | 1607 AddressList addrlist; |
| 1595 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL, | 1608 int rv = host_resolver->Resolve(info, &addrlist, callback.callback(), NULL, |
| 1596 BoundNetLog()); | 1609 BoundNetLog()); |
| 1597 EXPECT_EQ(ERR_IO_PENDING, rv); | 1610 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1598 | 1611 |
| 1599 // Resolve returns -4 to indicate that 3rd attempt has resolved the host. | 1612 // Resolve returns -4 to indicate that 3rd attempt has resolved the host. |
| 1600 EXPECT_EQ(-4, callback.WaitForResult()); | 1613 EXPECT_EQ(-4, callback.WaitForResult()); |
| 1601 | 1614 |
| 1602 resolver_proc->WaitForAllAttemptsToFinish(TimeDelta::FromMilliseconds(60000)); | 1615 resolver_proc->WaitForAllAttemptsToFinish(TimeDelta::FromMilliseconds(60000)); |
| 1603 MessageLoop::current()->RunAllPending(); | 1616 MessageLoop::current()->RunAllPending(); |
| 1604 | 1617 |
| 1605 EXPECT_EQ(resolver_proc->total_attempts_resolved(), kTotalAttempts); | 1618 EXPECT_EQ(resolver_proc->total_attempts_resolved(), kTotalAttempts); |
| 1606 EXPECT_EQ(resolver_proc->resolved_attempt_number(), kAttemptNumberToResolve); | 1619 EXPECT_EQ(resolver_proc->resolved_attempt_number(), kAttemptNumberToResolve); |
| 1607 } | 1620 } |
| 1608 | 1621 |
| 1609 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. | 1622 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. |
| 1610 | 1623 |
| 1611 } // namespace net | 1624 } // namespace net |
| OLD | NEW |