Search in sources :

Example 31 with Connection

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

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

Example 33 with Connection

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

the class HttpDestinationOverHTTPTest method timedPoll.

private Connection timedPoll(Queue<Connection> connections, long time, TimeUnit unit) throws InterruptedException {
    long start = System.nanoTime();
    while (unit.toNanos(time) > System.nanoTime() - start) {
        Connection connection = connections.poll();
        if (connection != null)
            return connection;
        TimeUnit.MILLISECONDS.sleep(5);
    }
    return connections.poll();
}
Also used : Connection(org.eclipse.jetty.client.api.Connection)

Example 34 with Connection

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

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

the class HttpSenderOverHTTPTest method test_Send_NoRequestContent_IncompleteFlush_Exception.

@Test
public void test_Send_NoRequestContent_IncompleteFlush_Exception() throws Exception {
    ByteArrayEndPoint endPoint = new ByteArrayEndPoint("", 16);
    HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", 8080));
    destination.start();
    HttpConnectionOverHTTP connection = new HttpConnectionOverHTTP(endPoint, destination, new Promise.Adapter<Connection>());
    Request request = client.newRequest(URI.create("http://localhost/"));
    final CountDownLatch failureLatch = new CountDownLatch(2);
    request.listener(new Request.Listener.Adapter() {

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

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            failureLatch.countDown();
        }
    });
    // Shutdown output to trigger the exception on write
    endPoint.shutdownOutput();
    // This take will free space in the buffer and allow for the write to complete
    // although it will fail because we shut down the output
    endPoint.takeOutputString();
    Assert.assertTrue(failureLatch.await(5, TimeUnit.SECONDS));
}
Also used : Origin(org.eclipse.jetty.client.Origin) Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) ByteArrayEndPoint(org.eclipse.jetty.io.ByteArrayEndPoint) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) Promise(org.eclipse.jetty.util.Promise) 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