Search in sources :

Example 1 with DefaultRetryPolicy

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());
}
Also used : RequestListener(com.octo.android.robospice.request.listener.RequestListener) DefaultRetryPolicy(com.octo.android.robospice.retry.DefaultRetryPolicy) RequestListenerWithProgressStub(com.octo.android.robospice.stub.RequestListenerWithProgressStub) PendingRequestListenerWithProgressStub(com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub) HashSet(java.util.HashSet)

Example 2 with DefaultRetryPolicy

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());
}
Also used : RequestListener(com.octo.android.robospice.request.listener.RequestListener) DefaultRetryPolicy(com.octo.android.robospice.retry.DefaultRetryPolicy) CacheLoadingException(com.octo.android.robospice.persistence.exception.CacheLoadingException) RequestListenerWithProgressStub(com.octo.android.robospice.stub.RequestListenerWithProgressStub) PendingRequestListenerWithProgressStub(com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub) HashSet(java.util.HashSet)

Example 3 with DefaultRetryPolicy

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);
}
Also used : RequestListener(com.octo.android.robospice.request.listener.RequestListener) NoNetworkException(com.octo.android.robospice.exception.NoNetworkException) DefaultRetryPolicy(com.octo.android.robospice.retry.DefaultRetryPolicy) RequestListenerStub(com.octo.android.robospice.stub.RequestListenerStub) HashSet(java.util.HashSet)

Example 4 with DefaultRetryPolicy

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());
}
Also used : SpiceException(com.octo.android.robospice.persistence.exception.SpiceException) DefaultRetryPolicy(com.octo.android.robospice.retry.DefaultRetryPolicy)

Example 5 with DefaultRetryPolicy

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());
}
Also used : RequestListener(com.octo.android.robospice.request.listener.RequestListener) CacheSavingException(com.octo.android.robospice.persistence.exception.CacheSavingException) DefaultRetryPolicy(com.octo.android.robospice.retry.DefaultRetryPolicy) RequestListenerWithProgressStub(com.octo.android.robospice.stub.RequestListenerWithProgressStub) PendingRequestListenerWithProgressStub(com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub) HashSet(java.util.HashSet)

Aggregations

DefaultRetryPolicy (com.octo.android.robospice.retry.DefaultRetryPolicy)5 RequestListener (com.octo.android.robospice.request.listener.RequestListener)4 HashSet (java.util.HashSet)4 PendingRequestListenerWithProgressStub (com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub)3 RequestListenerWithProgressStub (com.octo.android.robospice.stub.RequestListenerWithProgressStub)3 NoNetworkException (com.octo.android.robospice.exception.NoNetworkException)1 CacheLoadingException (com.octo.android.robospice.persistence.exception.CacheLoadingException)1 CacheSavingException (com.octo.android.robospice.persistence.exception.CacheSavingException)1 SpiceException (com.octo.android.robospice.persistence.exception.SpiceException)1 RequestListenerStub (com.octo.android.robospice.stub.RequestListenerStub)1