Search in sources :

Example 51 with Session

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

the class StreamCloseTest method testRequestClosedRemotelyClosesStream.

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

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            Assert.assertTrue(((HTTP2Stream) stream).isRemotelyClosed());
            latch.countDown();
            return null;
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    HeadersFrame frame = new HeadersFrame(newRequest("GET", new HttpFields()), null, true);
    FuturePromise<Stream> promise = new FuturePromise<>();
    session.newStream(frame, promise, null);
    Stream stream = promise.get(5, TimeUnit.SECONDS);
    Assert.assertTrue(((HTTP2Stream) stream).isLocallyClosed());
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Stream(org.eclipse.jetty.http2.HTTP2Stream) FuturePromise(org.eclipse.jetty.util.FuturePromise) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) 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 52 with Session

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

the class HTTP2Test method testServerSendsGoAwayOnStop.

@Test
public void testServerSendsGoAwayOnStop() throws Exception {
    start(new ServerSessionListener.Adapter());
    CountDownLatch closeLatch = new CountDownLatch(1);
    newClient(new Session.Listener.Adapter() {

        @Override
        public void onClose(Session session, GoAwayFrame frame) {
            closeLatch.countDown();
        }
    });
    sleep(1000);
    server.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)

Example 53 with Session

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

the class HTTP2Test method testMaxConcurrentStreams.

@Test
public void testMaxConcurrentStreams() throws Exception {
    int maxStreams = 2;
    start(new ServerSessionListener.Adapter() {

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

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields(), 0);
            stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
            return null;
        }
    });
    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));
    MetaData.Request request1 = newRequest("GET", new HttpFields());
    FuturePromise<Stream> promise1 = new FuturePromise<>();
    CountDownLatch exchangeLatch1 = new CountDownLatch(2);
    session.newStream(new HeadersFrame(request1, null, false), promise1, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (frame.isEndStream())
                exchangeLatch1.countDown();
        }
    });
    Stream stream1 = promise1.get(5, TimeUnit.SECONDS);
    MetaData.Request request2 = newRequest("GET", new HttpFields());
    FuturePromise<Stream> promise2 = new FuturePromise<>();
    CountDownLatch exchangeLatch2 = new CountDownLatch(2);
    session.newStream(new HeadersFrame(request2, null, false), promise2, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (frame.isEndStream())
                exchangeLatch2.countDown();
        }
    });
    Stream stream2 = promise2.get(5, TimeUnit.SECONDS);
    // The third stream must not be created.
    MetaData.Request request3 = newRequest("GET", new HttpFields());
    CountDownLatch maxStreamsLatch = new CountDownLatch(1);
    session.newStream(new HeadersFrame(request3, null, false), new Promise.Adapter<Stream>() {

        @Override
        public void failed(Throwable x) {
            if (x instanceof IllegalStateException)
                maxStreamsLatch.countDown();
        }
    }, new Stream.Listener.Adapter());
    Assert.assertTrue(maxStreamsLatch.await(5, TimeUnit.SECONDS));
    Assert.assertEquals(2, session.getStreams().size());
    // End the second stream.
    stream2.data(new DataFrame(stream2.getId(), BufferUtil.EMPTY_BUFFER, true), new Callback() {

        @Override
        public void succeeded() {
            exchangeLatch2.countDown();
        }
    });
    Assert.assertTrue(exchangeLatch2.await(5, TimeUnit.SECONDS));
    Assert.assertEquals(1, session.getStreams().size());
    // Create a fourth stream.
    MetaData.Request request4 = newRequest("GET", new HttpFields());
    CountDownLatch exchangeLatch4 = new CountDownLatch(2);
    session.newStream(new HeadersFrame(request4, null, true), new Promise.Adapter<Stream>() {

        @Override
        public void succeeded(Stream result) {
            exchangeLatch4.countDown();
        }
    }, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (frame.isEndStream())
                exchangeLatch4.countDown();
        }
    });
    Assert.assertTrue(exchangeLatch4.await(5, TimeUnit.SECONDS));
    Assert.assertEquals(1, session.getStreams().size());
    // End the first stream.
    stream1.data(new DataFrame(stream1.getId(), BufferUtil.EMPTY_BUFFER, true), new Callback() {

        @Override
        public void succeeded() {
            exchangeLatch1.countDown();
        }
    });
    Assert.assertTrue(exchangeLatch2.await(5, TimeUnit.SECONDS));
    Assert.assertEquals(0, session.getStreams().size());
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) FuturePromise(org.eclipse.jetty.util.FuturePromise) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) 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) Test(org.junit.Test)

Example 54 with Session

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

the class HTTP2Test method testRequestNoContentResponseEmptyContent.

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

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
            stream.headers(new HeadersFrame(stream.getId(), response, null, false), new Callback() {

                @Override
                public void succeeded() {
                    stream.data(new DataFrame(stream.getId(), BufferUtil.EMPTY_BUFFER, true), NOOP);
                }
            });
            return null;
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    HttpFields fields = new HttpFields();
    MetaData.Request metaData = newRequest("GET", fields);
    HeadersFrame frame = new HeadersFrame(metaData, null, true);
    final CountDownLatch latch = new CountDownLatch(1);
    session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            Assert.assertFalse(frame.isEndStream());
            Assert.assertEquals(stream.getId(), frame.getStreamId());
            MetaData.Response response = (MetaData.Response) frame.getMetaData();
            Assert.assertEquals(200, response.getStatus());
        }

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            Assert.assertTrue(frame.isEndStream());
            callback.succeeded();
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletResponse(javax.servlet.http.HttpServletResponse) 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) 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 55 with Session

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

the class IdleTimeoutTest method testServerEnforcingIdleTimeout.

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

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame requestFrame) {
            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;
        }
    });
    connector.setIdleTimeout(idleTimeout);
    final CountDownLatch latch = new CountDownLatch(1);
    Session session = newClient(new Session.Listener.Adapter() {

        @Override
        public void onClose(Session session, GoAwayFrame frame) {
            latch.countDown();
        }
    });
    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(latch.await(5 * idleTimeout, 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)

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