Search in sources :

Example 11 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.

the class HttpRequestAbortTest method testAbortOnContent.

@Test
public void testAbortOnContent() throws Exception {
    try (StacklessLogging suppressor = new StacklessLogging(org.eclipse.jetty.server.HttpChannel.class)) {
        CountDownLatch serverLatch = new CountDownLatch(1);
        start(new EmptyServerHandler() {

            @Override
            public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                try {
                    super.handle(target, baseRequest, request, response);
                    if (request.getDispatcherType() != DispatcherType.ERROR)
                        IO.copy(request.getInputStream(), response.getOutputStream());
                } finally {
                    serverLatch.countDown();
                }
            }
        });
        final Throwable cause = new Exception();
        final AtomicBoolean aborted = new AtomicBoolean();
        final CountDownLatch latch = new CountDownLatch(1);
        try {
            client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestContent((request, content) -> {
                aborted.set(request.abort(cause));
                latch.countDown();
            }).content(new ByteBufferContentProvider(ByteBuffer.wrap(new byte[] { 0 }), ByteBuffer.wrap(new byte[] { 1 })) {

                @Override
                public long getLength() {
                    return -1;
                }
            }).timeout(5, TimeUnit.SECONDS).send();
            Assert.fail();
        } catch (ExecutionException x) {
            Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
            if (aborted.get())
                Assert.assertSame(cause, x.getCause());
        }
        Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
        HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, "localhost", connector.getLocalPort());
        DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
        Assert.assertEquals(0, connectionPool.getConnectionCount());
        Assert.assertEquals(0, connectionPool.getActiveConnections().size());
        Assert.assertEquals(0, connectionPool.getIdleConnections().size());
    }
}
Also used : Result(org.eclipse.jetty.client.api.Result) 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) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) Test(org.junit.Test) Request(org.eclipse.jetty.client.api.Request) IO(org.eclipse.jetty.util.IO) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletRequest(javax.servlet.http.HttpServletRequest) DispatcherType(javax.servlet.DispatcherType) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Assert(org.junit.Assert) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 12 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.

the class HttpConnectionLifecycleTest method test_BadRequest_WithSlowRequest_RemovesConnection.

@Slow
@Test
public void test_BadRequest_WithSlowRequest_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());
    final long delay = 1000;
    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 onHeaders(Request request) {
            try {
                TimeUnit.MILLISECONDS.sleep(delay);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        @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(delay * 30, TimeUnit.MILLISECONDS));
    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) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 13 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.

the class HttpConnectionLifecycleTest method test_FailedRequest_RemovesConnection.

@Test
public void test_FailedRequest_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());
    final CountDownLatch beginLatch = new CountDownLatch(1);
    final CountDownLatch failureLatch = new CountDownLatch(2);
    client.newRequest(host, port).scheme(scheme).listener(new Request.Listener.Adapter() {

        @Override
        public void onBegin(Request request) {
            activeConnections.iterator().next().close();
            beginLatch.countDown();
        }

        @Override
        public void onFailure(Request request, Throwable failure) {
            failureLatch.countDown();
        }
    }).send(new Response.Listener.Adapter() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            Assert.assertEquals(0, idleConnections.size());
            Assert.assertEquals(0, activeConnections.size());
            failureLatch.countDown();
        }
    });
    Assert.assertTrue(beginLatch.await(30, TimeUnit.SECONDS));
    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) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Example 14 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.

the class ClientConnectionCloseTest method test_ClientConnectionClose_ServerPartialResponse_ClientIdleTimeout.

@Test
public void test_ClientConnectionClose_ServerPartialResponse_ClientIdleTimeout() throws Exception {
    long idleTimeout = 1000;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            ServletInputStream input = request.getInputStream();
            while (true) {
                int read = input.read();
                if (read < 0)
                    break;
            }
            response.getOutputStream().print("Hello");
            response.flushBuffer();
            try {
                Thread.sleep(2 * idleTimeout);
            } catch (InterruptedException x) {
                throw new InterruptedIOException();
            }
        }
    });
    String host = "localhost";
    int port = connector.getLocalPort();
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    DeferredContentProvider content = new DeferredContentProvider(ByteBuffer.allocate(8));
    CountDownLatch resultLatch = new CountDownLatch(1);
    client.newRequest(host, port).scheme(scheme).header(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()).content(content).idleTimeout(idleTimeout, TimeUnit.MILLISECONDS).onRequestSuccess(request -> {
        HttpConnectionOverHTTP connection = (HttpConnectionOverHTTP) connectionPool.getActiveConnections().iterator().next();
        Assert.assertFalse(connection.getEndPoint().isOutputShutdown());
    }).send(result -> {
        if (result.isFailed())
            resultLatch.countDown();
    });
    content.offer(ByteBuffer.allocate(8));
    content.close();
    Assert.assertTrue(resultLatch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
    Assert.assertEquals(0, connectionPool.getConnectionCount());
}
Also used : Request(org.eclipse.jetty.server.Request) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ServletInputStream(javax.servlet.ServletInputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) InterruptedIOException(java.io.InterruptedIOException) ByteBuffer(java.nio.ByteBuffer) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpStatus(org.eclipse.jetty.http.HttpStatus) Assert(org.junit.Assert) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) InterruptedIOException(java.io.InterruptedIOException) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Example 15 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.

the class ClientConnectionCloseTest method test_ClientConnectionClose_ServerDoesNotRespond_ClientIdleTimeout.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            request.startAsync();
        // Do not respond.
        }
    });
    String host = "localhost";
    int port = connector.getLocalPort();
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    CountDownLatch resultLatch = new CountDownLatch(1);
    long idleTimeout = 1000;
    client.newRequest(host, port).scheme(scheme).header(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()).idleTimeout(idleTimeout, TimeUnit.MILLISECONDS).onRequestSuccess(request -> {
        HttpConnectionOverHTTP connection = (HttpConnectionOverHTTP) connectionPool.getActiveConnections().iterator().next();
        Assert.assertFalse(connection.getEndPoint().isOutputShutdown());
    }).send(result -> {
        if (result.isFailed())
            resultLatch.countDown();
    });
    Assert.assertTrue(resultLatch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
    Assert.assertEquals(0, connectionPool.getConnectionCount());
}
Also used : Request(org.eclipse.jetty.server.Request) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ServletInputStream(javax.servlet.ServletInputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) InterruptedIOException(java.io.InterruptedIOException) ByteBuffer(java.nio.ByteBuffer) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpStatus(org.eclipse.jetty.http.HttpStatus) Assert(org.junit.Assert) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Aggregations

HttpDestinationOverHTTP (org.eclipse.jetty.client.http.HttpDestinationOverHTTP)30 Test (org.junit.Test)28 CountDownLatch (java.util.concurrent.CountDownLatch)23 HttpServletRequest (javax.servlet.http.HttpServletRequest)22 IOException (java.io.IOException)21 ServletException (javax.servlet.ServletException)21 HttpServletResponse (javax.servlet.http.HttpServletResponse)19 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)18 Request (org.eclipse.jetty.client.api.Request)17 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)14 Connection (org.eclipse.jetty.client.api.Connection)13 Result (org.eclipse.jetty.client.api.Result)12 ByteBuffer (java.nio.ByteBuffer)11 TimeUnit (java.util.concurrent.TimeUnit)11 Assert (org.junit.Assert)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)10 Response (org.eclipse.jetty.client.api.Response)9 ExecutionException (java.util.concurrent.ExecutionException)8 HttpConnectionOverHTTP (org.eclipse.jetty.client.http.HttpConnectionOverHTTP)8