use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpClientTimeoutTest method retryAfterConnectTimeout.
@Test
public void retryAfterConnectTimeout() throws Exception {
final String host = "10.255.255.1";
final int port = 80;
int connectTimeout = 1000;
assumeConnectTimeout(host, port, connectTimeout);
start(new EmptyServerHandler());
client.stop();
client.setConnectTimeout(connectTimeout);
client.start();
final CountDownLatch latch = new CountDownLatch(1);
Request request = client.newRequest(host, port);
request.scheme(scheme).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
if (result.isFailed()) {
// Retry
client.newRequest(host, port).scheme(scheme).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
if (result.isFailed())
latch.countDown();
}
});
}
}
});
Assert.assertTrue(latch.await(333 * connectTimeout, TimeUnit.MILLISECONDS));
Assert.assertNotNull(request.getAbortCause());
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpClientTimeoutTest method testTimeoutOnListener.
@Slow
@Test
public void testTimeoutOnListener() throws Exception {
long timeout = 1000;
start(new TimeoutHandler(2 * timeout));
final CountDownLatch latch = new CountDownLatch(1);
Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(timeout, TimeUnit.MILLISECONDS);
request.send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
Assert.assertTrue(result.isFailed());
latch.countDown();
}
});
Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
}
use of org.eclipse.jetty.client.api.Result 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.Result in project jetty.project by eclipse.
the class HttpResponseAbortTest method testAbortOnContentBeforeRequestTermination.
@Test
public void testAbortOnContentBeforeRequestTermination() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
baseRequest.setHandled(true);
OutputStream output = response.getOutputStream();
output.write(1);
output.flush();
output.write(2);
output.flush();
} catch (IOException ignored) {
// The client may have already closed, and we'll get an exception here, but it's expected
}
}
});
final CountDownLatch abortLatch = new CountDownLatch(1);
final AtomicInteger completes = new AtomicInteger();
final CountDownLatch completeLatch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestSuccess(new org.eclipse.jetty.client.api.Request.SuccessListener() {
@Override
public void onSuccess(org.eclipse.jetty.client.api.Request request) {
try {
abortLatch.await(5, TimeUnit.SECONDS);
} catch (InterruptedException x) {
x.printStackTrace();
}
}
}).onResponseContent(new Response.ContentListener() {
@Override
public void onContent(Response response, ByteBuffer content) {
try {
response.abort(new Exception());
abortLatch.countDown();
// Delay to let the request side to finish its processing.
Thread.sleep(1000);
} catch (InterruptedException x) {
x.printStackTrace();
}
}
}).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
completes.incrementAndGet();
Assert.assertTrue(result.isFailed());
completeLatch.countDown();
}
});
Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
// Wait to be sure that the complete event is only notified once.
Thread.sleep(1000);
Assert.assertEquals(1, completes.get());
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpResponseAbortTest method testAbortOnBegin.
@Test
public void testAbortOnBegin() throws Exception {
start(new EmptyServerHandler());
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseBegin(new Response.BeginListener() {
@Override
public void onBegin(Response response) {
response.abort(new Exception());
}
}).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
Assert.assertTrue(result.isFailed());
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Aggregations