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