use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpClientTest method testAllHeadersDiscarded.
@Test
public void testAllHeadersDiscarded() throws Exception {
start(new EmptyServerHandler());
int count = 10;
final CountDownLatch latch = new CountDownLatch(count);
for (int i = 0; i < count; ++i) {
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send(new Response.Listener.Adapter() {
@Override
public boolean onHeader(Response response, HttpField field) {
return false;
}
@Override
public void onComplete(Result result) {
if (result.isSucceeded())
latch.countDown();
}
});
}
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpClientTest method test_ExchangeIsComplete_OnlyWhenBothRequestAndResponseAreComplete.
@Test
public void test_ExchangeIsComplete_OnlyWhenBothRequestAndResponseAreComplete() throws Exception {
start(new RespondThenConsumeHandler());
// Prepare a big file to upload
Path targetTestsDir = testdir.getEmptyPathDir();
Files.createDirectories(targetTestsDir);
Path file = Paths.get(targetTestsDir.toString(), "http_client_conversation.big");
try (OutputStream output = Files.newOutputStream(file, StandardOpenOption.CREATE)) {
byte[] kb = new byte[1024];
for (int i = 0; i < 10 * 1024; ++i) output.write(kb);
}
final CountDownLatch latch = new CountDownLatch(3);
final AtomicLong exchangeTime = new AtomicLong();
final AtomicLong requestTime = new AtomicLong();
final AtomicLong responseTime = new AtomicLong();
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).file(file).onRequestSuccess(request -> {
requestTime.set(System.nanoTime());
latch.countDown();
}).send(new Response.Listener.Adapter() {
@Override
public void onSuccess(Response response) {
responseTime.set(System.nanoTime());
latch.countDown();
}
@Override
public void onComplete(Result result) {
exchangeTime.set(System.nanoTime());
latch.countDown();
}
});
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
Assert.assertTrue(requestTime.get() <= exchangeTime.get());
Assert.assertTrue(responseTime.get() <= exchangeTime.get());
// Give some time to the server to consume the request content
// This is just to avoid exception traces in the test output
Thread.sleep(1000);
Files.delete(file);
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpClientTest method testCompleteNotInvokedUntilContentConsumed.
@Test
public void testCompleteNotInvokedUntilContentConsumed() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
ServletOutputStream output = response.getOutputStream();
output.write(new byte[1024]);
}
});
final AtomicReference<Callback> callbackRef = new AtomicReference<>();
final CountDownLatch contentLatch = new CountDownLatch(1);
final CountDownLatch completeLatch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send(new Response.Listener.Adapter() {
@Override
public void onContent(Response response, ByteBuffer content, Callback callback) {
// Do not notify the callback yet.
callbackRef.set(callback);
contentLatch.countDown();
}
@Override
public void onComplete(Result result) {
if (result.isSucceeded())
completeLatch.countDown();
}
});
Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
// Make sure the complete event is not emitted.
Assert.assertFalse(completeLatch.await(1, TimeUnit.SECONDS));
// Consume the content.
callbackRef.get().succeeded();
// Now the complete event is emitted.
Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpClientTest method testRequestRetries.
@Test
public void testRequestRetries() throws Exception {
final int maxRetries = 3;
final AtomicInteger requests = new AtomicInteger();
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
int count = requests.incrementAndGet();
if (count == maxRetries)
baseRequest.setHandled(true);
consume(request.getInputStream(), true);
}
});
final CountDownLatch latch = new CountDownLatch(1);
new RetryListener(client, scheme, "localhost", connector.getLocalPort(), maxRetries) {
@Override
protected void completed(Result result) {
latch.countDown();
}
}.perform();
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpClientTimeoutTest method testConnectTimeoutIsCancelledByShorterRequestTimeout.
@Slow
@Test
public void testConnectTimeoutIsCancelledByShorterRequestTimeout() throws Exception {
String host = "10.255.255.1";
int port = 80;
int connectTimeout = 2000;
assumeConnectTimeout(host, port, connectTimeout);
start(new EmptyServerHandler());
client.stop();
client.setConnectTimeout(connectTimeout);
client.start();
final AtomicInteger completes = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(2);
Request request = client.newRequest(host, port);
request.scheme(scheme).timeout(connectTimeout / 2, TimeUnit.MILLISECONDS).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
completes.incrementAndGet();
latch.countDown();
}
});
Assert.assertFalse(latch.await(2 * connectTimeout, TimeUnit.MILLISECONDS));
Assert.assertEquals(1, completes.get());
Assert.assertNotNull(request.getAbortCause());
}
Aggregations