Search in sources :

Example 11 with SseEventSource

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

the class SseEventSourceImplTest method testWhenTryToConnectTwiceSecondAttemtShouldFail.

@Test
public void testWhenTryToConnectTwiceSecondAttemtShouldFail() throws InterruptedException, ExecutionException {
    try (SseEventSource eventSource = withReconnect(Type.BUSY)) {
        eventSource.open();
        // The attempt to open the SSE connection in another thread at the same
        // time should fail
        final Future<?> future = executor.submit(() -> eventSource.open());
        exception.expectCause(instanceOf(IllegalStateException.class));
        assertThat(future.get(), equalTo(null));
        assertThat(eventSource.isOpen(), equalTo(true));
        assertThat(events.size(), equalTo(1));
    }
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) Test(org.junit.Test)

Example 12 with SseEventSource

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

the class SseEventSourceImplTest method testReconnectWillBeScheduledOnError.

@Test
public void testReconnectWillBeScheduledOnError() throws InterruptedException {
    try (SseEventSource eventSource = withReconnect(Type.NO_SERVER)) {
        eventSource.open();
        assertThat(eventSource.isOpen(), equalTo(false));
        // Sleep a little bit for reconnect to reschedule
        Thread.sleep(150L);
        assertThat(errors.size(), equalTo(2));
    }
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) Test(org.junit.Test)

Example 13 with SseEventSource

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

the class SseEventSourceImplTest method testNoReconnectAndIncompleteEventIsDiscarded.

@Test
public void testNoReconnectAndIncompleteEventIsDiscarded() throws InterruptedException, IOException {
    try (SseEventSource eventSource = withNoReconnect(Type.EVENT_JUST_NAME)) {
        eventSource.open();
        assertThat(eventSource.isOpen(), equalTo(true));
        // Allow the event processor to pull for events (150ms)
        Thread.sleep(150L);
    }
    // incomplete event should be discarded
    await().during(Duration.ofMillis(500L)).until(events::isEmpty);
    assertThat(events.size(), equalTo(0));
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) Test(org.junit.Test)

Example 14 with SseEventSource

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

the class SseEventSourceImplTest method testReconnectWithLastEventId.

@Test
public void testReconnectWithLastEventId() throws InterruptedException, IOException {
    try (SseEventSource eventSource = withReconnect(Type.EVENT_RETRY_LAST_EVENT_ID, "10")) {
        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(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 15 with SseEventSource

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

the class SseEventSourceImplTest method withReconnect.

private SseEventSource withReconnect(Type type, String lastEventId) {
    SseEventSource eventSource = SseEventSource.target(target(type, lastEventId)).reconnectingEvery(100L, TimeUnit.MILLISECONDS).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