Search in sources :

Example 16 with SseEventSource

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

the class SseEventSourceImplTest method testNoReconnectAndNoEventsAreDetected.

@Test
public void testNoReconnectAndNoEventsAreDetected() throws InterruptedException, IOException {
    try (SseEventSource eventSource = withNoReconnect(Type.EVENT_BAD_NEW_LINES)) {
        eventSource.open();
        assertThat(eventSource.isOpen(), equalTo(true));
        // Allow the event processor to pull for events (150ms)
        Thread.sleep(150L);
    }
    assertThat(events.size(), equalTo(0));
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) Test(org.junit.Test)

Example 17 with SseEventSource

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

the class SseEventSourceImplTest method testConnectWithLastEventId.

@Test
public void testConnectWithLastEventId() throws InterruptedException, IOException {
    try (SseEventSource eventSource = withNoReconnect(Type.EVENT_LAST_EVENT_ID, "10")) {
        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("10"));
    assertThat(events.get(0).getReconnectDelay(), equalTo(10000L));
    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 18 with SseEventSource

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

the class SseEventSourceImplTest method testNoReconnectAndCloseTheStreamWhileEventIsBeingReceived.

@Test
public void testNoReconnectAndCloseTheStreamWhileEventIsBeingReceived() throws InterruptedException, IOException {
    try (SseEventSource eventSource = withNoReconnect(Type.BUSY)) {
        eventSource.open();
        assertThat(eventSource.isOpen(), equalTo(true));
        // Allow the event processor to pull for events (200ms)
        Thread.sleep(50L);
        assertThat(eventSource.close(100L, TimeUnit.MILLISECONDS), equalTo(true));
        assertThat(eventSource.isOpen(), equalTo(false));
    }
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) Test(org.junit.Test)

Example 19 with SseEventSource

use of javax.ws.rs.sse.SseEventSource in project jersey by jersey.

the class SseEventSinkToEventSourceTest method testWithSimpleSubscriber.

@Test
public void testWithSimpleSubscriber() {
    transmitLatch = new CountDownLatch(MSG_COUNT);
    final WebTarget endpoint = target().path("events");
    final List<InboundSseEvent> results = new ArrayList<>();
    try (final SseEventSource eventSource = SseEventSource.target(endpoint).build()) {
        final CountDownLatch receivedLatch = new CountDownLatch(3 * MSG_COUNT);
        eventSource.subscribe((event) -> {
            results.add(event);
            receivedLatch.countDown();
        });
        eventSource.open();
        final boolean allTransmitted = transmitLatch.await(5000, TimeUnit.MILLISECONDS);
        final boolean allReceived = receivedLatch.await(5000, TimeUnit.MILLISECONDS);
        Assert.assertTrue(allTransmitted);
        Assert.assertTrue(allReceived);
        Assert.assertEquals(30, results.size());
    } catch (final InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) ArrayList(java.util.ArrayList) InboundSseEvent(javax.ws.rs.sse.InboundSseEvent) WebTarget(javax.ws.rs.client.WebTarget) CountDownLatch(java.util.concurrent.CountDownLatch) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 20 with SseEventSource

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

the class SseEventSourceImplTest method testTryToCloseWhileConnecting.

@Test
public void testTryToCloseWhileConnecting() throws ExecutionException, InterruptedException {
    try (SseEventSource eventSource = withNoReconnect(Type.BUSY)) {
        final Future<?> future = executor.submit(() -> eventSource.open());
        // Wait a bit for open() to advance
        Thread.sleep(50L);
        eventSource.close();
        assertThat(future.get(), equalTo(null));
        assertThat(eventSource.isOpen(), equalTo(false));
    }
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) 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