Search in sources :

Example 1 with MemoryTrimType

use of com.facebook.common.memory.MemoryTrimType in project fresco by facebook.

the class CountingMemoryCacheTest method testTrimming.

@Test
public void testTrimming() {
    MemoryTrimType memoryTrimType = MemoryTrimType.OnCloseToDalvikHeapLimit;
    mParams = new MemoryCacheParams(1100, 10, 1100, 10, 110);
    when(mParamsSupplier.get()).thenReturn(mParams);
    PowerMockito.when(SystemClock.uptimeMillis()).thenReturn(CountingMemoryCache.PARAMS_INTERCHECK_INTERVAL_MS);
    InOrder inOrder = inOrder(mReleaser);
    // create original references
    CloseableReference<Integer>[] originalRefs = new CloseableReference[10];
    for (int i = 0; i < 10; i++) {
        originalRefs[i] = newReference(100 + i);
    }
    // cache items & close the original references
    CloseableReference<Integer>[] cachedRefs = new CloseableReference[10];
    for (int i = 0; i < 10; i++) {
        cachedRefs[i] = mCache.cache(KEYS[i], originalRefs[i]);
        originalRefs[i].close();
    }
    // cache should keep alive the items until evicted
    inOrder.verify(mReleaser, never()).release(anyInt());
    // trimming cannot evict shared entries
    when(mCacheTrimStrategy.getTrimRatio(memoryTrimType)).thenReturn(1.00);
    mCache.trim(memoryTrimType);
    assertSharedWithCount(KEYS[0], 100, 1);
    assertSharedWithCount(KEYS[1], 101, 1);
    assertSharedWithCount(KEYS[2], 102, 1);
    assertSharedWithCount(KEYS[3], 103, 1);
    assertSharedWithCount(KEYS[4], 104, 1);
    assertSharedWithCount(KEYS[5], 105, 1);
    assertSharedWithCount(KEYS[6], 106, 1);
    assertSharedWithCount(KEYS[7], 107, 1);
    assertSharedWithCount(KEYS[8], 108, 1);
    assertSharedWithCount(KEYS[9], 109, 1);
    assertTotalSize(10, 1045);
    assertExclusivelyOwnedSize(0, 0);
    // close 7 client references
    cachedRefs[8].close();
    cachedRefs[2].close();
    cachedRefs[7].close();
    cachedRefs[3].close();
    cachedRefs[6].close();
    cachedRefs[4].close();
    cachedRefs[5].close();
    assertSharedWithCount(KEYS[0], 100, 1);
    assertSharedWithCount(KEYS[1], 101, 1);
    assertSharedWithCount(KEYS[9], 109, 1);
    assertExclusivelyOwned(KEYS[8], 108);
    assertExclusivelyOwned(KEYS[2], 102);
    assertExclusivelyOwned(KEYS[7], 107);
    assertExclusivelyOwned(KEYS[3], 103);
    assertExclusivelyOwned(KEYS[6], 106);
    assertExclusivelyOwned(KEYS[4], 104);
    assertExclusivelyOwned(KEYS[5], 105);
    assertTotalSize(10, 1045);
    assertExclusivelyOwnedSize(7, 735);
    // Trim cache by 45%. This means that out of total of 1045 bytes cached, 574 should remain.
    // 310 bytes is used by the clients, which leaves 264 for the exclusively owned items.
    // Only the two most recent exclusively owned items fit, and they occupy 209 bytes.
    when(mCacheTrimStrategy.getTrimRatio(memoryTrimType)).thenReturn(0.45);
    mCache.trim(memoryTrimType);
    assertSharedWithCount(KEYS[0], 100, 1);
    assertSharedWithCount(KEYS[1], 101, 1);
    assertSharedWithCount(KEYS[9], 109, 1);
    assertExclusivelyOwned(KEYS[4], 104);
    assertExclusivelyOwned(KEYS[5], 105);
    assertNotCached(KEYS[8], 108);
    assertNotCached(KEYS[2], 102);
    assertNotCached(KEYS[7], 107);
    assertNotCached(KEYS[3], 103);
    assertNotCached(KEYS[6], 106);
    assertTotalSize(5, 519);
    assertExclusivelyOwnedSize(2, 209);
    inOrder.verify(mReleaser).release(108);
    inOrder.verify(mReleaser).release(102);
    inOrder.verify(mReleaser).release(107);
    inOrder.verify(mReleaser).release(103);
    inOrder.verify(mReleaser).release(106);
    // Full trim. All exclusively owned items should be evicted.
    when(mCacheTrimStrategy.getTrimRatio(memoryTrimType)).thenReturn(1.00);
    mCache.trim(memoryTrimType);
    assertSharedWithCount(KEYS[0], 100, 1);
    assertSharedWithCount(KEYS[1], 101, 1);
    assertSharedWithCount(KEYS[9], 109, 1);
    assertNotCached(KEYS[8], 108);
    assertNotCached(KEYS[2], 102);
    assertNotCached(KEYS[7], 107);
    assertNotCached(KEYS[3], 103);
    assertNotCached(KEYS[6], 106);
    assertNotCached(KEYS[6], 104);
    assertNotCached(KEYS[6], 105);
    assertTotalSize(3, 310);
    assertExclusivelyOwnedSize(0, 0);
    inOrder.verify(mReleaser).release(104);
    inOrder.verify(mReleaser).release(105);
}
Also used : MemoryTrimType(com.facebook.common.memory.MemoryTrimType) InOrder(org.mockito.InOrder) CloseableReference(com.facebook.common.references.CloseableReference) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

MemoryTrimType (com.facebook.common.memory.MemoryTrimType)1 CloseableReference (com.facebook.common.references.CloseableReference)1 Test (org.junit.Test)1 InOrder (org.mockito.InOrder)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1