Search in sources :

Example 21 with FakeClock

use of com.facebook.imagepipeline.testing.FakeClock in project fresco by facebook.

the class PriorityNetworkFetcherTest method changePriorityForDelayedRequests.

/**
 * Scenario:
 *
 * <p>The priority of a hi-pri image waiting in the delayedQueue is changed to lowi-pri. We expect
 * it to be re-queued to the low-pri queue.
 */
@Test
public void changePriorityForDelayedRequests() {
    RecordingNetworkFetcher recordingNetworkFetcher = new RecordingNetworkFetcher();
    FakeClock clock = new FakeClock();
    final int delayTime = 100;
    // Hi-pri is LIFO, Max hi-pri: 2, max low-pri: 1
    PriorityNetworkFetcher<FetchState> fetcher = new PriorityNetworkFetcher<>(recordingNetworkFetcher, false, 2, 0, true, 2, false, 0, delayTime, false, false, /* nonRecoverableExceptionPreventsRequeue */
    1, /* maxConnectAttemptCount */
    1, /* maxAttemptCount */
    false, /* retryLowPriAll */
    false, /* retryLowPriUnknownHostException */
    false, /* retryLowPriConnectionException */
    clock);
    // add a hi-pri, it will be fetched immediately.
    PriorityFetchState<FetchState> one = fetch(fetcher, "1", callback, true);
    // simulate a network failure, the request should wait in the delayed queue.
    getOnlyElement(recordingNetworkFetcher.callbacks.get(one.delegatedState)).onFailure(new Exception());
    // Change priority of 'one' to low-pri (while it's waiting in the delayed queue).
    ((SettableProducerContext) one.getContext()).setPriority(LOW);
    // delay + 1 ms
    clock.incrementBy(delayTime + 1);
    // to trigger a dequeue operation
    PriorityFetchState<FetchState> two = fetch(fetcher, "2", callback, true);
    assertThat(fetcher.getDelayedQeueue()).isEmpty();
    assertThat(fetcher.getLowPriQueue()).containsExactly(one);
    assertThat(fetcher.getHiPriQueue()).isEmpty();
    assertThat(fetcher.getCurrentlyFetching()).containsExactly(two);
}
Also used : PriorityFetchState(com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState) FakeClock(com.facebook.imagepipeline.testing.FakeClock) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Test(org.junit.Test)

Example 22 with FakeClock

use of com.facebook.imagepipeline.testing.FakeClock in project fresco by facebook.

the class PriorityNetworkFetcherTest method queueTimeIsReturnedInExtraMap.

@Test
public void queueTimeIsReturnedInExtraMap() {
    FakeClock clock = new FakeClock();
    PriorityNetworkFetcher<FetchState> fetcher = new PriorityNetworkFetcher<>(delegate, false, 1, 0, true, 0, false, NO_DELAYED_REQUESTS, 0, false, false, /* nonRecoverableExceptionPreventsRequeue */
    1, /* maxConnectAttemptCount */
    1, /* maxAttemptCount */
    false, /* retryLowPriAll */
    false, /* retryLowPriUnknownHostException */
    false, /* retryLowPriConnectionException */
    clock);
    // The queue is empty, so enqueuing a request immediately executes it. Therefore, the queue time
    // is 0.
    PriorityFetchState<FetchState> one = fetch(fetcher, "1", callback, true);
    assertThat(fetcher.getExtraMap(one, 123)).containsEntry("pri_queue_time", "0");
    // Enqueueing another fetch. The queue is now full (it allows at most 1 hi-pri concurrent
    // requests), so the request waits.
    PriorityFetchState<FetchState> two = fetch(fetcher, "1", callback, true);
    // 'one' completes, which causes 'two' to be dequeued 43ms after it was enqueued.
    clock.incrementBy(43);
    fetcher.onFetchCompletion(one, 123);
    // Complete 'two' and request its extras map.
    fetcher.onFetchCompletion(two, 123);
    assertThat(fetcher.getExtraMap(two, 123)).containsEntry("pri_queue_time", "43");
}
Also used : PriorityFetchState(com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState) FakeClock(com.facebook.imagepipeline.testing.FakeClock) Test(org.junit.Test)

Example 23 with FakeClock

use of com.facebook.imagepipeline.testing.FakeClock in project fresco by facebook.

the class PriorityNetworkFetcherTest method testMovingDelayedRequeue_requeueOnFail.

/**
 * Scenario: an image fetch fails. We expect it to be re-queued up to maxNumberOfRequeue times.
 * The reuqest should be re-queued immediately up to immediateRequeueCount and then it will wait
 * in the delayedQueue to be re-queued again after delayTimeInMillis.
 */
@Test
public void testMovingDelayedRequeue_requeueOnFail() {
    RecordingNetworkFetcher recordingNetworkFetcher = new RecordingNetworkFetcher();
    final int maxNumberOfRequeue = 3;
    final int immediateRequeueCount = 1;
    final int delayTimeInMillis = 300;
    FakeClock clock = new FakeClock();
    // Max hi-pri: 1, max low-pri: 0
    PriorityNetworkFetcher<FetchState> fetcher = new PriorityNetworkFetcher<>(recordingNetworkFetcher, false, 1, 0, true, maxNumberOfRequeue, false, immediateRequeueCount, delayTimeInMillis, false, false, /* nonRecoverableExceptionPreventsRequeue */
    1, /* maxConnectAttemptCount */
    1, /* maxAttemptCount */
    false, /* retryLowPriAll */
    false, /* retryLowPriUnknownHostException */
    false, /* retryLowPriConnectionException */
    clock);
    PriorityFetchState<FetchState> hipri1 = fetch(fetcher, "hipri1", callback, true);
    assertThat(fetcher.getCurrentlyFetching()).containsExactly(hipri1);
    assertThat(fetcher.getHiPriQueue()).isEmpty();
    assertThat(fetcher.getLowPriQueue()).isEmpty();
    // Simulate 1st failure in hipri1, the request should be requeued immediately.
    getOnlyElement(recordingNetworkFetcher.callbacks.get(hipri1.delegatedState)).onFailure(new Exception());
    assertThat(fetcher.getCurrentlyFetching()).containsExactly(hipri1);
    assertThat(fetcher.getHiPriQueue()).isEmpty();
    assertThat(fetcher.getLowPriQueue()).isEmpty();
    assertThat(fetcher.getDelayedQeueue()).isEmpty();
    assertThat(hipri1.requeueCount).isEqualTo(1);
    assertThat(fetcher.getExtraMap(hipri1, 123)).containsEntry("delay_count", "0");
    // Simulate 2nd failure in hipri1, the request should wait in the delayedQueue for
    // delayTimeInMillis.
    getOnlyElement(recordingNetworkFetcher.callbacks.get(hipri1.delegatedState)).onFailure(new Exception());
    assertThat(fetcher.getCurrentlyFetching()).isEmpty();
    assertThat(fetcher.getHiPriQueue()).isEmpty();
    assertThat(fetcher.getLowPriQueue()).isEmpty();
    assertThat(fetcher.getDelayedQeueue()).containsExactly(hipri1);
    assertThat(fetcher.getExtraMap(hipri1, 123)).containsEntry("delay_count", "1");
    clock.incrementBy(301);
    // to trigger inflight requests
    PriorityFetchState<FetchState> hipri2 = fetch(fetcher, "hipri2", callback, true);
    // 301 ms is bigger than delayTimeInMillis, so hipri1 request should be re-queued now.
    assertThat(fetcher.getCurrentlyFetching()).containsExactly(hipri1);
    assertThat(fetcher.getHiPriQueue()).containsExactly(hipri2);
    assertThat(fetcher.getLowPriQueue()).isEmpty();
    assertThat(fetcher.getDelayedQeueue()).isEmpty();
    assertThat(hipri1.requeueCount).isEqualTo(2);
}
Also used : PriorityFetchState(com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState) FakeClock(com.facebook.imagepipeline.testing.FakeClock) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Test(org.junit.Test)

Example 24 with FakeClock

use of com.facebook.imagepipeline.testing.FakeClock in project fresco by facebook.

the class QualifiedResourceFetchProducerTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    mExecutor = new TestExecutorService(new FakeClock());
    mQualifiedResourceFetchProducer = new QualifiedResourceFetchProducer(mExecutor, mPooledByteBufferFactory, mContentResolver);
    mContentUri = UriUtil.getUriForQualifiedResource(PACKAGE_NAME, RESOURCE_ID);
    mProducerContext = new SettableProducerContext(mImageRequest, REQUEST_ID, mProducerListener, CALLER_CONTEXT, ImageRequest.RequestLevel.FULL_FETCH, false, true, Priority.MEDIUM, mConfig);
    when(mImageRequest.getSourceUri()).thenReturn(mContentUri);
}
Also used : FakeClock(com.facebook.imagepipeline.testing.FakeClock) TestExecutorService(com.facebook.imagepipeline.testing.TestExecutorService) Before(org.junit.Before)

Example 25 with FakeClock

use of com.facebook.imagepipeline.testing.FakeClock in project fresco by facebook.

the class SingleUsePostprocessorProducerTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    mTestExecutorService = new TestExecutorService(new FakeClock());
    mPostprocessorProducer = new PostprocessorProducer(mInputProducer, mPlatformBitmapFactory, mTestExecutorService);
    when(mImageRequest.getPostprocessor()).thenReturn(mPostprocessor);
    when(mProducerContext.getId()).thenReturn(mRequestId);
    when(mProducerContext.getProducerListener()).thenReturn(mProducerListener);
    when(mProducerContext.getImageRequest()).thenReturn(mImageRequest);
    mResults = new ArrayList<>();
    when(mPostprocessor.getName()).thenReturn(POSTPROCESSOR_NAME);
    when(mProducerListener.requiresExtraMap(mProducerContext, PRODUCER_NAME)).thenReturn(true);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            mResults.add(((CloseableReference<CloseableImage>) invocation.getArguments()[0]).clone());
            return null;
        }
    }).when(mConsumer).onNewResult(any(CloseableReference.class), anyInt());
    mInOrder = inOrder(mPostprocessor, mProducerListener, mConsumer);
    mSourceBitmap = mock(Bitmap.class);
    mSourceCloseableStaticBitmap = mock(CloseableStaticBitmap.class);
    when(mSourceCloseableStaticBitmap.getUnderlyingBitmap()).thenReturn(mSourceBitmap);
    mSourceCloseableImageRef = CloseableReference.<CloseableImage>of(mSourceCloseableStaticBitmap);
    mDestinationBitmap = mock(Bitmap.class);
    mDestinationCloseableBitmapRef = CloseableReference.of(mDestinationBitmap, mBitmapResourceReleaser);
}
Also used : Bitmap(android.graphics.Bitmap) CloseableStaticBitmap(com.facebook.imagepipeline.image.CloseableStaticBitmap) FakeClock(com.facebook.imagepipeline.testing.FakeClock) TestExecutorService(com.facebook.imagepipeline.testing.TestExecutorService) CloseableReference(com.facebook.common.references.CloseableReference) CloseableStaticBitmap(com.facebook.imagepipeline.image.CloseableStaticBitmap)

Aggregations

FakeClock (com.facebook.imagepipeline.testing.FakeClock)27 TestExecutorService (com.facebook.imagepipeline.testing.TestExecutorService)19 Before (org.junit.Before)10 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)6 PriorityFetchState (com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState)5 TestScheduledExecutorService (com.facebook.imagepipeline.testing.TestScheduledExecutorService)5 Test (org.junit.Test)5 CloseableReference (com.facebook.common.references.CloseableReference)4 File (java.io.File)3 IOException (java.io.IOException)3 ConnectException (java.net.ConnectException)3 SocketTimeoutException (java.net.SocketTimeoutException)3 UnknownHostException (java.net.UnknownHostException)3 Bitmap (android.graphics.Bitmap)2 TestAnimatedDrawableBackend (com.facebook.imagepipeline.animated.testing.TestAnimatedDrawableBackend)2 CloseableStaticBitmap (com.facebook.imagepipeline.image.CloseableStaticBitmap)2 InputStream (java.io.InputStream)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Canvas (android.graphics.Canvas)1 Drawable (android.graphics.drawable.Drawable)1