Search in sources :

Example 1 with SimpleCacheKey

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

the class SmallCacheIfRequestedDiskCachePolicyTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    List<CacheKey> keys = new ArrayList<>(1);
    keys.add(new SimpleCacheKey("http://dummy.uri"));
    mCacheKey = new MultiCacheKey(keys);
    when(mCacheKeyFactory.getEncodedCacheKey(mImageRequest, mCallerContext)).thenReturn(mCacheKey);
    when(mImageRequest.getCacheChoice()).thenReturn(ImageRequest.CacheChoice.DEFAULT);
    when(mImageRequest.isDiskCacheEnabled()).thenReturn(true);
    mIsCancelled = new AtomicBoolean(false);
    mSmallCacheIfRequestedDiskCachePolicy = new SmallCacheIfRequestedDiskCachePolicy(mDefaultBufferedDiskCache, mSmallImageBufferedDiskCache, mCacheKeyFactory);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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) Before(org.junit.Before)

Example 2 with SimpleCacheKey

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

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

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

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

the class DiskStorageCacheTest method testVersioning.

/**
 * Test to make sure that the same item stored with two different versions of the cache will be
 * stored with two different file names.
 *
 * @throws UnsupportedEncodingException
 */
@Test
public void testVersioning() throws IOException {
    // Define data that will be written to cache
    CacheKey key = new SimpleCacheKey("version_test");
    byte[] value = new byte[32];
    value[0] = 'v';
    // Set up cache with version == 1
    DiskStorage storage1 = createDiskStorage(TESTCACHE_CURRENT_VERSION);
    DiskStorageCache cache1 = createDiskCache(storage1, false);
    // Write test data to cache 1
    cache1.insert(key, WriterCallbacks.from(value));
    // Get cached file
    BinaryResource resource1 = getResource(storage1, key);
    assertNotNull(resource1);
    // Set up cache with version == 2
    DiskStorage storageSupplier2 = createDiskStorage(TESTCACHE_NEXT_VERSION);
    DiskStorageCache cache2 = createDiskCache(storageSupplier2, false);
    // Write test data to cache 2
    cache2.insert(key, WriterCallbacks.from(value));
    // Get cached file
    BinaryResource resource2 = getResource(storageSupplier2, key);
    assertNotNull(resource2);
    // Make sure filenames of the two file are different
    assertFalse(resource2.equals(resource1));
}
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)

Aggregations

SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)27 CacheKey (com.facebook.cache.common.CacheKey)20 MultiCacheKey (com.facebook.cache.common.MultiCacheKey)19 Test (org.junit.Test)12 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)10 ArrayList (java.util.ArrayList)7 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)6 BinaryResource (com.facebook.binaryresource.BinaryResource)5 Before (org.junit.Before)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 WriterCallback (com.facebook.cache.common.WriterCallback)3 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)3 IOException (java.io.IOException)3 Uri (android.net.Uri)2 FileBinaryResource (com.facebook.binaryresource.FileBinaryResource)2 Predicate (com.facebook.common.internal.Predicate)2 ImageFormat (com.facebook.imageformat.ImageFormat)2 BitmapMemoryCacheKey (com.facebook.imagepipeline.cache.BitmapMemoryCacheKey)2 CloseableImage (com.facebook.imagepipeline.image.CloseableImage)2 File (java.io.File)2