Search in sources :

Example 1 with PausableThreadPoolExecutor

use of com.octo.android.robospice.priority.PausableThreadPoolExecutor in project robospice by stephanenicolas.

the class RequestProcessorTest method test_2_spiceservicelisteners_should_be_notified_of_all_events_from_request_processor_when_request_are_aggregated.

public void test_2_spiceservicelisteners_should_be_notified_of_all_events_from_request_processor_when_request_are_aggregated() throws Exception {
    // TDD for issue 182
    // given
    PausableThreadPoolExecutor executorService = PriorityThreadPoolExecutor.getPriorityExecutor(1);
    networkStateChecker = new MockNetworkStateChecker();
    mockRequestRunner = new DefaultRequestRunner(getContext(), mockCacheManager, executorService, mockRequestProgressManager, networkStateChecker);
    requestProcessorUnderTest = new RequestProcessor(mockCacheManager, mockRequestProgressManager, mockRequestRunner);
    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))).andReturn(TEST_RETURNED_DATA);
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(mockCacheManager);
    CachedSpiceRequestStub<String> spiceRequestStub = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA, WAIT_BEFORE_REQUEST_EXECUTION);
    spiceRequestStub.setPriority(SpiceRequest.PRIORITY_LOW);
    spiceRequestStub.setRetryPolicy(null);
    CachedSpiceRequestStub<String> spiceRequestStub2 = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA, WAIT_BEFORE_REQUEST_EXECUTION);
    spiceRequestStub2.setPriority(SpiceRequest.PRIORITY_HIGH);
    spiceRequestStub2.setRetryPolicy(null);
    RequestListenerWithProgressStub<String> mockRequestListener = new RequestListenerWithProgressStub<String>();
    Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
    requestListenerSet.add(mockRequestListener);
    RequestListenerWithProgressStub<String> mockRequestListener2 = new RequestListenerWithProgressStub<String>();
    Set<RequestListener<?>> requestListenerSet2 = new HashSet<RequestListener<?>>();
    requestListenerSet2.add(mockRequestListener2);
    SpiceServiceListener listener1 = EasyMock.createMock(SpiceServiceListener.class);
    listener1.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    listener1.onRequestAggregated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    listener1.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    listener1.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    listener1.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.replay(listener1);
    SpiceServiceListener listener2 = EasyMock.createMock(SpiceServiceListener.class);
    listener2.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    listener2.onRequestAggregated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    listener2.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    listener2.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    listener2.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.replay(listener2);
    requestProcessorUnderTest.addSpiceServiceListener(listener1);
    requestProcessorUnderTest.addSpiceServiceListener(listener2);
    // when
    requestProcessorUnderTest.addRequest(spiceRequestStub, requestListenerSet);
    requestProcessorUnderTest.addRequest(spiceRequestStub2, requestListenerSet2);
    mockRequestListener.awaitComplete(REQUEST_COMPLETION_TIME_OUT);
    mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
    mockRequestListener2.awaitComplete(REQUEST_COMPLETION_TIME_OUT);
    mockRequestListener2.await(REQUEST_COMPLETION_TIME_OUT);
    // test
    assertTrue(mockRequestListener.isComplete());
    assertTrue(mockRequestListener2.isComplete());
    assertTrue(mockRequestListener.isSuccessful());
    assertTrue(mockRequestListener2.isSuccessful());
    EasyMock.verify(listener1);
    EasyMock.verify(listener2);
}
Also used : PausableThreadPoolExecutor(com.octo.android.robospice.priority.PausableThreadPoolExecutor) RequestListener(com.octo.android.robospice.request.listener.RequestListener) SpiceServiceListener(com.octo.android.robospice.request.listener.SpiceServiceListener) RequestListenerWithProgressStub(com.octo.android.robospice.stub.RequestListenerWithProgressStub) PendingRequestListenerWithProgressStub(com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub) HashSet(java.util.HashSet)

Example 2 with PausableThreadPoolExecutor

use of com.octo.android.robospice.priority.PausableThreadPoolExecutor in project robospice by stephanenicolas.

the class RequestProcessorTest method test_2_spiceservicelisteners_should_be_notified_of_all_events_from_request_processor.

// ============================================================================================
// TDD
// ============================================================================================
public void test_2_spiceservicelisteners_should_be_notified_of_all_events_from_request_processor() throws Exception {
    // TDD for issue 182
    // given
    PausableThreadPoolExecutor executorService = PriorityThreadPoolExecutor.getPriorityExecutor(1);
    networkStateChecker = new MockNetworkStateChecker();
    mockRequestRunner = new DefaultRequestRunner(getContext(), mockCacheManager, executorService, mockRequestProgressManager, networkStateChecker);
    requestProcessorUnderTest = new RequestProcessor(mockCacheManager, mockRequestProgressManager, mockRequestRunner);
    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.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.eq(TEST_CACHE_KEY2), EasyMock.eq(TEST_DURATION))).andReturn(null);
    EasyMock.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA2), EasyMock.eq(TEST_CACHE_KEY2))).andReturn(TEST_RETURNED_DATA2);
    EasyMock.replay(mockCacheManager);
    CachedSpiceRequestStub<String> spiceRequestStub = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
    spiceRequestStub.setPriority(SpiceRequest.PRIORITY_LOW);
    spiceRequestStub.setRetryPolicy(null);
    CachedSpiceRequestStub<String> spiceRequestStub2 = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY2, TEST_DURATION, TEST_RETURNED_DATA2);
    spiceRequestStub2.setPriority(SpiceRequest.PRIORITY_HIGH);
    spiceRequestStub2.setRetryPolicy(null);
    RequestListenerWithProgressStub<String> mockRequestListener = new RequestListenerWithProgressStub<String>();
    Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
    requestListenerSet.add(mockRequestListener);
    RequestListenerWithProgressStub<String> mockRequestListener2 = new RequestListenerWithProgressStub<String>();
    Set<RequestListener<?>> requestListenerSet2 = new HashSet<RequestListener<?>>();
    requestListenerSet2.add(mockRequestListener2);
    SpiceServiceListener listener1 = EasyMock.createMock(SpiceServiceListener.class);
    listener1.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().times(2);
    listener1.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    listener1.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    listener1.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().times(2);
    EasyMock.replay(listener1);
    SpiceServiceListener listener2 = EasyMock.createMock(SpiceServiceListener.class);
    listener2.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().times(2);
    listener2.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    listener2.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    listener2.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().times(2);
    EasyMock.replay(listener2);
    requestProcessorUnderTest.addSpiceServiceListener(listener1);
    requestProcessorUnderTest.addSpiceServiceListener(listener2);
    // when
    requestProcessorUnderTest.addRequest(spiceRequestStub, requestListenerSet);
    requestProcessorUnderTest.addRequest(spiceRequestStub2, requestListenerSet2);
    mockRequestListener.awaitComplete(REQUEST_COMPLETION_TIME_OUT);
    mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
    mockRequestListener2.awaitComplete(REQUEST_COMPLETION_TIME_OUT);
    mockRequestListener2.await(REQUEST_COMPLETION_TIME_OUT);
    // test
    assertTrue(mockRequestListener.isComplete());
    assertTrue(mockRequestListener2.isComplete());
    assertTrue(mockRequestListener.isSuccessful());
    assertTrue(mockRequestListener2.isSuccessful());
    EasyMock.verify(listener1);
    EasyMock.verify(listener2);
}
Also used : PausableThreadPoolExecutor(com.octo.android.robospice.priority.PausableThreadPoolExecutor) RequestListener(com.octo.android.robospice.request.listener.RequestListener) SpiceServiceListener(com.octo.android.robospice.request.listener.SpiceServiceListener) RequestListenerWithProgressStub(com.octo.android.robospice.stub.RequestListenerWithProgressStub) PendingRequestListenerWithProgressStub(com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub) HashSet(java.util.HashSet)

Example 3 with PausableThreadPoolExecutor

use of com.octo.android.robospice.priority.PausableThreadPoolExecutor 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));
}
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 4 with PausableThreadPoolExecutor

use of com.octo.android.robospice.priority.PausableThreadPoolExecutor 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)

Aggregations

PausableThreadPoolExecutor (com.octo.android.robospice.priority.PausableThreadPoolExecutor)4 RequestListener (com.octo.android.robospice.request.listener.RequestListener)4 HashSet (java.util.HashSet)4 SpiceServiceListener (com.octo.android.robospice.request.listener.SpiceServiceListener)2 PendingRequestListenerWithProgressStub (com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub)2 RequestListenerStub (com.octo.android.robospice.stub.RequestListenerStub)2 RequestListenerWithProgressStub (com.octo.android.robospice.stub.RequestListenerWithProgressStub)2