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));
}
}
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));
}
}
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));
}
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"));
}
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;
}
Aggregations