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);
}
}
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);
}
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);
}
Aggregations