Search in sources :

Example 66 with Callback

use of org.eclipse.jetty.util.Callback 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));
}
Also used : FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Callback(org.eclipse.jetty.util.Callback) Test(org.junit.Test)

Example 67 with Callback

use of org.eclipse.jetty.util.Callback in project jetty.project by eclipse.

the class HttpClientStreamTest method testUploadWithDeferredContentProviderFailsMultipleOffers.

@Test
public void testUploadWithDeferredContentProviderFailsMultipleOffers() throws Exception {
    start(new EmptyServerHandler());
    final CountDownLatch failLatch = new CountDownLatch(2);
    final Callback callback = new Callback() {

        @Override
        public void failed(Throwable x) {
            failLatch.countDown();
        }
    };
    final CountDownLatch completeLatch = new CountDownLatch(1);
    final DeferredContentProvider content = new DeferredContentProvider();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).onRequestBegin(request -> {
        content.offer(ByteBuffer.wrap(new byte[256]), callback);
        content.offer(ByteBuffer.wrap(new byte[256]), callback);
        request.abort(new Exception("explicitly_thrown_by_test"));
    }).send(result -> {
        if (result.isFailed())
            completeLatch.countDown();
    });
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(failLatch.await(5, TimeUnit.SECONDS));
    // Make sure that adding more content results in the callback to be failed.
    final CountDownLatch latch = new CountDownLatch(1);
    content.offer(ByteBuffer.wrap(new byte[128]), new Callback() {

        @Override
        public void failed(Throwable x) {
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) Random(java.util.Random) ByteBuffer(java.nio.ByteBuffer) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpStatus(org.eclipse.jetty.http.HttpStatus) Path(java.nio.file.Path) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow) StandardOpenOption(java.nio.file.StandardOpenOption) IO(org.eclipse.jetty.util.IO) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) BufferUtil(org.eclipse.jetty.util.BufferUtil) Result(org.eclipse.jetty.client.api.Result) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStreamContentProvider(org.eclipse.jetty.client.util.OutputStreamContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) MavenTestingUtils(org.eclipse.jetty.toolchain.test.MavenTestingUtils) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InterruptedIOException(java.io.InterruptedIOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AsyncContext(javax.servlet.AsyncContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) Files(java.nio.file.Files) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Paths(java.nio.file.Paths) DispatcherType(javax.servlet.DispatcherType) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Assert(org.junit.Assert) InputStream(java.io.InputStream) Callback(org.eclipse.jetty.util.Callback) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ServletException(javax.servlet.ServletException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 68 with Callback

use of org.eclipse.jetty.util.Callback in project jetty.project by eclipse.

the class HttpClientStreamTest method testInputStreamResponseListenerFailedWhileWaiting.

@Test
public void testInputStreamResponseListenerFailedWhileWaiting() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            byte[] data = new byte[1024];
            response.setContentLength(data.length);
            ServletOutputStream output = response.getOutputStream();
            output.write(data);
        }
    });
    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));
    // Abort the response.
    response.abort(new Exception());
    // Make sure that the callback has been invoked.
    Assert.assertTrue(failedLatch.await(5, TimeUnit.SECONDS));
}
Also used : InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) ServletOutputStream(javax.servlet.ServletOutputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ServletException(javax.servlet.ServletException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Callback(org.eclipse.jetty.util.Callback) Test(org.junit.Test)

Example 69 with Callback

use of org.eclipse.jetty.util.Callback in project jetty.project by eclipse.

the class HttpClientStreamTest method testInputStreamResponseListenerClosedBeforeContent.

@Test(expected = AsynchronousCloseException.class)
public void testInputStreamResponseListenerClosedBeforeContent() throws Exception {
    AtomicReference<AsyncContext> contextRef = new AtomicReference<>();
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            contextRef.set(request.startAsync());
            response.flushBuffer();
        }
    });
    CountDownLatch latch = 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) {
                    latch.countDown();
                    callback.failed(x);
                }
            });
        }
    };
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    InputStream input = listener.getInputStream();
    input.close();
    AsyncContext asyncContext = contextRef.get();
    asyncContext.getResponse().getOutputStream().write(new byte[1024]);
    asyncContext.complete();
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    // Throws
    input.read();
}
Also used : InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) AsyncContext(javax.servlet.AsyncContext) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Callback(org.eclipse.jetty.util.Callback) Test(org.junit.Test)

Example 70 with Callback

use of org.eclipse.jetty.util.Callback in project jetty.project by eclipse.

the class HttpClientTest method testAsyncResponseContentBackPressure.

@Test
public void testAsyncResponseContentBackPressure() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Large write to generate multiple DATA frames.
            response.getOutputStream().write(new byte[256 * 1024]);
        }
    });
    CountDownLatch completeLatch = new CountDownLatch(1);
    AtomicInteger counter = new AtomicInteger();
    AtomicReference<Callback> callbackRef = new AtomicReference<>();
    AtomicReference<CountDownLatch> latchRef = new AtomicReference<>(new CountDownLatch(1));
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).onResponseContentAsync((response, content, callback) -> {
        if (counter.incrementAndGet() == 1) {
            callbackRef.set(callback);
            latchRef.get().countDown();
        } else {
            callback.succeeded();
        }
    }).send(result -> completeLatch.countDown());
    Assert.assertTrue(latchRef.get().await(5, TimeUnit.SECONDS));
    // Wait some time to verify that back pressure is applied correctly.
    Thread.sleep(1000);
    Assert.assertEquals(1, counter.get());
    callbackRef.get().succeeded();
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
}
Also used : IntStream(java.util.stream.IntStream) Request(org.eclipse.jetty.server.Request) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ServletInputStream(javax.servlet.ServletInputStream) Random(java.util.Random) FlowControlStrategy(org.eclipse.jetty.http2.FlowControlStrategy) InterruptedIOException(java.io.InterruptedIOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServletOutputStream(javax.servlet.ServletOutputStream) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Assume(org.junit.Assume) HttpStatus(org.eclipse.jetty.http.HttpStatus) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) EnumSet(java.util.EnumSet) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) IO(org.eclipse.jetty.util.IO) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) HttpMethod(org.eclipse.jetty.http.HttpMethod) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Assert(org.junit.Assert) InputStream(java.io.InputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Callback(org.eclipse.jetty.util.Callback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

Callback (org.eclipse.jetty.util.Callback)81 Test (org.junit.Test)71 CountDownLatch (java.util.concurrent.CountDownLatch)68 HttpFields (org.eclipse.jetty.http.HttpFields)52 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)52 Stream (org.eclipse.jetty.http2.api.Stream)51 MetaData (org.eclipse.jetty.http.MetaData)50 Session (org.eclipse.jetty.http2.api.Session)50 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)46 HttpServletResponse (javax.servlet.http.HttpServletResponse)44 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)41 IOException (java.io.IOException)39 FuturePromise (org.eclipse.jetty.util.FuturePromise)39 ByteBuffer (java.nio.ByteBuffer)38 HttpServletRequest (javax.servlet.http.HttpServletRequest)37 ServletException (javax.servlet.ServletException)34 Promise (org.eclipse.jetty.util.Promise)30 ServletOutputStream (javax.servlet.ServletOutputStream)26 HttpServlet (javax.servlet.http.HttpServlet)21 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)20