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