use of com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState 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);
}
Aggregations