Search in sources :

Example 16 with ResetFrame

use of org.eclipse.jetty.http2.frames.ResetFrame in project jetty.project by eclipse.

the class StreamCloseTest method testPushedStreamResetIsClosed.

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

        @Override
        public Stream.Listener onNewStream(final Stream stream, HeadersFrame frame) {
            PushPromiseFrame pushFrame = new PushPromiseFrame(stream.getId(), 0, newRequest("GET", new HttpFields()));
            stream.push(pushFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

                @Override
                public void onReset(Stream pushedStream, ResetFrame frame) {
                    Assert.assertTrue(pushedStream.isReset());
                    Assert.assertTrue(pushedStream.isClosed());
                    HeadersFrame response = new HeadersFrame(stream.getId(), new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields()), null, true);
                    stream.headers(response, Callback.NOOP);
                    serverLatch.countDown();
                }
            });
            return null;
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    HeadersFrame frame = new HeadersFrame(newRequest("GET", new HttpFields()), null, true);
    final CountDownLatch clientLatch = new CountDownLatch(2);
    session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public Stream.Listener onPush(final Stream pushedStream, PushPromiseFrame frame) {
            pushedStream.reset(new ResetFrame(pushedStream.getId(), ErrorCode.REFUSED_STREAM_ERROR.code), new Callback() {

                @Override
                public void succeeded() {
                    Assert.assertTrue(pushedStream.isReset());
                    Assert.assertTrue(pushedStream.isClosed());
                    clientLatch.countDown();
                }
            });
            return null;
        }

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            clientLatch.countDown();
        }
    });
    Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) CountDownLatch(java.util.concurrent.CountDownLatch) PushPromiseFrame(org.eclipse.jetty.http2.frames.PushPromiseFrame) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) 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) 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 17 with ResetFrame

use of org.eclipse.jetty.http2.frames.ResetFrame 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 18 with ResetFrame

use of org.eclipse.jetty.http2.frames.ResetFrame 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 19 with ResetFrame

use of org.eclipse.jetty.http2.frames.ResetFrame in project jetty.project by eclipse.

the class StreamResetTest method testAsyncWriteAfterStreamReceivingReset.

@Test
public void testAsyncWriteAfterStreamReceivingReset() throws Exception {
    final CountDownLatch resetLatch = new CountDownLatch(1);
    final CountDownLatch dataLatch = new CountDownLatch(1);
    start(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
            Charset charset = StandardCharsets.UTF_8;
            final ByteBuffer data = ByteBuffer.wrap("AFTER RESET".getBytes(charset));
            response.setStatus(200);
            response.setContentType("text/plain;charset=" + charset.name());
            response.setContentLength(data.remaining());
            response.flushBuffer();
            try {
                // Wait for the reset to happen.
                Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
                // Wait for the reset to arrive to the server and be processed.
                Thread.sleep(1000);
            } catch (InterruptedException x) {
                throw new InterruptedIOException();
            }
            // Write some content asynchronously after the stream has been reset.
            final AsyncContext context = request.startAsync();
            new Thread() {

                @Override
                public void run() {
                    try {
                        // Wait for the request thread to exit
                        // doGet() so this is really asynchronous.
                        Thread.sleep(1000);
                        HttpOutput output = (HttpOutput) response.getOutputStream();
                        output.sendContent(data, new Callback() {

                            @Override
                            public void failed(Throwable x) {
                                context.complete();
                                dataLatch.countDown();
                            }
                        });
                    } catch (Throwable x) {
                        x.printStackTrace();
                    }
                }
            }.start();
        }
    });
    Session client = newClient(new Session.Listener.Adapter());
    MetaData.Request request = newRequest("GET", new HttpFields());
    HeadersFrame frame = new HeadersFrame(request, null, true);
    client.newStream(frame, new FuturePromise<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            stream.reset(new ResetFrame(stream.getId(), ErrorCode.CANCEL_STREAM_ERROR.code), Callback.NOOP);
            resetLatch.countDown();
        }
    });
    Assert.assertTrue(dataLatch.await(5, TimeUnit.SECONDS));
}
Also used : InterruptedIOException(java.io.InterruptedIOException) 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) HttpServletResponse(javax.servlet.http.HttpServletResponse) Charset(java.nio.charset.Charset) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) HttpOutput(org.eclipse.jetty.server.HttpOutput) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) 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 20 with ResetFrame

use of org.eclipse.jetty.http2.frames.ResetFrame 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

ResetFrame (org.eclipse.jetty.http2.frames.ResetFrame)24 Test (org.junit.Test)21 Stream (org.eclipse.jetty.http2.api.Stream)20 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)20 CountDownLatch (java.util.concurrent.CountDownLatch)19 MetaData (org.eclipse.jetty.http.MetaData)19 HttpFields (org.eclipse.jetty.http.HttpFields)18 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)18 Session (org.eclipse.jetty.http2.api.Session)16 Callback (org.eclipse.jetty.util.Callback)13 FuturePromise (org.eclipse.jetty.util.FuturePromise)12 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)11 IOException (java.io.IOException)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)10 ByteBuffer (java.nio.ByteBuffer)9 ServletOutputStream (javax.servlet.ServletOutputStream)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 ISession (org.eclipse.jetty.http2.ISession)9 ServletException (javax.servlet.ServletException)8 WriteListener (javax.servlet.WriteListener)8