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);
}
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));
}
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);
}
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);
}
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);
}
}
Aggregations