Search in sources :

Example 6 with Origin

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

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

the class HttpSenderOverHTTPTest method test_Send_NoRequestContent.

@Test
public void test_Send_NoRequestContent() throws Exception {
    ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
    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 headersLatch = new CountDownLatch(1);
    final CountDownLatch successLatch = new CountDownLatch(1);
    request.listener(new Request.Listener.Adapter() {

        @Override
        public void onHeaders(Request request) {
            headersLatch.countDown();
        }

        @Override
        public void onSuccess(Request request) {
            successLatch.countDown();
        }
    });
    connection.send(request, null);
    String requestString = endPoint.takeOutputString();
    Assert.assertTrue(requestString.startsWith("GET "));
    Assert.assertTrue(requestString.endsWith("\r\n\r\n"));
    Assert.assertTrue(headersLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(successLatch.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) Promise(org.eclipse.jetty.util.Promise) Test(org.junit.Test)

Example 8 with Origin

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

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

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

Aggregations

Origin (org.eclipse.jetty.client.Origin)14 Connection (org.eclipse.jetty.client.api.Connection)12 Test (org.junit.Test)12 ByteArrayEndPoint (org.eclipse.jetty.io.ByteArrayEndPoint)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 Request (org.eclipse.jetty.client.api.Request)7 Promise (org.eclipse.jetty.util.Promise)7 AbstractHttpClientServerTest (org.eclipse.jetty.client.AbstractHttpClientServerTest)5 DuplexConnectionPool (org.eclipse.jetty.client.DuplexConnectionPool)5 HttpClient (org.eclipse.jetty.client.HttpClient)3 ByteBufferContentProvider (org.eclipse.jetty.client.util.ByteBufferContentProvider)3 Result (org.eclipse.jetty.client.api.Result)2 LeakTrackingConnectionPool (org.eclipse.jetty.client.LeakTrackingConnectionPool)1 HttpClientTransportOverFCGI (org.eclipse.jetty.fcgi.client.http.HttpClientTransportOverFCGI)1 HttpDestinationOverFCGI (org.eclipse.jetty.fcgi.client.http.HttpDestinationOverFCGI)1 LeakTrackingByteBufferPool (org.eclipse.jetty.io.LeakTrackingByteBufferPool)1 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)1 Server (org.eclipse.jetty.server.Server)1 ServerConnector (org.eclipse.jetty.server.ServerConnector)1 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)1