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);
}
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");
}
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);
}
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);
}
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);
}
Aggregations