Search in sources :

Example 1 with WriterCallback

use of com.facebook.cache.common.WriterCallback in project fresco by facebook.

the class DiskStorageCacheTest method testCacheFileWithIOException.

@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);
}
Also used : BinaryResource(com.facebook.binaryresource.BinaryResource) WriterCallback(com.facebook.cache.common.WriterCallback) OutputStream(java.io.OutputStream) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) IOException(java.io.IOException) CacheKey(com.facebook.cache.common.CacheKey) MultiCacheKey(com.facebook.cache.common.MultiCacheKey) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.Test)

Example 2 with WriterCallback

use of com.facebook.cache.common.WriterCallback in project fresco by facebook.

the class DiskStorageCacheTest method testConcurrency.

/**
 * Verify that multiple threads can write to the cache at the same time.
 */
@Test
public void testConcurrency() throws Exception {
    final CyclicBarrier barrier = new CyclicBarrier(3);
    WriterCallback writerCallback = new WriterCallback() {

        @Override
        public void write(OutputStream os) throws IOException {
            try {
                // Both threads will need to hit this barrier. If writing is serialized,
                // the second thread will never reach here as the first will hold
                // the write lock forever.
                barrier.await(10, TimeUnit.SECONDS);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    CacheKey key1 = new SimpleCacheKey("concurrent1");
    CacheKey key2 = new SimpleCacheKey("concurrent2");
    Thread t1 = runInsertionInSeparateThread(key1, writerCallback);
    Thread t2 = runInsertionInSeparateThread(key2, writerCallback);
    barrier.await(10, TimeUnit.SECONDS);
    t1.join(1000);
    t2.join(1000);
}
Also used : WriterCallback(com.facebook.cache.common.WriterCallback) OutputStream(java.io.OutputStream) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) IOException(java.io.IOException) CacheKey(com.facebook.cache.common.CacheKey) MultiCacheKey(com.facebook.cache.common.MultiCacheKey) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) CyclicBarrier(java.util.concurrent.CyclicBarrier) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.Test)

Example 3 with WriterCallback

use of com.facebook.cache.common.WriterCallback in project fresco by facebook.

the class BufferedDiskCache method writeToDiskCache.

/**
 * Writes to disk cache
 *
 * @throws IOException
 */
private void writeToDiskCache(final CacheKey key, final EncodedImage encodedImage) {
    FLog.v(TAG, "About to write to disk-cache for key %s", key.getUriString());
    try {
        mFileCache.insert(key, new WriterCallback() {

            @Override
            public void write(OutputStream os) throws IOException {
                InputStream inputStream = encodedImage.getInputStream();
                Preconditions.checkNotNull(inputStream);
                mPooledByteStreams.copy(inputStream, os);
            }
        });
        mImageCacheStatsTracker.onDiskCachePut(key);
        FLog.v(TAG, "Successful disk-cache write for key %s", key.getUriString());
    } catch (IOException ioe) {
        // Log failure
        // TODO: 3697790
        FLog.w(TAG, ioe, "Failed to write to disk-cache for key %s", key.getUriString());
    }
}
Also used : InputStream(java.io.InputStream) WriterCallback(com.facebook.cache.common.WriterCallback) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 4 with WriterCallback

use of com.facebook.cache.common.WriterCallback in project fresco by facebook.

the class BufferedDiskCacheTest method testWritesToDiskCache.

@Test
public void testWritesToDiskCache() throws Exception {
    mBufferedDiskCache.put(mCacheKey, mEncodedImage);
    reset(mPooledByteBuffer);
    when(mPooledByteBuffer.size()).thenReturn(0);
    final ArgumentCaptor<WriterCallback> wcCapture = ArgumentCaptor.forClass(WriterCallback.class);
    final OutputStream os = mock(OutputStream.class);
    when(mFileCache.insert(eq(mCacheKey), wcCapture.capture())).then(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WriterCallback wc = (WriterCallback) invocation.getArguments()[1];
            wc.write(os);
            return null;
        }
    });
    mWritePriorityExecutor.runUntilIdle();
    // Ref count should be equal to 2 ('owned' by the mCloseableReference and other 'owned' by
    // mEncodedImage)
    assertEquals(2, mCloseableReference.getUnderlyingReferenceTestOnly().getRefCountTestOnly());
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) WriterCallback(com.facebook.cache.common.WriterCallback) OutputStream(java.io.OutputStream) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Example 5 with WriterCallback

use of com.facebook.cache.common.WriterCallback in project fresco by facebook.

the class DiskStorageCacheTest method testSizeEvictionClearsIndex.

@Test
public void testSizeEvictionClearsIndex() throws Exception {
    when(mClock.now()).thenReturn(TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS));
    CacheKey key1 = putOneThingInCache();
    CacheKey key2 = new SimpleCacheKey("bar");
    CacheKey key3 = new SimpleCacheKey("duck");
    byte[] value2 = new byte[(int) FILE_CACHE_MAX_SIZE_HIGH_LIMIT];
    value2[80] = 'c';
    WriterCallback callback = WriterCallbacks.from(value2);
    when(mClock.now()).thenReturn(TimeUnit.MILLISECONDS.convert(2, TimeUnit.DAYS));
    mCache.insert(key2, callback);
    // now over limit. Next write will evict key1
    when(mClock.now()).thenReturn(TimeUnit.MILLISECONDS.convert(3, TimeUnit.DAYS));
    mCache.insert(key3, callback);
    assertFalse(mCache.hasKeySync(key1));
    assertFalse(mCache.hasKey(key1));
    assertTrue(mCache.hasKeySync(key3));
    assertTrue(mCache.hasKey(key3));
}
Also used : WriterCallback(com.facebook.cache.common.WriterCallback) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) CacheKey(com.facebook.cache.common.CacheKey) MultiCacheKey(com.facebook.cache.common.MultiCacheKey) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.Test)

Aggregations

WriterCallback (com.facebook.cache.common.WriterCallback)5 OutputStream (java.io.OutputStream)4 Test (org.junit.Test)4 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)4 CacheKey (com.facebook.cache.common.CacheKey)3 MultiCacheKey (com.facebook.cache.common.MultiCacheKey)3 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)3 IOException (java.io.IOException)3 BinaryResource (com.facebook.binaryresource.BinaryResource)1 InputStream (java.io.InputStream)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1