use of com.octo.android.robospice.retry.DefaultRetryPolicy 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.retry.DefaultRetryPolicy in project robospice by stephanenicolas.
the class RequestProcessorTest method testAddRequest_when_fail_on_error_true_and_request_has_retry_policy_loading_from_cache_throws_exception.
public void testAddRequest_when_fail_on_error_true_and_request_has_retry_policy_loading_from_cache_throws_exception() throws SpiceException, InterruptedException {
// given
CachedSpiceRequestStub<String> stubRequest = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
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))).andThrow(new CacheLoadingException(""));
EasyMock.expectLastCall().anyTimes();
EasyMock.replay(mockCacheManager);
// when
requestProcessorUnderTest.setFailOnCacheError(true);
requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
// then
assertNotNull(stubRequest.getRetryPolicy());
assertEquals(0, stubRequest.getRetryPolicy().getRetryCount());
EasyMock.verify(mockCacheManager);
assertFalse(stubRequest.isLoadDataFromNetworkCalled());
assertFalse(mockRequestListener.isSuccessful());
assertTrue(mockRequestListener.isComplete());
}
use of com.octo.android.robospice.retry.DefaultRetryPolicy in project robospice by stephanenicolas.
the class RequestProcessorTest method testAddRequestWhenNetworkIsDown_and_request_has_retry_policy.
public void testAddRequestWhenNetworkIsDown_and_request_has_retry_policy() throws CacheLoadingException, SpiceException, InterruptedException, Exception {
// given
CachedSpiceRequestStub<String> stubRequest = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
DefaultRetryPolicy retryPolicy = new DefaultRetryPolicy(TEST_RETRY_COUNT, TEST_DELAY_BEFORE_RETRY, TEST_RETRY_BACKOFF_MULTIPLIER);
stubRequest.setRetryPolicy(retryPolicy);
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.expectLastCall().times(1);
EasyMock.replay(mockCacheManager);
// when
requestProcessorUnderTest.setFailOnCacheError(true);
networkStateChecker.setNetworkAvailable(false);
requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
// then
assertNotNull(stubRequest.getRetryPolicy());
assertEquals(TEST_RETRY_COUNT, stubRequest.getRetryPolicy().getRetryCount());
EasyMock.verify(mockCacheManager);
assertFalse(stubRequest.isLoadDataFromNetworkCalled());
assertTrue(mockRequestListener.isExecutedInUIThread());
assertFalse(mockRequestListener.isSuccessful());
assertTrue(mockRequestListener.getReceivedException() instanceof NoNetworkException);
}
use of com.octo.android.robospice.retry.DefaultRetryPolicy in project robospice by stephanenicolas.
the class RequestProcessorTest method testDefaultRetryPolicy_implements_retry_countdown_and_exponential_backoff.
public void testDefaultRetryPolicy_implements_retry_countdown_and_exponential_backoff() throws Exception {
// define local values since class constant values didn't reveal
// incremental backoff
final long localDelayBeforeRetry = 200;
final float localRetryBackoffMultiplier = 5.0f;
final int localRetryCount = 3;
// given
DefaultRetryPolicy retryPolicy = new DefaultRetryPolicy(localRetryCount, localDelayBeforeRetry, localRetryBackoffMultiplier);
assertEquals(localRetryCount, retryPolicy.getRetryCount());
assertEquals(localDelayBeforeRetry, retryPolicy.getDelayBeforeRetry());
// when
SpiceException e = null;
retryPolicy.retry(e);
// then
assertEquals(localRetryCount - 1, retryPolicy.getRetryCount());
assertEquals((long) (localDelayBeforeRetry * localRetryBackoffMultiplier), retryPolicy.getDelayBeforeRetry());
}
use of com.octo.android.robospice.retry.DefaultRetryPolicy in project robospice by stephanenicolas.
the class RequestProcessorTest method testAddRequest_when_fail_on_error_and_request_has_retry_polic_true_saving_to_cache_throws_exception.
public void testAddRequest_when_fail_on_error_and_request_has_retry_polic_true_saving_to_cache_throws_exception() throws InterruptedException, SpiceException {
// given
CachedSpiceRequestStub<String> stubRequest = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
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.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA), EasyMock.eq(TEST_CACHE_KEY))).andThrow(new CacheSavingException(""));
EasyMock.expectLastCall().anyTimes();
EasyMock.replay(mockCacheManager);
// when
requestProcessorUnderTest.setFailOnCacheError(true);
requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
// then
assertNotNull(stubRequest.getRetryPolicy());
assertEquals(0, stubRequest.getRetryPolicy().getRetryCount());
EasyMock.verify(mockCacheManager);
assertTrue(stubRequest.isLoadDataFromNetworkCalled());
assertTrue(mockRequestListener.isExecutedInUIThread());
assertFalse(mockRequestListener.isSuccessful());
assertTrue(mockRequestListener.isComplete());
}
Aggregations