Search in sources :

Example 86 with Session

use of com.developmentontheedge.be5.api.Session 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 87 with Session

use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.

the class PingTest method testPing.

@Test
public void testPing() throws Exception {
    start(new ServerSessionListener.Adapter());
    final byte[] payload = new byte[8];
    new Random().nextBytes(payload);
    final CountDownLatch latch = new CountDownLatch(1);
    Session session = newClient(new Session.Listener.Adapter() {

        @Override
        public void onPing(Session session, PingFrame frame) {
            Assert.assertTrue(frame.isReply());
            Assert.assertArrayEquals(payload, frame.getPayload());
            latch.countDown();
        }
    });
    PingFrame frame = new PingFrame(payload, false);
    session.ping(frame, Callback.NOOP);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Random(java.util.Random) PingFrame(org.eclipse.jetty.http2.frames.PingFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 88 with Session

use of com.developmentontheedge.be5.api.Session 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 89 with Session

use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.

the class IdleTimeoutTest method testServerEnforcingStreamIdleTimeout.

@Test
public void testServerEnforcingStreamIdleTimeout() 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;
                }
            };
        }
    });
    final CountDownLatch resetLatch = new CountDownLatch(1);
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    // Stream does not end here, but we won't send any DATA frame.
    HeadersFrame requestFrame = new HeadersFrame(metaData, null, false);
    session.newStream(requestFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onReset(Stream stream, ResetFrame frame) {
            resetLatch.countDown();
        }
    });
    Assert.assertTrue(timeoutLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
    // Stream must be gone.
    Assert.assertTrue(session.getStreams().isEmpty());
    // Session must not be closed, nor disconnected.
    Assert.assertFalse(session.isClosed());
    Assert.assertFalse(((HTTP2Session) session).isDisconnected());
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) 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) 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 90 with Session

use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.

the class FlowControlStalledTest method testSessionStalledIsInvokedOnlyOnce.

@Test
public void testSessionStalledIsInvokedOnlyOnce() throws Exception {
    AtomicReference<CountDownLatch> stallLatch = new AtomicReference<>(new CountDownLatch(1));
    CountDownLatch unstallLatch = new CountDownLatch(1);
    start(() -> new BufferingFlowControlStrategy(0.5f) {

        @Override
        public void onSessionStalled(ISession session) {
            super.onSessionStalled(session);
            stallLatch.get().countDown();
        }

        @Override
        protected void onSessionUnstalled(ISession session) {
            super.onSessionUnstalled(session);
            unstallLatch.countDown();
        }
    }, new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Request request = (MetaData.Request) frame.getMetaData();
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
            if (request.getURIString().endsWith("/stall")) {
                stream.headers(new HeadersFrame(stream.getId(), response, null, false), new Callback() {

                    @Override
                    public void succeeded() {
                        // Send a large chunk of data so the session gets stalled.
                        ByteBuffer data = ByteBuffer.allocate(FlowControlStrategy.DEFAULT_WINDOW_SIZE + 1);
                        stream.data(new DataFrame(stream.getId(), data, true), NOOP);
                    }
                });
            } else {
                stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
            }
            return null;
        }
    });
    // Use a large stream window so that only the session gets stalled.
    client.setInitialStreamRecvWindow(5 * FlowControlStrategy.DEFAULT_WINDOW_SIZE);
    Session session = newClient(new Session.Listener.Adapter() {

        @Override
        public Map<Integer, Integer> onPreface(Session session) {
            Map<Integer, Integer> settings = new HashMap<>();
            settings.put(SettingsFrame.INITIAL_WINDOW_SIZE, client.getInitialStreamRecvWindow());
            return settings;
        }
    });
    CountDownLatch latch = new CountDownLatch(1);
    Queue<Callback> callbacks = new ArrayDeque<>();
    MetaData.Request request = newRequest("GET", "/stall", new HttpFields());
    session.newStream(new HeadersFrame(request, null, true), new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            callbacks.offer(callback);
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    Assert.assertTrue(stallLatch.get().await(5, TimeUnit.SECONDS));
    // The session is now stalled, check that writing a second stream
    // does not result in the session be notified again of being stalled.
    stallLatch.set(new CountDownLatch(1));
    request = newRequest("GET", "/", new HttpFields());
    session.newStream(new HeadersFrame(request, null, true), new Promise.Adapter<>(), new Stream.Listener.Adapter());
    Assert.assertFalse(stallLatch.get().await(1, TimeUnit.SECONDS));
    // Consume all data.
    while (!latch.await(10, TimeUnit.MILLISECONDS)) {
        Callback callback = callbacks.poll();
        if (callback != null)
            callback.succeeded();
    }
    // Make sure the unstall callback is invoked.
    Assert.assertTrue(unstallLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) 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) IStream(org.eclipse.jetty.http2.IStream) BufferingFlowControlStrategy(org.eclipse.jetty.http2.BufferingFlowControlStrategy) AtomicReference(java.util.concurrent.atomic.AtomicReference) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) ArrayDeque(java.util.ArrayDeque) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) ISession(org.eclipse.jetty.http2.ISession) Callback(org.eclipse.jetty.util.Callback) HashMap(java.util.HashMap) Map(java.util.Map) 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)

Aggregations

Session (org.eclipse.jetty.http2.api.Session)101 CountDownLatch (java.util.concurrent.CountDownLatch)93 Test (org.junit.Test)93 HttpFields (org.eclipse.jetty.http.HttpFields)89 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)89 Stream (org.eclipse.jetty.http2.api.Stream)88 MetaData (org.eclipse.jetty.http.MetaData)86 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)77 FuturePromise (org.eclipse.jetty.util.FuturePromise)74 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)51 Callback (org.eclipse.jetty.util.Callback)51 Promise (org.eclipse.jetty.util.Promise)51 HttpServletResponse (javax.servlet.http.HttpServletResponse)47 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)39 HttpServletRequest (javax.servlet.http.HttpServletRequest)35 IOException (java.io.IOException)33 ServletException (javax.servlet.ServletException)32 HttpServlet (javax.servlet.http.HttpServlet)30 ISession (org.eclipse.jetty.http2.ISession)26 ByteBuffer (java.nio.ByteBuffer)24