use of jakarta.ws.rs.sse.SseEventSource 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");
}
use of jakarta.ws.rs.sse.SseEventSource 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");
}
use of jakarta.ws.rs.sse.SseEventSource 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;
}
use of jakarta.ws.rs.sse.SseEventSource in project jaxrs-api by eclipse-ee4j.
the class SseClient method consumeAllEvents.
private static void consumeAllEvents() {
// consumes all events, writes then on standard out (System.out::println)
try (final SseEventSource eventSource = SseEventSource.target(TARGET).build()) {
eventSource.register(System.out::println);
eventSource.open();
for (int counter = 0; counter < 5; counter++) {
TARGET.request().post(Entity.text("message " + counter));
}
// make sure all the events have time to arrive
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// consumes all events and all exceptions, writing both on standard out.
try (final SseEventSource eventSource = SseEventSource.target(TARGET).build()) {
eventSource.register(System.out::println, Throwable::printStackTrace);
eventSource.open();
for (int counter = 0; counter < 5; counter++) {
TARGET.request().post(Entity.text("message " + counter));
}
// make sure all the events have time to arrive
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// registers onComplete callback, which will print out a message "There will be no further events."
try (final SseEventSource eventSource = SseEventSource.target(TARGET).build()) {
eventSource.register(System.out::println, Throwable::printStackTrace, () -> System.out.println("There will be no further events."));
eventSource.open();
for (int counter = 0; counter < 5; counter++) {
TARGET.request().post(Entity.text("message " + counter));
}
// make sure all the events have time to arrive
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
use of jakarta.ws.rs.sse.SseEventSource 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());
}
Aggregations