Search in sources :

Example 11 with GoAwayFrame

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

the class IdleTimeoutTest method testServerNotEnforcingIdleTimeoutWithinCallback.

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

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            stream.setIdleTimeout(10 * idleTimeout);
            // Stay in the callback for more than idleTimeout,
            // but not for an integer number of idle timeouts,
            // to avoid a race where the idle timeout fires
            // again before we can send the headers to the client.
            sleep(idleTimeout + idleTimeout / 2);
            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);
            return null;
        }
    });
    connector.setIdleTimeout(idleTimeout);
    final CountDownLatch closeLatch = new CountDownLatch(1);
    Session session = newClient(new ServerSessionListener.Adapter() {

        @Override
        public void onClose(Session session, GoAwayFrame frame) {
            closeLatch.countDown();
        }
    });
    final CountDownLatch replyLatch = new CountDownLatch(1);
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(metaData, null, true);
    session.newStream(requestFrame, new Promise.Adapter<Stream>() {

        @Override
        public void succeeded(Stream stream) {
            stream.setIdleTimeout(10 * idleTimeout);
        }
    }, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            replyLatch.countDown();
        }
    });
    Assert.assertTrue(replyLatch.await(5 * idleTimeout, TimeUnit.MILLISECONDS));
    // Just make sure onClose() has never been called, but don't wait too much
    Assert.assertFalse(closeLatch.await(idleTimeout / 2, TimeUnit.MILLISECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) 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 12 with GoAwayFrame

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

the class IdleTimeoutTest method testClientEnforcingIdleTimeout.

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

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            stream.setIdleTimeout(10 * idleTimeout);
            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);
            return null;
        }

        @Override
        public void onClose(Session session, GoAwayFrame frame) {
            closeLatch.countDown();
        }
    });
    client.setIdleTimeout(idleTimeout);
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(metaData, null, true);
    session.newStream(requestFrame, new Promise.Adapter<Stream>() {

        @Override
        public void succeeded(Stream stream) {
            stream.setIdleTimeout(10 * idleTimeout);
        }
    }, new Stream.Listener.Adapter());
    Assert.assertTrue(closeLatch.await(5 * idleTimeout, TimeUnit.MILLISECONDS));
    Assert.assertTrue(session.isClosed());
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) 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 13 with GoAwayFrame

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

the class GoAwayBodyParser method onGoAway.

private boolean onGoAway(int lastStreamId, int error, byte[] payload) {
    GoAwayFrame frame = new GoAwayFrame(lastStreamId, error, payload);
    reset();
    notifyGoAway(frame);
    return true;
}
Also used : GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame)

Example 14 with GoAwayFrame

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

the class FlowControlStrategyTest method testClientExceedingSessionWindow.

@Test
public void testClientExceedingSessionWindow() throws Exception {
    // On server, we don't consume the data.
    start(new ServerSessionListener.Adapter());
    final CountDownLatch closeLatch = new CountDownLatch(1);
    Session session = newClient(new Session.Listener.Adapter() {

        @Override
        public void onClose(Session session, GoAwayFrame frame) {
            if (frame.getError() == ErrorCode.FLOW_CONTROL_ERROR.code)
                closeLatch.countDown();
        }
    });
    // Consume the whole session and stream window.
    MetaData.Request metaData = newRequest("POST", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(metaData, null, false);
    CompletableFuture<Stream> completable = new CompletableFuture<>();
    session.newStream(requestFrame, Promise.from(completable), new Stream.Listener.Adapter());
    Stream stream = completable.get(5, TimeUnit.SECONDS);
    ByteBuffer data = ByteBuffer.allocate(FlowControlStrategy.DEFAULT_WINDOW_SIZE);
    final CountDownLatch dataLatch = new CountDownLatch(1);
    stream.data(new DataFrame(stream.getId(), data, false), new Callback() {

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

        @Override
        public void succeeded() {
            dataLatch.countDown();
        }
    });
    Assert.assertTrue(dataLatch.await(5, TimeUnit.SECONDS));
    // The following "sneaky" write may clash with the write
    // of the reply SETTINGS frame sent by the client in
    // response to the server SETTINGS frame.
    // It is not enough to use a latch on the server to
    // wait for the reply frame, since the client may have
    // sent the bytes, but not yet be ready to write again.
    Thread.sleep(1000);
    // Now the client is supposed to not send more frames.
    // If it does, the connection must be closed.
    HTTP2Session http2Session = (HTTP2Session) session;
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(connector.getByteBufferPool());
    ByteBuffer extraData = ByteBuffer.allocate(1024);
    http2Session.getGenerator().data(lease, new DataFrame(stream.getId(), extraData, true), extraData.remaining());
    List<ByteBuffer> buffers = lease.getByteBuffers();
    http2Session.getEndPoint().write(Callback.NOOP, buffers.toArray(new ByteBuffer[buffers.size()]));
    // Expect the connection to be closed.
    Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) InvocationType(org.eclipse.jetty.util.thread.Invocable.InvocationType) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) CompletableFuture(java.util.concurrent.CompletableFuture) 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) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) Callback(org.eclipse.jetty.util.Callback) FutureCallback(org.eclipse.jetty.util.FutureCallback) 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)

Example 15 with GoAwayFrame

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

the class HTTP2Test method testClientSendsGoAwayOnStop.

@Test
public void testClientSendsGoAwayOnStop() throws Exception {
    CountDownLatch closeLatch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public void onClose(Session session, GoAwayFrame frame) {
            closeLatch.countDown();
        }
    });
    newClient(new Session.Listener.Adapter());
    sleep(1000);
    client.stop();
    Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) CountDownLatch(java.util.concurrent.CountDownLatch) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)19 GoAwayFrame (org.eclipse.jetty.http2.frames.GoAwayFrame)18 CountDownLatch (java.util.concurrent.CountDownLatch)17 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)16 Session (org.eclipse.jetty.http2.api.Session)14 HttpFields (org.eclipse.jetty.http.HttpFields)13 MetaData (org.eclipse.jetty.http.MetaData)13 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)13 Stream (org.eclipse.jetty.http2.api.Stream)12 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)11 ByteBuffer (java.nio.ByteBuffer)9 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)9 FuturePromise (org.eclipse.jetty.util.FuturePromise)8 Parser (org.eclipse.jetty.http2.parser.Parser)7 Promise (org.eclipse.jetty.util.Promise)7 OutputStream (java.io.OutputStream)6 HashMap (java.util.HashMap)6 ServletInputStream (javax.servlet.ServletInputStream)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 Socket (java.net.Socket)5