Search in sources :

Example 26 with DeferredContentProvider

use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.

the class ServerTimeoutsTest method testBlockingReadWithMinimumDataRateAboveLimit.

@Test
public void testBlockingReadWithMinimumDataRateAboveLimit() throws Exception {
    int bytesPerSecond = 20;
    httpConfig.setMinRequestDataRate(bytesPerSecond);
    CountDownLatch handlerLatch = new CountDownLatch(1);
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            ServletInputStream input = request.getInputStream();
            while (true) {
                int read = input.read();
                if (read < 0)
                    break;
            }
            handlerLatch.countDown();
        }
    });
    DeferredContentProvider contentProvider = new DeferredContentProvider();
    CountDownLatch resultLatch = new CountDownLatch(1);
    client.newRequest(newURI()).content(contentProvider).send(result -> {
        if (result.getResponse().getStatus() == HttpStatus.OK_200)
            resultLatch.countDown();
    });
    for (int i = 0; i < 3; ++i) {
        contentProvider.offer(ByteBuffer.allocate(bytesPerSecond * 2));
        Thread.sleep(2500);
    }
    contentProvider.close();
    Assert.assertTrue(handlerLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 27 with DeferredContentProvider

use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.

the class ServerTimeoutsTest method testBlockingTimeoutSmallerThanIdleTimeoutBlockingReadBlockingTimeoutFires.

@Test
public void testBlockingTimeoutSmallerThanIdleTimeoutBlockingReadBlockingTimeoutFires() throws Exception {
    long blockingTimeout = 2500;
    httpConfig.setBlockingTimeout(blockingTimeout);
    CountDownLatch handlerLatch = new CountDownLatch(1);
    start(new BlockingReadHandler(handlerLatch));
    long idleTimeout = 3 * blockingTimeout;
    setServerIdleTimeout(idleTimeout);
    try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
        DeferredContentProvider contentProvider = new DeferredContentProvider(ByteBuffer.allocate(1));
        CountDownLatch resultLatch = new CountDownLatch(1);
        client.POST(newURI()).content(contentProvider).send(result -> {
            if (result.getResponse().getStatus() == HttpStatus.INTERNAL_SERVER_ERROR_500)
                resultLatch.countDown();
        });
        // Blocking read should timeout.
        Assert.assertTrue(handlerLatch.await(2 * blockingTimeout, TimeUnit.MILLISECONDS));
        // Complete the request.
        contentProvider.close();
        Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
    }
}
Also used : DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 28 with DeferredContentProvider

use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.

the class ServerTimeoutsTest method testNoBlockingTimeoutBlockingReadIdleTimeoutFires.

@Test
public void testNoBlockingTimeoutBlockingReadIdleTimeoutFires() throws Exception {
    httpConfig.setBlockingTimeout(-1);
    CountDownLatch handlerLatch = new CountDownLatch(1);
    start(new BlockingReadHandler(handlerLatch));
    long idleTimeout = 2500;
    setServerIdleTimeout(idleTimeout);
    try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
        DeferredContentProvider contentProvider = new DeferredContentProvider(ByteBuffer.allocate(1));
        CountDownLatch resultLatch = new CountDownLatch(1);
        client.POST(newURI()).content(contentProvider).send(result -> {
            if (result.getResponse().getStatus() == HttpStatus.INTERNAL_SERVER_ERROR_500)
                resultLatch.countDown();
        });
        // Blocking read should timeout.
        Assert.assertTrue(handlerLatch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
        // Complete the request.
        contentProvider.close();
        Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
    }
}
Also used : DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 29 with DeferredContentProvider

use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.

the class HttpClientTest method testContentDelimitedByEOFWithSlowRequest.

private void testContentDelimitedByEOFWithSlowRequest(final HttpVersion version, int length) throws Exception {
    // This test is crafted in a way that the response completes before the request is fully written.
    // With SSL, the response coming down will close the SSLEngine so it would not be possible to
    // write the last chunk of the request content, and the request will be failed, failing also the
    // test, which is not what we want.
    // This is a limit of Java's SSL implementation that does not allow half closes.
    Assume.assumeTrue(sslContextFactory == null);
    final byte[] data = new byte[length];
    new Random().nextBytes(data);
    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);
            // Send Connection: close to avoid that the server chunks the content with HTTP 1.1.
            if (version.compareTo(HttpVersion.HTTP_1_0) > 0)
                response.setHeader("Connection", "close");
            response.getOutputStream().write(data);
        }
    });
    DeferredContentProvider content = new DeferredContentProvider(ByteBuffer.wrap(new byte[] { 0 }));
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).version(version).content(content);
    FutureResponseListener listener = new FutureResponseListener(request);
    request.send(listener);
    // Wait some time to simulate a slow request.
    Thread.sleep(1000);
    content.close();
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(data, response.getContent());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener)

Example 30 with DeferredContentProvider

use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.

the class HttpClientFailureTest method testFailureAfterRequestCommit.

@Test
public void testFailureAfterRequestCommit() throws Exception {
    startServer(new EmptyServerHandler());
    final AtomicReference<HttpConnectionOverHTTP> connectionRef = new AtomicReference<>();
    client = new HttpClient(new HttpClientTransportOverHTTP() {

        @Override
        protected HttpConnectionOverHTTP newHttpConnection(EndPoint endPoint, HttpDestination destination, Promise<Connection> promise) {
            HttpConnectionOverHTTP connection = super.newHttpConnection(endPoint, destination, promise);
            connectionRef.set(connection);
            return connection;
        }
    }, null);
    client.start();
    final CountDownLatch commitLatch = new CountDownLatch(1);
    final CountDownLatch completeLatch = new CountDownLatch(1);
    DeferredContentProvider content = new DeferredContentProvider();
    client.newRequest("localhost", connector.getLocalPort()).onRequestCommit(request -> {
        connectionRef.get().getEndPoint().close();
        commitLatch.countDown();
    }).content(content).idleTimeout(2, TimeUnit.SECONDS).send(result -> {
        if (result.isFailed())
            completeLatch.countDown();
    });
    Assert.assertTrue(commitLatch.await(5, TimeUnit.SECONDS));
    final CountDownLatch contentLatch = new CountDownLatch(1);
    content.offer(ByteBuffer.allocate(1024), new Callback() {

        @Override
        public void failed(Throwable x) {
            contentLatch.countDown();
        }
    });
    Assert.assertTrue(commitLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) connectionRef.get().getHttpDestination().getConnectionPool();
    Assert.assertEquals(0, connectionPool.getConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnections().size());
    Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) Handler(org.eclipse.jetty.server.Handler) EndPoint(org.eclipse.jetty.io.EndPoint) Promise(org.eclipse.jetty.util.Promise) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) ServerConnector(org.eclipse.jetty.server.ServerConnector) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) After(org.junit.After) Assert(org.junit.Assert) Server(org.eclipse.jetty.server.Server) Callback(org.eclipse.jetty.util.Callback) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) EndPoint(org.eclipse.jetty.io.EndPoint) CountDownLatch(java.util.concurrent.CountDownLatch) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) Promise(org.eclipse.jetty.util.Promise) Callback(org.eclipse.jetty.util.Callback) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) Test(org.junit.Test)

Aggregations

DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)46 Test (org.junit.Test)41 CountDownLatch (java.util.concurrent.CountDownLatch)38 HttpServletRequest (javax.servlet.http.HttpServletRequest)35 HttpServletResponse (javax.servlet.http.HttpServletResponse)34 IOException (java.io.IOException)33 ServletException (javax.servlet.ServletException)28 ServletInputStream (javax.servlet.ServletInputStream)18 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)18 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)17 Request (org.eclipse.jetty.server.Request)16 Result (org.eclipse.jetty.client.api.Result)15 InterruptedIOException (java.io.InterruptedIOException)14 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)12 AsyncContext (javax.servlet.AsyncContext)11 Request (org.eclipse.jetty.client.api.Request)11 ByteBuffer (java.nio.ByteBuffer)10 Response (org.eclipse.jetty.client.api.Response)9 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)8 ReadListener (javax.servlet.ReadListener)7