diff options
author | ericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-18 07:31:04 +0000 |
---|---|---|
committer | ericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-18 07:31:04 +0000 |
commit | a2c2fb9ff48cd37dbed258dca5b40461c92b71a8 (patch) | |
tree | 4526d3db799c1e1f49b1f5387d8d8b267f006a5a /net/base | |
parent | eb5f5505a0b430c27f64c44f4928a0a50faf5f3d (diff) | |
download | chromium_src-a2c2fb9ff48cd37dbed258dca5b40461c92b71a8.zip chromium_src-a2c2fb9ff48cd37dbed258dca5b40461c92b71a8.tar.gz chromium_src-a2c2fb9ff48cd37dbed258dca5b40461c92b71a8.tar.bz2 |
Add synchronous-mode operation to MockHostResolver; this helps clarify some tests, which were using caching to get synchronous resolutions.TEST=existingBUG=NONE (addresses a TODO however).
Review URL: http://codereview.chromium.org/155620
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21052 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r-- | net/base/host_resolver_proc.h | 2 | ||||
-rw-r--r-- | net/base/mock_host_resolver.cc | 38 | ||||
-rw-r--r-- | net/base/mock_host_resolver.h | 40 |
3 files changed, 54 insertions, 26 deletions
diff --git a/net/base/host_resolver_proc.h b/net/base/host_resolver_proc.h index 2690987..66978f7 100644 --- a/net/base/host_resolver_proc.h +++ b/net/base/host_resolver_proc.h @@ -36,7 +36,7 @@ class HostResolverProc : public base::RefCountedThreadSafe<HostResolverProc> { private: friend class HostResolverImpl; - friend class MockHostResolver; + friend class MockHostResolverBase; friend class ScopedDefaultHostResolverProc; // Sets the previous procedure in the chain. diff --git a/net/base/mock_host_resolver.cc b/net/base/mock_host_resolver.cc index 58ad552..508ef38 100644 --- a/net/base/mock_host_resolver.cc +++ b/net/base/mock_host_resolver.cc @@ -11,36 +11,41 @@ namespace net { -MockHostResolver::MockHostResolver() { - Reset(NULL, 0, 0); -} - -int MockHostResolver::Resolve(const RequestInfo& info, - AddressList* addresses, - CompletionCallback* callback, - RequestHandle* out_req) { +MockHostResolverBase::MockHostResolverBase(bool use_caching) + : use_caching_(use_caching) { + Reset(NULL); +} + +int MockHostResolverBase::Resolve(const RequestInfo& info, + AddressList* addresses, + CompletionCallback* callback, + RequestHandle* out_req) { + if (synchronous_mode_) { + callback = NULL; + out_req = NULL; + } return impl_->Resolve(info, addresses, callback, out_req); } -void MockHostResolver::CancelRequest(RequestHandle req) { +void MockHostResolverBase::CancelRequest(RequestHandle req) { impl_->CancelRequest(req); } -void MockHostResolver::AddObserver(Observer* observer) { +void MockHostResolverBase::AddObserver(Observer* observer) { impl_->AddObserver(observer); } -void MockHostResolver::RemoveObserver(Observer* observer) { +void MockHostResolverBase::RemoveObserver(Observer* observer) { impl_->RemoveObserver(observer); } -void MockHostResolver::Shutdown() { +void MockHostResolverBase::Shutdown() { impl_->Shutdown(); } -void MockHostResolver::Reset(HostResolverProc* interceptor, - int max_cache_entries, - int max_cache_age_ms) { +void MockHostResolverBase::Reset(HostResolverProc* interceptor) { + synchronous_mode_ = false; + // At the root of the chain, map everything to localhost. scoped_refptr<RuleBasedHostResolverProc> catchall = new RuleBasedHostResolverProc(NULL); @@ -57,6 +62,9 @@ void MockHostResolver::Reset(HostResolverProc* interceptor, proc = interceptor; } + int max_cache_entries = use_caching_ ? 100 : 0; + int max_cache_age_ms = use_caching_ ? 60000 : 0; + impl_ = new HostResolverImpl(proc, max_cache_entries, max_cache_age_ms); } diff --git a/net/base/mock_host_resolver.h b/net/base/mock_host_resolver.h index ee83105..1d29684 100644 --- a/net/base/mock_host_resolver.h +++ b/net/base/mock_host_resolver.h @@ -34,14 +34,11 @@ class RuleBasedHostResolverProc; // // Replacement doesn't have to be string representing an IP address. It can // re-map one hostname to another as well. -class MockHostResolver : public HostResolver { - public: - // Creates a MockHostResolver that does NOT cache entries - // (the HostResolverProc will be called for every lookup). If you need - // caching behavior, call Reset() with non-zero cache size. - MockHostResolver(); - virtual ~MockHostResolver() {} +// Base class shared by MockHostResolver and MockCachingHostResolver. +class MockHostResolverBase : public HostResolver { + public: + virtual ~MockHostResolverBase() {} // HostResolver methods: virtual int Resolve(const RequestInfo& info, AddressList* addresses, @@ -54,14 +51,37 @@ class MockHostResolver : public HostResolver { RuleBasedHostResolverProc* rules() { return rules_; } + // Controls whether resolutions complete synchronously or asynchronously. + void set_synchronous_mode(bool is_synchronous) { + synchronous_mode_ = is_synchronous; + } + // Resets the mock. - void Reset(HostResolverProc* interceptor, - int max_cache_entries, - int max_cache_age_ms); + void Reset(HostResolverProc* interceptor); + + protected: + MockHostResolverBase(bool use_caching); private: scoped_refptr<HostResolverImpl> impl_; scoped_refptr<RuleBasedHostResolverProc> rules_; + bool synchronous_mode_; + bool use_caching_; +}; + +class MockHostResolver : public MockHostResolverBase { + public: + MockHostResolver() : MockHostResolverBase(false /*use_caching*/) {} +}; + +// Same as MockHostResolver, except internally it uses a host-cache. +// +// Note that tests are advised to use MockHostResolver instead, since it is +// more predictable. (MockHostResolver also can be put into synchronous +// operation mode in case that is what you needed from the caching version). +class MockCachingHostResolver : public MockHostResolverBase { + public: + MockCachingHostResolver() : MockHostResolverBase(true /*use_caching*/) {} }; // RuleBasedHostResolverProc applies a set of rules to map a host string to |