Search in sources :

Example 1 with RequestListener

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

the class RequestProcessor method addRequest.

// ============================================================================================
// PUBLIC
// ============================================================================================
public void addRequest(final CachedSpiceRequest<?> request, final Set<RequestListener<?>> listRequestListener) {
    if (isStopped) {
        Ln.d("Dropping request : " + request + " as processor is stopped.");
        return;
    }
    Ln.d("Adding request to queue " + hashCode() + ": " + request + " size is " + mapRequestToRequestListener.size());
    if (request.isCancelled()) {
        synchronized (mapRequestToRequestListener) {
            for (final CachedSpiceRequest<?> cachedSpiceRequest : mapRequestToRequestListener.keySet()) {
                if (request.equals(cachedSpiceRequest)) {
                    cachedSpiceRequest.cancel();
                    requestProgressManager.notifyListenersOfRequestCancellation(request);
                    return;
                }
            }
        }
    }
    boolean aggregated = false;
    Set<RequestListener<?>> listRequestListenerForThisRequest;
    synchronized (mapRequestToRequestListener) {
        listRequestListenerForThisRequest = mapRequestToRequestListener.get(request);
        if (listRequestListenerForThisRequest == null) {
            if (request.isProcessable()) {
                Ln.d("Adding entry for type %s and cacheKey %s.", request.getResultType(), request.getRequestCacheKey());
                listRequestListenerForThisRequest = Collections.synchronizedSet(new HashSet<RequestListener<?>>());
                this.mapRequestToRequestListener.put(request, listRequestListenerForThisRequest);
            }
        } else {
            Ln.d("Request for type %s and cacheKey %s already exists.", request.getResultType(), request.getRequestCacheKey());
            aggregated = true;
        }
    }
    if (listRequestListener != null && listRequestListenerForThisRequest != null) {
        listRequestListenerForThisRequest.addAll(listRequestListener);
    }
    if (aggregated) {
        requestProgressManager.notifyListenersOfRequestAggregated(request, listRequestListener);
        return;
    }
    if (request.isProcessable()) {
        requestProgressManager.notifyListenersOfRequestAdded(request, listRequestListener);
    } else {
        if (listRequestListenerForThisRequest == null) {
            requestProgressManager.notifyListenersOfRequestNotFound(request, listRequestListener);
        }
        requestProgressManager.notifyOfRequestProcessed(request, listRequestListener);
        // fix bug https://github.com/octo-online/robospice/issues/215
        return;
    }
    final RequestCancellationListener requestCancellationListener = new RequestCancellationListener() {

        @Override
        public void onRequestCancelled() {
            requestProgressManager.notifyListenersOfRequestCancellation(request);
            mapRequestToRequestListener.remove(request);
        }
    };
    request.setRequestCancellationListener(requestCancellationListener);
    if (request.isCancelled()) {
        requestProgressManager.notifyListenersOfRequestCancellation(request);
        mapRequestToRequestListener.remove(request);
        return;
    } else {
        requestRunner.executeRequest(request);
    }
}
Also used : RequestListener(com.octo.android.robospice.request.listener.RequestListener) RequestCancellationListener(com.octo.android.robospice.request.listener.RequestCancellationListener) HashSet(java.util.HashSet)

Example 2 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_true_loading_from_cache_throws_exception.

public void testAddRequest_when_fail_on_error_true_loading_from_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))).andThrow(new CacheLoadingException(""));
    EasyMock.replay(mockCacheManager);
    // when
    requestProcessorUnderTest.setFailOnCacheError(true);
    requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
    mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
    // then
    EasyMock.verify(mockCacheManager);
    assertFalse(stubRequest.isLoadDataFromNetworkCalled());
    assertFalse(mockRequestListener.isSuccessful());
    assertTrue(mockRequestListener.isComplete());
}
Also used : RequestListener(com.octo.android.robospice.request.listener.RequestListener) 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 RequestListener

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

the class RequestProcessorTest method testAddRequestsFromManyThreads.

@SuppressWarnings("rawtypes")
public void testAddRequestsFromManyThreads() throws Exception {
    final ArrayList<RequestListenerStub> listeners = new ArrayList<RequestListenerStub>();
    final ArrayList<Thread> threads = new ArrayList<Thread>();
    final int threadCount = 50;
    EasyMock.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.eq(TEST_CACHE_KEY), EasyMock.eq(TEST_DURATION))).andReturn(TEST_RETURNED_DATA);
    EasyMock.expectLastCall().atLeastOnce();
    EasyMock.replay(mockCacheManager);
    for (int i = 0; i < threadCount; i++) {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                CachedSpiceRequestStub<String> stubRequest = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
                RequestListenerStub<String> mockRequestListener = new RequestListenerStub<String>();
                synchronized (listeners) {
                    listeners.add(mockRequestListener);
                }
                Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
                requestListenerSet.add(mockRequestListener);
                requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
            }
        });
        thread.start();
        threads.add(thread);
    }
    // wait for all threads to have added their requests and listeners
    for (Thread thread : threads) {
        thread.join(REQUEST_COMPLETION_TIME_OUT_LARGE);
    }
    int listenersCalledCount = 0;
    for (RequestListenerStub listener : listeners) {
        // wait for all listeners to have been invoked
        listener.await(REQUEST_COMPLETION_TIME_OUT_LARGE);
        if (listener.isSuccessful() != null) {
            listenersCalledCount++;
        }
    }
    EasyMock.verify(mockCacheManager);
    assertEquals(threadCount, listeners.size());
    assertEquals(threadCount, listenersCalledCount);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) RequestListener(com.octo.android.robospice.request.listener.RequestListener) ArrayList(java.util.ArrayList) CachedSpiceRequestStub(com.octo.android.robospice.stub.CachedSpiceRequestStub) RequestListenerStub(com.octo.android.robospice.stub.RequestListenerStub)

Example 4 with RequestListener

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

the class RequestProcessorTest method testObservers_with_AddRequest_when_nothing_is_found_in_cache_and_request_fails.

public void testObservers_with_AddRequest_when_nothing_is_found_in_cache_and_request_fails() throws CacheLoadingException, CacheSavingException, InterruptedException, CacheCreationException {
    // given
    CachedSpiceRequestStub<String> stubRequest = createFailedRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION);
    stubRequest.setRetryPolicy(null);
    // prepare observers
    ExecutorService executorService = PriorityThreadPoolExecutor.getPriorityExecutor(1);
    mockRequestRunner = new DefaultRequestRunner(getContext(), mockCacheManager, executorService, mockRequestProgressManager, networkStateChecker);
    requestProcessorUnderTest = new RequestProcessor(mockCacheManager, mockRequestProgressManager, mockRequestRunner);
    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.replay(mockCacheManager);
    // when
    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());
}
Also used : RequestListener(com.octo.android.robospice.request.listener.RequestListener) ExecutorService(java.util.concurrent.ExecutorService) RequestListenerWithProgressStub(com.octo.android.robospice.stub.RequestListenerWithProgressStub) PendingRequestListenerWithProgressStub(com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub) HashSet(java.util.HashSet)

Example 5 with RequestListener

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

the class RequestProcessorTest method testObservers_with_AddRequest_when_cache_is_not_used.

public void testObservers_with_AddRequest_when_cache_is_not_used() throws InterruptedException {
    // given
    String cacheKey = null;
    CachedSpiceRequestStub<String> stubRequest = createSuccessfulRequest(TEST_CLASS, cacheKey, TEST_DURATION, TEST_RETURNED_DATA);
    ExecutorService executorService = PriorityThreadPoolExecutor.getPriorityExecutor(1);
    mockRequestRunner = new DefaultRequestRunner(getContext(), mockCacheManager, executorService, mockRequestProgressManager, networkStateChecker);
    requestProcessorUnderTest = new RequestProcessor(mockCacheManager, mockRequestProgressManager, mockRequestRunner);
    RequestListenerWithProgressStub<String> mockRequestListener = new RequestListenerWithProgressStub<String>();
    Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
    requestListenerSet.add(mockRequestListener);
    EasyMock.replay(mockCacheManager);
    // when
    requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
    mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
    // then
    EasyMock.verify(mockCacheManager);
    assertTrue(stubRequest.isLoadDataFromNetworkCalled());
    assertTrue(mockRequestListener.isExecutedInUIThread());
    assertTrue(mockRequestListener.isSuccessful());
    assertTrue(mockRequestListener.isComplete());
}
Also used : RequestListener(com.octo.android.robospice.request.listener.RequestListener) ExecutorService(java.util.concurrent.ExecutorService) RequestListenerWithProgressStub(com.octo.android.robospice.stub.RequestListenerWithProgressStub) PendingRequestListenerWithProgressStub(com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub) HashSet(java.util.HashSet)

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