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