use of com.octo.android.robospice.stub.RequestListenerStub in project robospice by stephanenicolas.
the class RequestProcessorTest method testRequestPriority_should_execute_lazyly_low_priority_requests.
public void testRequestPriority_should_execute_lazyly_low_priority_requests() throws CacheLoadingException, CacheSavingException, InterruptedException, CacheCreationException {
// when
PausableThreadPoolExecutor executorService = PriorityThreadPoolExecutor.getPriorityExecutor(1);
networkStateChecker = new MockNetworkStateChecker();
mockRequestRunner = new DefaultRequestRunner(getContext(), mockCacheManager, executorService, mockRequestProgressManager, networkStateChecker);
requestProcessorUnderTest = new RequestProcessor(mockCacheManager, mockRequestProgressManager, mockRequestRunner);
CachedSpiceRequestStub<String> stubRequestLowPriority = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY2, TEST_DURATION, TEST_RETURNED_DATA2);
stubRequestLowPriority.setPriority(SpiceRequest.PRIORITY_LOW);
RequestListenerStub<String> mockRequestListener = new RequestListenerStub<String>();
Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
requestListenerSet.add(mockRequestListener);
EasyMock.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.anyObject(), EasyMock.eq(TEST_DURATION))).andReturn(null);
EasyMock.expectLastCall().anyTimes();
EasyMock.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA), EasyMock.eq(TEST_CACHE_KEY))).andReturn(TEST_RETURNED_DATA);
EasyMock.expectLastCall().anyTimes();
EasyMock.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA2), EasyMock.eq(TEST_CACHE_KEY2))).andReturn(TEST_RETURNED_DATA2);
EasyMock.expectLastCall().anyTimes();
EasyMock.replay(mockCacheManager);
executorService.pause();
final int lowRequestCount = 10;
for (int i = 0; i < lowRequestCount; i++) {
CachedSpiceRequestStub<String> stubRequestNormalPriority = createSuccessfulRequest(TEST_CLASS, TEST_RETURNED_DATA);
stubRequestNormalPriority.setPriority(SpiceRequest.PRIORITY_NORMAL);
requestProcessorUnderTest.addRequest(stubRequestNormalPriority, requestListenerSet);
}
requestProcessorUnderTest.addRequest(stubRequestLowPriority, requestListenerSet);
for (int i = 0; i < lowRequestCount; i++) {
CachedSpiceRequestStub<String> stubRequestNormalPriority = createSuccessfulRequest(TEST_CLASS, TEST_RETURNED_DATA);
stubRequestNormalPriority.setPriority(SpiceRequest.PRIORITY_NORMAL);
requestProcessorUnderTest.addRequest(stubRequestNormalPriority, requestListenerSet);
}
executorService.resume();
for (int i = 0; i < 2 * lowRequestCount; i++) {
mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
mockRequestListener.resetSuccess();
Ln.d("reset complete");
}
mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
// then
EasyMock.verify(mockCacheManager);
assertTrue(stubRequestLowPriority.isLoadDataFromNetworkCalled());
assertTrue(mockRequestListener.isExecutedInUIThread());
assertEquals(2 * lowRequestCount + 1, mockRequestListener.getResultHistory().size());
assertEquals(TEST_RETURNED_DATA2, mockRequestListener.getResultHistory().get(2 * lowRequestCount));
}
Aggregations