Search in sources :

Example 6 with SseEventSource

use of jakarta.ws.rs.sse.SseEventSource 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

SseEventSource (jakarta.ws.rs.sse.SseEventSource)6 WebTarget (jakarta.ws.rs.client.WebTarget)5 InboundSseEvent (jakarta.ws.rs.sse.InboundSseEvent)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