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);
}
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);
}
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);
}
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);
}
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));
}
Aggregations