use of com.octo.android.robospice.request.listener.RequestListener in project robospice by stephanenicolas.
the class RequestProcessorTest method test_2_spiceservicelisteners_should_be_notified_of_all_events_from_request_processor_when_request_are_aggregated.
public void test_2_spiceservicelisteners_should_be_notified_of_all_events_from_request_processor_when_request_are_aggregated() throws Exception {
// TDD for issue 182
// given
PausableThreadPoolExecutor executorService = PriorityThreadPoolExecutor.getPriorityExecutor(1);
networkStateChecker = new MockNetworkStateChecker();
mockRequestRunner = new DefaultRequestRunner(getContext(), mockCacheManager, executorService, mockRequestProgressManager, networkStateChecker);
requestProcessorUnderTest = new RequestProcessor(mockCacheManager, mockRequestProgressManager, mockRequestRunner);
EasyMock.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.eq(TEST_CACHE_KEY), 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.replay(mockCacheManager);
CachedSpiceRequestStub<String> spiceRequestStub = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA, WAIT_BEFORE_REQUEST_EXECUTION);
spiceRequestStub.setPriority(SpiceRequest.PRIORITY_LOW);
spiceRequestStub.setRetryPolicy(null);
CachedSpiceRequestStub<String> spiceRequestStub2 = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA, WAIT_BEFORE_REQUEST_EXECUTION);
spiceRequestStub2.setPriority(SpiceRequest.PRIORITY_HIGH);
spiceRequestStub2.setRetryPolicy(null);
RequestListenerWithProgressStub<String> mockRequestListener = new RequestListenerWithProgressStub<String>();
Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
requestListenerSet.add(mockRequestListener);
RequestListenerWithProgressStub<String> mockRequestListener2 = new RequestListenerWithProgressStub<String>();
Set<RequestListener<?>> requestListenerSet2 = new HashSet<RequestListener<?>>();
requestListenerSet2.add(mockRequestListener2);
SpiceServiceListener listener1 = EasyMock.createMock(SpiceServiceListener.class);
listener1.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
listener1.onRequestAggregated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
listener1.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
listener1.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
listener1.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.replay(listener1);
SpiceServiceListener listener2 = EasyMock.createMock(SpiceServiceListener.class);
listener2.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
listener2.onRequestAggregated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
listener2.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
listener2.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
listener2.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.replay(listener2);
requestProcessorUnderTest.addSpiceServiceListener(listener1);
requestProcessorUnderTest.addSpiceServiceListener(listener2);
// when
requestProcessorUnderTest.addRequest(spiceRequestStub, requestListenerSet);
requestProcessorUnderTest.addRequest(spiceRequestStub2, requestListenerSet2);
mockRequestListener.awaitComplete(REQUEST_COMPLETION_TIME_OUT);
mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
mockRequestListener2.awaitComplete(REQUEST_COMPLETION_TIME_OUT);
mockRequestListener2.await(REQUEST_COMPLETION_TIME_OUT);
// test
assertTrue(mockRequestListener.isComplete());
assertTrue(mockRequestListener2.isComplete());
assertTrue(mockRequestListener.isSuccessful());
assertTrue(mockRequestListener2.isSuccessful());
EasyMock.verify(listener1);
EasyMock.verify(listener2);
}
use of com.octo.android.robospice.request.listener.RequestListener in project robospice by stephanenicolas.
the class RequestProcessorTest method testAddRequest_should_process_offline_request_even_if_network_is_down.
public void testAddRequest_should_process_offline_request_even_if_network_is_down() throws CacheLoadingException, CacheSavingException, InterruptedException, CacheCreationException {
// given
CachedSpiceRequestStub<String> stubRequest = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
stubRequest.setOffline(true);
RequestListenerStub<String> mockRequestListener = new RequestListenerStub<String>();
Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
requestListenerSet.add(mockRequestListener);
EasyMock.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.eq(TEST_CACHE_KEY), EasyMock.eq(TEST_DURATION))).andReturn(null);
EasyMock.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA), EasyMock.eq(TEST_CACHE_KEY))).andReturn(TEST_RETURNED_DATA);
EasyMock.replay(mockCacheManager);
SpiceServiceListener mockSpiceServiceListener = EasyMock.createMock(SpiceServiceListener.class);
mockSpiceServiceListener.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
mockSpiceServiceListener.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
mockSpiceServiceListener.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
mockSpiceServiceListener.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
EasyMock.replay(mockSpiceServiceListener);
requestProcessorUnderTest.addSpiceServiceListener(mockSpiceServiceListener);
// when
requestProcessorUnderTest.setFailOnCacheError(true);
networkStateChecker.setNetworkAvailable(false);
requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
// then
EasyMock.verify(mockCacheManager);
assertTrue(stubRequest.isLoadDataFromNetworkCalled());
assertTrue(mockRequestListener.isExecutedInUIThread());
assertTrue(mockRequestListener.isSuccessful());
EasyMock.verify(mockSpiceServiceListener);
}
use of com.octo.android.robospice.request.listener.RequestListener in project robospice by stephanenicolas.
the class RequestProcessorTest method testAddRequest_when_nothing_is_found_in_cache_and_request_has_retry_policy.
// ============================================================================================
// RETRY POLICY
// ============================================================================================
public void testAddRequest_when_nothing_is_found_in_cache_and_request_has_retry_policy() throws CacheLoadingException, CacheSavingException, InterruptedException, CacheCreationException {
// given
CachedSpiceRequestStub<String> stubRequest = createFailedRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION);
DefaultRetryPolicy retryPolicy = new DefaultRetryPolicy(TEST_RETRY_COUNT, TEST_DELAY_BEFORE_RETRY, TEST_RETRY_BACKOFF_MULTIPLIER);
stubRequest.setRetryPolicy(retryPolicy);
RequestListenerWithProgressStub<String> mockRequestListener = new RequestListenerWithProgressStub<String>();
Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
requestListenerSet.add(mockRequestListener);
EasyMock.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.eq(TEST_CACHE_KEY), EasyMock.eq(TEST_DURATION))).andReturn(null);
EasyMock.expectLastCall().anyTimes();
EasyMock.replay(mockCacheManager);
// when
requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
mockRequestListener.await(RequestProcessorTest.REQUEST_COMPLETION_TIME_OUT);
// then
assertNotNull(stubRequest.getRetryPolicy());
assertEquals(0, stubRequest.getRetryPolicy().getRetryCount());
EasyMock.verify(mockCacheManager);
assertTrue(stubRequest.isLoadDataFromNetworkCalled());
assertNotNull(mockRequestListener.isSuccessful());
assertFalse(mockRequestListener.isSuccessful());
assertTrue(mockRequestListener.isExecutedInUIThread());
assertTrue(mockRequestListener.isComplete());
}
use of com.octo.android.robospice.request.listener.RequestListener in project robospice by stephanenicolas.
the class RequestProcessorTest method test_2_spiceservicelisteners_should_be_notified_of_all_events_from_request_processor.
// ============================================================================================
// TDD
// ============================================================================================
public void test_2_spiceservicelisteners_should_be_notified_of_all_events_from_request_processor() throws Exception {
// TDD for issue 182
// given
PausableThreadPoolExecutor executorService = PriorityThreadPoolExecutor.getPriorityExecutor(1);
networkStateChecker = new MockNetworkStateChecker();
mockRequestRunner = new DefaultRequestRunner(getContext(), mockCacheManager, executorService, mockRequestProgressManager, networkStateChecker);
requestProcessorUnderTest = new RequestProcessor(mockCacheManager, mockRequestProgressManager, mockRequestRunner);
EasyMock.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.eq(TEST_CACHE_KEY), EasyMock.eq(TEST_DURATION))).andReturn(null);
EasyMock.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA), EasyMock.eq(TEST_CACHE_KEY))).andReturn(TEST_RETURNED_DATA);
EasyMock.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.eq(TEST_CACHE_KEY2), EasyMock.eq(TEST_DURATION))).andReturn(null);
EasyMock.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA2), EasyMock.eq(TEST_CACHE_KEY2))).andReturn(TEST_RETURNED_DATA2);
EasyMock.replay(mockCacheManager);
CachedSpiceRequestStub<String> spiceRequestStub = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
spiceRequestStub.setPriority(SpiceRequest.PRIORITY_LOW);
spiceRequestStub.setRetryPolicy(null);
CachedSpiceRequestStub<String> spiceRequestStub2 = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY2, TEST_DURATION, TEST_RETURNED_DATA2);
spiceRequestStub2.setPriority(SpiceRequest.PRIORITY_HIGH);
spiceRequestStub2.setRetryPolicy(null);
RequestListenerWithProgressStub<String> mockRequestListener = new RequestListenerWithProgressStub<String>();
Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
requestListenerSet.add(mockRequestListener);
RequestListenerWithProgressStub<String> mockRequestListener2 = new RequestListenerWithProgressStub<String>();
Set<RequestListener<?>> requestListenerSet2 = new HashSet<RequestListener<?>>();
requestListenerSet2.add(mockRequestListener2);
SpiceServiceListener listener1 = EasyMock.createMock(SpiceServiceListener.class);
listener1.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().times(2);
listener1.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
listener1.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
listener1.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().times(2);
EasyMock.replay(listener1);
SpiceServiceListener listener2 = EasyMock.createMock(SpiceServiceListener.class);
listener2.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().times(2);
listener2.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
listener2.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
listener2.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().times(2);
EasyMock.replay(listener2);
requestProcessorUnderTest.addSpiceServiceListener(listener1);
requestProcessorUnderTest.addSpiceServiceListener(listener2);
// when
requestProcessorUnderTest.addRequest(spiceRequestStub, requestListenerSet);
requestProcessorUnderTest.addRequest(spiceRequestStub2, requestListenerSet2);
mockRequestListener.awaitComplete(REQUEST_COMPLETION_TIME_OUT);
mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
mockRequestListener2.awaitComplete(REQUEST_COMPLETION_TIME_OUT);
mockRequestListener2.await(REQUEST_COMPLETION_TIME_OUT);
// test
assertTrue(mockRequestListener.isComplete());
assertTrue(mockRequestListener2.isComplete());
assertTrue(mockRequestListener.isSuccessful());
assertTrue(mockRequestListener2.isSuccessful());
EasyMock.verify(listener1);
EasyMock.verify(listener2);
}
use of com.octo.android.robospice.request.listener.RequestListener in project robospice by stephanenicolas.
the class RequestProcessorTest method testAddRequest_when_fail_on_error_true_saving_to_cache_throws_exception.
public void testAddRequest_when_fail_on_error_true_saving_to_cache_throws_exception() throws CacheLoadingException, CacheSavingException, InterruptedException, CacheCreationException {
// given
CachedSpiceRequestStub<String> stubRequest = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
stubRequest.setRetryPolicy(null);
RequestListenerWithProgressStub<String> mockRequestListener = new RequestListenerWithProgressStub<String>();
Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
requestListenerSet.add(mockRequestListener);
EasyMock.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.eq(TEST_CACHE_KEY), EasyMock.eq(TEST_DURATION))).andReturn(null);
EasyMock.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA), EasyMock.eq(TEST_CACHE_KEY))).andThrow(new CacheSavingException(""));
EasyMock.replay(mockCacheManager);
// when
requestProcessorUnderTest.setFailOnCacheError(true);
requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
// then
EasyMock.verify(mockCacheManager);
assertTrue(stubRequest.isLoadDataFromNetworkCalled());
assertTrue(mockRequestListener.isExecutedInUIThread());
assertFalse(mockRequestListener.isSuccessful());
assertTrue(mockRequestListener.isComplete());
}
Aggregations