use of org.glassfish.jersey.media.sse.EventInput in project jersey by jersey.
the class EventOutputTest method testSseContentTypeWithCharset.
@Test
public void testSseContentTypeWithCharset() {
/**
* Need to disable HTTP Keep-Alive to prevent this test from hanging in HttpURLConnection
* due to an attempt to read from a stale, out-of-sync connection closed by the server.
* Thus setting the "Connection: close" HTTP header on all requests.
*/
Response r;
r = target().path("test/charset").request().header("Connection", "close").get();
assertTrue(r.getMediaType().getParameters().get("charset").equalsIgnoreCase("utf-8"));
final EventInput eventInput = r.readEntity(EventInput.class);
String eventData = eventInput.read().readData();
assertEquals("charset", eventData);
eventInput.close();
}
use of org.glassfish.jersey.media.sse.EventInput in project jersey by jersey.
the class EventOutputTest method testReadFromClosedOutput.
@Test
public void testReadFromClosedOutput() throws Exception {
/**
* Need to disable HTTP Keep-Alive to prevent this test from hanging in HttpURLConnection
* due to an attempt to read from a stale, out-of-sync connection closed by the server.
* Thus setting the "Connection: close" HTTP header on all requests.
*/
Response r;
r = target().path("test/closed-empty").request().header("Connection", "close").get();
assertTrue(r.readEntity(String.class).isEmpty());
r = target().path("test/closed-single").request().header("Connection", "close").get();
assertTrue(r.readEntity(String.class).contains("closed"));
//
EventInput input;
input = target().path("test/closed-single").request().header("Connection", "close").get(EventInput.class);
assertEquals("closed", input.read().readData());
assertEquals(null, input.read());
assertTrue(input.isClosed());
input = target().path("test/closed-empty").request().header("Connection", "close").get(EventInput.class);
assertEquals(null, input.read());
assertTrue(input.isClosed());
}
use of org.glassfish.jersey.media.sse.EventInput in project Hystrix by Netflix.
the class HystricsMetricsControllerTest method testConcurrency.
@Test
public void testConcurrency() throws Exception {
// Execute a Hystrix command so that metrics are initialized.
executeHystrixCommand();
List<EventInput> streamList = new ArrayList<EventInput>();
// Fire 5 requests, validate their responses and hold these connections.
for (int i = 0; i < 5; i++) {
EventInput stream = getStream();
System.out.println("Received Response for Request#" + (i + 1));
streamList.add(stream);
validateStream(stream, 1000);
System.out.println("Validated Response#" + (i + 1));
}
// Sixth request should fail since max configured connection is 5.
try {
streamList.add(getStreamFailFast());
Assert.fail("Expected 'ServiceUnavailableException' but, request went through.");
} catch (ServiceUnavailableException e) {
System.out.println("Got ServiceUnavailableException as expected.");
}
// Close one of the connections
streamList.get(0).close();
// Try again after closing one of the connections. This request should go through.
EventInput eventInput = getStream();
streamList.add(eventInput);
validateStream(eventInput, 1000);
}
use of org.glassfish.jersey.media.sse.EventInput in project jersey by jersey.
the class ServerSentEventsTest method testInboundEventReader.
/**
* Test consuming multiple SSE events sequentially using event input.
*
* @throws Exception in case of a failure during the test execution.
*/
@Test
public void testInboundEventReader() throws Exception {
final int MAX_MESSAGES = 5;
final CountDownLatch startLatch = new CountDownLatch(1);
final ExecutorService executor = Executors.newSingleThreadExecutor();
try {
final Future<List<String>> futureMessages = executor.submit(new Callable<List<String>>() {
@Override
public List<String> call() throws Exception {
final EventInput eventInput = target(App.ROOT_PATH).register(SseFeature.class).request().get(EventInput.class);
startLatch.countDown();
final List<String> messages = new ArrayList<String>(MAX_MESSAGES);
try {
for (int i = 0; i < MAX_MESSAGES; i++) {
InboundEvent event = eventInput.read();
messages.add(event.readData());
}
} finally {
if (eventInput != null) {
eventInput.close();
}
}
return messages;
}
});
assertTrue("Waiting for receiver thread to start has timed out.", startLatch.await(5, TimeUnit.SECONDS));
for (int i = 0; i < MAX_MESSAGES; i++) {
target(App.ROOT_PATH).request().post(Entity.text("message " + i));
}
int i = 0;
for (String message : futureMessages.get(5000, TimeUnit.SECONDS)) {
assertThat("Unexpected SSE event data value.", message, equalTo("message " + i++));
}
} finally {
executor.shutdownNow();
}
}
use of org.glassfish.jersey.media.sse.EventInput in project Hystrix by Netflix.
the class HystricsMetricsControllerTest method testInfiniteStream.
@Test
public void testInfiniteStream() throws Exception {
// Execute a Hystrix command so that metrics are initialized.
executeHystrixCommand();
// Invoke Stream API which returns a steady stream output.
EventInput stream = getStream();
// Validate the stream.
validateStream(stream, 1000);
System.out.println("Validated Stream Output 1");
// Execute Hystrix Command again so that request count is updated.
executeHystrixCommand();
// Stream should show updated request count
validateStream(stream, 1000);
System.out.println("Validated Stream Output 2");
stream.close();
}
Aggregations