@Test
  public void testCleanOldCache() throws IOException, NoSuchFieldException, IllegalAccessException {
    long cacheExpirationMs = TimeUnit.DAYS.toMillis(5);
    CacheKey key1 = new SimpleCacheKey("aaa");
    int value1Size = 41;
    byte[] value1 = new byte[value1Size];
    value1[25] = 'a';
    mCache.insert(key1, WriterCallbacks.from(value1));

    String resourceId1 = verifyListenerOnWriteSuccessAndGetResourceId(key1, value1Size);

    CacheKey key2 = new SimpleCacheKey("bbb");
    int value2Size = 42;
    byte[] value2 = new byte[value2Size];
    value2[25] = 'b';
    mCache.insert(key2, WriterCallbacks.from(value2));

    String resourceId2 = verifyListenerOnWriteSuccessAndGetResourceId(key2, value2Size);

    // Increment clock by default expiration time + 1 day
    when(mClock.now()).thenReturn(cacheExpirationMs + TimeUnit.DAYS.toMillis(1));

    CacheKey key3 = new SimpleCacheKey("ccc");
    int value3Size = 43;
    byte[] value3 = new byte[value3Size];
    value3[25] = 'c';
    mCache.insert(key3, WriterCallbacks.from(value3));
    long valueAge3 = TimeUnit.HOURS.toMillis(1);
    when(mClock.now()).thenReturn(cacheExpirationMs + TimeUnit.DAYS.toMillis(1) + valueAge3);

    long oldestEntry = mCache.clearOldEntries(cacheExpirationMs);
    assertEquals(valueAge3, oldestEntry);

    assertArrayEquals(value3, getContents(getResource(key3)));
    assertNull(getResource(key1));
    assertNull(getResource(key2));

    String[] resourceIds = new String[] {resourceId1, resourceId2};
    long[] itemSizes = new long[] {value1Size, value2Size};
    long cacheSizeBeforeEviction = value1Size + value2Size + value3Size;
    verifyListenerOnEviction(
        resourceIds,
        itemSizes,
        CacheEventListener.EvictionReason.CONTENT_STALE,
        cacheSizeBeforeEviction);
  }
Tests size based file eviction of cache files. Also tests that unexpected files (which are not
in the format expected by the cache) do not count towards the cache size, and are also evicted
during both evictions (LRU and Old).

@throws Exception
imagepipeline-base/src/test/java/com/facebook/cache/disk/DiskStorageCacheTest.java