This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include "net/base/expiring_cache.h"
#include <functional>
#include <string>
#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::Pointee;
using testing::StrEq;
namespace net {
namespace {
const int kMaxCacheEntries = 10;
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;
}
};
}
TEST(ExpiringCacheTest, Basic) {
const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
Cache cache(kMaxCacheEntries);
base::TimeTicks now;
EXPECT_EQ(0U, cache.size());
EXPECT_FALSE(cache.Get("entry1", now));
cache.Put("entry1", "test1", now, now + kTTL);
EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1")));
EXPECT_EQ(1U, cache.size());
now += base::TimeDelta::FromSeconds(5);
EXPECT_FALSE(cache.Get("entry2", now));
cache.Put("entry2", "test2", now, now + kTTL);
EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));
EXPECT_EQ(2U, cache.size());
now += base::TimeDelta::FromSeconds(4);
EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1")));
EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));
now += base::TimeDelta::FromSeconds(1);
EXPECT_FALSE(cache.Get("entry1", now));
EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));
EXPECT_EQ(1U, cache.size());
cache.Put("entry1", "test1", now, now + kTTL);
EXPECT_EQ(2U, cache.size());
EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1")));
EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));
now += base::TimeDelta::FromSeconds(10);
EXPECT_FALSE(cache.Get("entry1", now));
EXPECT_FALSE(cache.Get("entry2", now));
}
TEST(ExpiringCacheTest, Compact) {
const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
Cache cache(kMaxCacheEntries);
base::TimeTicks now;
EXPECT_EQ(0U, cache.size());
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, t10 + kTTL);
}
EXPECT_EQ(5U, cache.size());
for (int i = 0; i < 3; ++i) {
std::string name = base::StringPrintf("expired%d", i);
cache.Put(name, "I'm expired.", now, t10);
}
EXPECT_EQ(8U, cache.size());
for (int i = 0; i < 2; ++i) {
std::string name = base::StringPrintf("negative%d", i);
cache.Put(name, "I was never valid.", now, now);
}
EXPECT_EQ(10U, cache.size());
EXPECT_TRUE(ContainsKey(cache.entries_, "valid0"));
EXPECT_TRUE(ContainsKey(cache.entries_, "valid1"));
EXPECT_TRUE(ContainsKey(cache.entries_, "valid2"));
EXPECT_TRUE(ContainsKey(cache.entries_, "valid3"));
EXPECT_TRUE(ContainsKey(cache.entries_, "valid4"));
EXPECT_TRUE(ContainsKey(cache.entries_, "expired0"));
EXPECT_TRUE(ContainsKey(cache.entries_, "expired1"));
EXPECT_TRUE(ContainsKey(cache.entries_, "expired2"));
EXPECT_TRUE(ContainsKey(cache.entries_, "negative0"));
EXPECT_TRUE(ContainsKey(cache.entries_, "negative1"));
cache.max_entries_ = 6;
cache.Compact(now);
EXPECT_EQ(5U, cache.size());
EXPECT_TRUE(ContainsKey(cache.entries_, "valid0"));
EXPECT_TRUE(ContainsKey(cache.entries_, "valid1"));
EXPECT_TRUE(ContainsKey(cache.entries_, "valid2"));
EXPECT_TRUE(ContainsKey(cache.entries_, "valid3"));
EXPECT_TRUE(ContainsKey(cache.entries_, "valid4"));
EXPECT_FALSE(ContainsKey(cache.entries_, "expired0"));
EXPECT_FALSE(ContainsKey(cache.entries_, "expired1"));
EXPECT_FALSE(ContainsKey(cache.entries_, "expired2"));
EXPECT_FALSE(ContainsKey(cache.entries_, "negative0"));
EXPECT_FALSE(ContainsKey(cache.entries_, "negative1"));
cache.max_entries_ = 4;
cache.Compact(now);
EXPECT_EQ(3U, cache.size());
}
TEST(ExpiringCacheTest, SetWithCompact) {
const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
Cache cache(3);
base::TimeTicks now = base::TimeTicks() + kTTL;
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());
EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("test1")));
EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("test2")));
EXPECT_FALSE(cache.Get("expired", now));
cache.Put("test3", "test3", now, now + kTTL);
EXPECT_EQ(3U, cache.size());
EXPECT_FALSE(cache.Get("expired", now));
EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("test1")));
EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("test2")));
EXPECT_THAT(cache.Get("test3", now), Pointee(StrEq("test3")));
cache.Put("test4", "test4", now, now + kTTL);
EXPECT_EQ(3U, cache.size());
cache.Put("test5", "test5", now, now + kTTL);
EXPECT_EQ(3U, cache.size());
EXPECT_THAT(cache.Get("test5", now), Pointee(StrEq("test5")));
}
TEST(ExpiringCacheTest, Clear) {
const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
Cache cache(kMaxCacheEntries);
base::TimeTicks now;
EXPECT_EQ(0U, cache.size());
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();
EXPECT_EQ(0U, cache.size());
}
TEST(ExpiringCacheTest, GetTruncatesExpiredEntries) {
const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
Cache cache(kMaxCacheEntries);
base::TimeTicks now;
EXPECT_EQ(0U, cache.size());
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());
EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("foo1")));
EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("foo2")));
EXPECT_THAT(cache.Get("test3", now), Pointee(StrEq("foo3")));
now += kTTL;
for (int i = 0; i < 5; ++i) {
std::string name = base::StringPrintf("valid%d", i);
cache.Put(name, name, now, now + kTTL);
}
EXPECT_EQ(8U, cache.size());
EXPECT_FALSE(cache.Get("test1", now));
EXPECT_FALSE(cache.Get("test2", now));
EXPECT_EQ(6U, cache.size());
for (int i = 0; i < 5; ++i) {
std::string name = base::StringPrintf("valid%d", i);
EXPECT_THAT(cache.Get(name, now), Pointee(StrEq(name)));
}
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());
cache.Put("test1", "foo1", kNow, kLater);
cache.Put("test2", "foo2", kNow, kLater);
cache.Put("test3", "foo3", kNow, kLater);
EXPECT_EQ(3U, cache.size());
cache.Put("test4", "foo4", kNow, kMuchLater);
cache.Put("test5", "foo5", kNow, kMuchLater);
EXPECT_EQ(5U, cache.size());
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")));
cache.Put("test6", "foo6", kLater, kHeatDeath);
EXPECT_THAT(cache.Get("test6", kLater), Pointee(StrEq("foo6")));
EXPECT_EQ(3U, cache.size());
cache.max_entries_ = 2;
cache.Compact(kMuchLater);
EXPECT_EQ(1U, cache.size());
EXPECT_THAT(cache.Get("test6", kMuchLater), Pointee(StrEq("foo6")));
EXPECT_FALSE(cache.Get("test6", kHeatDeath));
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));
}
}