Search in sources :

Example 6 with CacheKey

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

the class DiskStorageCacheTest method testCacheEventListener.

@Test
public void testCacheEventListener() throws Exception {
    // 1. Add first cache file
    CacheKey key1 = new SimpleCacheKey("foo");
    int value1Size = 101;
    byte[] value1 = new byte[value1Size];
    // just so it's not all zeros for the equality test below.
    value1[80] = 'c';
    BinaryResource resource1 = mCache.insert(key1, WriterCallbacks.from(value1));
    verifyListenerOnWriteAttempt(key1);
    String resourceId1 = verifyListenerOnWriteSuccessAndGetResourceId(key1, value1Size);
    BinaryResource resource1Again = mCache.getResource(key1);
    assertEquals(resource1, resource1Again);
    verifyListenerOnHit(key1, resourceId1);
    BinaryResource resource1Again2 = mCache.getResource(key1);
    assertEquals(resource1, resource1Again2);
    verifyListenerOnHit(key1, resourceId1);
    SimpleCacheKey missingKey = new SimpleCacheKey("nonexistent_key");
    BinaryResource res2 = mCache.getResource(missingKey);
    assertNull(res2);
    verifyListenerOnMiss(missingKey);
    mCache.clearAll();
    verify(mCacheEventListener).onCleared();
    verifyNoMoreInteractions(mCacheEventListener);
}
Also used : BinaryResource(com.facebook.binaryresource.BinaryResource) 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)

Example 7 with CacheKey

use of com.facebook.cache.common.CacheKey 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 8 with CacheKey

use of com.facebook.cache.common.CacheKey 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 9 with CacheKey

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

the class DiskStorageCacheTest method testHasKeyWithoutPopulateAtStartupWithAwaitingIndex.

@Test
public void testHasKeyWithoutPopulateAtStartupWithAwaitingIndex() throws Exception {
    // A new cache object in the same directory. Equivalent to a process restart.
    // Index may not yet updated.
    DiskStorageCache cache2 = createDiskCache(mStorage, false);
    CacheKey key = putOneThingInCache();
    // Wait for index populated in cache before use of cache
    cache2.awaitIndex();
    assertTrue(cache2.isIndexReady());
    assertTrue(cache2.hasKey(key));
    assertTrue(cache2.hasKeySync(key));
}
Also used : 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 10 with CacheKey

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

the class DiskStorageCacheTest method testWithMultiCacheKeys.

@Test
public void testWithMultiCacheKeys() throws Exception {
    CacheKey insertKey1 = new SimpleCacheKey("foo");
    byte[] value1 = new byte[101];
    // just so it's not all zeros for the equality test below.
    value1[50] = 'a';
    mCache.insert(insertKey1, WriterCallbacks.from(value1));
    List<CacheKey> keys1 = new ArrayList<>(2);
    keys1.add(new SimpleCacheKey("bar"));
    keys1.add(new SimpleCacheKey("foo"));
    MultiCacheKey matchingMultiKey = new MultiCacheKey(keys1);
    assertArrayEquals(value1, getContents(mCache.getResource(matchingMultiKey)));
    List<CacheKey> keys2 = new ArrayList<>(2);
    keys2.add(new SimpleCacheKey("one"));
    keys2.add(new SimpleCacheKey("two"));
    MultiCacheKey insertKey2 = new MultiCacheKey(keys2);
    byte[] value2 = new byte[101];
    // just so it's not all zeros for the equality test below.
    value1[50] = 'b';
    mCache.insert(insertKey2, WriterCallbacks.from(value2));
    CacheKey matchingSimpleKey = new SimpleCacheKey("one");
    assertArrayEquals(value2, getContents(mCache.getResource(matchingSimpleKey)));
}
Also used : MultiCacheKey(com.facebook.cache.common.MultiCacheKey) ArrayList(java.util.ArrayList) 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

CacheKey (com.facebook.cache.common.CacheKey)49 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)31 MultiCacheKey (com.facebook.cache.common.MultiCacheKey)29 Test (org.junit.Test)22 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)19 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)10 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)8 ArrayList (java.util.ArrayList)8 CloseableImage (com.facebook.imagepipeline.image.CloseableImage)7 BinaryResource (com.facebook.binaryresource.BinaryResource)5 BitmapMemoryCacheKey (com.facebook.imagepipeline.cache.BitmapMemoryCacheKey)5 File (java.io.File)5 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Before (org.junit.Before)4 Uri (android.net.Uri)3 FileBinaryResource (com.facebook.binaryresource.FileBinaryResource)3 WriterCallback (com.facebook.cache.common.WriterCallback)3 CloseableReference (com.facebook.common.references.CloseableReference)3 IOException (java.io.IOException)3