Search in sources :

Example 1 with AR

use of org.atmosphere.runtime.BroadcasterCacheTest.AR in project atmosphere by Atmosphere.

the class DefaultBroadcasterTest method testSimultaneousAddResourceAndPush.

@Test
public void testSimultaneousAddResourceAndPush() throws ExecutionException, InterruptedException, ServletException {
    final Map<String, BroadcastMessage> cache = new HashMap<String, BroadcastMessage>();
    broadcaster.getBroadcasterConfig().setBroadcasterCache(new AbstractBroadcasterCache() {

        @Override
        public CacheMessage addToCache(String id, String uuid, BroadcastMessage e) {
            CacheMessage c = put(e, System.nanoTime(), uuid, broadcaster.getID());
            cache.put(id, e);
            return c;
        }

        @Override
        public List<Object> retrieveFromCache(String id, String uuid) {
            ArrayList<Object> cacheContents = new ArrayList<Object>();
            if (!cache.isEmpty()) {
                cacheContents.add(cache.get(id).message());
                cache.clear();
            }
            return cacheContents;
        }
    });
    final AtmosphereResourceImpl ar = new AtmosphereResourceImpl(new AtmosphereFramework().getAtmosphereConfig(), broadcaster, mock(AtmosphereRequestImpl.class), AtmosphereResponseImpl.newInstance(), mock(Servlet30CometSupport.class), new AR());
    final String message = "foo";
    Runnable broadcastRunnable = new Runnable() {

        @Override
        public void run() {
            try {
                broadcaster.broadcast(message).get();
            } catch (InterruptedException ex) {
                Logger.getLogger(DefaultBroadcasterTest.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ExecutionException ex) {
                Logger.getLogger(DefaultBroadcasterTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };
    Runnable pollRunnable = new Runnable() {

        @Override
        public void run() {
            broadcaster.addAtmosphereResource(ar);
        }
    };
    int remainingTest = 10;
    ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(2);
    while (remainingTest > 0) {
        remainingTest--;
        Future<?> pollFuture = newFixedThreadPool.submit(pollRunnable);
        Future<?> broadcastFuture = newFixedThreadPool.submit(broadcastRunnable);
        broadcastFuture.get();
        pollFuture.get();
        Object eventMessage = ar.getAtmosphereResourceEvent().getMessage();
        Object retrievedMessage;
        if (eventMessage instanceof List) {
            retrievedMessage = ((List) eventMessage).get(0);
        } else {
            retrievedMessage = eventMessage;
        }
        //cleanup
        broadcaster.removeAtmosphereResource(ar);
        cache.clear();
        //System.out.println(iterations++ + ": message:" + retrievedMessage);
        assertEquals(retrievedMessage, message);
    }
}
Also used : HashMap(java.util.HashMap) AbstractBroadcasterCache(org.atmosphere.cache.AbstractBroadcasterCache) ArrayList(java.util.ArrayList) Servlet30CometSupport(org.atmosphere.container.Servlet30CometSupport) CacheMessage(org.atmosphere.cache.CacheMessage) AR(org.atmosphere.runtime.BroadcasterCacheTest.AR) BroadcastMessage(org.atmosphere.cache.BroadcastMessage) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 AbstractBroadcasterCache (org.atmosphere.cache.AbstractBroadcasterCache)1 BroadcastMessage (org.atmosphere.cache.BroadcastMessage)1 CacheMessage (org.atmosphere.cache.CacheMessage)1 Servlet30CometSupport (org.atmosphere.container.Servlet30CometSupport)1 AR (org.atmosphere.runtime.BroadcasterCacheTest.AR)1 Test (org.testng.annotations.Test)1