Search in sources :

Example 6 with PriorityFetchState

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

the class PriorityNetworkFetcherTest method queueSizesAreReturnedInExtraMap.

@Test
public void queueSizesAreReturnedInExtraMap() {
    FakeClock clock = new FakeClock();
    // Max hi-pri: 1, max low-pri: 0
    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);
    PriorityFetchState<FetchState> hipri1 = fetch(fetcher, "hipri1", callback, true);
    PriorityFetchState<FetchState> hipri2 = fetch(fetcher, "hipri2", callback, true);
    PriorityFetchState<FetchState> hipri3 = fetch(fetcher, "hipri3", callback, true);
    PriorityFetchState<FetchState> lowpri1 = fetch(fetcher, "lowpri1", callback, false);
    PriorityFetchState<FetchState> lowpri2 = fetch(fetcher, "lowpri2", callback, false);
    // When hipri1 is created, there hasn't been other requests yet.
    Map<String, String> hipri1Extras = fetcher.getExtraMap(hipri1, 123);
    assertThat(hipri1Extras).containsEntry("hipri_queue_size", "0");
    assertThat(hipri1Extras).containsEntry("lowpri_queue_size", "0");
    // When hipri2 is created, only hipri1 has previously been created, and it was immediately
    // dequeued, so the queue size is 0.
    Map<String, String> hipri2Extras = fetcher.getExtraMap(hipri2, 123);
    assertThat(hipri2Extras).containsEntry("hipri_queue_size", "0");
    assertThat(hipri2Extras).containsEntry("lowpri_queue_size", "0");
    // When hipri3 is created, hipri2 is in the queue.
    Map<String, String> hipri3Extras = fetcher.getExtraMap(hipri3, 123);
    assertThat(hipri3Extras).containsEntry("hipri_queue_size", "1");
    assertThat(hipri3Extras).containsEntry("lowpri_queue_size", "0");
    // When lowpri1 is created, hipri2 and hipri3 are in the queue.
    Map<String, String> lowpri1Extras = fetcher.getExtraMap(lowpri1, 123);
    assertThat(lowpri1Extras).containsEntry("hipri_queue_size", "2");
    assertThat(lowpri1Extras).containsEntry("lowpri_queue_size", "0");
    // When lowpri2 is created, hipri2 and hipri3 are in the hipri queue, and lowpri1 is in the
    // low-pri queue.
    Map<String, String> lowpri2Extras = fetcher.getExtraMap(lowpri2, 123);
    assertThat(lowpri2Extras).containsEntry("hipri_queue_size", "2");
    assertThat(lowpri2Extras).containsEntry("lowpri_queue_size", "1");
}
Also used : PriorityFetchState(com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState) FakeClock(com.facebook.imagepipeline.testing.FakeClock) Test(org.junit.Test)

Example 7 with PriorityFetchState

use of com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState 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 8 with PriorityFetchState

use of com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState 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 9 with PriorityFetchState

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

the class PriorityNetworkFetcherTest method testInfiniteRequeues_changePriThenFail.

/**
 * Scenario: an image changes priority and then fails. We expect it to be re-queued in the new
 * priority queue.
 */
@Test
public void testInfiniteRequeues_changePriThenFail() {
    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();
    ((SettableProducerContext) hipri1.getContext()).setPriority(LOW);
    // Simulate a failure in hipri1.
    getOnlyElement(recordingNetworkFetcher.callbacks.get(hipri1.delegatedState)).onFailure(new Exception());
    assertThat(fetcher.getCurrentlyFetching()).containsExactly(hipri2);
    assertThat(fetcher.getHiPriQueue()).isEmpty();
    assertThat(fetcher.getLowPriQueue()).containsExactly(hipri1);
    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 10 with PriorityFetchState

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

the class PriorityNetworkFetcherTest method testMaxNumberOfRequeue_requeueOnFail.

/**
 * Scenario: an image fetch fails. We expect it to be re-queued up to maxNumberOfRequeue times.
 */
@Test
public void testMaxNumberOfRequeue_requeueOnFail() {
    RecordingNetworkFetcher recordingNetworkFetcher = new RecordingNetworkFetcher();
    final int maxNumberOfRequeue = 2;
    // Max hi-pri: 1, max low-pri: 0
    PriorityNetworkFetcher<FetchState> fetcher = newFetcher(recordingNetworkFetcher, false, 1, 0, true, maxNumberOfRequeue, 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 2 failures in hipri1, the request should be requeued.
    getOnlyElement(recordingNetworkFetcher.callbacks.get(hipri1.delegatedState)).onFailure(new Exception());
    getOnlyElement(recordingNetworkFetcher.callbacks.get(hipri1.delegatedState)).onFailure(new Exception());
    assertThat(fetcher.getCurrentlyFetching()).containsExactly(hipri1);
    assertThat(fetcher.getHiPriQueue()).containsExactly(hipri2);
    assertThat(fetcher.getLowPriQueue()).isEmpty();
    // Simulate a 3rd failure in hipri1, the request should NOT be requeued.
    getOnlyElement(recordingNetworkFetcher.callbacks.get(hipri1.delegatedState)).onFailure(new Exception());
    assertThat(hipri1.requeueCount).isEqualTo(2);
    // we will start fetching hipri2 immediately.
    assertThat(fetcher.getCurrentlyFetching()).containsExactly(hipri2);
    assertThat(fetcher.getHiPriQueue()).isEmpty();
    assertThat(fetcher.getLowPriQueue()).isEmpty();
}
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)

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