use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.
the class HttpClientTimeoutTest method testTimeoutIsCancelledOnSuccessWithExplicitConnection.
@Slow
@Test
public void testTimeoutIsCancelledOnSuccessWithExplicitConnection() throws Exception {
long timeout = 1000;
start(new TimeoutHandler(timeout));
final CountDownLatch latch = new CountDownLatch(1);
Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
FuturePromise<Connection> futureConnection = new FuturePromise<>();
destination.newConnection(futureConnection);
try (Connection connection = futureConnection.get(5, TimeUnit.SECONDS)) {
Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(scheme).timeout(2 * timeout, TimeUnit.MILLISECONDS);
connection.send(request, new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
Response response = result.getResponse();
Assert.assertEquals(200, response.getStatus());
Assert.assertFalse(result.isFailed());
latch.countDown();
}
});
Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
TimeUnit.MILLISECONDS.sleep(2 * timeout);
Assert.assertNull(request.getAbortCause());
}
}
use of org.eclipse.jetty.client.api.Connection 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.api.Connection in project jetty.project by eclipse.
the class HttpDestinationOverHTTPTest method test_Request_Failed_If_MaxRequestsQueuedPerDestination_Exceeded.
@Test
public void test_Request_Failed_If_MaxRequestsQueuedPerDestination_Exceeded() throws Exception {
int maxQueued = 1;
client.setMaxRequestsQueuedPerDestination(maxQueued);
client.setMaxConnectionsPerDestination(1);
// Make one request to open the connection and be sure everything is setup properly
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send();
Assert.assertEquals(200, response.getStatus());
// Send another request that is sent immediately
final CountDownLatch successLatch = new CountDownLatch(1);
final CountDownLatch failureLatch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/one").onRequestQueued(request -> {
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/two").send(result -> {
Assert.assertTrue(result.isFailed());
Assert.assertThat(result.getRequestFailure(), Matchers.instanceOf(RejectedExecutionException.class));
failureLatch.countDown();
});
}).send(result -> {
if (result.isSucceeded())
successLatch.countDown();
});
Assert.assertTrue(failureLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(successLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method test_BadRequest_WithSlowRequest_RemovesConnection.
@Slow
@Test
public void test_BadRequest_WithSlowRequest_RemovesConnection() throws Exception {
start(new EmptyServerHandler());
String host = "localhost";
int port = connector.getLocalPort();
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
final Collection<Connection> idleConnections = connectionPool.getIdleConnections();
Assert.assertEquals(0, idleConnections.size());
final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
Assert.assertEquals(0, activeConnections.size());
final long delay = 1000;
final CountDownLatch successLatch = new CountDownLatch(3);
client.newRequest(host, port).scheme(scheme).listener(new Request.Listener.Adapter() {
@Override
public void onBegin(Request request) {
// Remove the host header, this will make the request invalid
request.header(HttpHeader.HOST, null);
}
@Override
public void onHeaders(Request request) {
try {
TimeUnit.MILLISECONDS.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void onSuccess(Request request) {
successLatch.countDown();
}
}).send(new Response.Listener.Adapter() {
@Override
public void onSuccess(Response response) {
Assert.assertEquals(400, response.getStatus());
// 400 response also come with a Connection: close,
// so the connection is closed and removed
successLatch.countDown();
}
@Override
public void onComplete(Result result) {
Assert.assertFalse(result.isFailed());
successLatch.countDown();
}
});
Assert.assertTrue(successLatch.await(delay * 30, TimeUnit.MILLISECONDS));
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
}
use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method test_FailedRequest_RemovesConnection.
@Test
public void test_FailedRequest_RemovesConnection() throws Exception {
start(new EmptyServerHandler());
String host = "localhost";
int port = connector.getLocalPort();
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
final Collection<Connection> idleConnections = connectionPool.getIdleConnections();
Assert.assertEquals(0, idleConnections.size());
final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
Assert.assertEquals(0, activeConnections.size());
final CountDownLatch beginLatch = new CountDownLatch(1);
final CountDownLatch failureLatch = new CountDownLatch(2);
client.newRequest(host, port).scheme(scheme).listener(new Request.Listener.Adapter() {
@Override
public void onBegin(Request request) {
activeConnections.iterator().next().close();
beginLatch.countDown();
}
@Override
public void onFailure(Request request, Throwable failure) {
failureLatch.countDown();
}
}).send(new Response.Listener.Adapter() {
@Override
public void onComplete(Result result) {
Assert.assertTrue(result.isFailed());
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
failureLatch.countDown();
}
});
Assert.assertTrue(beginLatch.await(30, TimeUnit.SECONDS));
Assert.assertTrue(failureLatch.await(30, TimeUnit.SECONDS));
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
}
Aggregations