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

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

Issue 2083643003: DNS: Let requests specify a callback for future cache hits (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Clarify a couple comments. Created 4 years, 4 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') | net/dns/host_resolver.h » ('j') | 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 "base/format_macros.h" 7 #include "base/format_macros.h"
8 #include "base/stl_util.h" 8 #include "base/stl_util.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 EXPECT_FALSE(cache.Lookup(key3, now)); 322 EXPECT_FALSE(cache.Lookup(key3, now));
323 323
324 // |key2| should be chosen for eviction, since it expires sooner. 324 // |key2| should be chosen for eviction, since it expires sooner.
325 cache.Set(key3, entry, now, base::TimeDelta::FromSeconds(10)); 325 cache.Set(key3, entry, now, base::TimeDelta::FromSeconds(10));
326 EXPECT_EQ(2u, cache.size()); 326 EXPECT_EQ(2u, cache.size());
327 EXPECT_TRUE(cache.Lookup(key1, now)); 327 EXPECT_TRUE(cache.Lookup(key1, now));
328 EXPECT_FALSE(cache.Lookup(key2, now)); 328 EXPECT_FALSE(cache.Lookup(key2, now));
329 EXPECT_TRUE(cache.Lookup(key3, now)); 329 EXPECT_TRUE(cache.Lookup(key3, now));
330 } 330 }
331 331
332 void TestEvictionCallback(int* evict_count,
333 HostCache::Key* key_out,
334 const HostCache::Key& key,
335 const HostCache::Entry& entry) {
336 ++*evict_count;
337 *key_out = key;
338 }
339
340 // Try to add too many entries to cache; it should evict the one with the oldest
341 // expiration time.
342 TEST(HostCacheTest, EvictWithCallback) {
343 HostCache cache(2);
344
345 int evict_count = 0;
346 HostCache::Key evicted_key = Key("nothingevicted.com");
347 cache.set_eviction_callback(
348 base::Bind(&TestEvictionCallback, &evict_count, &evicted_key));
349
350 base::TimeTicks now;
351
352 HostCache::Key key1 = Key("foobar.com");
353 HostCache::Key key2 = Key("foobar2.com");
354 HostCache::Key key3 = Key("foobar3.com");
355 HostCache::Entry entry = HostCache::Entry(OK, AddressList());
356
357 EXPECT_EQ(0u, cache.size());
358 EXPECT_FALSE(cache.Lookup(key1, now));
359 EXPECT_FALSE(cache.Lookup(key2, now));
360 EXPECT_FALSE(cache.Lookup(key3, now));
361
362 // |key1| expires in 10 seconds, but |key2| in just 5.
363 cache.Set(key1, entry, now, base::TimeDelta::FromSeconds(10));
364 cache.Set(key2, entry, now, base::TimeDelta::FromSeconds(5));
365 EXPECT_EQ(2u, cache.size());
366 EXPECT_TRUE(cache.Lookup(key1, now));
367 EXPECT_TRUE(cache.Lookup(key2, now));
368 EXPECT_FALSE(cache.Lookup(key3, now));
369
370 EXPECT_EQ(0, evict_count);
371
372 // |key2| should be chosen for eviction, since it expires sooner.
373 cache.Set(key3, entry, now, base::TimeDelta::FromSeconds(10));
374 EXPECT_EQ(2u, cache.size());
375 EXPECT_TRUE(cache.Lookup(key1, now));
376 EXPECT_FALSE(cache.Lookup(key2, now));
377 EXPECT_TRUE(cache.Lookup(key3, now));
378
379 EXPECT_EQ(1, evict_count);
380 EXPECT_EQ(key2.hostname, evicted_key.hostname);
381 }
382
332 // Try to retrieve stale entries from the cache. They should be returned by 383 // Try to retrieve stale entries from the cache. They should be returned by
333 // |LookupStale()| but not |Lookup()|, with correct |EntryStaleness| data. 384 // |LookupStale()| but not |Lookup()|, with correct |EntryStaleness| data.
334 TEST(HostCacheTest, Stale) { 385 TEST(HostCacheTest, Stale) {
335 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); 386 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
336 387
337 HostCache cache(kMaxCacheEntries); 388 HostCache cache(kMaxCacheEntries);
338 389
339 // Start at t=0. 390 // Start at t=0.
340 base::TimeTicks now; 391 base::TimeTicks now;
341 HostCache::EntryStaleness stale; 392 HostCache::EntryStaleness stale;
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 EXPECT_FALSE(key1 < key2); 529 EXPECT_FALSE(key1 < key2);
479 EXPECT_TRUE(key2 < key1); 530 EXPECT_TRUE(key2 < key1);
480 break; 531 break;
481 default: 532 default:
482 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; 533 FAIL() << "Invalid expectation. Can be only -1, 0, 1";
483 } 534 }
484 } 535 }
485 } 536 }
486 537
487 } // namespace net 538 } // namespace net
OLDNEW
« no previous file with comments | « net/dns/host_cache.cc ('k') | net/dns/host_resolver.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698