Search in sources :

Example 6 with SseEventSource

use of javax.ws.rs.sse.SseEventSource in project cxf by apache.

the class AbstractSseTest method testNoDataIsReturnedFromInboundSseEvents.

@Test
public void testNoDataIsReturnedFromInboundSseEvents() throws InterruptedException {
    final WebTarget target = createWebTarget("/rest/api/bookstore/nodata");
    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
        Thread.sleep(1000);
    }
    // Easing the test verification here, it does not work well for Atm + Jetty
    assertTrue(books.isEmpty());
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) ArrayList(java.util.ArrayList) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 7 with SseEventSource

use of javax.ws.rs.sse.SseEventSource in project cxf by apache.

the class AbstractSseTest method testBookTitlesStreamIsReturnedFromInboundSseEvents.

@Test
public void testBookTitlesStreamIsReturnedFromInboundSseEvents() throws InterruptedException {
    final WebTarget target = createWebTarget("/rest/api/bookstore/titles/sse");
    final Collection<String> titles = new ArrayList<>();
    try (SseEventSource eventSource = SseEventSource.target(target).build()) {
        eventSource.register(collectRaw(titles), System.out::println);
        eventSource.open();
        // Give the SSE stream some time to collect all events
        awaitEvents(5000, titles, 4);
    }
    // Easing the test verification here, it does not work well for Atm + Jetty
    assertThat(titles, hasItems("New Book #1", "New Book #2", "New Book #3", "New Book #4"));
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) ArrayList(java.util.ArrayList) WebTarget(javax.ws.rs.client.WebTarget) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 8 with SseEventSource

use of javax.ws.rs.sse.SseEventSource in project cxf by apache.

the class AbstractSseTest method testBooksStreamIsReturnedFromInboundSseEvents.

@Test
public void testBooksStreamIsReturnedFromInboundSseEvents() throws InterruptedException {
    final WebTarget target = createWebTarget("/rest/api/bookstore/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, 4);
    }
    // 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)));
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) ArrayList(java.util.ArrayList) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 9 with SseEventSource

use of javax.ws.rs.sse.SseEventSource in project cxf by apache.

the class SseEventSourceImplTest method testNoReconnectAndNotAuthorized.

@Test
public void testNoReconnectAndNotAuthorized() throws InterruptedException, IOException {
    try (SseEventSource eventSource = withNoReconnect(Type.EVENT_NOT_AUTHORIZED)) {
        eventSource.open();
        assertThat(eventSource.isOpen(), equalTo(false));
        assertThat(errors.size(), equalTo(1));
        // Allow the event processor to pull for events (150ms)
        Thread.sleep(150L);
    }
    await().atMost(Duration.ofMillis(500L)).untilAsserted(() -> assertThat(errors.size(), equalTo(1)));
    assertThat(events.size(), equalTo(0));
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) Test(org.junit.Test)

Example 10 with SseEventSource

use of javax.ws.rs.sse.SseEventSource in project cxf by apache.

the class SseEventSourceImplTest method withNoReconnect.

private SseEventSource withNoReconnect(Type type, String lastEventId, Map<String, Object> properties) {
    SseEventSource eventSource = SseEventSource.target(target(type, lastEventId, properties)).build();
    eventSource.register(events::add, errors::add);
    return eventSource;
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource)

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