Search in sources :

Example 1 with CachedSpiceRequestStub

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

Aggregations

RequestListener (com.octo.android.robospice.request.listener.RequestListener)1 CachedSpiceRequestStub (com.octo.android.robospice.stub.CachedSpiceRequestStub)1 RequestListenerStub (com.octo.android.robospice.stub.RequestListenerStub)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1