Search in sources :

Example 76 with Result

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));
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Example 77 with Result

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());
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) EndPoint(org.eclipse.jetty.io.EndPoint) Result(org.eclipse.jetty.client.api.Result)

Example 78 with Result

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());
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Example 79 with Result

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());
}
Also used : Request(org.eclipse.jetty.server.Request) ServletException(javax.servlet.ServletException) Context(org.eclipse.jetty.server.handler.ContextHandler.Context) ByteBuffer(java.nio.ByteBuffer) Assert.assertThat(org.junit.Assert.assertThat) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) BufferUtil.toArray(org.eclipse.jetty.util.BufferUtil.toArray) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpStatus(org.eclipse.jetty.http.HttpStatus) HttpInput(org.eclipse.jetty.server.HttpInput) Response(org.eclipse.jetty.client.api.Response) HttpServlet(javax.servlet.http.HttpServlet) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) UncheckedIOException(java.io.UncheckedIOException) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CountDownLatch(java.util.concurrent.CountDownLatch) Session(org.eclipse.jetty.http2.api.Session) FuturePromise(org.eclipse.jetty.util.FuturePromise) Content(org.eclipse.jetty.server.HttpInput.Content) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Matchers.is(org.hamcrest.Matchers.is) Queue(java.util.Queue) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) BufferUtil(org.eclipse.jetty.util.BufferUtil) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Result(org.eclipse.jetty.client.api.Result) Handler(org.eclipse.jetty.server.Handler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpChannel(org.eclipse.jetty.server.HttpChannel) ServletInputStream(javax.servlet.ServletInputStream) ByteBuffer.wrap(java.nio.ByteBuffer.wrap) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) Deque(java.util.Deque) InterruptedIOException(java.io.InterruptedIOException) AsyncContext(javax.servlet.AsyncContext) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) WriteListener(javax.servlet.WriteListener) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) HttpConnectionOverHTTP2(org.eclipse.jetty.http2.client.http.HttpConnectionOverHTTP2) Assume(org.junit.Assume) Executor(java.util.concurrent.Executor) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers(org.hamcrest.Matchers) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) TimeUnit(java.util.concurrent.TimeUnit) HttpMethod(org.eclipse.jetty.http.HttpMethod) Connection(org.eclipse.jetty.io.Connection) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ReadListener(javax.servlet.ReadListener) DispatcherType(javax.servlet.DispatcherType) Destination(org.eclipse.jetty.client.api.Destination) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) ServletException(javax.servlet.ServletException) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WriteListener(javax.servlet.WriteListener) Test(org.junit.Test)

Example 80 with Result

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());
    }
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Result(org.eclipse.jetty.client.api.Result)

Aggregations

Result (org.eclipse.jetty.client.api.Result)100 Test (org.junit.Test)80 CountDownLatch (java.util.concurrent.CountDownLatch)79 HttpServletResponse (javax.servlet.http.HttpServletResponse)74 HttpServletRequest (javax.servlet.http.HttpServletRequest)66 Response (org.eclipse.jetty.client.api.Response)62 IOException (java.io.IOException)60 ServletException (javax.servlet.ServletException)56 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)44 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)41 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)39 Request (org.eclipse.jetty.client.api.Request)31 ByteBuffer (java.nio.ByteBuffer)29 Request (org.eclipse.jetty.server.Request)27 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)24 InterruptedIOException (java.io.InterruptedIOException)21 ServletOutputStream (javax.servlet.ServletOutputStream)20 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)16