Search in sources :

Example 1 with EventInput

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();
}
Also used : Response(javax.ws.rs.core.Response) EventInput(org.glassfish.jersey.media.sse.EventInput) Matchers.containsString(org.hamcrest.Matchers.containsString) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 2 with EventInput

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());
}
Also used : Response(javax.ws.rs.core.Response) EventInput(org.glassfish.jersey.media.sse.EventInput) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 3 with EventInput

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);
}
Also used : EventInput(org.glassfish.jersey.media.sse.EventInput) ArrayList(java.util.ArrayList) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 4 with EventInput

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();
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ProcessingException(javax.ws.rs.ProcessingException) SseFeature(org.glassfish.jersey.media.sse.SseFeature) InboundEvent(org.glassfish.jersey.media.sse.InboundEvent) EventInput(org.glassfish.jersey.media.sse.EventInput) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) List(java.util.List) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 5 with EventInput

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();
}
Also used : EventInput(org.glassfish.jersey.media.sse.EventInput) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Aggregations

EventInput (org.glassfish.jersey.media.sse.EventInput)5 JerseyTest (org.glassfish.jersey.test.JerseyTest)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)2 Response (javax.ws.rs.core.Response)2 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 ProcessingException (javax.ws.rs.ProcessingException)1 ServiceUnavailableException (javax.ws.rs.ServiceUnavailableException)1 InboundEvent (org.glassfish.jersey.media.sse.InboundEvent)1 SseFeature (org.glassfish.jersey.media.sse.SseFeature)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1