use of org.glassfish.jersey.media.sse.InboundEvent in project jersey by jersey.
the class ItemStoreResourceITCase method testEventSourceReconnect.
/**
* Test the {@link EventSource} reconnect feature.
*
* @throws Exception in case of a test failure.
*/
@Test
public void testEventSourceReconnect() throws Exception {
final WebTarget itemsTarget = target("items");
// countdown only on new item events
final CountDownLatch latch = new CountDownLatch(MAX_ITEMS * MAX_LISTENERS * 2);
final List<Queue<String>> receivedQueues = new ArrayList<Queue<String>>(MAX_LISTENERS);
final EventSource[] sources = new EventSource[MAX_LISTENERS];
for (int i = 0; i < MAX_LISTENERS; i++) {
final int id = i;
final EventSource es = EventSource.target(itemsTarget.path("events")).named("SOURCE " + id).build();
sources[id] = es;
final Queue<String> received = new ConcurrentLinkedQueue<String>();
receivedQueues.add(received);
es.register(new EventListener() {
@Override
public void onEvent(InboundEvent inboundEvent) {
try {
if (inboundEvent.getName() == null) {
latch.countDown();
final String data = inboundEvent.readData();
LOGGER.info("[-i-] SOURCE " + id + ": Received event id=" + inboundEvent.getId() + " data=" + data);
received.add(data);
}
} catch (ProcessingException ex) {
LOGGER.log(Level.SEVERE, "[-x-] SOURCE " + id + ": Error getting event data.", ex);
received.add("[data processing error]");
}
}
});
}
final String[] postedItems = new String[MAX_ITEMS * 2];
try {
open(sources);
for (int i = 0; i < MAX_ITEMS; i++) {
final String item = String.format("round-1-%02d", i);
postItem(itemsTarget, item);
postedItems[i] = item;
sendCommand(itemsTarget, "disconnect");
Thread.sleep(100);
}
final int reconnectDelay = 1;
sendCommand(itemsTarget, "reconnect " + reconnectDelay);
sendCommand(itemsTarget, "disconnect");
Thread.sleep(reconnectDelay * 1000);
for (int i = 0; i < MAX_ITEMS; i++) {
final String item = String.format("round-2-%02d", i);
postedItems[i + MAX_ITEMS] = item;
postItem(itemsTarget, item);
}
sendCommand(itemsTarget, "reconnect now");
assertTrue("Waiting to receive all events has timed out.", latch.await((1 + MAX_LISTENERS * (MAX_ITEMS + 1) * reconnectDelay) * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS));
// need to force disconnect on server in order for EventSource.close(...) to succeed with HttpUrlConnection
sendCommand(itemsTarget, "disconnect");
} finally {
close(sources);
}
final String storedItems = itemsTarget.request().get(String.class);
for (String item : postedItems) {
assertThat("Posted item '" + item + "' stored on server", storedItems, containsString(item));
}
int sourceId = 0;
for (Queue<String> queue : receivedQueues) {
assertThat("Received events in source " + sourceId, queue, describedAs("Collection containing %0", hasItems(postedItems), Arrays.asList(postedItems).toString()));
assertThat("Size of received queue for source " + sourceId, queue.size(), equalTo(postedItems.length));
sourceId++;
}
}
use of org.glassfish.jersey.media.sse.InboundEvent in project Hystrix by Netflix.
the class HystricsMetricsControllerTest method validateStream.
private void validateStream(EventInput eventInput, long waitTime) {
long timeElapsed = System.currentTimeMillis();
while (!eventInput.isClosed() && System.currentTimeMillis() - timeElapsed < waitTime) {
final InboundEvent inboundEvent = eventInput.read();
if (inboundEvent == null) {
Assert.fail("Failed while verifying stream. Looks like connection has been closed.");
break;
}
String data = inboundEvent.readData(String.class);
System.out.println(data);
if (isStreamValid(data)) {
return;
}
}
Assert.fail("Failed while verifying stream");
}
Aggregations