Search in sources :

Example 61 with Result

use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.

the class HttpConnectionLifecycleTest method test_ConnectionFailure_RemovesConnection.

@Test
public void test_ConnectionFailure_RemovesConnection() throws Exception {
    start(new EmptyServerHandler());
    String host = "localhost";
    int port = connector.getLocalPort();
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    final Collection<Connection> idleConnections = connectionPool.getIdleConnections();
    Assert.assertEquals(0, idleConnections.size());
    final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
    Assert.assertEquals(0, activeConnections.size());
    server.stop();
    final CountDownLatch failureLatch = new CountDownLatch(2);
    client.newRequest(host, port).scheme(scheme).onRequestFailure((request, failure) -> failureLatch.countDown()).send(result -> {
        Assert.assertTrue(result.isFailed());
        failureLatch.countDown();
    });
    Assert.assertTrue(failureLatch.await(30, TimeUnit.SECONDS));
    Assert.assertEquals(0, idleConnections.size());
    Assert.assertEquals(0, activeConnections.size());
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) Result(org.eclipse.jetty.client.api.Result) Handler(org.eclipse.jetty.server.Handler) Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) HttpVersion(org.eclipse.jetty.http.HttpVersion) Request(org.eclipse.jetty.client.api.Request) ByteBuffer(java.nio.ByteBuffer) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) Log(org.eclipse.jetty.util.log.Log) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Queue(java.util.Queue) Assert(org.junit.Assert) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Connection(org.eclipse.jetty.client.api.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 62 with Result

use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.

the class HttpConnectionLifecycleTest method test_SuccessfulRequest_ReturnsConnection.

@Test
public void test_SuccessfulRequest_ReturnsConnection() throws Exception {
    start(new EmptyServerHandler());
    String host = "localhost";
    int port = connector.getLocalPort();
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    final Collection<Connection> idleConnections = connectionPool.getIdleConnections();
    Assert.assertEquals(0, idleConnections.size());
    final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
    Assert.assertEquals(0, activeConnections.size());
    final CountDownLatch headersLatch = new CountDownLatch(1);
    final CountDownLatch successLatch = new CountDownLatch(3);
    client.newRequest(host, port).scheme(scheme).onRequestSuccess(request -> successLatch.countDown()).onResponseHeaders(response -> {
        Assert.assertEquals(0, idleConnections.size());
        Assert.assertEquals(1, activeConnections.size());
        headersLatch.countDown();
    }).send(new Response.Listener.Adapter() {

        @Override
        public void onSuccess(Response response) {
            successLatch.countDown();
        }

        @Override
        public void onComplete(Result result) {
            Assert.assertFalse(result.isFailed());
            successLatch.countDown();
        }
    });
    Assert.assertTrue(headersLatch.await(30, TimeUnit.SECONDS));
    Assert.assertTrue(successLatch.await(30, TimeUnit.SECONDS));
    Assert.assertEquals(1, idleConnections.size());
    Assert.assertEquals(0, activeConnections.size());
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) Result(org.eclipse.jetty.client.api.Result) Handler(org.eclipse.jetty.server.Handler) Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) HttpVersion(org.eclipse.jetty.http.HttpVersion) Request(org.eclipse.jetty.client.api.Request) ByteBuffer(java.nio.ByteBuffer) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) Log(org.eclipse.jetty.util.log.Log) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Queue(java.util.Queue) Assert(org.junit.Assert) Connection(org.eclipse.jetty.client.api.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Example 63 with Result

use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.

the class HttpConnectionLifecycleTest method test_BadRequest_RemovesConnection.

@Test
public void test_BadRequest_RemovesConnection() throws Exception {
    start(new EmptyServerHandler());
    String host = "localhost";
    int port = connector.getLocalPort();
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    final Queue<Connection> idleConnections = connectionPool.getIdleConnections();
    Assert.assertEquals(0, idleConnections.size());
    final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
    Assert.assertEquals(0, activeConnections.size());
    final CountDownLatch successLatch = new CountDownLatch(3);
    client.newRequest(host, port).scheme(scheme).listener(new Request.Listener.Adapter() {

        @Override
        public void onBegin(Request request) {
            // Remove the host header, this will make the request invalid
            request.header(HttpHeader.HOST, null);
        }

        @Override
        public void onSuccess(Request request) {
            successLatch.countDown();
        }
    }).send(new Response.Listener.Adapter() {

        @Override
        public void onSuccess(Response response) {
            Assert.assertEquals(400, response.getStatus());
            // 400 response also come with a Connection: close,
            // so the connection is closed and removed
            successLatch.countDown();
        }

        @Override
        public void onComplete(Result result) {
            Assert.assertFalse(result.isFailed());
            successLatch.countDown();
        }
    });
    Assert.assertTrue(successLatch.await(30, TimeUnit.SECONDS));
    Assert.assertEquals(0, idleConnections.size());
    Assert.assertEquals(0, activeConnections.size());
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Example 64 with Result

use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.

the class HttpConnectionLifecycleTest method test_BigRequestContent_ResponseWithConnectionCloseHeader_RemovesConnection.

@Test
public void test_BigRequestContent_ResponseWithConnectionCloseHeader_RemovesConnection() throws Exception {
    try (StacklessLogging stackless = new StacklessLogging(HttpConnection.class)) {
        start(new AbstractHandler() {

            @Override
            public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                response.setHeader("Connection", "close");
                baseRequest.setHandled(true);
            // Don't read request content; this causes the server parser to be closed
            }
        });
        String host = "localhost";
        int port = connector.getLocalPort();
        HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
        DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
        final Collection<Connection> idleConnections = connectionPool.getIdleConnections();
        Assert.assertEquals(0, idleConnections.size());
        final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
        Assert.assertEquals(0, activeConnections.size());
        Log.getLogger(HttpConnection.class).info("Expecting java.lang.IllegalStateException: HttpParser{s=CLOSED,...");
        final CountDownLatch latch = new CountDownLatch(1);
        ByteBuffer buffer = ByteBuffer.allocate(16 * 1024 * 1024);
        Arrays.fill(buffer.array(), (byte) 'x');
        client.newRequest(host, port).scheme(scheme).content(new ByteBufferContentProvider(buffer)).send(new Response.Listener.Adapter() {

            @Override
            public void onComplete(Result result) {
                Assert.assertEquals(1, latch.getCount());
                Assert.assertEquals(0, idleConnections.size());
                Assert.assertEquals(0, activeConnections.size());
                latch.countDown();
            }
        });
        Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
        Assert.assertEquals(0, idleConnections.size());
        Assert.assertEquals(0, activeConnections.size());
        server.stop();
    }
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Test(org.junit.Test)

Example 65 with Result

use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.

the class HttpConnectionLifecycleTest method test_ResponseWithConnectionCloseHeader_RemovesConnection.

@Test
public void test_ResponseWithConnectionCloseHeader_RemovesConnection() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            response.setHeader("Connection", "close");
            baseRequest.setHandled(true);
        }
    });
    String host = "localhost";
    int port = connector.getLocalPort();
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    final Collection<Connection> idleConnections = connectionPool.getIdleConnections();
    Assert.assertEquals(0, idleConnections.size());
    final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
    Assert.assertEquals(0, activeConnections.size());
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(host, port).scheme(scheme).send(new Response.Listener.Adapter() {

        @Override
        public void onComplete(Result result) {
            Assert.assertFalse(result.isFailed());
            Assert.assertEquals(0, idleConnections.size());
            Assert.assertEquals(0, activeConnections.size());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
    Assert.assertEquals(0, idleConnections.size());
    Assert.assertEquals(0, activeConnections.size());
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Aggregations

Result (org.eclipse.jetty.client.api.Result)100 Test (org.junit.Test)80 CountDownLatch (java.util.concurrent.CountDownLatch)79 HttpServletResponse (javax.servlet.http.HttpServletResponse)74 HttpServletRequest (javax.servlet.http.HttpServletRequest)66 Response (org.eclipse.jetty.client.api.Response)62 IOException (java.io.IOException)60 ServletException (javax.servlet.ServletException)56 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)44 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)41 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)39 Request (org.eclipse.jetty.client.api.Request)31 ByteBuffer (java.nio.ByteBuffer)29 Request (org.eclipse.jetty.server.Request)27 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)24 InterruptedIOException (java.io.InterruptedIOException)21 ServletOutputStream (javax.servlet.ServletOutputStream)20 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)16