@Test
  public void testCacheFileWithIOException() throws IOException {
    CacheKey key1 = new SimpleCacheKey("aaa");

    // Before inserting, make sure files not exist.
    final BinaryResource resource1 = getResource(key1);
    assertNull(resource1);

    // 1. Should not create cache files if IOException happens in the middle.
    final IOException writeException = new IOException();
    try {
      mCache.insert(
          key1,
          new WriterCallback() {
            @Override
            public void write(OutputStream os) throws IOException {
              throw writeException;
            }
          });
      fail();
    } catch (IOException e) {
      assertNull(getResource(key1));
    }

    verifyListenerOnWriteAttempt(key1);
    verifyListenerOnWriteException(key1, writeException);

    // 2. Test a read failure from DiskStorage
    CacheKey key2 = new SimpleCacheKey("bbb");
    int value2Size = 42;
    byte[] value2 = new byte[value2Size];
    value2[25] = 'b';
    mCache.insert(key2, WriterCallbacks.from(value2));

    verifyListenerOnWriteAttempt(key2);
    String resourceId2 = verifyListenerOnWriteSuccessAndGetResourceId(key2, value2Size);

    ((DiskStorageWithReadFailures) mStorage).setPoisonResourceId(resourceId2);

    assertNull(mCache.getResource(key2));
    verifyListenerOnReadException(key2, DiskStorageWithReadFailures.POISON_EXCEPTION);

    assertFalse(mCache.probe(key2));
    verifyListenerOnReadException(key2, DiskStorageWithReadFailures.POISON_EXCEPTION);

    verifyNoMoreInteractions(mCacheEventListener);
  }
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