Search in sources :

Example 36 with SseEventSource

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

Example 37 with SseEventSource

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

Example 38 with SseEventSource

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")));
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) Book(org.apache.cxf.systest.jaxrs.resources.Book) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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