use of com.octo.android.robospice.stub.RequestListenerStub 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.stub.RequestListenerStub in project robospice by stephanenicolas.
the class RequestProcessorTest method testAddRequest_should_process_offline_request_even_if_network_is_down.
public void testAddRequest_should_process_offline_request_even_if_network_is_down() throws CacheLoadingException, CacheSavingException, InterruptedException, CacheCreationException {
// given
CachedSpiceRequestStub<String> stubRequest = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
stubRequest.setOffline(true);
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.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA), EasyMock.eq(TEST_CACHE_KEY))).andReturn(TEST_RETURNED_DATA);
EasyMock.replay(mockCacheManager);
SpiceServiceListener mockSpiceServiceListener = EasyMock.createMock(SpiceServiceListener.class);
mockSpiceServiceListener.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
mockSpiceServiceListener.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
mockSpiceServiceListener.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
mockSpiceServiceListener.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
EasyMock.replay(mockSpiceServiceListener);
requestProcessorUnderTest.addSpiceServiceListener(mockSpiceServiceListener);
// when
requestProcessorUnderTest.setFailOnCacheError(true);
networkStateChecker.setNetworkAvailable(false);
requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
// then
EasyMock.verify(mockCacheManager);
assertTrue(stubRequest.isLoadDataFromNetworkCalled());
assertTrue(mockRequestListener.isExecutedInUIThread());
assertTrue(mockRequestListener.isSuccessful());
EasyMock.verify(mockSpiceServiceListener);
}
use of com.octo.android.robospice.stub.RequestListenerStub 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.stub.RequestListenerStub in project robospice by stephanenicolas.
the class RequestProcessorTest method testAddRequestWhenNetworkIsDown.
// ============================================================================================
// TESTING NETWORK MANAGER DEPENDENCY & SPICE LISTENER
// ============================================================================================
public void testAddRequestWhenNetworkIsDown() throws CacheLoadingException, CacheSavingException, InterruptedException, CacheCreationException {
// given
CachedSpiceRequestStub<String> stubRequest = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
stubRequest.setRetryPolicy(null);
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.replay(mockCacheManager);
SpiceServiceListener mockSpiceServiceListener = EasyMock.createMock(SpiceServiceListener.class);
mockSpiceServiceListener.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
mockSpiceServiceListener.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
mockSpiceServiceListener.onRequestFailed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
mockSpiceServiceListener.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
EasyMock.replay(mockSpiceServiceListener);
requestProcessorUnderTest.addSpiceServiceListener(mockSpiceServiceListener);
// when
requestProcessorUnderTest.setFailOnCacheError(true);
networkStateChecker.setNetworkAvailable(false);
requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
// then
EasyMock.verify(mockCacheManager);
assertFalse(stubRequest.isLoadDataFromNetworkCalled());
assertTrue(mockRequestListener.isExecutedInUIThread());
assertFalse(mockRequestListener.isSuccessful());
assertTrue(mockRequestListener.getReceivedException() instanceof NoNetworkException);
EasyMock.verify(mockSpiceServiceListener);
}
use of com.octo.android.robospice.stub.RequestListenerStub in project robospice by stephanenicolas.
the class RequestProcessorTest method testRequestPriority_should_execute_asap_hight_priority_requests.
// ============================================================================================
// TESTING REQUEST PRIORITY
// ============================================================================================
// TODO
// both tests need some rewrite. There is no reason to receive 21 times the
// success result. They can be received twice before 1 reset, thus
// one will be missing.
// TODO
/*
* Those tests are really tricky. We want to test request priority. There
* are some limitations to using a PriorityBlockingQueue inside an
* ExecutorService. Here, to get a smooth test, we inject a lot of low
* requests and make assertions on the last executed request. That is the
* only way to get a stable test.
*/
public void testRequestPriority_should_execute_asap_hight_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> stubRequestHighPriority = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY2, TEST_DURATION, TEST_RETURNED_DATA2);
stubRequestHighPriority.setPriority(SpiceRequest.PRIORITY_HIGH);
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> stubRequestLowPriority = createSuccessfulRequest(TEST_CLASS, TEST_RETURNED_DATA);
stubRequestLowPriority.setPriority(SpiceRequest.PRIORITY_LOW);
requestProcessorUnderTest.addRequest(stubRequestLowPriority, requestListenerSet);
}
requestProcessorUnderTest.addRequest(stubRequestHighPriority, requestListenerSet);
executorService.resume();
for (int i = 0; i < lowRequestCount; i++) {
mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
mockRequestListener.resetSuccess();
}
mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
// then
EasyMock.verify(mockCacheManager);
assertTrue(stubRequestHighPriority.isLoadDataFromNetworkCalled());
assertTrue(mockRequestListener.isExecutedInUIThread());
assertEquals(lowRequestCount + 1, mockRequestListener.getResultHistory().size());
assertNotSame(TEST_RETURNED_DATA2, mockRequestListener.getResultHistory().get(lowRequestCount));
}
Aggregations