Search in sources :

Example 6 with DuplexConnectionPool

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

the class HttpDestinationOverHTTPTest method test_SecondAcquire_AfterFirstAcquire_WithEmptyQueue_ReturnsSameConnection.

@Test
public void test_SecondAcquire_AfterFirstAcquire_WithEmptyQueue_ReturnsSameConnection() throws Exception {
    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);
        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 7 with DuplexConnectionPool

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

the class ProxyServletTest method testProxyRequestFailureInTheMiddleOfProxyingBigContent.

@Test
public void testProxyRequestFailureInTheMiddleOfProxyingBigContent() throws Exception {
    int outputBufferSize = 1024;
    final CountDownLatch chunk1Latch = new CountDownLatch(1);
    final byte[] chunk1 = new byte[outputBufferSize];
    new Random().nextBytes(chunk1);
    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));
    proxyParams.put("outputBufferSize", String.valueOf(outputBufferSize));
    startProxy(proxyParams);
    startClient();
    InputStreamResponseListener listener = new InputStreamResponseListener();
    int port = serverConnector.getLocalPort();
    client.newRequest("localhost", port).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
    InputStream input = listener.getInputStream();
    for (int i = 0; i < chunk1.length; ++i) Assert.assertEquals(chunk1[i] & 0xFF, input.read());
    TimeUnit.MILLISECONDS.sleep(2 * proxyTimeout);
    chunk1Latch.countDown();
    try {
        // Make sure the proxy does not receive chunk2.
        input.read();
        Assert.fail();
    } catch (EOFException x) {
    // Expected
    }
    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) 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) Random(java.util.Random) EOFException(java.io.EOFException) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Aggregations

DuplexConnectionPool (org.eclipse.jetty.client.DuplexConnectionPool)7 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