use of javax.ws.rs.sse.SseEventSource in project cxf by apache.
the class AbstractSseTest method testBooksStreamIsReturnedFromInboundSseEventsNoDelay.
@Test
public void testBooksStreamIsReturnedFromInboundSseEventsNoDelay() throws InterruptedException {
final WebTarget target = createWebTarget("/rest/api/bookstore/nodelay/sse/0");
final Collection<Book> books = new ArrayList<>();
try (SseEventSource eventSource = SseEventSource.target(target).build()) {
eventSource.register(collect(books), System.out::println);
eventSource.open();
// Give the SSE stream some time to collect all events
awaitEvents(5000, books, 5);
}
// Easing the test verification here, it does not work well for Atm + Jetty
assertThat(books, hasItems(new Book("New Book #1", 1), new Book("New Book #2", 2), new Book("New Book #3", 3), new Book("New Book #4", 4), new Book("New Book #5", 5)));
}
use of javax.ws.rs.sse.SseEventSource in project cxf by apache.
the class AbstractSseTest method testBooksStreamIsReconnectedFromInboundSseEvents.
@Test
public void testBooksStreamIsReconnectedFromInboundSseEvents() throws InterruptedException {
final WebTarget target = createWebTarget("/rest/api/bookstore/sse/0");
final Collection<Book> books = new ArrayList<>();
final Builder builder = SseEventSource.target(target).reconnectingEvery(1, TimeUnit.SECONDS);
try (SseEventSource eventSource = builder.build()) {
eventSource.register(collect(books), System.out::println);
eventSource.open();
// Give the SSE stream some time to collect all events
awaitEvents(5000, books, 12);
}
assertThat(books, hasItems(new Book("New Book #1", 1), new Book("New Book #2", 2), new Book("New Book #3", 3), new Book("New Book #4", 4), new Book("New Book #5", 5), new Book("New Book #6", 6), new Book("New Book #7", 7), new Book("New Book #8", 8), new Book("New Book #9", 9), new Book("New Book #10", 10), new Book("New Book #11", 11), new Book("New Book #12", 12)));
}
use of javax.ws.rs.sse.SseEventSource in project cxf by apache.
the class SpringSseEmitterTest method testSseEvents.
@Test
public void testSseEvents() throws InterruptedException {
final WebTarget target = createWebTarget();
final Collection<Book> books = new ArrayList<>(CNT);
final AtomicReference<Throwable> throwable = new AtomicReference<>();
try (SseEventSource eventSource = SseEventSource.target(target).build()) {
eventSource.register(event -> {
books.add(event.readData(Book.class, javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE));
if (books.size() == CNT) {
synchronized (books) {
books.notify();
}
}
}, e -> throwable.set(e));
eventSource.open();
// Give the SSE stream some time to collect all events
synchronized (books) {
books.wait(5000L);
}
}
assertThat(throwable.get(), nullValue());
assertThat(books, hasItems(new Book("New Book #1", "Author #1"), new Book("New Book #2", "Author #2"), new Book("New Book #3", "Author #3"), new Book("New Book #4", "Author #4"), new Book("New Book #5", "Author #5")));
}
Aggregations