@Test
public void testWithEventSource() throws IOException, NoSuchAlgorithmException, InterruptedException {
final WebTarget endpoint = target().register(SseFeature.class).path("events");
EventSource eventSource = EventSource.target(endpoint).build();
final CountDownLatch count = new CountDownLatch(MSG_COUNT);
final EventListener listener = new EventListener() {
@Override
public void onEvent(InboundEvent inboundEvent) {
try {
final Integer data = inboundEvent.readData(Integer.class);
System.out.println(inboundEvent.getName() + "; " + data);
Assert.assertEquals(SSE_NAME, inboundEvent.getName());
Assert.assertEquals(MSG_COUNT - count.getCount(), data.intValue());
count.countDown();
} catch (ProcessingException ex) {
throw new RuntimeException("Error when deserializing of data.", ex);
}
}
};
eventSource.register(listener, "message-to-client");
eventSource.open();
final boolean sent = latch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
Assert.assertTrue("Awaiting for SSE message has timeout. Not all message were sent.", sent);
final boolean handled = count.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
Assert.assertTrue("Awaiting for SSE message has timeout. Not all message were handled by the listener.", handled);
}