Search in sources :

Example 51 with Frame

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

the class StreamResetTest method testServerExceptionConsumesQueuedData.

@Test
public void testServerExceptionConsumesQueuedData() throws Exception {
    try (StacklessLogging suppressor = new StacklessLogging(HttpChannel.class)) {
        start(new HttpServlet() {

            @Override
            protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                try {
                    // Wait to let the data sent by the client to be queued.
                    Thread.sleep(1000);
                    throw new IllegalStateException("explictly_thrown_by_test");
                } catch (InterruptedException e) {
                    throw new InterruptedIOException();
                }
            }
        });
        Session client = newClient(new Session.Listener.Adapter());
        Log.getLogger(HttpChannel.class).info("Expecting java.lang.IllegalStateException: explictly_thrown_by_test");
        MetaData.Request request = newRequest("GET", new HttpFields());
        HeadersFrame frame = new HeadersFrame(request, null, false);
        FuturePromise<Stream> promise = new FuturePromise<>();
        client.newStream(frame, promise, new Stream.Listener.Adapter());
        Stream stream = promise.get(5, TimeUnit.SECONDS);
        ByteBuffer data = ByteBuffer.allocate(FlowControlStrategy.DEFAULT_WINDOW_SIZE);
        CountDownLatch dataLatch = new CountDownLatch(1);
        stream.data(new DataFrame(stream.getId(), data, false), new Callback() {

            @Override
            public void succeeded() {
                dataLatch.countDown();
            }
        });
        // The server does not read the data, so the flow control window should be zero.
        Assert.assertTrue(dataLatch.await(5, TimeUnit.SECONDS));
        Assert.assertEquals(0, ((ISession) client).updateSendWindow(0));
        // Wait for the server process the exception, and
        // for the client to process the window updates.
        Thread.sleep(2000);
        Assert.assertThat(((ISession) client).updateSendWindow(0), Matchers.greaterThan(0));
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) WriteListener(javax.servlet.WriteListener) 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) HttpChannel(org.eclipse.jetty.server.HttpChannel) 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) ByteBuffer(java.nio.ByteBuffer) Callback(org.eclipse.jetty.util.Callback) FutureCallback(org.eclipse.jetty.util.FutureCallback) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Session(org.eclipse.jetty.http2.api.Session) ISession(org.eclipse.jetty.http2.ISession) Test(org.junit.Test)

Example 52 with Frame

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

the class StreamResetTest method testBlockingWriteAfterStreamReceivingReset.

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

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Charset charset = StandardCharsets.UTF_8;
            byte[] data = "AFTER RESET".getBytes(charset);
            response.setStatus(200);
            response.setContentType("text/plain;charset=" + charset.name());
            response.setContentLength(data.length * 10);
            response.flushBuffer();
            try {
                // Wait for the reset to happen.
                Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
            } catch (InterruptedException x) {
                throw new InterruptedIOException();
            }
            try {
                // been reset, it should throw an exception.
                for (int i = 0; i < 10; i++) {
                    Thread.sleep(500);
                    response.getOutputStream().write(data);
                    response.flushBuffer();
                }
            } catch (InterruptedException x) {
            } catch (IOException x) {
                dataLatch.countDown();
            }
        }
    });
    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) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Charset(java.nio.charset.Charset) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) 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) 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 53 with Frame

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

the class StreamResetTest method testStreamReceivingResetIsRemoved.

@Test
public void testStreamReceivingResetIsRemoved() throws Exception {
    final AtomicReference<Stream> streamRef = new AtomicReference<>();
    final CountDownLatch resetLatch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

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

                @Override
                public void onReset(Stream stream, ResetFrame frame) {
                    Assert.assertNotNull(stream);
                    Assert.assertTrue(stream.isReset());
                    streamRef.set(stream);
                    resetLatch.countDown();
                }
            };
        }
    });
    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);
    stream.reset(resetFrame, Callback.NOOP);
    Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
    // Wait a while to let the server remove the
    // stream after returning from onReset().
    Thread.sleep(1000);
    Stream serverStream = streamRef.get();
    Assert.assertEquals(0, serverStream.getSession().getStreams().size());
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) WriteListener(javax.servlet.WriteListener) FuturePromise(org.eclipse.jetty.util.FuturePromise) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) 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) Session(org.eclipse.jetty.http2.api.Session) ISession(org.eclipse.jetty.http2.ISession) Test(org.junit.Test)

Example 54 with Frame

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

the class HTTP2ClientSession method onPushPromise.

@Override
public void onPushPromise(PushPromiseFrame frame) {
    if (LOG.isDebugEnabled())
        LOG.debug("Received {}", frame);
    int streamId = frame.getStreamId();
    int pushStreamId = frame.getPromisedStreamId();
    IStream stream = getStream(streamId);
    if (stream == null) {
        if (LOG.isDebugEnabled())
            LOG.debug("Ignoring {}, stream #{} not found", frame, streamId);
    } else {
        IStream pushStream = createRemoteStream(pushStreamId);
        pushStream.process(frame, Callback.NOOP);
        Stream.Listener listener = notifyPush(stream, pushStream, frame);
        pushStream.setListener(listener);
    }
}
Also used : IStream(org.eclipse.jetty.http2.IStream) Stream(org.eclipse.jetty.http2.api.Stream) IStream(org.eclipse.jetty.http2.IStream) EndPoint(org.eclipse.jetty.io.EndPoint)

Example 55 with Frame

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

the class AsyncIOTest method testSomeContentAvailableAfterServiceReturns.

@Test
public void testSomeContentAvailableAfterServiceReturns() throws Exception {
    final AtomicInteger count = new AtomicInteger();
    start(new HttpServlet() {

        @Override
        protected void service(final HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            final AsyncContext asyncContext = request.startAsync();
            asyncContext.setTimeout(0);
            request.getInputStream().setReadListener(new EmptyReadListener() {

                @Override
                public void onDataAvailable() throws IOException {
                    count.incrementAndGet();
                    ServletInputStream input = request.getInputStream();
                    while (input.isReady()) {
                        int read = input.read();
                        if (read < 0)
                            break;
                    }
                    if (input.isFinished())
                        asyncContext.complete();
                }
            });
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    HttpFields fields = new HttpFields();
    MetaData.Request metaData = newRequest("GET", fields);
    HeadersFrame frame = new HeadersFrame(metaData, null, false);
    final CountDownLatch latch = new CountDownLatch(1);
    FuturePromise<Stream> promise = new FuturePromise<>();
    session.newStream(frame, promise, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    Stream stream = promise.get(5, TimeUnit.SECONDS);
    // Wait until service() returns.
    Thread.sleep(1000);
    stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), false), Callback.NOOP);
    // Wait until onDataAvailable() returns.
    Thread.sleep(1000);
    stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), true), Callback.NOOP);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    // Make sure onDataAvailable() has been called twice
    Assert.assertEquals(2, count.get());
}
Also used : ReadListener(javax.servlet.ReadListener) HttpServlet(javax.servlet.http.HttpServlet) FuturePromise(org.eclipse.jetty.util.FuturePromise) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServletInputStream(javax.servlet.ServletInputStream) Stream(org.eclipse.jetty.http2.api.Stream) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)124 HttpFields (org.eclipse.jetty.http.HttpFields)107 MetaData (org.eclipse.jetty.http.MetaData)107 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)107 CountDownLatch (java.util.concurrent.CountDownLatch)104 Stream (org.eclipse.jetty.http2.api.Stream)99 Session (org.eclipse.jetty.http2.api.Session)94 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)85 FuturePromise (org.eclipse.jetty.util.FuturePromise)69 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)60 ByteBuffer (java.nio.ByteBuffer)59 Callback (org.eclipse.jetty.util.Callback)53 HttpServletResponse (javax.servlet.http.HttpServletResponse)51 Promise (org.eclipse.jetty.util.Promise)49 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)43 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)39 IOException (java.io.IOException)38 HttpServletRequest (javax.servlet.http.HttpServletRequest)38 ServletException (javax.servlet.ServletException)35 HashMap (java.util.HashMap)34