Search in sources :

Example 16 with RequestListener

use of com.octo.android.robospice.request.listener.RequestListener 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));
}
Also used : PausableThreadPoolExecutor(com.octo.android.robospice.priority.PausableThreadPoolExecutor) RequestListener(com.octo.android.robospice.request.listener.RequestListener) RequestListenerStub(com.octo.android.robospice.stub.RequestListenerStub) HashSet(java.util.HashSet)

Example 17 with RequestListener

use of com.octo.android.robospice.request.listener.RequestListener 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)

Example 18 with RequestListener

use of com.octo.android.robospice.request.listener.RequestListener in project robospice by stephanenicolas.

the class RequestProgressManager method createProgressListener.

public <T> RequestProgressListener createProgressListener(final CachedSpiceRequest<T> request) {
    // add a progress listener to the request to be notified of
    // progress during load data from network
    final RequestProgressListener requestProgressListener = new RequestProgressListener() {

        @Override
        public void onRequestProgressUpdate(final RequestProgress progress) {
            final Set<RequestListener<?>> listeners = mapRequestToRequestListener.get(request);
            notifyListenersOfRequestProgress(request, listeners, progress);
        }
    };
    return requestProgressListener;
}
Also used : RequestProgressListener(com.octo.android.robospice.request.listener.RequestProgressListener) RequestListener(com.octo.android.robospice.request.listener.RequestListener) RequestProgress(com.octo.android.robospice.request.listener.RequestProgress)

Aggregations

RequestListener (com.octo.android.robospice.request.listener.RequestListener)18 HashSet (java.util.HashSet)17 PendingRequestListenerWithProgressStub (com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub)10 RequestListenerWithProgressStub (com.octo.android.robospice.stub.RequestListenerWithProgressStub)10 RequestListenerStub (com.octo.android.robospice.stub.RequestListenerStub)6 PausableThreadPoolExecutor (com.octo.android.robospice.priority.PausableThreadPoolExecutor)4 SpiceServiceListener (com.octo.android.robospice.request.listener.SpiceServiceListener)4 DefaultRetryPolicy (com.octo.android.robospice.retry.DefaultRetryPolicy)4 CacheSavingException (com.octo.android.robospice.persistence.exception.CacheSavingException)3 NoNetworkException (com.octo.android.robospice.exception.NoNetworkException)2 CacheLoadingException (com.octo.android.robospice.persistence.exception.CacheLoadingException)2 ExecutorService (java.util.concurrent.ExecutorService)2 RequestCancellationListener (com.octo.android.robospice.request.listener.RequestCancellationListener)1 RequestProgress (com.octo.android.robospice.request.listener.RequestProgress)1 RequestProgressListener (com.octo.android.robospice.request.listener.RequestProgressListener)1 CachedSpiceRequestStub (com.octo.android.robospice.stub.CachedSpiceRequestStub)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1