Search in sources :

Example 31 with SseEventSource

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));
    }
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) Test(org.junit.Test)

Example 32 with SseEventSource

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"));
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) Test(org.junit.Test)

Example 33 with SseEventSource

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"));
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) Test(org.junit.Test)

Example 34 with SseEventSource

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);
}
Also used : Response(javax.ws.rs.core.Response) SseEventSource(javax.ws.rs.sse.SseEventSource) ArrayList(java.util.ArrayList) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 35 with SseEventSource

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));
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) ArrayList(java.util.ArrayList) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test)

Aggregations

SseEventSource (javax.ws.rs.sse.SseEventSource)38 Test (org.junit.Test)34 WebTarget (javax.ws.rs.client.WebTarget)13 ArrayList (java.util.ArrayList)11 CountDownLatch (java.util.concurrent.CountDownLatch)4 JerseyTest (org.glassfish.jersey.test.JerseyTest)3 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 JsonObject (javax.json.JsonObject)1 Client (javax.ws.rs.client.Client)1 Response (javax.ws.rs.core.Response)1 InboundSseEvent (javax.ws.rs.sse.InboundSseEvent)1 Builder (javax.ws.rs.sse.SseEventSource.Builder)1 Book (org.apache.cxf.systest.jaxrs.resources.Book)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Test (org.junit.jupiter.api.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1