use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpClientTimeoutTest method testVeryShortTimeout.
@Test
public void testVeryShortTimeout() throws Exception {
start(new EmptyServerHandler());
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(1, // Very short timeout
TimeUnit.MILLISECONDS).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
latch.countDown();
}
});
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 testConnectTimeoutFailsRequest.
private void testConnectTimeoutFailsRequest(boolean blocking) throws Exception {
String host = "10.255.255.1";
int port = 80;
int connectTimeout = 1000;
assumeConnectTimeout(host, port, connectTimeout);
start(new EmptyServerHandler());
client.stop();
client.setConnectTimeout(connectTimeout);
client.setConnectBlocking(blocking);
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())
latch.countDown();
}
});
Assert.assertTrue(latch.await(2 * 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 testTimeoutCancelledWhenSendingThrowsException.
@Test
public void testTimeoutCancelledWhenSendingThrowsException() throws Exception {
start(new EmptyServerHandler());
long timeout = 1000;
Request request = client.newRequest("badscheme://localhost:" + connector.getLocalPort());
try {
request.timeout(timeout, TimeUnit.MILLISECONDS).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
}
});
Assert.fail();
} catch (Exception expected) {
}
Thread.sleep(2 * timeout);
// If the task was not cancelled, it aborted the request.
Assert.assertNull(request.getAbortCause());
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class AsyncIOServletTest method testAsyncWriteLessThanContentLengthFlushed.
@Test
public void testAsyncWriteLessThanContentLengthFlushed() throws Exception {
CountDownLatch complete = new CountDownLatch(1);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentLength(10);
AsyncContext async = request.startAsync();
ServletOutputStream out = response.getOutputStream();
AtomicInteger state = new AtomicInteger(0);
out.setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
while (true) {
if (!out.isReady())
return;
switch(state.get()) {
case 0:
state.incrementAndGet();
WriteListener listener = this;
new Thread(() -> {
try {
Thread.sleep(50);
listener.onWritePossible();
} catch (Exception e) {
}
}).start();
return;
case 1:
state.incrementAndGet();
out.flush();
break;
case 2:
state.incrementAndGet();
out.write("12345".getBytes());
break;
case 3:
async.complete();
complete.countDown();
return;
}
}
}
@Override
public void onError(Throwable t) {
}
});
}
});
AtomicBoolean failed = new AtomicBoolean(false);
CountDownLatch clientLatch = new CountDownLatch(3);
client.newRequest(newURI()).path(servletPath).onResponseHeaders(response -> {
if (response.getStatus() == HttpStatus.OK_200)
clientLatch.countDown();
}).onResponseContent(new Response.ContentListener() {
@Override
public void onContent(Response response, ByteBuffer content) {
// System.err.println("Content: "+BufferUtil.toDetailString(content));
}
}).onResponseFailure(new Response.FailureListener() {
@Override
public void onFailure(Response response, Throwable failure) {
clientLatch.countDown();
}
}).send(result -> {
failed.set(result.isFailed());
clientLatch.countDown();
clientLatch.countDown();
clientLatch.countDown();
});
assertTrue(complete.await(10, TimeUnit.SECONDS));
assertTrue(clientLatch.await(10, TimeUnit.SECONDS));
assertTrue(failed.get());
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpClientLoadTest method test.
private void test(String scheme, String host, String method, boolean clientClose, boolean serverClose, int contentLength, final boolean checkContentLength, final CountDownLatch latch, final List<String> failures) {
long requestId = requestCount.incrementAndGet();
Request request = client.newRequest(host, connector.getLocalPort()).scheme(scheme).path("/" + requestId).method(method);
if (clientClose)
request.header(HttpHeader.CONNECTION, "close");
else if (serverClose)
request.header("X-Close", "true");
switch(method) {
case "GET":
request.header("X-Download", String.valueOf(contentLength));
break;
case "POST":
request.header("X-Upload", String.valueOf(contentLength));
request.content(new BytesContentProvider(new byte[contentLength]));
break;
}
final CountDownLatch requestLatch = new CountDownLatch(1);
request.send(new Response.Listener.Adapter() {
private final AtomicInteger contentLength = new AtomicInteger();
@Override
public void onHeaders(Response response) {
if (checkContentLength) {
String content = response.getHeaders().get("X-Content");
if (content != null)
contentLength.set(Integer.parseInt(content));
}
}
@Override
public void onContent(Response response, ByteBuffer content) {
if (checkContentLength)
contentLength.addAndGet(-content.remaining());
}
@Override
public void onComplete(Result result) {
if (result.isFailed()) {
result.getFailure().printStackTrace();
failures.add("Result failed " + result);
}
if (checkContentLength && contentLength.get() != 0)
failures.add("Content length mismatch " + contentLength);
requestLatch.countDown();
latch.countDown();
}
});
if (!await(requestLatch, 5, TimeUnit.SECONDS)) {
logger.warn("Request {} took too long{}{}{}{}", requestId, System.lineSeparator(), server.dump(), System.lineSeparator(), client.dump());
}
}
Aggregations