Search in sources :

Example 1 with Origin

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

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

the class HttpReceiverOverHTTPTest method init.

@Before
public void init() throws Exception {
    client = new HttpClient();
    client.start();
    destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", 8080));
    destination.start();
    endPoint = new ByteArrayEndPoint();
    connection = new HttpConnectionOverHTTP(endPoint, destination, new Promise.Adapter<>());
    endPoint.setConnection(connection);
}
Also used : Origin(org.eclipse.jetty.client.Origin) HttpClient(org.eclipse.jetty.client.HttpClient) ByteArrayEndPoint(org.eclipse.jetty.io.ByteArrayEndPoint) Before(org.junit.Before)

Example 3 with Origin

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

the class HttpSenderOverHTTPTest method test_Send_NoRequestContent_IncompleteFlush.

@Slow
@Test
public void test_Send_NoRequestContent_IncompleteFlush() 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/"));
    connection.send(request, null);
    // This take will free space in the buffer and allow for the write to complete
    StringBuilder builder = new StringBuilder(endPoint.takeOutputString());
    // Wait for the write to complete
    TimeUnit.SECONDS.sleep(1);
    String chunk = endPoint.takeOutputString();
    while (chunk.length() > 0) {
        builder.append(chunk);
        chunk = endPoint.takeOutputString();
    }
    String requestString = builder.toString();
    Assert.assertTrue(requestString.startsWith("GET "));
    Assert.assertTrue(requestString.endsWith("\r\n\r\n"));
}
Also used : Origin(org.eclipse.jetty.client.Origin) Promise(org.eclipse.jetty.util.Promise) Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) ByteArrayEndPoint(org.eclipse.jetty.io.ByteArrayEndPoint) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 4 with Origin

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

the class HttpSenderOverHTTPTest method test_Send_NoRequestContent_Exception.

@Test
public void test_Send_NoRequestContent_Exception() throws Exception {
    ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
    // Shutdown output to trigger the exception on write
    endPoint.shutdownOutput();
    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();
        }
    });
    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)

Example 5 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)

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