use of javax.ws.rs.sse.SseEventSource in project cxf by apache.
the class SseEventSourceImplTest method testNoReconnectWhenUnavailableIsReturned.
@Test
public void testNoReconnectWhenUnavailableIsReturned() {
try (SseEventSource eventSource = withNoReconnect(Type.UNAVAILABLE)) {
eventSource.open();
assertThat(eventSource.isOpen(), equalTo(false));
await().during(Duration.ofMillis(3000L)).untilAsserted(() -> assertThat(eventSource.isOpen(), equalTo(false)));
assertThat(events.size(), equalTo(0));
}
}
use of javax.ws.rs.sse.SseEventSource in project cxf by apache.
the class SseEventSourceImplTest method testReconnectAndTwoEventsReceived.
@Test
public void testReconnectAndTwoEventsReceived() throws InterruptedException, IOException {
try (SseEventSource eventSource = withReconnect(Type.EVENT_NO_RETRY)) {
eventSource.open();
assertThat(eventSource.isOpen(), equalTo(true));
// Allow the event processor to pull for events (200ms)
Thread.sleep(150L);
}
await().atMost(Duration.ofMillis(500L)).untilAsserted(() -> assertThat(events.size(), equalTo(2)));
assertThat(events.get(0).getId(), equalTo("1"));
assertThat(events.get(0).getComment(), equalTo("test comment"));
assertThat(events.get(0).readData(), equalTo("test data"));
assertThat(events.get(1).getId(), equalTo("1"));
assertThat(events.get(1).getComment(), equalTo("test comment"));
assertThat(events.get(1).readData(), equalTo("test data"));
}
use of javax.ws.rs.sse.SseEventSource in project cxf by apache.
the class SseEventSourceImplTest method testInvalidReconnectDelayInTheEvent.
@Test
public void testInvalidReconnectDelayInTheEvent() throws InterruptedException, IOException {
try (SseEventSource eventSource = withNoReconnect(Type.EVENT_BAD_RETRY)) {
eventSource.open();
assertThat(eventSource.isOpen(), equalTo(true));
// Allow the event processor to pull for events (150ms)
Thread.sleep(150L);
}
await().atMost(Duration.ofMillis(500L)).untilAsserted(() -> assertThat(events.size(), equalTo(1)));
assertThat(events.get(0).getId(), equalTo("1"));
assertThat(events.get(0).getReconnectDelay(), equalTo(-1L));
assertThat(events.get(0).getComment(), equalTo("test comment"));
assertThat(events.get(0).readData(), equalTo("test data"));
}
use of javax.ws.rs.sse.SseEventSource in project cxf by apache.
the class AbstractSseTest method testClientClosesEventSource.
@Test
public void testClientClosesEventSource() throws InterruptedException {
final WebTarget target = createWebTarget("/rest/api/bookstore/client-closes-connection/sse/0");
final Collection<Book> books = new ArrayList<>();
try (SseEventSource eventSource = SseEventSource.target(target).build()) {
eventSource.register(collect(books), System.out::println);
eventSource.open();
// wait for single event, close before server sends other 3
awaitEvents(200, books, 1);
// Only two out of 4 messages should be delivered, others should be discarded
final Response r = createWebClient("/rest/api/bookstore/client-closes-connection/received", MediaType.APPLICATION_JSON).put(null);
assertThat(r.getStatus(), equalTo(204));
assertThat(eventSource.close(1, TimeUnit.SECONDS), equalTo(true));
}
// Easing the test verification here, it does not work well for Atm + Jetty
assertThat(books, hasItems(new Book("New Book #1", 1)));
// Only two out of 4 messages should be delivered, others should be discarded
final Response r = createWebClient("/rest/api/bookstore/client-closes-connection/closed", MediaType.APPLICATION_JSON).put(null);
assertThat(r.getStatus(), equalTo(204));
// Give server some time to finish up the sink
Thread.sleep(2000);
// Only two out of 4 messages should be delivered, others should be discarded
final BookBroadcasterStats stats = createWebClient("/rest/api/bookstore/client-closes-connection/stats", MediaType.APPLICATION_JSON).get().readEntity(BookBroadcasterStats.class);
// Tomcat will feedback through onError callback, others through onComplete
assertThat(stats.isErrored(), equalTo(supportsErrorPropagation()));
// The sink should be in closed state
assertThat(stats.isWasClosed(), equalTo(true));
// The onClose callback should be called
assertThat(stats.isClosed(), equalTo(true));
// It is very hard to get the predictable match here, but at most
// 2 events could get through before the client's connection drop off
assertTrue(stats.getCompleted() == 2 || stats.getCompleted() == 1);
}
use of javax.ws.rs.sse.SseEventSource in project cxf by apache.
the class AbstractSseTest method testBooksSseContainerResponseFilterIsCalled.
@Test
public void testBooksSseContainerResponseFilterIsCalled() throws InterruptedException {
final WebTarget target = createWebTarget("/rest/api/bookstore/filtered/sse");
final Collection<Book> books = new ArrayList<>();
assertThat(createWebTarget("/rest/api/bookstore/filtered/stats").request().get(Integer.class), equalTo(0));
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
Thread.sleep(1000);
}
// Easing the test verification here, it does not work well for Atm + Jetty
assertTrue(books.isEmpty());
assertThat(createWebTarget("/rest/api/bookstore/filtered/stats").request().get(Integer.class), equalTo(1));
}
Aggregations