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);
}
}
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());
}
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);
}
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());
}
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());
}
Aggregations