| Index: net/base/expiring_cache_unittest.cc
|
| diff --git a/net/base/expiring_cache_unittest.cc b/net/base/expiring_cache_unittest.cc
|
| index 364af81c30f453a80042feff425fb6fcac0c854d..c968cb7bd4610359de8be7d5906043e5b9861d73 100644
|
| --- a/net/base/expiring_cache_unittest.cc
|
| +++ b/net/base/expiring_cache_unittest.cc
|
| @@ -4,8 +4,12 @@
|
|
|
| #include "net/base/expiring_cache.h"
|
|
|
| +#include <functional>
|
| +#include <string>
|
| +
|
| #include "base/stl_util.h"
|
| #include "base/stringprintf.h"
|
| +#include "base/time.h"
|
| #include "testing/gmock/include/gmock/gmock.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| @@ -17,7 +21,15 @@ namespace net {
|
| namespace {
|
|
|
| const int kMaxCacheEntries = 10;
|
| -typedef ExpiringCache<std::string, std::string> Cache;
|
| +typedef ExpiringCache<std::string, std::string, base::TimeTicks,
|
| + std::less<base::TimeTicks> > Cache;
|
| +
|
| +struct TestFunctor {
|
| + bool operator()(const std::string& now,
|
| + const std::string& expiration) const {
|
| + return now != expiration;
|
| + }
|
| +};
|
|
|
| } // namespace
|
|
|
| @@ -32,7 +44,7 @@ TEST(ExpiringCacheTest, Basic) {
|
|
|
| // Add an entry at t=0
|
| EXPECT_FALSE(cache.Get("entry1", now));
|
| - cache.Put("entry1", "test1", now, kTTL);
|
| + cache.Put("entry1", "test1", now, now + kTTL);
|
| EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1")));
|
| EXPECT_EQ(1U, cache.size());
|
|
|
| @@ -41,7 +53,7 @@ TEST(ExpiringCacheTest, Basic) {
|
|
|
| // Add an entry at t=5.
|
| EXPECT_FALSE(cache.Get("entry2", now));
|
| - cache.Put("entry2", "test2", now, kTTL);
|
| + cache.Put("entry2", "test2", now, now + kTTL);
|
| EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));
|
| EXPECT_EQ(2U, cache.size());
|
|
|
| @@ -62,7 +74,7 @@ TEST(ExpiringCacheTest, Basic) {
|
| EXPECT_EQ(1U, cache.size());
|
|
|
| // Update entry1 so it is no longer expired.
|
| - cache.Put("entry1", "test1", now, kTTL);
|
| + cache.Put("entry1", "test1", now, now + kTTL);
|
|
|
| // Both entries should be retrievable and usable.
|
| EXPECT_EQ(2U, cache.size());
|
| @@ -85,25 +97,25 @@ TEST(ExpiringCacheTest, Compact) {
|
| base::TimeTicks now;
|
| EXPECT_EQ(0U, cache.size());
|
|
|
| - // Add five valid entries at t=10.
|
| + // Add five valid entries at t=10 that expire at t=20.
|
| base::TimeTicks t10 = now + kTTL;
|
| for (int i = 0; i < 5; ++i) {
|
| std::string name = base::StringPrintf("valid%d", i);
|
| - cache.Put(name, "I'm valid!", t10, kTTL); // Expire at t=20.
|
| + cache.Put(name, "I'm valid!", t10, t10 + kTTL);
|
| }
|
| EXPECT_EQ(5U, cache.size());
|
|
|
| - // Add three expired entries at t=10.
|
| + // Add three entries at t=0 that expire at t=10.
|
| for (int i = 0; i < 3; ++i) {
|
| std::string name = base::StringPrintf("expired%d", i);
|
| - cache.Put(name, "I'm expired.", now - kTTL, kTTL); // Expire at t=10.
|
| + cache.Put(name, "I'm expired.", now, t10);
|
| }
|
| EXPECT_EQ(8U, cache.size());
|
|
|
| - // Add two negative (instantly expired) entriies at t=10.
|
| + // Add two negative (instantly expired) entries at t=0 that expire at t=0.
|
| for (int i = 0; i < 2; ++i) {
|
| std::string name = base::StringPrintf("negative%d", i);
|
| - cache.Put(name, "I was never valid.", now, base::TimeDelta::FromSeconds(0));
|
| + cache.Put(name, "I was never valid.", now, now);
|
| }
|
| EXPECT_EQ(10U, cache.size());
|
|
|
| @@ -151,9 +163,9 @@ TEST(ExpiringCacheTest, SetWithCompact) {
|
| // t=10
|
| base::TimeTicks now = base::TimeTicks() + kTTL;
|
|
|
| - cache.Put("test1", "test1", now, kTTL);
|
| - cache.Put("test2", "test2", now, kTTL);
|
| - cache.Put("expired", "expired", now, base::TimeDelta::FromSeconds(0));
|
| + cache.Put("test1", "test1", now, now + kTTL);
|
| + cache.Put("test2", "test2", now, now + kTTL);
|
| + cache.Put("expired", "expired", now, now);
|
|
|
| EXPECT_EQ(3U, cache.size());
|
|
|
| @@ -163,7 +175,7 @@ TEST(ExpiringCacheTest, SetWithCompact) {
|
| EXPECT_FALSE(cache.Get("expired", now));
|
|
|
| // Adding the fourth entry will cause "expired" to be evicted.
|
| - cache.Put("test3", "test3", now, kTTL);
|
| + cache.Put("test3", "test3", now, now + kTTL);
|
| EXPECT_EQ(3U, cache.size());
|
|
|
| EXPECT_FALSE(cache.Get("expired", now));
|
| @@ -173,9 +185,9 @@ TEST(ExpiringCacheTest, SetWithCompact) {
|
|
|
| // Add two more entries. Something should be evicted, however "test5"
|
| // should definitely be in there (since it was last inserted).
|
| - cache.Put("test4", "test4", now, kTTL);
|
| + cache.Put("test4", "test4", now, now + kTTL);
|
| EXPECT_EQ(3U, cache.size());
|
| - cache.Put("test5", "test5", now, kTTL);
|
| + cache.Put("test5", "test5", now, now + kTTL);
|
| EXPECT_EQ(3U, cache.size());
|
| EXPECT_THAT(cache.Get("test5", now), Pointee(StrEq("test5")));
|
| }
|
| @@ -190,9 +202,9 @@ TEST(ExpiringCacheTest, Clear) {
|
| EXPECT_EQ(0U, cache.size());
|
|
|
| // Add three entries.
|
| - cache.Put("test1", "foo", now, kTTL);
|
| - cache.Put("test2", "foo", now, kTTL);
|
| - cache.Put("test3", "foo", now, kTTL);
|
| + cache.Put("test1", "foo", now, now + kTTL);
|
| + cache.Put("test2", "foo", now, now + kTTL);
|
| + cache.Put("test3", "foo", now, now + kTTL);
|
| EXPECT_EQ(3U, cache.size());
|
|
|
| cache.Clear();
|
| @@ -200,7 +212,7 @@ TEST(ExpiringCacheTest, Clear) {
|
| EXPECT_EQ(0U, cache.size());
|
| }
|
|
|
| -TEST(ExpiringCache, GetTruncatesExpiredEntries) {
|
| +TEST(ExpiringCacheTest, GetTruncatesExpiredEntries) {
|
| const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
|
|
|
| Cache cache(kMaxCacheEntries);
|
| @@ -210,9 +222,9 @@ TEST(ExpiringCache, GetTruncatesExpiredEntries) {
|
| EXPECT_EQ(0U, cache.size());
|
|
|
| // Add three entries at t=0.
|
| - cache.Put("test1", "foo1", now, kTTL);
|
| - cache.Put("test2", "foo2", now, kTTL);
|
| - cache.Put("test3", "foo3", now, kTTL);
|
| + cache.Put("test1", "foo1", now, now + kTTL);
|
| + cache.Put("test2", "foo2", now, now + kTTL);
|
| + cache.Put("test3", "foo3", now, now + kTTL);
|
| EXPECT_EQ(3U, cache.size());
|
|
|
| // Ensure the entries were added.
|
| @@ -224,7 +236,7 @@ TEST(ExpiringCache, GetTruncatesExpiredEntries) {
|
| now += kTTL;
|
| for (int i = 0; i < 5; ++i) {
|
| std::string name = base::StringPrintf("valid%d", i);
|
| - cache.Put(name, name, now, kTTL); // Expire at t=20.
|
| + cache.Put(name, name, now, now + kTTL); // Expire at t=20.
|
| }
|
| EXPECT_EQ(8U, cache.size());
|
|
|
| @@ -242,4 +254,58 @@ TEST(ExpiringCache, GetTruncatesExpiredEntries) {
|
| EXPECT_EQ(6U, cache.size());
|
| }
|
|
|
| +TEST(ExpiringCacheTest, CustomFunctor) {
|
| + ExpiringCache<std::string, std::string, std::string, TestFunctor> cache(5);
|
| +
|
| + const std::string kNow("Now");
|
| + const std::string kLater("A little bit later");
|
| + const std::string kMuchLater("Much later");
|
| + const std::string kHeatDeath("The heat death of the universe");
|
| +
|
| + EXPECT_EQ(0u, cache.size());
|
| +
|
| + // Add three entries at t=kNow that expire at kLater.
|
| + cache.Put("test1", "foo1", kNow, kLater);
|
| + cache.Put("test2", "foo2", kNow, kLater);
|
| + cache.Put("test3", "foo3", kNow, kLater);
|
| + EXPECT_EQ(3U, cache.size());
|
| +
|
| + // Add two entries at t=kNow that expire at kMuchLater
|
| + cache.Put("test4", "foo4", kNow, kMuchLater);
|
| + cache.Put("test5", "foo5", kNow, kMuchLater);
|
| + EXPECT_EQ(5U, cache.size());
|
| +
|
| + // Ensure the entries were added.
|
| + EXPECT_THAT(cache.Get("test1", kNow), Pointee(StrEq("foo1")));
|
| + EXPECT_THAT(cache.Get("test2", kNow), Pointee(StrEq("foo2")));
|
| + EXPECT_THAT(cache.Get("test3", kNow), Pointee(StrEq("foo3")));
|
| + EXPECT_THAT(cache.Get("test4", kNow), Pointee(StrEq("foo4")));
|
| + EXPECT_THAT(cache.Get("test5", kNow), Pointee(StrEq("foo5")));
|
| +
|
| + // Add one entry at t=kLater that expires at kHeatDeath, which will expire
|
| + // one of test1-3.
|
| + cache.Put("test6", "foo6", kLater, kHeatDeath);
|
| + EXPECT_THAT(cache.Get("test6", kLater), Pointee(StrEq("foo6")));
|
| + EXPECT_EQ(3U, cache.size());
|
| +
|
| + // Now compact at kMuchLater, which should remove all but "test6".
|
| + cache.max_entries_ = 2;
|
| + cache.Compact(kMuchLater);
|
| +
|
| + EXPECT_EQ(1U, cache.size());
|
| + EXPECT_THAT(cache.Get("test6", kMuchLater), Pointee(StrEq("foo6")));
|
| +
|
| + // Finally, "test6" should not be valid at the end of the universe.
|
| + EXPECT_FALSE(cache.Get("test6", kHeatDeath));
|
| +
|
| + // Because comparison is based on equality, not strict weak ordering, we
|
| + // should be able to add something at kHeatDeath that expires at kMuchLater.
|
| + cache.Put("test7", "foo7", kHeatDeath, kMuchLater);
|
| + EXPECT_EQ(1U, cache.size());
|
| + EXPECT_THAT(cache.Get("test7", kNow), Pointee(StrEq("foo7")));
|
| + EXPECT_THAT(cache.Get("test7", kLater), Pointee(StrEq("foo7")));
|
| + EXPECT_THAT(cache.Get("test7", kHeatDeath), Pointee(StrEq("foo7")));
|
| + EXPECT_FALSE(cache.Get("test7", kMuchLater));
|
| +}
|
| +
|
| } // namespace net
|
|
|