Search in sources :

Example 6 with BroadcastMessage

use of org.atmosphere.cache.BroadcastMessage 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)

Example 7 with BroadcastMessage

use of org.atmosphere.cache.BroadcastMessage in project atmosphere by Atmosphere.

the class BroadcasterCacheTest method testRejectedCache.

@Test
public void testRejectedCache() throws ExecutionException, InterruptedException, ServletException {
    broadcaster.getBroadcasterConfig().setBroadcasterCache(new AbstractBroadcasterCache() {

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

        @Override
        public List<Object> retrieveFromCache(String id, String uuid) {
            return Collections.<Object>emptyList();
        }
    }).getBroadcasterCache().inspector(new BroadcasterCacheInspector() {

        @Override
        public boolean inspect(BroadcastMessage message) {
            return false;
        }
    });
    broadcaster.broadcast("foo", ar).get();
    assertEquals(cachedMessage.get().size(), 0);
}
Also used : CacheMessage(org.atmosphere.cache.CacheMessage) AbstractBroadcasterCache(org.atmosphere.cache.AbstractBroadcasterCache) BroadcastMessage(org.atmosphere.cache.BroadcastMessage) List(java.util.List) BroadcasterCacheInspector(org.atmosphere.cache.BroadcasterCacheInspector) Test(org.testng.annotations.Test)

Example 8 with BroadcastMessage

use of org.atmosphere.cache.BroadcastMessage in project atmosphere by Atmosphere.

the class BroadcasterCacheTest method testEmptyRejectedCache.

@Test
public void testEmptyRejectedCache() throws ExecutionException, InterruptedException, ServletException {
    final CountDownLatch latch = new CountDownLatch(1);
    broadcaster.getBroadcasterConfig().setBroadcasterCache(new AbstractBroadcasterCache() {

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

        @Override
        public List<Object> retrieveFromCache(String id, String uuid) {
            return Collections.<Object>emptyList();
        }
    }).getBroadcasterCache().inspector(new BroadcasterCacheInspector() {

        @Override
        public boolean inspect(BroadcastMessage message) {
            return false;
        }
    });
    broadcaster.broadcast("foo", ar);
    latch.await(10, TimeUnit.SECONDS);
    assertEquals(cachedMessage.get().size(), 0);
}
Also used : CacheMessage(org.atmosphere.cache.CacheMessage) AbstractBroadcasterCache(org.atmosphere.cache.AbstractBroadcasterCache) BroadcastMessage(org.atmosphere.cache.BroadcastMessage) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) BroadcasterCacheInspector(org.atmosphere.cache.BroadcasterCacheInspector) Test(org.testng.annotations.Test)

Aggregations

BroadcastMessage (org.atmosphere.cache.BroadcastMessage)8 List (java.util.List)6 CacheMessage (org.atmosphere.cache.CacheMessage)6 AbstractBroadcasterCache (org.atmosphere.cache.AbstractBroadcasterCache)5 Test (org.testng.annotations.Test)5 BroadcasterCacheInspector (org.atmosphere.cache.BroadcasterCacheInspector)4 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Servlet30CometSupport (org.atmosphere.container.Servlet30CometSupport)1 AR (org.atmosphere.runtime.BroadcasterCacheTest.AR)1