Search in sources :

Example 6 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 7 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 8 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 9 with SimpleCacheKey

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

Example 10 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)25 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 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Before (org.junit.Before)4 BinaryResource (com.facebook.binaryresource.BinaryResource)3 WriterCallback (com.facebook.cache.common.WriterCallback)3 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)3 Uri (android.net.Uri)2 Predicate (com.android.internal.util.Predicate)2 BitmapMemoryCacheKey (com.facebook.imagepipeline.cache.BitmapMemoryCacheKey)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 MemoryCacheParams (com.facebook.imagepipeline.cache.MemoryCacheParams)1 CloseableImage (com.facebook.imagepipeline.image.CloseableImage)1 FakeClock (com.facebook.imagepipeline.testing.FakeClock)1