Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(339)

Unified Diff: net/proxy/proxy_resolver_js_bindings_unittest.cc

Issue 2833021: Add an additional per-request DNS cache when executing FindProxyForURL() from... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Address wtc's comments Created 10 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/proxy/proxy_resolver_js_bindings_unittest.cc
===================================================================
--- net/proxy/proxy_resolver_js_bindings_unittest.cc (revision 50290)
+++ net/proxy/proxy_resolver_js_bindings_unittest.cc (working copy)
@@ -10,6 +10,7 @@
#include "net/base/net_util.h"
#include "net/base/sys_addrinfo.h"
#include "net/proxy/proxy_resolver_js_bindings.h"
+#include "net/proxy/proxy_resolver_request_context.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
@@ -78,6 +79,33 @@
}
};
+class MockFailingHostResolver : public HostResolver {
+ public:
+ MockFailingHostResolver() : count_(0) {}
+
+ // HostResolver methods:
+ virtual int Resolve(const RequestInfo& info,
+ AddressList* addresses,
+ CompletionCallback* callback,
+ RequestHandle* out_req,
+ const BoundNetLog& net_log) {
+ count_++;
+ return ERR_NAME_NOT_RESOLVED;
+ }
+
+ virtual void CancelRequest(RequestHandle req) {}
+ virtual void AddObserver(Observer* observer) {}
+ virtual void RemoveObserver(Observer* observer) {}
+ virtual void Shutdown() {}
+
+ // Returns the number of times Resolve() has been called.
+ int count() const { return count_; }
+ void ResetCount() { count_ = 0; }
+
+ private:
+ int count_;
+};
+
TEST(ProxyResolverJSBindingsTest, DnsResolve) {
scoped_refptr<MockHostResolver> host_resolver(new MockHostResolver);
@@ -186,5 +214,52 @@
bindings->DnsResolveEx("FOO"));
}
+TEST(ProxyResolverJSBindingsTest, PerRequestDNSCache) {
+ scoped_refptr<MockFailingHostResolver> host_resolver(
+ new MockFailingHostResolver);
+
+ // Get a hold of a DefaultJSBindings* (it is a hidden impl class).
+ scoped_ptr<ProxyResolverJSBindings> bindings(
+ ProxyResolverJSBindings::CreateDefault(host_resolver));
+
+ // Call DnsResolve() 4 times for the same hostname -- this should issue
+ // 4 separate calls to the underlying host resolver, since there is no
+ // current request context.
+ EXPECT_EQ("", bindings->DnsResolve("foo"));
+ EXPECT_EQ("", bindings->DnsResolve("foo"));
+ EXPECT_EQ("", bindings->DnsResolve("foo"));
+ EXPECT_EQ("", bindings->DnsResolve("foo"));
+ EXPECT_EQ(4, host_resolver->count());
+
+ host_resolver->ResetCount();
+
+ // Now setup a per-request context, and try the same experiment -- we
+ // expect the underlying host resolver to receive only 1 request this time,
+ // since it will service the others from the per-request DNS cache.
+ HostCache cache(50,
+ base::TimeDelta::FromMinutes(10),
+ base::TimeDelta::FromMinutes(10));
+ ProxyResolverRequestContext context(NULL, NULL, &cache);
+ bindings->set_current_request_context(&context);
+
+ EXPECT_EQ("", bindings->DnsResolve("foo"));
+ EXPECT_EQ("", bindings->DnsResolve("foo"));
+ EXPECT_EQ("", bindings->DnsResolve("foo"));
+ EXPECT_EQ("", bindings->DnsResolve("foo"));
+ EXPECT_EQ(1, host_resolver->count());
+
+ host_resolver->ResetCount();
+
+ // The "Ex" version shares this same cache, however since the flags
+ // are different it won't reuse this particular entry.
+ EXPECT_EQ("", bindings->DnsResolveEx("foo"));
+ EXPECT_EQ(1, host_resolver->count());
+ EXPECT_EQ("", bindings->DnsResolveEx("foo"));
+ EXPECT_EQ("", bindings->DnsResolveEx("foo"));
+ EXPECT_EQ(1, host_resolver->count());
+
+ bindings->set_current_request_context(NULL);
+}
+
} // namespace
} // namespace net

Powered by Google App Engine
This is Rietveld 408576698