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

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

Issue 1908543002: DNS: Retain stale entries in HostCache and return when requested (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and rearrange .h slightly per sleevi's suggestion. Created 4 years, 7 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
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 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 EXPECT_EQ(3U, cache.size()); 246 EXPECT_EQ(3U, cache.size());
247 247
248 // Even though the hostnames were the same, we should have two unique 248 // Even though the hostnames were the same, we should have two unique
249 // entries (because the HostResolverFlags differ). 249 // entries (because the HostResolverFlags differ).
250 EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now)); 250 EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now));
251 EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key3, now)); 251 EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key3, now));
252 EXPECT_NE(cache.Lookup(key2, now), cache.Lookup(key3, now)); 252 EXPECT_NE(cache.Lookup(key2, now), cache.Lookup(key3, now));
253 } 253 }
254 254
255 TEST(HostCacheTest, NoCache) { 255 TEST(HostCacheTest, NoCache) {
256 // Disable caching.
257 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); 256 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
258 257
258 // Disable caching.
259 HostCache cache(0); 259 HostCache cache(0);
260 EXPECT_TRUE(cache.caching_is_disabled()); 260 EXPECT_TRUE(cache.caching_is_disabled());
261 261
262 // Set t=0. 262 // Set t=0.
263 base::TimeTicks now; 263 base::TimeTicks now;
264 264
265 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); 265 HostCache::Entry entry = HostCache::Entry(OK, AddressList());
266 266
267 // Lookup and Set should have no effect. 267 // Lookup and Set should have no effect.
268 EXPECT_FALSE(cache.Lookup(Key("foobar.com"),now)); 268 EXPECT_FALSE(cache.Lookup(Key("foobar.com"),now));
(...skipping 20 matching lines...) Expand all
289 cache.Set(Key("foobar2.com"), entry, now, kTTL); 289 cache.Set(Key("foobar2.com"), entry, now, kTTL);
290 cache.Set(Key("foobar3.com"), entry, now, kTTL); 290 cache.Set(Key("foobar3.com"), entry, now, kTTL);
291 291
292 EXPECT_EQ(3u, cache.size()); 292 EXPECT_EQ(3u, cache.size());
293 293
294 cache.clear(); 294 cache.clear();
295 295
296 EXPECT_EQ(0u, cache.size()); 296 EXPECT_EQ(0u, cache.size());
297 } 297 }
298 298
299 TEST(HostCacheTest, Evict) {
Randy Smith (Not in Mondays) 2016/05/04 18:15:25 Any chance you'd be willing to add a description o
Julia Tuttle 2016/05/05 14:35:53 Oh! Sure.
300 HostCache cache(2);
301
302 base::TimeTicks now;
303
304 HostCache::Key key1 = Key("foobar.com");
305 HostCache::Key key2 = Key("foobar2.com");
306 HostCache::Key key3 = Key("foobar3.com");
307 HostCache::Entry entry = HostCache::Entry(OK, AddressList());
308
309 EXPECT_EQ(0u, cache.size());
310 EXPECT_FALSE(cache.Lookup(key1, now));
311 EXPECT_FALSE(cache.Lookup(key2, now));
312 EXPECT_FALSE(cache.Lookup(key3, now));
313
314 // |key1| expires in 10 seconds, but |key2| in just 5.
315 cache.Set(key1, entry, now, base::TimeDelta::FromSeconds(10));
316 cache.Set(key2, entry, now, base::TimeDelta::FromSeconds(5));
317 EXPECT_EQ(2u, cache.size());
318 EXPECT_TRUE(cache.Lookup(key1, now));
319 EXPECT_TRUE(cache.Lookup(key2, now));
320 EXPECT_FALSE(cache.Lookup(key3, now));
321
322 // |key2| should be chosen for eviction, since it expires sooner.
323 cache.Set(key3, entry, now, base::TimeDelta::FromSeconds(10));
324 EXPECT_EQ(2u, cache.size());
325 EXPECT_TRUE(cache.Lookup(key1, now));
326 EXPECT_FALSE(cache.Lookup(key2, now));
327 EXPECT_TRUE(cache.Lookup(key3, now));
328 }
329
330 TEST(HostCacheTest, Stale) {
331 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
332
333 HostCache cache(kMaxCacheEntries);
334
335 // Start at t=0.
336 base::TimeTicks now;
337 HostCache::EntryStaleness stale;
338
339 HostCache::Key key = Key("foobar.com");
340 HostCache::Entry entry = HostCache::Entry(OK, AddressList());
341
342 EXPECT_EQ(0U, cache.size());
343
344 // Add an entry for "foobar.com" at t=0.
345 EXPECT_FALSE(cache.Lookup(key, now));
346 EXPECT_FALSE(cache.LookupStale(key, now, &stale));
347 cache.Set(key, entry, now, kTTL);
348 EXPECT_TRUE(cache.Lookup(key, now));
349 EXPECT_TRUE(cache.LookupStale(key, now, &stale));
350 EXPECT_FALSE(stale.is_stale());
351 EXPECT_EQ(0, stale.stale_hits);
352
353 EXPECT_EQ(1U, cache.size());
354
355 // Advance to t=5.
356 now += base::TimeDelta::FromSeconds(5);
357
358 EXPECT_TRUE(cache.Lookup(key, now));
359 EXPECT_TRUE(cache.LookupStale(key, now, &stale));
360 EXPECT_FALSE(stale.is_stale());
361 EXPECT_EQ(0, stale.stale_hits);
362
363 // Advance to t=15.
364 now += base::TimeDelta::FromSeconds(10);
365
366 EXPECT_FALSE(cache.Lookup(key, now));
367 EXPECT_TRUE(cache.LookupStale(key, now, &stale));
368 EXPECT_TRUE(stale.is_stale());
369 EXPECT_EQ(base::TimeDelta::FromSeconds(5), stale.expired_by);
370 EXPECT_EQ(0, stale.network_changes);
371 EXPECT_EQ(1, stale.stale_hits);
372
373 // Advance to t=20.
374 now += base::TimeDelta::FromSeconds(5);
375
376 EXPECT_FALSE(cache.Lookup(key, now));
377 EXPECT_TRUE(cache.LookupStale(key, now, &stale));
378 EXPECT_TRUE(stale.is_stale());
379 EXPECT_EQ(base::TimeDelta::FromSeconds(10), stale.expired_by);
380 EXPECT_EQ(0, stale.network_changes);
381 EXPECT_EQ(2, stale.stale_hits);
382
383 // Simulate network change.
384 cache.OnNetworkChange();
385
386 EXPECT_FALSE(cache.Lookup(key, now));
387 EXPECT_TRUE(cache.LookupStale(key, now, &stale));
388 EXPECT_TRUE(stale.is_stale());
389 EXPECT_EQ(base::TimeDelta::FromSeconds(10), stale.expired_by);
390 EXPECT_EQ(1, stale.network_changes);
391 EXPECT_EQ(3, stale.stale_hits);
392 }
393
299 // Tests the less than and equal operators for HostCache::Key work. 394 // Tests the less than and equal operators for HostCache::Key work.
300 TEST(HostCacheTest, KeyComparators) { 395 TEST(HostCacheTest, KeyComparators) {
301 struct { 396 struct {
302 // Inputs. 397 // Inputs.
303 HostCache::Key key1; 398 HostCache::Key key1;
304 HostCache::Key key2; 399 HostCache::Key key2;
305 400
306 // Expectation. 401 // Expectation.
307 // -1 means key1 is less than key2 402 // -1 means key1 is less than key2
308 // 0 means key1 equals key2 403 // 0 means key1 equals key2
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 EXPECT_FALSE(key1 < key2); 474 EXPECT_FALSE(key1 < key2);
380 EXPECT_TRUE(key2 < key1); 475 EXPECT_TRUE(key2 < key1);
381 break; 476 break;
382 default: 477 default:
383 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; 478 FAIL() << "Invalid expectation. Can be only -1, 0, 1";
384 } 479 }
385 } 480 }
386 } 481 }
387 482
388 } // namespace net 483 } // namespace net
OLDNEW
« net/dns/host_cache.cc ('K') | « net/dns/host_cache.cc ('k') | net/log/net_log_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698