Search in sources :

Example 1 with DuplexConnectionPool

use of org.eclipse.jetty.client.DuplexConnectionPool in project jetty.project by eclipse.

the class ProxyServletTest method testProxyRequestFailureInTheMiddleOfProxyingSmallContent.

@Test
public void testProxyRequestFailureInTheMiddleOfProxyingSmallContent() throws Exception {
    final CountDownLatch chunk1Latch = new CountDownLatch(1);
    final int chunk1 = 'q';
    final int chunk2 = 'w';
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletOutputStream output = response.getOutputStream();
            output.write(chunk1);
            response.flushBuffer();
            // Wait for the client to receive this chunk.
            await(chunk1Latch, 5000);
            // Send second chunk, must not be received by proxy.
            output.write(chunk2);
        }

        private boolean await(CountDownLatch latch, long ms) throws IOException {
            try {
                return latch.await(ms, TimeUnit.MILLISECONDS);
            } catch (InterruptedException x) {
                throw new InterruptedIOException();
            }
        }
    });
    final long proxyTimeout = 1000;
    Map<String, String> proxyParams = new HashMap<>();
    proxyParams.put("timeout", String.valueOf(proxyTimeout));
    startProxy(proxyParams);
    startClient();
    InputStreamResponseListener listener = new InputStreamResponseListener();
    int port = serverConnector.getLocalPort();
    client.newRequest("localhost", port).send(listener);
    // Make the proxy request fail; given the small content, the
    // proxy-to-client response is not committed yet so it will be reset.
    TimeUnit.MILLISECONDS.sleep(2 * proxyTimeout);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(504, response.getStatus());
    // Make sure there is no content, as the proxy-to-client response has been reset.
    InputStream input = listener.getInputStream();
    Assert.assertEquals(-1, input.read());
    chunk1Latch.countDown();
    // Result succeeds because a 504 is a valid HTTP response.
    Result result = listener.await(5, TimeUnit.SECONDS);
    Assert.assertTrue(result.isSucceeded());
    // Make sure the proxy does not receive chunk2.
    Assert.assertEquals(-1, input.read());
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination("http", "localhost", port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Also used : InterruptedIOException(java.io.InterruptedIOException) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) DuplexConnectionPool(org.eclipse.jetty.client.DuplexConnectionPool) ServletOutputStream(javax.servlet.ServletOutputStream) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpServlet(javax.servlet.http.HttpServlet) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Example 2 with DuplexConnectionPool

use of org.eclipse.jetty.client.DuplexConnectionPool in project jetty.project by eclipse.

the class HttpDestinationOverHTTPTest method test_IdleConnection_IdleTimeout.

@Test
public void test_IdleConnection_IdleTimeout() throws Exception {
    long idleTimeout = 1000;
    client.setIdleTimeout(idleTimeout);
    HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", connector.getLocalPort()));
    destination.start();
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    Connection connection1 = connectionPool.acquire();
    if (connection1 == null) {
        // There are no queued requests, so the newly created connection will be idle
        long start = System.nanoTime();
        while (connection1 == null && TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start) < 5) {
            TimeUnit.MILLISECONDS.sleep(50);
            connection1 = connectionPool.getIdleConnections().peek();
        }
        Assert.assertNotNull(connection1);
        TimeUnit.MILLISECONDS.sleep(2 * idleTimeout);
        connection1 = connectionPool.getIdleConnections().poll();
        Assert.assertNull(connection1);
    }
}
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 3 with DuplexConnectionPool

use of org.eclipse.jetty.client.DuplexConnectionPool 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 4 with DuplexConnectionPool

use of org.eclipse.jetty.client.DuplexConnectionPool in project jetty.project by eclipse.

the class HttpDestinationOverHTTPTest method test_FirstAcquire_WithEmptyQueue.

@Test
public void test_FirstAcquire_WithEmptyQueue() throws Exception {
    HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", connector.getLocalPort()));
    destination.start();
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    Connection connection = connectionPool.acquire();
    if (connection == null) {
        // There are no queued requests, so the newly created connection will be idle
        connection = timedPoll(connectionPool.getIdleConnections(), 5, TimeUnit.SECONDS);
    }
    Assert.assertNotNull(connection);
}
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 5 with DuplexConnectionPool

use of org.eclipse.jetty.client.DuplexConnectionPool in project jetty.project by eclipse.

the class HttpDestinationOverHTTPTest method test_SecondAcquire_ConcurrentWithFirstAcquire_WithEmptyQueue_CreatesTwoConnections.

@Test
public void test_SecondAcquire_ConcurrentWithFirstAcquire_WithEmptyQueue_CreatesTwoConnections() throws Exception {
    final CountDownLatch idleLatch = new CountDownLatch(1);
    final CountDownLatch latch = new CountDownLatch(1);
    HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", connector.getLocalPort())) {

        @Override
        protected ConnectionPool newConnectionPool(HttpClient client) {
            return new DuplexConnectionPool(this, client.getMaxConnectionsPerDestination(), this) {

                @Override
                protected void onCreated(Connection connection) {
                    try {
                        idleLatch.countDown();
                        latch.await(5, TimeUnit.SECONDS);
                        super.onCreated(connection);
                    } catch (InterruptedException x) {
                        x.printStackTrace();
                    }
                }
            };
        }
    };
    destination.start();
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    Connection connection1 = connectionPool.acquire();
    // Make sure we entered idleCreated().
    Assert.assertTrue(idleLatch.await(5, TimeUnit.SECONDS));
    // There are no available existing connections, so acquire()
    // returns null because we delayed idleCreated() above
    Assert.assertNull(connection1);
    // Second attempt also returns null because we delayed idleCreated() above.
    Connection connection2 = connectionPool.acquire();
    Assert.assertNull(connection2);
    latch.countDown();
    // There must be 2 idle connections.
    Queue<Connection> idleConnections = connectionPool.getIdleConnections();
    Connection connection = timedPoll(idleConnections, 5, TimeUnit.SECONDS);
    Assert.assertNotNull(connection);
    connection = timedPoll(idleConnections, 5, TimeUnit.SECONDS);
    Assert.assertNotNull(connection);
}
Also used : Origin(org.eclipse.jetty.client.Origin) DuplexConnectionPool(org.eclipse.jetty.client.DuplexConnectionPool) HttpClient(org.eclipse.jetty.client.HttpClient) Connection(org.eclipse.jetty.client.api.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHttpClientServerTest(org.eclipse.jetty.client.AbstractHttpClientServerTest) Test(org.junit.Test)

Aggregations

DuplexConnectionPool (org.eclipse.jetty.client.DuplexConnectionPool)8 Test (org.junit.Test)7 AbstractHttpClientServerTest (org.eclipse.jetty.client.AbstractHttpClientServerTest)5 Origin (org.eclipse.jetty.client.Origin)5 Connection (org.eclipse.jetty.client.api.Connection)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InterruptedIOException (java.io.InterruptedIOException)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ServletException (javax.servlet.ServletException)2 ServletInputStream (javax.servlet.ServletInputStream)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 ServletResponse (javax.servlet.ServletResponse)2 HttpServlet (javax.servlet.http.HttpServlet)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpContentResponse (org.eclipse.jetty.client.HttpContentResponse)2 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)2