use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.
the class HttpClientUploadDuringServerShutdown method testUploadDuringServerShutdown.
@Test
public void testUploadDuringServerShutdown() throws Exception {
final AtomicReference<EndPoint> endPointRef = new AtomicReference<>();
final CountDownLatch serverLatch = new CountDownLatch(1);
QueuedThreadPool serverThreads = new QueuedThreadPool();
serverThreads.setName("server");
Server server = new Server(serverThreads);
ServerConnector connector = new ServerConnector(server);
server.addConnector(connector);
server.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
endPointRef.set(baseRequest.getHttpChannel().getEndPoint());
serverLatch.countDown();
}
});
server.start();
final AtomicBoolean afterSetup = new AtomicBoolean();
final CountDownLatch sendLatch = new CountDownLatch(1);
final CountDownLatch beginLatch = new CountDownLatch(1);
final CountDownLatch associateLatch = new CountDownLatch(1);
QueuedThreadPool clientThreads = new QueuedThreadPool();
clientThreads.setName("client");
HttpClient client = new HttpClient(new HttpClientTransportOverHTTP(1) {
@Override
protected HttpConnectionOverHTTP newHttpConnection(EndPoint endPoint, HttpDestination destination, Promise<Connection> promise) {
return new HttpConnectionOverHTTP(endPoint, destination, promise) {
@Override
protected HttpChannelOverHTTP newHttpChannel() {
return new HttpChannelOverHTTP(this) {
@Override
public void send() {
if (afterSetup.get()) {
associateLatch.countDown();
}
super.send();
}
};
}
@Override
protected void close(Throwable failure) {
try {
sendLatch.countDown();
beginLatch.await(5, TimeUnit.SECONDS);
super.close(failure);
} catch (InterruptedException x) {
x.printStackTrace();
}
}
@Override
protected boolean abort(Throwable failure) {
try {
associateLatch.await(5, TimeUnit.SECONDS);
return super.abort(failure);
} catch (InterruptedException x) {
x.printStackTrace();
return false;
}
}
};
}
}, null);
client.setIdleTimeout(10000);
client.setExecutor(clientThreads);
client.start();
// Create one connection.
client.newRequest("localhost", connector.getLocalPort()).send();
Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
afterSetup.set(true);
Thread.sleep(1000);
// Close the connection, so that the receiver is woken
// up and will call HttpConnectionOverHTTP.close().
EndPoint endPoint = endPointRef.get();
endPoint.close();
// Wait for close() so that the connection that
// is being closed is used to send the request.
Assert.assertTrue(sendLatch.await(5, TimeUnit.SECONDS));
final CountDownLatch completeLatch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).timeout(10, TimeUnit.SECONDS).onRequestBegin(request -> {
try {
beginLatch.countDown();
completeLatch.await(5, TimeUnit.SECONDS);
} catch (InterruptedException x) {
x.printStackTrace();
}
}).send(result -> completeLatch.countDown());
Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination("http", "localhost", connector.getLocalPort());
DuplexConnectionPool pool = (DuplexConnectionPool) destination.getConnectionPool();
Assert.assertEquals(0, pool.getConnectionCount());
Assert.assertEquals(0, pool.getIdleConnections().size());
Assert.assertEquals(0, pool.getActiveConnections().size());
}
use of org.eclipse.jetty.client.api.Connection 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.Connection in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method testConnectionForHTTP10ResponseIsRemoved.
@Test
public void testConnectionForHTTP10ResponseIsRemoved() 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());
client.setStrictEventOrdering(false);
ContentResponse response = client.newRequest(host, port).scheme(scheme).onResponseBegin(response1 -> {
((HttpResponse) response1).version(HttpVersion.HTTP_1_0);
}).send();
Assert.assertEquals(200, response.getStatus());
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_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.Connection 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());
}
Aggregations