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

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

Issue 2701673003: HostCache: always evict stale entries before valid entries (Closed)
Patch Set: Created 3 years, 10 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> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 cache.OnNetworkChange(); 481 cache.OnNetworkChange();
482 482
483 EXPECT_FALSE(cache.Lookup(key, now)); 483 EXPECT_FALSE(cache.Lookup(key, now));
484 EXPECT_TRUE(cache.LookupStale(key, now, &stale)); 484 EXPECT_TRUE(cache.LookupStale(key, now, &stale));
485 EXPECT_TRUE(stale.is_stale()); 485 EXPECT_TRUE(stale.is_stale());
486 EXPECT_EQ(base::TimeDelta::FromSeconds(10), stale.expired_by); 486 EXPECT_EQ(base::TimeDelta::FromSeconds(10), stale.expired_by);
487 EXPECT_EQ(1, stale.network_changes); 487 EXPECT_EQ(1, stale.network_changes);
488 EXPECT_EQ(3, stale.stale_hits); 488 EXPECT_EQ(3, stale.stale_hits);
489 } 489 }
490 490
491 TEST(HostCacheTest, EvictStale) {
492 HostCache cache(2);
493
494 base::TimeTicks now;
495 HostCache::EntryStaleness stale;
496
497 HostCache::Key key1 = Key("foobar.com");
498 HostCache::Key key2 = Key("foobar2.com");
499 HostCache::Key key3 = Key("foobar3.com");
500 HostCache::Entry entry = HostCache::Entry(OK, AddressList());
501
502 EXPECT_EQ(0u, cache.size());
503 EXPECT_FALSE(cache.Lookup(key1, now));
504 EXPECT_FALSE(cache.Lookup(key2, now));
505 EXPECT_FALSE(cache.Lookup(key3, now));
506
507 // |key1| expires in 10 seconds.
508 cache.Set(key1, entry, now, base::TimeDelta::FromSeconds(10));
509 EXPECT_EQ(1u, cache.size());
510 EXPECT_TRUE(cache.Lookup(key1, now));
511 EXPECT_FALSE(cache.Lookup(key2, now));
512 EXPECT_FALSE(cache.Lookup(key3, now));
513
514 // Simulate network change, expiring the cache.
515 cache.OnNetworkChange();
516
517 EXPECT_EQ(1u, cache.size());
518 EXPECT_FALSE(cache.Lookup(key1, now));
519 EXPECT_TRUE(cache.LookupStale(key1, now, &stale));
520 EXPECT_EQ(1, stale.network_changes);
521
522 // Advance to t=1.
523 now += base::TimeDelta::FromSeconds(1);
524
525 // |key2| expires before |key1| would originally have expired.
526 cache.Set(key2, entry, now, base::TimeDelta::FromSeconds(5));
527 EXPECT_EQ(2u, cache.size());
528 EXPECT_FALSE(cache.Lookup(key1, now));
529 EXPECT_TRUE(cache.LookupStale(key1, now, &stale));
530 EXPECT_TRUE(cache.Lookup(key2, now));
531 EXPECT_FALSE(cache.Lookup(key3, now));
532
533 // |key1| should be chosen for eviction, since it is stale.
534 cache.Set(key3, entry, now, base::TimeDelta::FromSeconds(1));
535 EXPECT_EQ(2u, cache.size());
536 EXPECT_FALSE(cache.Lookup(key1, now));
537 EXPECT_FALSE(cache.LookupStale(key1, now, &stale));
538 EXPECT_TRUE(cache.Lookup(key2, now));
539 EXPECT_TRUE(cache.Lookup(key3, now));
540
541 // Advance to t=6.
542 now += base::TimeDelta::FromSeconds(5);
543
544 // Insert |key1| again. |key3| should be evicted.
545 cache.Set(key1, entry, now, base::TimeDelta::FromSeconds(10));
546 EXPECT_EQ(2u, cache.size());
547 EXPECT_TRUE(cache.Lookup(key1, now));
548 EXPECT_FALSE(cache.Lookup(key2, now));
549 EXPECT_TRUE(cache.LookupStale(key2, now, &stale));
550 EXPECT_FALSE(cache.Lookup(key3, now));
551 EXPECT_FALSE(cache.LookupStale(key3, now, &stale));
552 }
553
491 // Tests the less than and equal operators for HostCache::Key work. 554 // Tests the less than and equal operators for HostCache::Key work.
492 TEST(HostCacheTest, KeyComparators) { 555 TEST(HostCacheTest, KeyComparators) {
493 struct { 556 struct {
494 // Inputs. 557 // Inputs.
495 HostCache::Key key1; 558 HostCache::Key key1;
496 HostCache::Key key2; 559 HostCache::Key key2;
497 560
498 // Expectation. 561 // Expectation.
499 // -1 means key1 is less than key2 562 // -1 means key1 is less than key2
500 // 0 means key1 equals key2 563 // 0 means key1 equals key2
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 EXPECT_FALSE(key1 < key2); 634 EXPECT_FALSE(key1 < key2);
572 EXPECT_TRUE(key2 < key1); 635 EXPECT_TRUE(key2 < key1);
573 break; 636 break;
574 default: 637 default:
575 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; 638 FAIL() << "Invalid expectation. Can be only -1, 0, 1";
576 } 639 }
577 } 640 }
578 } 641 }
579 642
580 } // namespace net 643 } // 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