Search in sources :

Example 26 with FuturePromise

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

the class HttpClientTest method testHTTP10WithKeepAliveAndNoContentLength.

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

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            // Send the headers at this point, then write the content
            response.flushBuffer();
            response.getOutputStream().print("TEST");
        }
    });
    FuturePromise<Connection> promise = new FuturePromise<>();
    Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
    destination.newConnection(promise);
    try (Connection connection = promise.get(5, TimeUnit.SECONDS)) {
        long timeout = 5000;
        Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(destination.getScheme()).version(HttpVersion.HTTP_1_0).header(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.asString()).timeout(timeout, TimeUnit.MILLISECONDS);
        FutureResponseListener listener = new FutureResponseListener(request);
        connection.send(request, listener);
        ContentResponse response = listener.get(2 * timeout, TimeUnit.MILLISECONDS);
        Assert.assertEquals(200, response.getStatus());
        // The parser notifies end-of-content and therefore the CompleteListener
        // before closing the connection, so we need to wait before checking
        // that the connection is closed to avoid races.
        Thread.sleep(1000);
        Assert.assertTrue(connection.isClosed());
    }
}
Also used : Destination(org.eclipse.jetty.client.api.Destination) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) FuturePromise(org.eclipse.jetty.util.FuturePromise) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) Connection(org.eclipse.jetty.client.api.Connection) 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) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 27 with FuturePromise

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

the class HttpClientTest method testCONNECTWithHTTP10.

@Test
public void testCONNECTWithHTTP10() throws Exception {
    try (ServerSocket server = new ServerSocket(0)) {
        startClient();
        String host = "localhost";
        int port = server.getLocalPort();
        Request request = client.newRequest(host, port).method(HttpMethod.CONNECT).version(HttpVersion.HTTP_1_0);
        FuturePromise<Connection> promise = new FuturePromise<>();
        client.getDestination("http", host, port).newConnection(promise);
        Connection connection = promise.get(5, TimeUnit.SECONDS);
        FutureResponseListener listener = new FutureResponseListener(request);
        connection.send(request, listener);
        try (Socket socket = server.accept()) {
            InputStream input = socket.getInputStream();
            consume(input, false);
            // HTTP/1.0 response, the client must not close the connection.
            String httpResponse = "" + "HTTP/1.0 200 OK\r\n" + "\r\n";
            OutputStream output = socket.getOutputStream();
            output.write(httpResponse.getBytes(StandardCharsets.UTF_8));
            output.flush();
            ContentResponse response = listener.get(5, TimeUnit.SECONDS);
            Assert.assertEquals(200, response.getStatus());
            // Because the tunnel was successful, this connection will be
            // upgraded to an SslConnection, so it will not be fill interested.
            // This test doesn't upgrade, so it needs to restore the fill interest.
            ((AbstractConnection) connection).fillInterested();
            // Test that I can send another request on the same connection.
            request = client.newRequest(host, port);
            listener = new FutureResponseListener(request);
            connection.send(request, listener);
            consume(input, false);
            httpResponse = "" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 0\r\n" + "\r\n";
            output.write(httpResponse.getBytes(StandardCharsets.UTF_8));
            output.flush();
            listener.get(5, TimeUnit.SECONDS);
        }
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) InputStream(java.io.InputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) FuturePromise(org.eclipse.jetty.util.FuturePromise) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) Connection(org.eclipse.jetty.client.api.Connection) ServerSocket(java.net.ServerSocket) EndPoint(org.eclipse.jetty.io.EndPoint) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 28 with FuturePromise

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

the class HttpClientTimeoutTest method testTimeoutOnListenerWithExplicitConnection.

@Slow
@Test
public void testTimeoutOnListenerWithExplicitConnection() throws Exception {
    long timeout = 1000;
    start(new TimeoutHandler(2 * timeout));
    final CountDownLatch latch = new CountDownLatch(1);
    Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
    FuturePromise<Connection> futureConnection = new FuturePromise<>();
    destination.newConnection(futureConnection);
    try (Connection connection = futureConnection.get(5, TimeUnit.SECONDS)) {
        Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(timeout, TimeUnit.MILLISECONDS);
        connection.send(request, new Response.CompleteListener() {

            @Override
            public void onComplete(Result result) {
                Assert.assertTrue(result.isFailed());
                latch.countDown();
            }
        });
        Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
    }
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Destination(org.eclipse.jetty.client.api.Destination) FuturePromise(org.eclipse.jetty.util.FuturePromise) Connection(org.eclipse.jetty.client.api.Connection) SslConnection(org.eclipse.jetty.io.ssl.SslConnection) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 29 with FuturePromise

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

the class HttpClientTimeoutTest method testTimeoutIsCancelledOnSuccessWithExplicitConnection.

@Slow
@Test
public void testTimeoutIsCancelledOnSuccessWithExplicitConnection() throws Exception {
    long timeout = 1000;
    start(new TimeoutHandler(timeout));
    final CountDownLatch latch = new CountDownLatch(1);
    Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
    FuturePromise<Connection> futureConnection = new FuturePromise<>();
    destination.newConnection(futureConnection);
    try (Connection connection = futureConnection.get(5, TimeUnit.SECONDS)) {
        Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(scheme).timeout(2 * timeout, TimeUnit.MILLISECONDS);
        connection.send(request, new Response.CompleteListener() {

            @Override
            public void onComplete(Result result) {
                Response response = result.getResponse();
                Assert.assertEquals(200, response.getStatus());
                Assert.assertFalse(result.isFailed());
                latch.countDown();
            }
        });
        Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
        TimeUnit.MILLISECONDS.sleep(2 * timeout);
        Assert.assertNull(request.getAbortCause());
    }
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Destination(org.eclipse.jetty.client.api.Destination) FuturePromise(org.eclipse.jetty.util.FuturePromise) Connection(org.eclipse.jetty.client.api.Connection) SslConnection(org.eclipse.jetty.io.ssl.SslConnection) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 30 with FuturePromise

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

the class Usage method testRequestWithExplicitConnectionControl.

@Test
public void testRequestWithExplicitConnectionControl() throws Exception {
    HttpClient client = new HttpClient();
    client.start();
    // Create an explicit connection, and use try-with-resources to manage it
    FuturePromise<Connection> futureConnection = new FuturePromise<>();
    client.getDestination("http", "localhost", 8080).newConnection(futureConnection);
    try (Connection connection = futureConnection.get(5, TimeUnit.SECONDS)) {
        Request request = client.newRequest("localhost", 8080);
        // Asynchronous send but using FutureResponseListener
        FutureResponseListener listener = new FutureResponseListener(request);
        connection.send(request, listener);
        // Wait for the response on the listener
        Response response = listener.get(5, TimeUnit.SECONDS);
        Assert.assertNotNull(response);
        Assert.assertEquals(200, response.getStatus());
    }
}
Also used : HttpClient(org.eclipse.jetty.client.HttpClient) FuturePromise(org.eclipse.jetty.util.FuturePromise) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Aggregations

FuturePromise (org.eclipse.jetty.util.FuturePromise)49 Test (org.junit.Test)42 Session (org.eclipse.jetty.http2.api.Session)40 CountDownLatch (java.util.concurrent.CountDownLatch)38 HttpFields (org.eclipse.jetty.http.HttpFields)36 Stream (org.eclipse.jetty.http2.api.Stream)36 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)36 MetaData (org.eclipse.jetty.http.MetaData)35 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)29 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)25 Callback (org.eclipse.jetty.util.Callback)22 HttpServletResponse (javax.servlet.http.HttpServletResponse)18 HttpServletRequest (javax.servlet.http.HttpServletRequest)17 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)15 ISession (org.eclipse.jetty.http2.ISession)15 IOException (java.io.IOException)13 ServletException (javax.servlet.ServletException)13 FutureCallback (org.eclipse.jetty.util.FutureCallback)11 ByteBuffer (java.nio.ByteBuffer)10 HttpServlet (javax.servlet.http.HttpServlet)10