Search in sources :

Example 11 with Connection

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

the class HttpClientTimeoutTest method testTimeoutIsCancelledOnSuccessWithExplicitConnection.

@Slow
@Test
public void testTimeoutIsCancelledOnSuccessWithExplicitConnection() throws Exception {
    long timeout = 1000;
    start(new TimeoutHandler(timeout));
    final CountDownLatch latch = new CountDownLatch(1);
    Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
    FuturePromise<Connection> futureConnection = new FuturePromise<>();
    destination.newConnection(futureConnection);
    try (Connection connection = futureConnection.get(5, TimeUnit.SECONDS)) {
        Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(scheme).timeout(2 * timeout, TimeUnit.MILLISECONDS);
        connection.send(request, new Response.CompleteListener() {

            @Override
            public void onComplete(Result result) {
                Response response = result.getResponse();
                Assert.assertEquals(200, response.getStatus());
                Assert.assertFalse(result.isFailed());
                latch.countDown();
            }
        });
        Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
        TimeUnit.MILLISECONDS.sleep(2 * timeout);
        Assert.assertNull(request.getAbortCause());
    }
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Destination(org.eclipse.jetty.client.api.Destination) FuturePromise(org.eclipse.jetty.util.FuturePromise) Connection(org.eclipse.jetty.client.api.Connection) SslConnection(org.eclipse.jetty.io.ssl.SslConnection) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 12 with Connection

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

the class HttpDestinationOverHTTPTest method test_Acquire_Process_Release_Acquire_ReturnsSameConnection.

@Test
public void test_Acquire_Process_Release_Acquire_ReturnsSameConnection() throws Exception {
    HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", connector.getLocalPort()));
    destination.start();
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    HttpConnectionOverHTTP connection1 = (HttpConnectionOverHTTP) connectionPool.acquire();
    long start = System.nanoTime();
    while (connection1 == null && TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start) < 5) {
        TimeUnit.MILLISECONDS.sleep(50);
        connection1 = (HttpConnectionOverHTTP) connectionPool.getIdleConnections().peek();
    }
    Assert.assertNotNull(connection1);
    // Acquire the connection to make it active
    Assert.assertSame(connection1, connectionPool.acquire());
    destination.process(connection1);
    destination.release(connection1);
    Connection connection2 = connectionPool.acquire();
    Assert.assertSame(connection1, connection2);
}
Also used : Origin(org.eclipse.jetty.client.Origin) DuplexConnectionPool(org.eclipse.jetty.client.DuplexConnectionPool) Connection(org.eclipse.jetty.client.api.Connection) AbstractHttpClientServerTest(org.eclipse.jetty.client.AbstractHttpClientServerTest) Test(org.junit.Test)

Example 13 with Connection

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

the class HttpDestinationOverHTTPTest method test_Request_Failed_If_MaxRequestsQueuedPerDestination_Exceeded.

@Test
public void test_Request_Failed_If_MaxRequestsQueuedPerDestination_Exceeded() throws Exception {
    int maxQueued = 1;
    client.setMaxRequestsQueuedPerDestination(maxQueued);
    client.setMaxConnectionsPerDestination(1);
    // Make one request to open the connection and be sure everything is setup properly
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send();
    Assert.assertEquals(200, response.getStatus());
    // Send another request that is sent immediately
    final CountDownLatch successLatch = new CountDownLatch(1);
    final CountDownLatch failureLatch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/one").onRequestQueued(request -> {
        client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/two").send(result -> {
            Assert.assertTrue(result.isFailed());
            Assert.assertThat(result.getRequestFailure(), Matchers.instanceOf(RejectedExecutionException.class));
            failureLatch.countDown();
        });
    }).send(result -> {
        if (result.isSucceeded())
            successLatch.countDown();
    });
    Assert.assertTrue(failureLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(successLatch.await(5, TimeUnit.SECONDS));
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) AbstractHttpClientServerTest(org.eclipse.jetty.client.AbstractHttpClientServerTest) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) DuplexConnectionPool(org.eclipse.jetty.client.DuplexConnectionPool) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) HttpClient(org.eclipse.jetty.client.HttpClient) CountDownLatch(java.util.concurrent.CountDownLatch) HttpHeader(org.eclipse.jetty.http.HttpHeader) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Origin(org.eclipse.jetty.client.Origin) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ConnectionPool(org.eclipse.jetty.client.ConnectionPool) Queue(java.util.Queue) Destination(org.eclipse.jetty.client.api.Destination) EmptyServerHandler(org.eclipse.jetty.client.EmptyServerHandler) Assert(org.junit.Assert) Before(org.junit.Before) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHttpClientServerTest(org.eclipse.jetty.client.AbstractHttpClientServerTest) Test(org.junit.Test)

Example 14 with Connection

use of org.eclipse.jetty.client.api.Connection 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 15 with Connection

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

Aggregations

Connection (org.eclipse.jetty.client.api.Connection)40 Test (org.junit.Test)33 CountDownLatch (java.util.concurrent.CountDownLatch)23 Request (org.eclipse.jetty.client.api.Request)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)14 Origin (org.eclipse.jetty.client.Origin)13 Result (org.eclipse.jetty.client.api.Result)13 HttpDestinationOverHTTP (org.eclipse.jetty.client.http.HttpDestinationOverHTTP)13 Promise (org.eclipse.jetty.util.Promise)13 HttpServletResponse (javax.servlet.http.HttpServletResponse)12 IOException (java.io.IOException)9 Destination (org.eclipse.jetty.client.api.Destination)9 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)9 ServletException (javax.servlet.ServletException)8 Response (org.eclipse.jetty.client.api.Response)8 TimeUnit (java.util.concurrent.TimeUnit)7 ByteBufferContentProvider (org.eclipse.jetty.client.util.ByteBufferContentProvider)7 FutureResponseListener (org.eclipse.jetty.client.util.FutureResponseListener)7 ByteArrayEndPoint (org.eclipse.jetty.io.ByteArrayEndPoint)7