Search in sources :

Example 1 with InboundSseEvent

use of jakarta.ws.rs.sse.InboundSseEvent in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method connectionLostFor1500msTest.

/*
   * @testName: connectionLostFor1500msTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:1242; JAXRS:JAVADOC:1197; JAXRS:JAVADOC:1198;
   * JAXRS:JAVADOC:1199; JAXRS:JAVADOC:1200; JAXRS:JAVADOC:1201;
   * JAXRS:JAVADOC:1202; JAXRS:JAVADOC:1241; JAXRS:JAVADOC:1240;
   * JAXRS:JAVADOC:1236;
   * 
   * @test_Strategy: reconnectingEvery should send request to the server just
   * twice, once without response, once after reconnect timeout with a response
   */
@Test
public void connectionLostFor1500msTest() throws Fault {
    resetUnavailableServer();
    // set to wait 3 seconds
    setRequestContentEntity("1");
    setProperty(Property.REQUEST, buildRequest(Request.POST, "su/lost"));
    setProperty(Property.REQUEST_HEADERS, buildContentType(MediaType.TEXT_PLAIN_TYPE));
    setProperty(Property.SEARCH_STRING, "1");
    invoke();
    Holder<InboundSseEvent> holder = new Holder<>();
    WebTarget target = ClientBuilder.newClient().target(getAbsoluteUrl("su/sselost"));
    try (SseEventSource source = SseEventSource.target(target).reconnectingEvery(2000L, TimeUnit.MILLISECONDS).build()) {
        source.register(holder::set);
        source.open();
        sleep = sleepUntilHolderGetsFilled(holder);
    }
    int cnt = getServerCount();
    logTrace("Slept for", sleep * millis, "ms");
    assertNotNull(holder.get(), "Message not received, reconnect was done", cnt - 1, "times.");
    logTrace("Received message", holder.get().readData());
    assertTrue(sleep > 3, "The message has been sent unexpectedly soon, sooner then 2000ms");
    assertTrue(sleep < 5, "The message has been sent unexpectedly late, after 3000ms");
    assertEquals(SSEMessage.MESSAGE, holder.get().readData(), "Unexpected message received", holder.get().readData());
    assertEquals(cnt, 2, "Server was reconnected", cnt, "times, unexpectedly");
}
Also used : SseEventSource(jakarta.ws.rs.sse.SseEventSource) LinkedHolder(ee.jakarta.tck.ws.rs.common.util.LinkedHolder) Holder(ee.jakarta.tck.ws.rs.common.util.Holder) InboundSseEvent(jakarta.ws.rs.sse.InboundSseEvent) WebTarget(jakarta.ws.rs.client.WebTarget) Test(org.junit.jupiter.api.Test)

Example 2 with InboundSseEvent

use of jakarta.ws.rs.sse.InboundSseEvent in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method defaultWaiting1s.

/* Run test */
/*
   * @testName: defaultWaiting1s
   * 
   * @assertion_ids: JAXRS:JAVADOC:1197; JAXRS:JAVADOC:1198; JAXRS:JAVADOC:1199;
   * JAXRS:JAVADOC:1200; JAXRS:JAVADOC:1201; JAXRS:JAVADOC:1202;
   * JAXRS:JAVADOC:1241; JAXRS:JAVADOC:1240; JAXRS:JAVADOC:1236;
   * 
   * @test_Strategy: In addition to handling the standard connection loss
   * failures, JAX-RS SseEventSource automatically deals with any HTTP 503
   * Service Unavailable responses from an SSE endpoint, that contain a
   * "Retry-After" HTTP header with a valid value. The HTTP 503 + "Retry-After"
   * technique is often used by HTTP endpoints as a means of connection and
   * traffic throttling. In case a HTTP 503 + "Retry-After" response is received
   * in return to a connection request, JAX-RS SSE event source will
   * automatically schedule a new reconnect attempt and use the received
   * "Retry-After" HTTP header value as a one-time override of the reconnect
   * delay.
   */
@Test
public void defaultWaiting1s() throws Fault {
    // define
    Holder<InboundSseEvent> holder = new Holder<>();
    WebTarget target = ClientBuilder.newClient().target(getAbsoluteUrl("su/sse"));
    // check its working
    try (SseEventSource source = SseEventSource.target(target).build()) {
        source.register(holder::set);
        source.open();
        sleepUntilHolderGetsFilled(holder);
    } catch (Exception e) {
        throw new Fault(e);
    }
    assertNotNull(holder.get(), "Holder was not filled");
    logTrace("Received message", holder.get().readData());
    holder.set(null);
    // set to return 503
    setProperty(Property.REQUEST, buildRequest(Request.GET, "su/available"));
    setProperty(Property.SEARCH_STRING, "OK");
    invoke();
    try (SseEventSource source = SseEventSource.target(target).build()) {
        source.register(holder::set);
        source.open();
    } catch (Exception e) {
        throw new Fault(e);
    }
    assertNull(holder.get(), "The event should be sent after default 1s in retry, not sooner");
    // SseEventSource.close() has been called
    // set to return 503
    setProperty(Property.REQUEST, buildRequest(Request.GET, "su/available"));
    setProperty(Property.SEARCH_STRING, "OK");
    invoke();
    holder = querySSEEndpoint("su/sse");
    logTrace("Received message", holder.get().readData());
    logTrace("Slept for", sleep * millis, "ms");
    assertTrue(sleep < 4, "The message has been sent unexpectedly late, after 2000ms");
}
Also used : SseEventSource(jakarta.ws.rs.sse.SseEventSource) LinkedHolder(ee.jakarta.tck.ws.rs.common.util.LinkedHolder) Holder(ee.jakarta.tck.ws.rs.common.util.Holder) InboundSseEvent(jakarta.ws.rs.sse.InboundSseEvent) WebTarget(jakarta.ws.rs.client.WebTarget) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 3 with InboundSseEvent

use of jakarta.ws.rs.sse.InboundSseEvent in project jaxrs-api by eclipse-ee4j.

the class SSEJAXRSClient method querySSEEndpoint.

protected Holder<InboundSseEvent> querySSEEndpoint(String endpoint, BiConsumer<SseEventSource, Holder<InboundSseEvent>> registrator) throws Fault {
    Holder<InboundSseEvent> holder = new Holder<>();
    WebTarget target = ClientBuilder.newClient().target(getAbsoluteUrl(endpoint));
    try (SseEventSource source = SseEventSource.target(target).build()) {
        registrator.accept(source, holder);
        source.open();
        sleep = sleepUntilHolderGetsFilled(holder);
        assertTrue(source.isOpen(), "SseEventSource#isOpen returns false");
    // } catch (Fault f) {
    // throw f;
    } catch (Exception e) {
        throw new Fault(e);
    }
    assertNotNull(holder.get(), "The message was not received");
    return holder;
}
Also used : SseEventSource(jakarta.ws.rs.sse.SseEventSource) Holder(ee.jakarta.tck.ws.rs.common.util.Holder) InboundSseEvent(jakarta.ws.rs.sse.InboundSseEvent) WebTarget(jakarta.ws.rs.client.WebTarget)

Example 4 with InboundSseEvent

use of jakarta.ws.rs.sse.InboundSseEvent in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method sseStageCheckTest.

/*
   * @testName: sseStageCheckTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:1232; JAXRS:JAVADOC:1217;
   * 
   * @test_Strategy: check the stage is ever done
   */
@Test
public void sseStageCheckTest() throws Fault {
    LinkedHolder<InboundSseEvent> holder = new LinkedHolder<>();
    WebTarget target = ClientBuilder.newClient().target(getAbsoluteUrl("stage"));
    try (SseEventSource source = SseEventSource.target(target).build()) {
        source.register(holder::add);
        source.open();
        sleepUntilHolderGetsFilled(holder);
        assertNotNull(holder.get(), "No message received");
        assertTrue(holder.get(0).readData().contains(SSEJAXRSClient.MESSAGE), holder.get(0) + "does not contain expected" + SSEJAXRSClient.MESSAGE);
        if (!holder.get(1).readData().contains(StageCheckerResource.DONE)) {
            sleepUntilHolderGetsFilled(holder);
        }
    }
    assertNotNull(holder.get(1), "No message received");
    assertEquals(holder.get(1).readData(), StageCheckerResource.DONE, "The stage has not ever been done, message is", holder.get().readData());
}
Also used : LinkedHolder(ee.jakarta.tck.ws.rs.common.util.LinkedHolder) SseEventSource(jakarta.ws.rs.sse.SseEventSource) InboundSseEvent(jakarta.ws.rs.sse.InboundSseEvent) WebTarget(jakarta.ws.rs.client.WebTarget) Test(org.junit.jupiter.api.Test)

Example 5 with InboundSseEvent

use of jakarta.ws.rs.sse.InboundSseEvent in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method closeTest.

/*
   * @testName: closeTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:1233; JAXRS:JAVADOC:1197; JAXRS:JAVADOC:1198;
   * JAXRS:JAVADOC:1199; JAXRS:JAVADOC:1200; JAXRS:JAVADOC:1201;
   * JAXRS:JAVADOC:1202; JAXRS:JAVADOC:1241; JAXRS:JAVADOC:1240;
   * JAXRS:JAVADOC:1236;
   * 
   * @test_Strategy:
   */
@Test
public void closeTest() throws Fault {
    boolean isOpen = true;
    LinkedHolder<InboundSseEvent> holder = new LinkedHolder<>();
    WebTarget target = ClientBuilder.newClient().target(getAbsoluteUrl("repeat/cast"));
    // set clear
    setRequestContentEntity(Boolean.TRUE);
    setProperty(Property.REQUEST, buildRequest(Request.POST, "repeat/set"));
    setProperty(Property.REQUEST_HEADERS, buildContentType(MediaType.TEXT_PLAIN_TYPE));
    invoke();
    assertTrue(getResponse().readEntity(Boolean.class), "Cast has not been set");
    // ask for a message
    SseEventSource source = SseEventSource.target(target).build();
    source.register(holder::add);
    source.open();
    sleepUntilHolderGetsFilled(holder);
    assertNotNull(holder.get(), "Message was not received");
    for (InboundSseEvent e : holder) logMsg("Received message no", e.readData());
    // check the session is opened
    setProperty(Property.REQUEST, buildRequest(Request.GET, "repeat/isopen"));
    invoke();
    isOpen = getResponseBody(Boolean.class);
    assertTrue(isOpen, "SseEventSource is closed");
    // Wait 3 times
    for (int i = 0; i != 3; i++) {
        holder.clear();
        sleepUntilHolderGetsFilled(holder);
        for (InboundSseEvent e : holder) logMsg("Received message no", e.readData());
    }
    // close
    source.close();
    assertFalse(source.isOpen(), "SseEventSource was not closed");
    // check it was closed
    int cnt = 0;
    int size = holder.size();
    try {
        while (isOpen && cnt < 20) {
            // check the server sends events
            holder.clear();
            cnt++;
            setProperty(Property.REQUEST, buildRequest(Request.GET, "repeat/isopen"));
            invoke();
            isOpen = getResponseBody(Boolean.class);
            assertFalse(size == 0 && isOpen, "Message was not received and SseEventSink is open");
            sleepUntilHolderGetsFilled(holder);
            for (InboundSseEvent e : holder) logMsg("Received message no", e.readData());
            size = holder.size();
        }
    } finally {
        System.out.println("Sending false");
        setRequestContentEntity(Boolean.FALSE);
        setProperty(Property.REQUEST, buildRequest(Request.POST, "repeat/set"));
        setProperty(Property.REQUEST_HEADERS, buildContentType(MediaType.TEXT_PLAIN_TYPE));
        invoke();
        System.out.println("Sent false");
    }
}
Also used : LinkedHolder(ee.jakarta.tck.ws.rs.common.util.LinkedHolder) SseEventSource(jakarta.ws.rs.sse.SseEventSource) InboundSseEvent(jakarta.ws.rs.sse.InboundSseEvent) WebTarget(jakarta.ws.rs.client.WebTarget) Test(org.junit.jupiter.api.Test)

Aggregations

WebTarget (jakarta.ws.rs.client.WebTarget)5 InboundSseEvent (jakarta.ws.rs.sse.InboundSseEvent)5 SseEventSource (jakarta.ws.rs.sse.SseEventSource)5 LinkedHolder (ee.jakarta.tck.ws.rs.common.util.LinkedHolder)4 Test (org.junit.jupiter.api.Test)4 Holder (ee.jakarta.tck.ws.rs.common.util.Holder)3 IOException (java.io.IOException)1