@@ -53,11 +53,7 @@ class FileCacheTest : public CacheTestBase {
5353 kTargetInodeLimit(10 ),
5454 stats_(thread_system_.get()) {
5555 FileCache::InitStats (&stats_);
56- cache_.reset (new FileCache (
57- GTestTempDir (), &file_system_, thread_system_.get (), &worker_,
58- new FileCache::CachePolicy (&mock_timer_, &hasher_, kCleanIntervalMs ,
59- kTargetSize , kTargetInodeLimit ),
60- &stats_, &message_handler_));
56+ ResetFileCache (kCleanIntervalMs , kTargetSize );
6157 disk_checks_ = stats_.GetVariable (FileCache::kDiskChecks );
6258 cleanups_ = stats_.GetVariable (FileCache::kCleanups );
6359 evictions_ = stats_.GetVariable (FileCache::kEvictions );
@@ -69,6 +65,14 @@ class FileCacheTest : public CacheTestBase {
6965 file_system_.set_advance_time_on_update (true , &mock_timer_);
7066 }
7167
68+ void ResetFileCache (int64 clean_interval_ms, int64 target_size_bytes) {
69+ cache_.reset (new FileCache (
70+ GTestTempDir (), &file_system_, thread_system_.get (), &worker_,
71+ new FileCache::CachePolicy (&mock_timer_, &hasher_, clean_interval_ms,
72+ target_size_bytes, kTargetInodeLimit ),
73+ &stats_, &message_handler_));
74+ }
75+
7276 void CheckCleanTimestamp (int64 min_time_ms) {
7377 GoogleString buffer;
7478 file_system_.ReadFile (cache_->clean_time_path_ .c_str (), &buffer,
@@ -278,4 +282,54 @@ TEST_F(FileCacheTest, CheckClean) {
278282 CheckCleanTimestamp (time_ms);
279283}
280284
285+ // Test cleaning doesn't mean deleting everything in the cache.
286+ TEST_F (FileCacheTest, CheckPartialClean) {
287+ // Set the cache capacity to be big enough to hold two entries. We clean down
288+ // to something like 75% full, though, so after cleaning only one entry will
289+ // be left.
290+ const int target_size = StrCat (" Name1" , " Value1" ,
291+ " Name2" , " Value2" ).length ();
292+ ResetFileCache (kCleanIntervalMs , target_size);
293+
294+ CheckPut (" Name1" , " Value1" );
295+ CheckPut (" Name2" , " Value2" );
296+ // Advance time to make sure we clean the old ones first.
297+ mock_timer_.SleepMs (1 );
298+ CheckPut (" Name3" , " Value3" );
299+ mock_timer_.SleepMs (kCleanIntervalMs + 1 );
300+
301+ // The cache is now oversize. Run clean. It deletes entries 1 and 2.
302+ RunClean ();
303+
304+ CheckNotFound (" Name1" ); // cleaned
305+ CheckNotFound (" Name2" ); // cleaned
306+ CheckGet (" Name3" , " Value3" );
307+ }
308+
309+ // Test that if we disable cleaning then cleaning doesn't happen. This is the
310+ // same as CheckPartialClean, with cleaning disabled.
311+ TEST_F (FileCacheTest, CheckPartialCleanWithCleaningDisabled) {
312+ // Set the cache capacity to be big enough to hold two entries. We clean down
313+ // to something like 75% full, though, so after cleaning only one entry will
314+ // be left.
315+ const int target_size = StrCat (" Name1" , " Value1" ,
316+ " Name2" , " Value2" ).length ();
317+ ResetFileCache (FileCache::kDisableCleaning , target_size);
318+
319+ CheckPut (" Name1" , " Value1" );
320+ CheckPut (" Name2" , " Value2" );
321+ // Advance time for consistency with the yes-clean version above.
322+ mock_timer_.SleepMs (1 );
323+ CheckPut (" Name3" , " Value3" );
324+ mock_timer_.SleepMs (kCleanIntervalMs + 1 );
325+
326+ // The cache is now oversize, but we won't clean because that's turned off.
327+ RunClean ();
328+
329+ // Everything is still there.
330+ CheckGet (" Name1" , " Value1" );
331+ CheckGet (" Name2" , " Value2" );
332+ CheckGet (" Name3" , " Value3" );
333+ }
334+
281335} // namespace net_instaweb
0 commit comments