Search in sources :

Example 1 with Book

use of org.apache.cxf.systest.jaxrs.resources.Book 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

ArrayList (java.util.ArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 WebTarget (javax.ws.rs.client.WebTarget)1 SseEventSource (javax.ws.rs.sse.SseEventSource)1 Book (org.apache.cxf.systest.jaxrs.resources.Book)1 Test (org.junit.jupiter.api.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1