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

Side by Side Diff: net/dns/host_cache_unittest.cc

Issue 2298173002: Implement host-based deletion for the hostname resolution cache. (Closed)
Patch Set: Take TimeTicks::Now() out of the loop Created 4 years, 3 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 unified diff | Download patch
« no previous file with comments | « net/dns/host_cache.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/dns/host_cache.h" 5 #include "net/dns/host_cache.h"
6 6
7 #include <string>
8
9 #include "base/bind.h"
10 #include "base/callback.h"
7 #include "base/format_macros.h" 11 #include "base/format_macros.h"
8 #include "base/stl_util.h" 12 #include "base/stl_util.h"
9 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
11 #include "net/base/net_errors.h" 15 #include "net/base/net_errors.h"
12 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
13 17
14 namespace net { 18 namespace net {
15 19
16 namespace { 20 namespace {
17 21
18 const int kMaxCacheEntries = 10; 22 const int kMaxCacheEntries = 10;
19 23
20 // Builds a key for |hostname|, defaulting the address family to unspecified. 24 // Builds a key for |hostname|, defaulting the address family to unspecified.
21 HostCache::Key Key(const std::string& hostname) { 25 HostCache::Key Key(const std::string& hostname) {
22 return HostCache::Key(hostname, ADDRESS_FAMILY_UNSPECIFIED, 0); 26 return HostCache::Key(hostname, ADDRESS_FAMILY_UNSPECIFIED, 0);
23 } 27 }
24 28
29 bool FoobarIndexIsOdd(const std::string& foobarx_com) {
30 return (foobarx_com[6] - '0') % 2 == 1;
31 }
32
25 } // namespace 33 } // namespace
26 34
27 TEST(HostCacheTest, Basic) { 35 TEST(HostCacheTest, Basic) {
28 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); 36 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
29 37
30 HostCache cache(kMaxCacheEntries); 38 HostCache cache(kMaxCacheEntries);
31 39
32 // Start at t=0. 40 // Start at t=0.
33 base::TimeTicks now; 41 base::TimeTicks now;
34 42
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 cache.Set(Key("foobar2.com"), entry, now, kTTL); 297 cache.Set(Key("foobar2.com"), entry, now, kTTL);
290 cache.Set(Key("foobar3.com"), entry, now, kTTL); 298 cache.Set(Key("foobar3.com"), entry, now, kTTL);
291 299
292 EXPECT_EQ(3u, cache.size()); 300 EXPECT_EQ(3u, cache.size());
293 301
294 cache.clear(); 302 cache.clear();
295 303
296 EXPECT_EQ(0u, cache.size()); 304 EXPECT_EQ(0u, cache.size());
297 } 305 }
298 306
307 TEST(HostCacheTest, ClearForHosts) {
308 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
309
310 HostCache cache(kMaxCacheEntries);
311
312 // Set t=0.
313 base::TimeTicks now;
314
315 HostCache::Entry entry = HostCache::Entry(OK, AddressList());
316
317 EXPECT_EQ(0u, cache.size());
318
319 // Add several entries.
320 cache.Set(Key("foobar1.com"), entry, now, kTTL);
321 cache.Set(Key("foobar2.com"), entry, now, kTTL);
322 cache.Set(Key("foobar3.com"), entry, now, kTTL);
323 cache.Set(Key("foobar4.com"), entry, now, kTTL);
324 cache.Set(Key("foobar5.com"), entry, now, kTTL);
325
326 EXPECT_EQ(5u, cache.size());
327
328 // Clear the hosts matching a certain predicate, such as the number being odd.
329 cache.ClearForHosts(base::Bind(&FoobarIndexIsOdd));
330
331 EXPECT_EQ(2u, cache.size());
332 EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), now));
333 EXPECT_TRUE(cache.Lookup(Key("foobar4.com"), now));
334
335 // Passing null callback will delete all hosts.
336 cache.ClearForHosts(base::Callback<bool(const std::string&)>());
337
338 EXPECT_EQ(0u, cache.size());
339 }
340
299 // Try to add too many entries to cache; it should evict the one with the oldest 341 // Try to add too many entries to cache; it should evict the one with the oldest
300 // expiration time. 342 // expiration time.
301 TEST(HostCacheTest, Evict) { 343 TEST(HostCacheTest, Evict) {
302 HostCache cache(2); 344 HostCache cache(2);
303 345
304 base::TimeTicks now; 346 base::TimeTicks now;
305 347
306 HostCache::Key key1 = Key("foobar.com"); 348 HostCache::Key key1 = Key("foobar.com");
307 HostCache::Key key2 = Key("foobar2.com"); 349 HostCache::Key key2 = Key("foobar2.com");
308 HostCache::Key key3 = Key("foobar3.com"); 350 HostCache::Key key3 = Key("foobar3.com");
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
529 EXPECT_FALSE(key1 < key2); 571 EXPECT_FALSE(key1 < key2);
530 EXPECT_TRUE(key2 < key1); 572 EXPECT_TRUE(key2 < key1);
531 break; 573 break;
532 default: 574 default:
533 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; 575 FAIL() << "Invalid expectation. Can be only -1, 0, 1";
534 } 576 }
535 } 577 }
536 } 578 }
537 579
538 } // namespace net 580 } // namespace net
OLDNEW
« no previous file with comments | « net/dns/host_cache.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698