use of org.eclipse.jetty.util.Callback in project jetty.project by eclipse.
the class ServerTimeoutsTest method testBlockingTimeoutLargerThanIdleTimeoutBlockingWriteIdleTimeoutFires.
@Test
public void testBlockingTimeoutLargerThanIdleTimeoutBlockingWriteIdleTimeoutFires() throws Exception {
long idleTimeout = 2500;
long blockingTimeout = 3 * idleTimeout;
httpConfig.setBlockingTimeout(blockingTimeout);
CountDownLatch handlerLatch = new CountDownLatch(1);
start(new BlockingWriteHandler(handlerLatch));
setServerIdleTimeout(idleTimeout);
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
BlockingQueue<Callback> callbacks = new LinkedBlockingQueue<>();
CountDownLatch resultLatch = new CountDownLatch(1);
client.newRequest(newURI()).onResponseContentAsync((response, content, callback) -> {
// Do not succeed the callback so the server will block writing.
callbacks.offer(callback);
}).send(result -> {
if (result.isFailed())
resultLatch.countDown();
});
// Blocking read should timeout.
Assert.assertTrue(handlerLatch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
// After the server stopped sending, consume on the client to read the early EOF.
while (true) {
Callback callback = callbacks.poll(1, TimeUnit.SECONDS);
if (callback == null)
break;
callback.succeeded();
}
Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
}
}
use of org.eclipse.jetty.util.Callback in project jetty.project by eclipse.
the class HttpClientStreamTest method testInputStreamResponseListenerClosedWhileWaiting.
@Test
public void testInputStreamResponseListenerClosedWhileWaiting() throws Exception {
byte[] chunk1 = new byte[] { 0, 1 };
byte[] chunk2 = new byte[] { 2, 3 };
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
response.setContentLength(chunk1.length + chunk2.length);
ServletOutputStream output = response.getOutputStream();
output.write(chunk1);
output.flush();
output.write(chunk2);
}
});
CountDownLatch failedLatch = new CountDownLatch(1);
CountDownLatch contentLatch = new CountDownLatch(1);
InputStreamResponseListener listener = new InputStreamResponseListener() {
@Override
public void onContent(Response response, ByteBuffer content, Callback callback) {
super.onContent(response, content, new Callback() {
@Override
public void failed(Throwable x) {
failedLatch.countDown();
callback.failed(x);
}
});
contentLatch.countDown();
}
};
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).send(listener);
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
// Wait until we get some content.
Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
// Close the stream.
InputStream stream = listener.getInputStream();
stream.close();
// Make sure that the callback has been invoked.
Assert.assertTrue(failedLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.util.Callback in project jetty.project by eclipse.
the class HttpClientStreamTest method testUploadWithDeferredContentAvailableCallbacksNotifiedOnce.
@Test
public void testUploadWithDeferredContentAvailableCallbacksNotifiedOnce() 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);
IO.copy(request.getInputStream(), new ByteArrayOutputStream());
}
});
final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger succeeds = new AtomicInteger();
try (DeferredContentProvider content = new DeferredContentProvider()) {
// Make the content immediately available.
content.offer(ByteBuffer.allocate(1024), new Callback() {
@Override
public void succeeded() {
succeeds.incrementAndGet();
}
});
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).send(result -> {
if (result.isSucceeded() && result.getResponse().getStatus() == 200)
latch.countDown();
});
}
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
Assert.assertEquals(1, succeeds.get());
}
use of org.eclipse.jetty.util.Callback in project jetty.project by eclipse.
the class HttpTransportOverHTTP2 method send.
@Override
public void send(MetaData.Response info, boolean isHeadRequest, ByteBuffer content, boolean lastContent, Callback callback) {
boolean hasContent = BufferUtil.hasContent(content) && !isHeadRequest;
if (info != null) {
int status = info.getStatus();
boolean informational = HttpStatus.isInformational(status) && status != HttpStatus.SWITCHING_PROTOCOLS_101;
boolean committed = false;
if (!informational)
committed = commit.compareAndSet(false, true);
if (committed || informational) {
if (hasContent) {
Callback commitCallback = new Callback.Nested(callback) {
@Override
public void succeeded() {
if (transportCallback.start(callback, false))
send(content, lastContent, transportCallback);
}
};
if (transportCallback.start(commitCallback, true))
commit(info, false, transportCallback);
} else {
if (transportCallback.start(callback, false))
commit(info, lastContent, transportCallback);
}
} else {
callback.failed(new IllegalStateException("committed"));
}
} else {
if (hasContent || lastContent) {
if (transportCallback.start(callback, false))
send(content, lastContent, transportCallback);
} else {
callback.succeeded();
}
}
}
use of org.eclipse.jetty.util.Callback in project jetty.project by eclipse.
the class HttpOutputTest method testWriteInterception.
@Test
public void testWriteInterception() throws Exception {
final Resource big = Resource.newClassPathResource("simple/big.txt");
_handler._writeLengthIfKnown = false;
_handler._content = BufferUtil.toBuffer(big, false);
_handler._arrayBuffer = new byte[1024];
_handler._interceptor = new ChainedInterceptor() {
Interceptor _next;
@Override
public void write(ByteBuffer content, boolean complete, Callback callback) {
String s = BufferUtil.toString(content).toUpperCase().replaceAll("BIG", "BIGGER");
_next.write(BufferUtil.toBuffer(s), complete, callback);
}
@Override
public boolean isOptimizedForDirectBuffers() {
return _next.isOptimizedForDirectBuffers();
}
@Override
public Interceptor getNextInterceptor() {
return _next;
}
@Override
public void setNext(Interceptor interceptor) {
_next = interceptor;
}
};
String response = _connector.getResponse("GET / HTTP/1.0\nHost: localhost:80\n\n");
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertThat(response, Matchers.not(containsString("Content-Length")));
assertThat(response, containsString("400\tTHIS IS A BIGGER FILE"));
}
Aggregations