Basic verification on the cache behavior for entries which don't expire during the test.
| 194 | // Basic verification on the cache behavior for entries which don't expire |
| 195 | // during the test. |
| 196 | TEST_F(TTLCacheTest, BasicNoExpiration) { |
| 197 | const auto entry_ttl = MonoDelta::FromSeconds(300); |
| 198 | |
| 199 | TTLTestCache cache(1, entry_ttl); |
| 200 | { |
| 201 | unique_ptr<TTLCacheTestMetrics> metrics( |
| 202 | new TTLCacheTestMetrics(metric_entity_)); |
| 203 | cache.SetMetrics(std::move(metrics)); |
| 204 | } |
| 205 | |
| 206 | // Try to fetch an entry from an empty cache. |
| 207 | ASSERT_FALSE(cache.Get("key0")); |
| 208 | ASSERT_EQ(0, GetCacheHits()); |
| 209 | ASSERT_EQ(1, GetCacheLookups()); |
| 210 | ASSERT_EQ(1, GetCacheMisses()); |
| 211 | ASSERT_EQ(0, GetCacheUsage()); |
| 212 | |
| 213 | { |
| 214 | // Verify how Put() works. |
| 215 | auto put_h(cache.Put("key0", unique_ptr<TestValue>(new TestValue), 1)); |
| 216 | NO_FATALS(VerifyEntryValue(put_h, 0)); |
| 217 | |
| 218 | { |
| 219 | // Once put in the cache and not yet evicted, the entry must be available |
| 220 | // for retrieval from the cache. |
| 221 | auto h(cache.Get("key0")); |
| 222 | NO_FATALS(VerifyEntryValue(h, 0);); |
| 223 | ASSERT_EQ(2, GetCacheLookups()); |
| 224 | ASSERT_EQ(1, GetCacheHits()); |
| 225 | // That's the same entry. |
| 226 | ASSERT_EQ(&put_h.value(), &h.value()); |
| 227 | } |
| 228 | |
| 229 | // One more entry is added, but since the cache was at capacity ... |
| 230 | auto h1 = cache.Put("key1", unique_ptr<TestValue>(new TestValue(1)), 1); |
| 231 | // However, since the 'put_h' handle still references the entry, it should |
| 232 | // not be freed yet. |
| 233 | ASSERT_EQ(0, GetCacheEvictions()); |
| 234 | NO_FATALS(VerifyEntryValue(h1, 1)); |
| 235 | // .. the formerly cached entry should no longer be available. |
| 236 | ASSERT_FALSE(cache.Get("key0")); |
| 237 | // One extra miss should be registered. |
| 238 | ASSERT_EQ(2, GetCacheMisses()); |
| 239 | // No new hits. |
| 240 | ASSERT_EQ(1, GetCacheHits()); |
| 241 | |
| 242 | // However, since the reference to the entry is still present in the scope, |
| 243 | // it's still available via that reference (but not from the cache as is). |
| 244 | NO_FATALS(VerifyEntryValue(put_h, 0)); |
| 245 | ASSERT_EQ(2, GetCacheUsage()); |
| 246 | } |
| 247 | // Once 'put_h' handle is out of scope, the earlier removed entry should be |
| 248 | // freed. |
| 249 | ASSERT_EQ(1, GetCacheEvictions()); |
| 250 | ASSERT_EQ(1, GetCacheUsage()); |
| 251 | |
| 252 | // Have multiple handles for the same entry in the cache in one scope |
| 253 | // and make sure the reference counting in the underlying cache works |
nothing calls this directly
no test coverage detected