Search in sources :

Example 31 with FuturePromise

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

the class StreamCloseTest method testRequestDataClosedResponseDataClosedClosesStream.

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

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
            HeadersFrame response = new HeadersFrame(stream.getId(), metaData, null, false);
            Callback.Completable completable = new Callback.Completable();
            stream.headers(response, completable);
            return new Stream.Listener.Adapter() {

                @Override
                public void onData(final Stream stream, DataFrame frame, final Callback callback) {
                    Assert.assertTrue(((HTTP2Stream) stream).isRemotelyClosed());
                    // We must copy the data that we send asynchronously.
                    ByteBuffer data = frame.getData();
                    ByteBuffer copy = ByteBuffer.allocate(data.remaining());
                    copy.put(data).flip();
                    completable.thenRun(() -> stream.data(new DataFrame(stream.getId(), copy, frame.isEndStream()), new Callback() {

                        @Override
                        public void succeeded() {
                            Assert.assertTrue(stream.isClosed());
                            Assert.assertEquals(0, stream.getSession().getStreams().size());
                            callback.succeeded();
                            serverDataLatch.countDown();
                        }
                    }));
                }
            };
        }
    });
    final CountDownLatch completeLatch = new CountDownLatch(1);
    Session session = newClient(new Session.Listener.Adapter());
    HeadersFrame frame = new HeadersFrame(newRequest("GET", new HttpFields()), null, false);
    FuturePromise<Stream> promise = new FuturePromise<>();
    session.newStream(frame, promise, new Stream.Listener.Adapter() {

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            // The sent data callback may not be notified yet here.
            callback.succeeded();
            completeLatch.countDown();
        }
    });
    final Stream stream = promise.get(5, TimeUnit.SECONDS);
    Assert.assertFalse(stream.isClosed());
    Assert.assertFalse(((HTTP2Stream) stream).isLocallyClosed());
    final CountDownLatch clientDataLatch = new CountDownLatch(1);
    stream.data(new DataFrame(stream.getId(), ByteBuffer.wrap(new byte[512]), true), new Callback() {

        @Override
        public void succeeded() {
            // Here the stream may be just locally closed or fully closed.
            clientDataLatch.countDown();
        }
    });
    Assert.assertTrue(clientDataLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(serverDataLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(stream.isClosed());
    Assert.assertEquals(0, stream.getSession().getStreams().size());
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Stream(org.eclipse.jetty.http2.HTTP2Stream) 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) ByteBuffer(java.nio.ByteBuffer) Callback(org.eclipse.jetty.util.Callback) 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) 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 32 with FuturePromise

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

the class StreamCountTest method testServerAllowsOneStreamEnforcedByClient.

@Test
public void testServerAllowsOneStreamEnforcedByClient() throws Exception {
    start(new ServerSessionListener.Adapter() {

        @Override
        public Map<Integer, Integer> onPreface(Session session) {
            Map<Integer, Integer> settings = new HashMap<>();
            settings.put(SettingsFrame.MAX_CONCURRENT_STREAMS, 1);
            return settings;
        }

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

                @Override
                public void onData(Stream stream, DataFrame frame, Callback callback) {
                    if (frame.isEndStream()) {
                        HttpFields fields = new HttpFields();
                        MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, fields);
                        stream.headers(new HeadersFrame(stream.getId(), metaData, null, true), callback);
                    } else {
                        callback.succeeded();
                    }
                }
            };
        }
    });
    final CountDownLatch settingsLatch = new CountDownLatch(1);
    Session session = newClient(new Session.Listener.Adapter() {

        @Override
        public void onSettings(Session session, SettingsFrame frame) {
            settingsLatch.countDown();
        }
    });
    Assert.assertTrue(settingsLatch.await(5, TimeUnit.SECONDS));
    HttpFields fields = new HttpFields();
    MetaData.Request metaData = newRequest("GET", fields);
    HeadersFrame frame1 = new HeadersFrame(metaData, null, false);
    FuturePromise<Stream> streamPromise1 = new FuturePromise<>();
    final CountDownLatch responseLatch = new CountDownLatch(1);
    session.newStream(frame1, streamPromise1, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (frame.isEndStream())
                responseLatch.countDown();
        }
    });
    Stream stream1 = streamPromise1.get(5, TimeUnit.SECONDS);
    HeadersFrame frame2 = new HeadersFrame(metaData, null, false);
    FuturePromise<Stream> streamPromise2 = new FuturePromise<>();
    session.newStream(frame2, streamPromise2, new Stream.Listener.Adapter());
    try {
        streamPromise2.get(5, TimeUnit.SECONDS);
        Assert.fail();
    } catch (ExecutionException x) {
    // Expected
    }
    stream1.data(new DataFrame(stream1.getId(), BufferUtil.EMPTY_BUFFER, true), Callback.NOOP);
    Assert.assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
}
Also used : 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) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) 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 33 with FuturePromise

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

the class StreamResetTest method testResetAfterAsyncRequestBlockingWriteStalledByFlowControl.

@Test
public void testResetAfterAsyncRequestBlockingWriteStalledByFlowControl() throws Exception {
    int windowSize = FlowControlStrategy.DEFAULT_WINDOW_SIZE;
    CountDownLatch writeLatch = new CountDownLatch(1);
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            AsyncContext asyncContext = request.startAsync();
            asyncContext.start(() -> {
                try {
                    // Make sure we are in async wait before writing.
                    Thread.sleep(1000);
                    response.getOutputStream().write(new byte[10 * windowSize]);
                    asyncContext.complete();
                } catch (IOException x) {
                    writeLatch.countDown();
                } catch (Throwable x) {
                    x.printStackTrace();
                }
            });
        }
    });
    Deque<Object> dataQueue = new ArrayDeque<>();
    AtomicLong received = new AtomicLong();
    CountDownLatch latch = new CountDownLatch(1);
    Session client = newClient(new Session.Listener.Adapter());
    MetaData.Request request = newRequest("GET", new HttpFields());
    HeadersFrame frame = new HeadersFrame(request, null, true);
    FuturePromise<Stream> promise = new FuturePromise<>();
    client.newStream(frame, promise, new Stream.Listener.Adapter() {

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            dataQueue.offer(frame);
            dataQueue.offer(callback);
            // Do not consume the data yet.
            if (received.addAndGet(frame.getData().remaining()) == windowSize)
                latch.countDown();
        }
    });
    Stream stream = promise.get(5, TimeUnit.SECONDS);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    // Reset and consume.
    stream.reset(new ResetFrame(stream.getId(), ErrorCode.CANCEL_STREAM_ERROR.code), Callback.NOOP);
    dataQueue.stream().filter(item -> item instanceof Callback).map(item -> (Callback) item).forEach(Callback::succeeded);
    Assert.assertTrue(writeLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServletException(javax.servlet.ServletException) HttpOutput(org.eclipse.jetty.server.HttpOutput) HttpChannel(org.eclipse.jetty.server.HttpChannel) HttpVersion(org.eclipse.jetty.http.HttpVersion) ErrorCode(org.eclipse.jetty.http2.ErrorCode) FlowControlStrategy(org.eclipse.jetty.http2.FlowControlStrategy) Deque(java.util.Deque) InterruptedIOException(java.io.InterruptedIOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) Stream(org.eclipse.jetty.http2.api.Stream) AsyncContext(javax.servlet.AsyncContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) Charset(java.nio.charset.Charset) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) ServletOutputStream(javax.servlet.ServletOutputStream) IStream(org.eclipse.jetty.http2.IStream) WriteListener(javax.servlet.WriteListener) 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) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Session(org.eclipse.jetty.http2.api.Session) FuturePromise(org.eclipse.jetty.util.FuturePromise) FutureCallback(org.eclipse.jetty.util.FutureCallback) Log(org.eclipse.jetty.util.log.Log) ISession(org.eclipse.jetty.http2.ISession) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ArrayDeque(java.util.ArrayDeque) Assert(org.junit.Assert) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) WriteListener(javax.servlet.WriteListener) AsyncContext(javax.servlet.AsyncContext) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ServletOutputStream(javax.servlet.ServletOutputStream) IStream(org.eclipse.jetty.http2.IStream) HttpServlet(javax.servlet.http.HttpServlet) FuturePromise(org.eclipse.jetty.util.FuturePromise) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ArrayDeque(java.util.ArrayDeque) AtomicLong(java.util.concurrent.atomic.AtomicLong) Callback(org.eclipse.jetty.util.Callback) FutureCallback(org.eclipse.jetty.util.FutureCallback) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) Session(org.eclipse.jetty.http2.api.Session) ISession(org.eclipse.jetty.http2.ISession) Test(org.junit.Test)

Example 34 with FuturePromise

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

the class StreamResetTest method testStreamSendingResetIsRemoved.

@Test
public void testStreamSendingResetIsRemoved() throws Exception {
    start(new ServerSessionListener.Adapter());
    Session client = newClient(new Session.Listener.Adapter());
    MetaData.Request request = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(request, null, false);
    FuturePromise<Stream> promise = new FuturePromise<>();
    client.newStream(requestFrame, promise, new Stream.Listener.Adapter());
    Stream stream = promise.get(5, TimeUnit.SECONDS);
    ResetFrame resetFrame = new ResetFrame(stream.getId(), ErrorCode.CANCEL_STREAM_ERROR.code);
    FutureCallback resetCallback = new FutureCallback();
    stream.reset(resetFrame, resetCallback);
    resetCallback.get(5, TimeUnit.SECONDS);
    // After reset the stream should be gone.
    Assert.assertEquals(0, client.getStreams().size());
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) WriteListener(javax.servlet.WriteListener) FuturePromise(org.eclipse.jetty.util.FuturePromise) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ServletOutputStream(javax.servlet.ServletOutputStream) IStream(org.eclipse.jetty.http2.IStream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) FutureCallback(org.eclipse.jetty.util.FutureCallback) Session(org.eclipse.jetty.http2.api.Session) ISession(org.eclipse.jetty.http2.ISession) Test(org.junit.Test)

Example 35 with FuturePromise

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

the class StreamResetTest method testResetAfterAsyncRequestAsyncWriteStalledByFlowControl.

@Test
public void testResetAfterAsyncRequestAsyncWriteStalledByFlowControl() throws Exception {
    int windowSize = FlowControlStrategy.DEFAULT_WINDOW_SIZE;
    CountDownLatch writeLatch = new CountDownLatch(1);
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            AsyncContext asyncContext = request.startAsync();
            ServletOutputStream output = response.getOutputStream();
            output.setWriteListener(new WriteListener() {

                private boolean written;

                @Override
                public void onWritePossible() throws IOException {
                    while (output.isReady()) {
                        if (written) {
                            asyncContext.complete();
                            break;
                        } else {
                            output.write(new byte[10 * windowSize]);
                            written = true;
                        }
                    }
                }

                @Override
                public void onError(Throwable t) {
                    writeLatch.countDown();
                }
            });
        }
    });
    Deque<Callback> dataQueue = new ArrayDeque<>();
    AtomicLong received = new AtomicLong();
    CountDownLatch latch = new CountDownLatch(1);
    Session client = newClient(new Session.Listener.Adapter());
    MetaData.Request request = newRequest("GET", new HttpFields());
    HeadersFrame frame = new HeadersFrame(request, null, true);
    FuturePromise<Stream> promise = new FuturePromise<>();
    client.newStream(frame, promise, new Stream.Listener.Adapter() {

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            dataQueue.offer(callback);
            // Do not consume the data yet.
            if (received.addAndGet(frame.getData().remaining()) == windowSize)
                latch.countDown();
        }
    });
    Stream stream = promise.get(5, TimeUnit.SECONDS);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    // Reset and consume.
    stream.reset(new ResetFrame(stream.getId(), ErrorCode.CANCEL_STREAM_ERROR.code), Callback.NOOP);
    dataQueue.forEach(Callback::succeeded);
    Assert.assertTrue(writeLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) WriteListener(javax.servlet.WriteListener) ServletOutputStream(javax.servlet.ServletOutputStream) AsyncContext(javax.servlet.AsyncContext) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ServletOutputStream(javax.servlet.ServletOutputStream) IStream(org.eclipse.jetty.http2.IStream) WriteListener(javax.servlet.WriteListener) HttpServlet(javax.servlet.http.HttpServlet) FuturePromise(org.eclipse.jetty.util.FuturePromise) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ArrayDeque(java.util.ArrayDeque) AtomicLong(java.util.concurrent.atomic.AtomicLong) Callback(org.eclipse.jetty.util.Callback) FutureCallback(org.eclipse.jetty.util.FutureCallback) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) 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