use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method test_ConnectionFailure_RemovesConnection.
@Test
public void test_ConnectionFailure_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());
server.stop();
final CountDownLatch failureLatch = new CountDownLatch(2);
client.newRequest(host, port).scheme(scheme).onRequestFailure((request, failure) -> failureLatch.countDown()).send(result -> {
Assert.assertTrue(result.isFailed());
failureLatch.countDown();
});
Assert.assertTrue(failureLatch.await(30, TimeUnit.SECONDS));
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method test_SuccessfulRequest_ReturnsConnection.
@Test
public void test_SuccessfulRequest_ReturnsConnection() 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 headersLatch = new CountDownLatch(1);
final CountDownLatch successLatch = new CountDownLatch(3);
client.newRequest(host, port).scheme(scheme).onRequestSuccess(request -> successLatch.countDown()).onResponseHeaders(response -> {
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(1, activeConnections.size());
headersLatch.countDown();
}).send(new Response.Listener.Adapter() {
@Override
public void onSuccess(Response response) {
successLatch.countDown();
}
@Override
public void onComplete(Result result) {
Assert.assertFalse(result.isFailed());
successLatch.countDown();
}
});
Assert.assertTrue(headersLatch.await(30, TimeUnit.SECONDS));
Assert.assertTrue(successLatch.await(30, TimeUnit.SECONDS));
Assert.assertEquals(1, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method test_BadRequest_RemovesConnection.
@Test
public void test_BadRequest_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 Queue<Connection> idleConnections = connectionPool.getIdleConnections();
Assert.assertEquals(0, idleConnections.size());
final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
Assert.assertEquals(0, activeConnections.size());
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 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(30, TimeUnit.SECONDS));
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method test_BigRequestContent_ResponseWithConnectionCloseHeader_RemovesConnection.
@Test
public void test_BigRequestContent_ResponseWithConnectionCloseHeader_RemovesConnection() throws Exception {
try (StacklessLogging stackless = new StacklessLogging(HttpConnection.class)) {
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setHeader("Connection", "close");
baseRequest.setHandled(true);
// Don't read request content; this causes the server parser to be closed
}
});
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());
Log.getLogger(HttpConnection.class).info("Expecting java.lang.IllegalStateException: HttpParser{s=CLOSED,...");
final CountDownLatch latch = new CountDownLatch(1);
ByteBuffer buffer = ByteBuffer.allocate(16 * 1024 * 1024);
Arrays.fill(buffer.array(), (byte) 'x');
client.newRequest(host, port).scheme(scheme).content(new ByteBufferContentProvider(buffer)).send(new Response.Listener.Adapter() {
@Override
public void onComplete(Result result) {
Assert.assertEquals(1, latch.getCount());
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
latch.countDown();
}
});
Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
server.stop();
}
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method test_ResponseWithConnectionCloseHeader_RemovesConnection.
@Test
public void test_ResponseWithConnectionCloseHeader_RemovesConnection() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setHeader("Connection", "close");
baseRequest.setHandled(true);
}
});
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 latch = new CountDownLatch(1);
client.newRequest(host, port).scheme(scheme).send(new Response.Listener.Adapter() {
@Override
public void onComplete(Result result) {
Assert.assertFalse(result.isFailed());
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
latch.countDown();
}
});
Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
}
Aggregations