use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class ValidatingConnectionPoolTest method testServerClosesConnectionAfterResponseWithQueuedRequestWithMaxConnections.
private void testServerClosesConnectionAfterResponseWithQueuedRequestWithMaxConnections(Handler handler) throws Exception {
start(handler);
client.setMaxConnectionsPerDestination(1);
final CountDownLatch latch = new CountDownLatch(1);
Request request1 = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/one").onRequestBegin(r -> {
try {
latch.await();
} catch (InterruptedException x) {
r.abort(x);
}
});
FutureResponseListener listener1 = new FutureResponseListener(request1);
request1.send(listener1);
Request request2 = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/two");
FutureResponseListener listener2 = new FutureResponseListener(request2);
request2.send(listener2);
// Now we have one request about to be sent, and one queued.
latch.countDown();
ContentResponse response1 = listener1.get(5, TimeUnit.SECONDS);
Assert.assertEquals(200, response1.getStatus());
ContentResponse response2 = listener2.get(5, TimeUnit.SECONDS);
Assert.assertEquals(200, response2.getStatus());
}
use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class ForwardProxyTLSServerTest method testTwoConcurrentExchanges.
@Test
public void testTwoConcurrentExchanges() throws Exception {
startTLSServer(new ServerHandler());
startProxy();
final HttpClient httpClient = new HttpClient(newSslContextFactory());
httpClient.getProxyConfiguration().getProxies().add(newHttpProxy());
httpClient.start();
try {
final AtomicReference<Connection> connection = new AtomicReference<>();
final CountDownLatch connectionLatch = new CountDownLatch(1);
String content1 = "BODY";
ContentResponse response1 = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.GET).path("/echo?body=" + URLEncoder.encode(content1, "UTF-8")).onRequestCommit(request -> {
Destination destination = httpClient.getDestination(HttpScheme.HTTPS.asString(), "localhost", serverConnector.getLocalPort());
destination.newConnection(new Promise.Adapter<Connection>() {
@Override
public void succeeded(Connection result) {
connection.set(result);
connectionLatch.countDown();
}
});
}).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(HttpStatus.OK_200, response1.getStatus());
String content = response1.getContentAsString();
Assert.assertEquals(content1, content);
Assert.assertTrue(connectionLatch.await(5, TimeUnit.SECONDS));
String body2 = "body=" + content1;
org.eclipse.jetty.client.api.Request request2 = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.POST).path("/echo").header(HttpHeader.CONTENT_TYPE, MimeTypes.Type.FORM_ENCODED.asString()).header(HttpHeader.CONTENT_LENGTH, String.valueOf(body2.length())).content(new StringContentProvider(body2));
// Make sure the second connection can send the exchange via the tunnel
FutureResponseListener listener2 = new FutureResponseListener(request2);
connection.get().send(request2, listener2);
ContentResponse response2 = listener2.get(5, TimeUnit.SECONDS);
Assert.assertEquals(HttpStatus.OK_200, response2.getStatus());
String content2 = response2.getContentAsString();
Assert.assertEquals(content1, content2);
} finally {
httpClient.stop();
}
}
use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.
the class HttpRequest method send.
@Override
public ContentResponse send() throws InterruptedException, TimeoutException, ExecutionException {
FutureResponseListener listener = new FutureResponseListener(this);
send(this, listener);
try {
long timeout = getTimeout();
if (timeout <= 0)
return listener.get();
return listener.get(timeout, TimeUnit.MILLISECONDS);
} catch (Throwable x) {
// Differently from the Future, the semantic of this method is that if
// the send() is interrupted or times out, we abort the request.
abort(x);
throw x;
}
}
Aggregations