Search in sources :

Example 41 with FuturePromise

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

the class IdleTimeoutTest method testStreamIdleTimeoutIsNotEnforcedWhenReceiving.

@Test
public void testStreamIdleTimeoutIsNotEnforcedWhenReceiving() throws Exception {
    final CountDownLatch timeoutLatch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            stream.setIdleTimeout(idleTimeout);
            return new Stream.Listener.Adapter() {

                @Override
                public boolean onIdleTimeout(Stream stream, Throwable x) {
                    timeoutLatch.countDown();
                    return true;
                }
            };
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(metaData, null, false);
    FuturePromise<Stream> promise = new FuturePromise<>();
    session.newStream(requestFrame, promise, new Stream.Listener.Adapter());
    final Stream stream = promise.get(5, TimeUnit.SECONDS);
    sleep(idleTimeout / 2);
    final CountDownLatch dataLatch = new CountDownLatch(1);
    stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), false), new Callback() {

        private int sends;

        @Override
        public void succeeded() {
            sleep(idleTimeout / 2);
            final boolean last = ++sends == 2;
            stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), last), !last ? this : new Callback() {

                @Override
                public InvocationType getInvocationType() {
                    return InvocationType.NON_BLOCKING;
                }

                @Override
                public void succeeded() {
                    // Idle timeout should not fire while receiving.
                    Assert.assertEquals(1, timeoutLatch.getCount());
                    dataLatch.countDown();
                }
            });
        }
    });
    Assert.assertTrue(dataLatch.await(5 * idleTimeout, TimeUnit.MILLISECONDS));
    // The server did not send a response, so it will eventually timeout.
    Assert.assertTrue(timeoutLatch.await(5 * idleTimeout, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) FuturePromise(org.eclipse.jetty.util.FuturePromise) InvocationType(org.eclipse.jetty.util.thread.Invocable.InvocationType) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServletInputStream(javax.servlet.ServletInputStream) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 42 with FuturePromise

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

the class IdleTimeoutTest method testStreamIdleTimeoutIsNotEnforcedWhenSending.

@Test
public void testStreamIdleTimeoutIsNotEnforcedWhenSending() throws Exception {
    final CountDownLatch resetLatch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
            stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
            return null;
        }

        @Override
        public void onReset(Session session, ResetFrame frame) {
            resetLatch.countDown();
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(metaData, null, false);
    FuturePromise<Stream> promise = new FuturePromise<Stream>() {

        @Override
        public void succeeded(Stream stream) {
            stream.setIdleTimeout(idleTimeout);
            super.succeeded(stream);
        }
    };
    session.newStream(requestFrame, promise, new Stream.Listener.Adapter());
    final Stream stream = promise.get(5, TimeUnit.SECONDS);
    Callback.Completable completable1 = new Callback.Completable();
    sleep(idleTimeout / 2);
    stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), false), completable1);
    completable1.thenCompose(nil -> {
        Callback.Completable completable2 = new Callback.Completable();
        sleep(idleTimeout / 2);
        stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), false), completable2);
        return completable2;
    }).thenRun(() -> {
        sleep(idleTimeout / 2);
        stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), true), Callback.NOOP);
    });
    Assert.assertFalse(resetLatch.await(1, TimeUnit.SECONDS));
}
Also used : ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) HttpVersion(org.eclipse.jetty.http.HttpVersion) TimeoutException(java.util.concurrent.TimeoutException) FlowControlStrategy(org.eclipse.jetty.http2.FlowControlStrategy) ByteBuffer(java.nio.ByteBuffer) Stream(org.eclipse.jetty.http2.api.Stream) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) MetaData(org.eclipse.jetty.http.MetaData) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) HttpFields(org.eclipse.jetty.http.HttpFields) Callback(org.eclipse.jetty.util.Callback) InvocationType(org.eclipse.jetty.util.thread.Invocable.InvocationType) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Session(org.eclipse.jetty.http2.api.Session) FuturePromise(org.eclipse.jetty.util.FuturePromise) Log(org.eclipse.jetty.util.log.Log) Assert(org.junit.Assert) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) FuturePromise(org.eclipse.jetty.util.FuturePromise) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletResponse(javax.servlet.http.HttpServletResponse) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServletInputStream(javax.servlet.ServletInputStream) Stream(org.eclipse.jetty.http2.api.Stream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 43 with FuturePromise

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

the class PriorityTest method testPriorityAfterHeaders.

@Test
public void testPriorityAfterHeaders() throws Exception {
    CountDownLatch beforeRequests = new CountDownLatch(1);
    CountDownLatch afterRequests = new CountDownLatch(2);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            try {
                beforeRequests.await(5, TimeUnit.SECONDS);
                MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
                HeadersFrame responseFrame = new HeadersFrame(stream.getId(), metaData, null, true);
                stream.headers(responseFrame, Callback.NOOP);
                afterRequests.countDown();
                return null;
            } catch (InterruptedException x) {
                x.printStackTrace();
                return null;
            }
        }
    });
    CountDownLatch responses = new CountDownLatch(2);
    Stream.Listener.Adapter listener = new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (frame.isEndStream())
                responses.countDown();
        }
    };
    Session session = newClient(new Session.Listener.Adapter());
    MetaData metaData1 = newRequest("GET", "/one", new HttpFields());
    HeadersFrame headersFrame1 = new HeadersFrame(metaData1, null, true);
    FuturePromise<Stream> promise1 = new FuturePromise<>();
    session.newStream(headersFrame1, promise1, listener);
    Stream stream1 = promise1.get(5, TimeUnit.SECONDS);
    MetaData metaData2 = newRequest("GET", "/two", new HttpFields());
    HeadersFrame headersFrame2 = new HeadersFrame(metaData2, null, true);
    FuturePromise<Stream> promise2 = new FuturePromise<>();
    session.newStream(headersFrame2, promise2, listener);
    Stream stream2 = promise2.get(5, TimeUnit.SECONDS);
    int streamId = session.priority(new PriorityFrame(stream1.getId(), stream2.getId(), 13, false), Callback.NOOP);
    Assert.assertEquals(stream1.getId(), streamId);
    // Give time to the PRIORITY frame to arrive to server.
    Thread.sleep(1000);
    beforeRequests.countDown();
    Assert.assertTrue(afterRequests.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(responses.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) FuturePromise(org.eclipse.jetty.util.FuturePromise) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) PriorityFrame(org.eclipse.jetty.http2.frames.PriorityFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 44 with FuturePromise

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

the class HttpClientExplicitConnectionTest method testExplicitConnectionResponseListeners.

@Test
public void testExplicitConnectionResponseListeners() throws Exception {
    start(new EmptyServerHandler());
    Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
    FuturePromise<Connection> futureConnection = new FuturePromise<>();
    destination.newConnection(futureConnection);
    Connection connection = futureConnection.get(5, TimeUnit.SECONDS);
    CountDownLatch responseLatch = new CountDownLatch(1);
    Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(scheme).onResponseSuccess(response -> responseLatch.countDown());
    FutureResponseListener listener = new FutureResponseListener(request);
    connection.send(request, listener);
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
}
Also used : Destination(org.eclipse.jetty.client.api.Destination) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) FuturePromise(org.eclipse.jetty.util.FuturePromise) Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) CountDownLatch(java.util.concurrent.CountDownLatch) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 45 with FuturePromise

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

the class FlowControlStrategyTest method testFlowControlWithConcurrentSettings.

@Test
public void testFlowControlWithConcurrentSettings() throws Exception {
    // Initial window is 64 KiB. We allow the client to send 1024 B
    // then we change the window to 512 B. At this point, the client
    // must stop sending data (although the initial window allows it).
    final int size = 512;
    // We get 3 data frames: the first of 1024 and 2 of 512 each
    // after the flow control window has been reduced.
    final CountDownLatch dataLatch = new CountDownLatch(3);
    final AtomicReference<Callback> callbackRef = new AtomicReference<>();
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame requestFrame) {
            HttpFields fields = new HttpFields();
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, 200, fields);
            HeadersFrame responseFrame = new HeadersFrame(stream.getId(), response, null, true);
            stream.headers(responseFrame, Callback.NOOP);
            return new Stream.Listener.Adapter() {

                private final AtomicInteger dataFrames = new AtomicInteger();

                @Override
                public void onData(Stream stream, DataFrame frame, Callback callback) {
                    dataLatch.countDown();
                    int dataFrameCount = dataFrames.incrementAndGet();
                    if (dataFrameCount == 1) {
                        callbackRef.set(callback);
                        Map<Integer, Integer> settings = new HashMap<>();
                        settings.put(SettingsFrame.INITIAL_WINDOW_SIZE, size);
                        stream.getSession().settings(new SettingsFrame(settings, false), Callback.NOOP);
                    // Do not succeed the callback here.
                    } else if (dataFrameCount > 1) {
                        // Consume the data.
                        callback.succeeded();
                    }
                }
            };
        }
    });
    // Two SETTINGS frames, the initial one and the one we send from the server.
    final CountDownLatch settingsLatch = new CountDownLatch(2);
    Session session = newClient(new Session.Listener.Adapter() {

        @Override
        public void onSettings(Session session, SettingsFrame frame) {
            settingsLatch.countDown();
        }
    });
    MetaData.Request request = newRequest("POST", new HttpFields());
    FuturePromise<Stream> promise = new FuturePromise<>();
    session.newStream(new HeadersFrame(request, null, false), promise, new Stream.Listener.Adapter());
    Stream stream = promise.get(5, TimeUnit.SECONDS);
    // Send first chunk that exceeds the window.
    Callback.Completable completable = new Callback.Completable();
    stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(size * 2), false), completable);
    settingsLatch.await(5, TimeUnit.SECONDS);
    completable.thenRun(() -> {
        // Send the second chunk of data, must not arrive since we're flow control stalled on the client.
        stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(size * 2), true), Callback.NOOP);
    });
    Assert.assertFalse(dataLatch.await(1, TimeUnit.SECONDS));
    // Consume the data arrived to server, this will resume flow control on the client.
    callbackRef.get().succeeded();
    Assert.assertTrue(dataLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HTTP2Stream(org.eclipse.jetty.http2.HTTP2Stream) Stream(org.eclipse.jetty.http2.api.Stream) FuturePromise(org.eclipse.jetty.util.FuturePromise) AtomicReference(java.util.concurrent.atomic.AtomicReference) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) Callback(org.eclipse.jetty.util.Callback) FutureCallback(org.eclipse.jetty.util.FutureCallback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) HashMap(java.util.HashMap) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) ISession(org.eclipse.jetty.http2.ISession) 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