Search in sources :

Example 1 with PriorityFetchState

use of com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState in project fresco by facebook.

the class PriorityNetworkFetcherTest method testNotMovingDelayedRequeue_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. In this test we make sure
 * the request is not moved back to the priority queue unless delayTimeInMillis has passed.
 */
@Test
public void testNotMovingDelayedRequeue_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(200);
    // to trigger inflight requests
    PriorityFetchState<FetchState> hipri2 = fetch(fetcher, "hipri2", callback, true);
    // 200 ms is smaller than delayTimeInMillis, so hipri1 request should still wait in the
    // delayedQueue.
    assertThat(fetcher.getCurrentlyFetching()).containsExactly(hipri2);
    assertThat(fetcher.getHiPriQueue()).isEmpty();
    assertThat(fetcher.getLowPriQueue()).isEmpty();
    assertThat(fetcher.getDelayedQeueue()).containsExactly(hipri1);
    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 2 with PriorityFetchState

use of com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState in project fresco by facebook.

the class PriorityNetworkFetcherTest method fetch.

private PriorityFetchState<FetchState> fetch(PriorityNetworkFetcher<FetchState> fetcher, String uri, NetworkFetcher.Callback callback, boolean isHiPri) {
    Consumer<EncodedImage> consumer = mock(Consumer.class);
    SettableProducerContext context = new SettableProducerContext(ImageRequest.fromUri(uri), "dontcare", null, null, null, !isHiPri, false, isHiPri ? HIGH : LOW, null);
    FetchState delegateFetchState = new FetchState(consumer, context);
    when(delegate.createFetchState(eq(consumer), eq(context))).thenReturn(delegateFetchState);
    PriorityFetchState<FetchState> fetchState = fetcher.createFetchState(consumer, context);
    fetcher.fetch(fetchState, callback);
    return fetchState;
}
Also used : PriorityFetchState(com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 3 with PriorityFetchState

use of com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState in project fresco by facebook.

the class PriorityNetworkFetcherTest method delegateFetchStateIsRecreatedOnRequeue.

/**
 * When we requeue a request, we recreate its delegate FetchState. This is required to reset any
 * state it might have (e.g., maximum number of retries).
 */
@Test
public void delegateFetchStateIsRecreatedOnRequeue() {
    RecordingNetworkFetcher recordingNetworkFetcher = new RecordingNetworkFetcher();
    // Max hi-pri: 1, max low-pri: 0
    PriorityNetworkFetcher<FetchState> fetcher = newFetcher(recordingNetworkFetcher, false, 1, 0, true, INFINITE_REQUEUE, false, false);
    PriorityFetchState<FetchState> fetchState = fetch(fetcher, "url", callback, true);
    assertThat(recordingNetworkFetcher.createdFetchStates).hasSize(1);
    assertThat(fetchState.delegatedState).isSameInstanceAs(recordingNetworkFetcher.createdFetchStates.get(0));
    // Simulate a failure in fetchState, triggering a requeue.
    getOnlyElement(recordingNetworkFetcher.callbacks.get(fetchState.delegatedState)).onFailure(new Exception());
    assertThat(recordingNetworkFetcher.createdFetchStates).hasSize(2);
    assertThat(fetchState.delegatedState).isSameInstanceAs(recordingNetworkFetcher.createdFetchStates.get(1));
    Map<String, String> extrasMap = fetcher.getExtraMap(fetchState, 123);
    assertThat(extrasMap).containsEntry("requeueCount", "1");
}
Also used : PriorityFetchState(com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Test(org.junit.Test)

Example 4 with PriorityFetchState

use of com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState in project fresco by facebook.

the class PriorityNetworkFetcherTest method testInfiniteRequeues_requeueOnFail.

/**
 * Scenario: an image fetch fails. We expect it to be re-queued, and since it is hi-pri, to be
 * retried immediately.
 */
@Test
public void testInfiniteRequeues_requeueOnFail() {
    RecordingNetworkFetcher recordingNetworkFetcher = new RecordingNetworkFetcher();
    // Max hi-pri: 1, max low-pri: 0
    PriorityNetworkFetcher<FetchState> fetcher = newFetcher(recordingNetworkFetcher, false, 1, 0, true, INFINITE_REQUEUE, false, false);
    PriorityFetchState<FetchState> hipri1 = fetch(fetcher, "hipri1", callback, true);
    PriorityFetchState<FetchState> hipri2 = fetch(fetcher, "hipri2", callback, true);
    assertThat(fetcher.getCurrentlyFetching()).containsExactly(hipri1);
    assertThat(fetcher.getHiPriQueue()).containsExactly(hipri2);
    assertThat(fetcher.getLowPriQueue()).isEmpty();
    // Simulate a failure in hipri1.
    getOnlyElement(recordingNetworkFetcher.callbacks.get(hipri1.delegatedState)).onFailure(new Exception());
    assertThat(fetcher.getCurrentlyFetching()).containsExactly(hipri1);
    assertThat(fetcher.getHiPriQueue()).containsExactly(hipri2);
    assertThat(fetcher.getLowPriQueue()).isEmpty();
    assertThat(hipri1.requeueCount).isEqualTo(1);
}
Also used : PriorityFetchState(com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Test(org.junit.Test)

Example 5 with PriorityFetchState

use of com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState in project fresco by facebook.

the class PriorityNetworkFetcherTest method failuresAreRetried_eventualSuccess.

/**
 * Scenario: two requests are enqueued. Up to 1 request may be processed at a time. The first one
 * fails twice and then succeeds.
 *
 * <p>Verify that we attempt the first one 3 times, and only once it succeeds, we move on to the
 * second request.
 */
@Test
public void failuresAreRetried_eventualSuccess() throws IOException {
    final int maxAttemptCount = 3;
    RecordingNetworkFetcher delegateFetcher = new RecordingNetworkFetcher();
    PriorityNetworkFetcher<FetchState> fetcher = new PriorityNetworkFetcher<>(delegateFetcher, true, 1, 0, true, 0, false, NO_DELAYED_REQUESTS, 0, /* requeueDelayTimeInMillis */
    false, false, /* nonRecoverableExceptionPreventsRequeue */
    1, /* maxConnectAttemptCount */
    maxAttemptCount, false, /* retryLowPriAll */
    false, /* retryLowPriUnknownHostException */
    false);
    PriorityFetchState<FetchState> state1 = fetch(fetcher, "uri1", callback, true);
    fetch(fetcher, "uri2", callback, true);
    // Simulate delegate fails twice.
    for (int i = 0; i < 2; i++) {
        assertThat(delegateFetcher.callbacks).hasSize(i + 1);
        FetchState lastFetched = getLast(delegateFetcher.callbacks.keys());
        assertThat(lastFetched.getUri().toString()).isEqualTo("uri1");
        getOnlyElement(delegateFetcher.callbacks.get(lastFetched)).onFailure(new IOException("Canceled"));
    }
    // Simulate delegate succeeds 3rd attempt.
    assertThat(delegateFetcher.callbacks).hasSize(3);
    FetchState lastFetched = getLast(delegateFetcher.callbacks.keys());
    assertThat(lastFetched.getUri().toString()).isEqualTo("uri1");
    getOnlyElement(delegateFetcher.callbacks.get(lastFetched)).onResponse(mock(InputStream.class), 0);
    fetcher.onFetchCompletion(state1, 0);
    // Assert that eventually we succeed.
    verify(callback).onResponse(isA(InputStream.class), anyInt());
    // After we're done with uri1, 'fetcher' moves on to uti2 (but not before).
    assertThat(delegateFetcher.callbacks).hasSize(4);
    assertThat(getLast(delegateFetcher.callbacks.keys()).getUri().toString()).isEqualTo("uri2");
}
Also used : PriorityFetchState(com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState) InputStream(java.io.InputStream) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

PriorityFetchState (com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState)11 Test (org.junit.Test)10 IOException (java.io.IOException)8 ConnectException (java.net.ConnectException)7 SocketTimeoutException (java.net.SocketTimeoutException)7 UnknownHostException (java.net.UnknownHostException)7 FakeClock (com.facebook.imagepipeline.testing.FakeClock)5 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)1 InputStream (java.io.InputStream)1