| 23 | namespace { |
| 24 | |
| 25 | TEST(ExpiringLRUCacheTest, MaxAge) { |
| 26 | const string key = "a"; |
| 27 | std::unique_ptr<NowSecondsEnv> env(new NowSecondsEnv); |
| 28 | ExpiringLRUCache<int> cache(1, 0, env.get()); |
| 29 | env->SetNowSeconds(1); |
| 30 | // Verify that replacement of an existing element works, and updates the |
| 31 | // timestamp of the entry. |
| 32 | cache.Insert(key, 41); |
| 33 | env->SetNowSeconds(2); |
| 34 | cache.Insert(key, 42); |
| 35 | // 1 second after the most recent insertion, the entry is still valid. |
| 36 | env->SetNowSeconds(3); |
| 37 | int value = 0; |
| 38 | EXPECT_TRUE(cache.Lookup(key, &value)); |
| 39 | EXPECT_EQ(value, 42); |
| 40 | // 2 seconds after the most recent insertion, the entry is no longer valid. |
| 41 | env->SetNowSeconds(4); |
| 42 | EXPECT_FALSE(cache.Lookup(key, &value)); |
| 43 | // Re-insert the entry. |
| 44 | cache.Insert(key, 43); |
| 45 | EXPECT_TRUE(cache.Lookup(key, &value)); |
| 46 | EXPECT_EQ(value, 43); |
| 47 | // The entry is valid 1 second after the insertion... |
| 48 | env->SetNowSeconds(5); |
| 49 | value = 0; |
| 50 | EXPECT_TRUE(cache.Lookup(key, &value)); |
| 51 | EXPECT_EQ(value, 43); |
| 52 | // ...but is no longer valid 2 seconds after the insertion. |
| 53 | env->SetNowSeconds(6); |
| 54 | EXPECT_FALSE(cache.Lookup(key, &value)); |
| 55 | } |
| 56 | |
| 57 | TEST(ExpiringLRUCacheTest, MaxEntries) { |
| 58 | // max_age of 0 means nothing will be cached. |
nothing calls this directly
no test coverage detected