Search in sources :

Example 16 with HttpDestinationOverHTTP

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

the class HttpClientTest method test_ExchangeIsComplete_WhenRequestFails_WithNoResponse.

@Test
public void test_ExchangeIsComplete_WhenRequestFails_WithNoResponse() throws Exception {
    start(new EmptyServerHandler());
    final CountDownLatch latch = new CountDownLatch(1);
    final String host = "localhost";
    final int port = connector.getLocalPort();
    client.newRequest(host, port).scheme(scheme).onRequestBegin(request -> {
        HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
        DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
        connectionPool.getActiveConnections().iterator().next().close();
    }).send(new Response.Listener.Adapter() {

        @Override
        public void onComplete(Result result) {
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Arrays(java.util.Arrays) EndPoint(org.eclipse.jetty.io.EndPoint) TestingDir(org.eclipse.jetty.toolchain.test.TestingDir) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) Request(org.eclipse.jetty.client.api.Request) ByteBuffer(java.nio.ByteBuffer) Assert.assertThat(org.junit.Assert.assertThat) ServerSocket(java.net.ServerSocket) HttpCookie(java.net.HttpCookie) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) URI(java.net.URI) Path(java.nio.file.Path) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) SocketAddressResolver(org.eclipse.jetty.util.SocketAddressResolver) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow) StandardOpenOption(java.nio.file.StandardOpenOption) IO(org.eclipse.jetty.util.IO) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) FuturePromise(org.eclipse.jetty.util.FuturePromise) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) BadMessageException(org.eclipse.jetty.http.BadMessageException) Connection(org.eclipse.jetty.client.api.Connection) Result(org.eclipse.jetty.client.api.Result) Socket(java.net.Socket) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpVersion(org.eclipse.jetty.http.HttpVersion) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) ArrayList(java.util.ArrayList) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) Assume(org.junit.Assume) NoSuchElementException(java.util.NoSuchElementException) OutputStream(java.io.OutputStream) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) Iterator(java.util.Iterator) Files(java.nio.file.Files) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) AtomicLong(java.util.concurrent.atomic.AtomicLong) URLEncoder(java.net.URLEncoder) HttpMethod(org.eclipse.jetty.http.HttpMethod) Rule(org.junit.Rule) Paths(java.nio.file.Paths) HttpField(org.eclipse.jetty.http.HttpField) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) Destination(org.eclipse.jetty.client.api.Destination) Assert(org.junit.Assert) Collections(java.util.Collections) InputStream(java.io.InputStream) Exchanger(java.util.concurrent.Exchanger) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) EndPoint(org.eclipse.jetty.io.EndPoint) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Example 17 with HttpDestinationOverHTTP

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

the class HttpClientUploadDuringServerShutdown method testUploadDuringServerShutdown.

@Test
public void testUploadDuringServerShutdown() throws Exception {
    final AtomicReference<EndPoint> endPointRef = new AtomicReference<>();
    final CountDownLatch serverLatch = new CountDownLatch(1);
    QueuedThreadPool serverThreads = new QueuedThreadPool();
    serverThreads.setName("server");
    Server server = new Server(serverThreads);
    ServerConnector connector = new ServerConnector(server);
    server.addConnector(connector);
    server.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            endPointRef.set(baseRequest.getHttpChannel().getEndPoint());
            serverLatch.countDown();
        }
    });
    server.start();
    final AtomicBoolean afterSetup = new AtomicBoolean();
    final CountDownLatch sendLatch = new CountDownLatch(1);
    final CountDownLatch beginLatch = new CountDownLatch(1);
    final CountDownLatch associateLatch = new CountDownLatch(1);
    QueuedThreadPool clientThreads = new QueuedThreadPool();
    clientThreads.setName("client");
    HttpClient client = new HttpClient(new HttpClientTransportOverHTTP(1) {

        @Override
        protected HttpConnectionOverHTTP newHttpConnection(EndPoint endPoint, HttpDestination destination, Promise<Connection> promise) {
            return new HttpConnectionOverHTTP(endPoint, destination, promise) {

                @Override
                protected HttpChannelOverHTTP newHttpChannel() {
                    return new HttpChannelOverHTTP(this) {

                        @Override
                        public void send() {
                            if (afterSetup.get()) {
                                associateLatch.countDown();
                            }
                            super.send();
                        }
                    };
                }

                @Override
                protected void close(Throwable failure) {
                    try {
                        sendLatch.countDown();
                        beginLatch.await(5, TimeUnit.SECONDS);
                        super.close(failure);
                    } catch (InterruptedException x) {
                        x.printStackTrace();
                    }
                }

                @Override
                protected boolean abort(Throwable failure) {
                    try {
                        associateLatch.await(5, TimeUnit.SECONDS);
                        return super.abort(failure);
                    } catch (InterruptedException x) {
                        x.printStackTrace();
                        return false;
                    }
                }
            };
        }
    }, null);
    client.setIdleTimeout(10000);
    client.setExecutor(clientThreads);
    client.start();
    // Create one connection.
    client.newRequest("localhost", connector.getLocalPort()).send();
    Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
    afterSetup.set(true);
    Thread.sleep(1000);
    // Close the connection, so that the receiver is woken
    // up and will call HttpConnectionOverHTTP.close().
    EndPoint endPoint = endPointRef.get();
    endPoint.close();
    // Wait for close() so that the connection that
    // is being closed is used to send the request.
    Assert.assertTrue(sendLatch.await(5, TimeUnit.SECONDS));
    final CountDownLatch completeLatch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).timeout(10, TimeUnit.SECONDS).onRequestBegin(request -> {
        try {
            beginLatch.countDown();
            completeLatch.await(5, TimeUnit.SECONDS);
        } catch (InterruptedException x) {
            x.printStackTrace();
        }
    }).send(result -> completeLatch.countDown());
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination("http", "localhost", connector.getLocalPort());
    DuplexConnectionPool pool = (DuplexConnectionPool) destination.getConnectionPool();
    Assert.assertEquals(0, pool.getConnectionCount());
    Assert.assertEquals(0, pool.getIdleConnections().size());
    Assert.assertEquals(0, pool.getActiveConnections().size());
}
Also used : Request(org.eclipse.jetty.server.Request) Connection(org.eclipse.jetty.client.api.Connection) EndPoint(org.eclipse.jetty.io.EndPoint) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) HttpChannelOverHTTP(org.eclipse.jetty.client.http.HttpChannelOverHTTP) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) HttpServletRequest(javax.servlet.http.HttpServletRequest) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Server(org.eclipse.jetty.server.Server) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) 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) ServerConnector(org.eclipse.jetty.server.ServerConnector) Assert(org.junit.Assert) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) InputStream(java.io.InputStream) Server(org.eclipse.jetty.server.Server) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) EndPoint(org.eclipse.jetty.io.EndPoint) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Connection(org.eclipse.jetty.client.api.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) HttpChannelOverHTTP(org.eclipse.jetty.client.http.HttpChannelOverHTTP) Test(org.junit.Test)

Example 18 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP 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 19 with HttpDestinationOverHTTP

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

the class HttpConnectionLifecycleTest method testConnectionForHTTP10ResponseIsRemoved.

@Test
public void testConnectionForHTTP10ResponseIsRemoved() 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());
    client.setStrictEventOrdering(false);
    ContentResponse response = client.newRequest(host, port).scheme(scheme).onResponseBegin(response1 -> {
        ((HttpResponse) response1).version(HttpVersion.HTTP_1_0);
    }).send();
    Assert.assertEquals(200, response.getStatus());
    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) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Connection(org.eclipse.jetty.client.api.Connection) Test(org.junit.Test)

Example 20 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP 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)

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