Search in sources :

Example 91 with Session

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

the class FlowControlStalledTest method testStreamStalledIsInvokedOnlyOnce.

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

        @Override
        public void onStreamStalled(IStream stream) {
            super.onStreamStalled(stream);
            stallLatch.get().countDown();
        }

        @Override
        protected void onStreamUnstalled(IStream stream) {
            super.onStreamUnstalled(stream);
            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 stream 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 session window so that only the stream gets stalled.
    client.setInitialSessionRecvWindow(5 * FlowControlStrategy.DEFAULT_WINDOW_SIZE);
    Session client = newClient(new Session.Listener.Adapter());
    CountDownLatch latch = new CountDownLatch(1);
    Queue<Callback> callbacks = new ArrayDeque<>();
    MetaData.Request request = newRequest("GET", "/stall", new HttpFields());
    client.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));
    // First stream is now stalled, check that writing a second stream
    // does not result in the first be notified again of being stalled.
    stallLatch.set(new CountDownLatch(1));
    request = newRequest("GET", "/", new HttpFields());
    client.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) IStream(org.eclipse.jetty.http2.IStream) 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) Callback(org.eclipse.jetty.util.Callback) 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 92 with Session

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

the class FlowControlStrategyTest method testFlowControlWithConcurrentSettings.

@Test
public void testFlowControlWithConcurrentSettings() throws Exception {
    // Initial window is 64 KiB. We allow the client to send 1024 B
    // then we change the window to 512 B. At this point, the client
    // must stop sending data (although the initial window allows it).
    final int size = 512;
    // We get 3 data frames: the first of 1024 and 2 of 512 each
    // after the flow control window has been reduced.
    final CountDownLatch dataLatch = new CountDownLatch(3);
    final AtomicReference<Callback> callbackRef = new AtomicReference<>();
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame requestFrame) {
            HttpFields fields = new HttpFields();
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, 200, fields);
            HeadersFrame responseFrame = new HeadersFrame(stream.getId(), response, null, true);
            stream.headers(responseFrame, Callback.NOOP);
            return new Stream.Listener.Adapter() {

                private final AtomicInteger dataFrames = new AtomicInteger();

                @Override
                public void onData(Stream stream, DataFrame frame, Callback callback) {
                    dataLatch.countDown();
                    int dataFrameCount = dataFrames.incrementAndGet();
                    if (dataFrameCount == 1) {
                        callbackRef.set(callback);
                        Map<Integer, Integer> settings = new HashMap<>();
                        settings.put(SettingsFrame.INITIAL_WINDOW_SIZE, size);
                        stream.getSession().settings(new SettingsFrame(settings, false), Callback.NOOP);
                    // Do not succeed the callback here.
                    } else if (dataFrameCount > 1) {
                        // Consume the data.
                        callback.succeeded();
                    }
                }
            };
        }
    });
    // Two SETTINGS frames, the initial one and the one we send from the server.
    final CountDownLatch settingsLatch = new CountDownLatch(2);
    Session session = newClient(new Session.Listener.Adapter() {

        @Override
        public void onSettings(Session session, SettingsFrame frame) {
            settingsLatch.countDown();
        }
    });
    MetaData.Request request = newRequest("POST", new HttpFields());
    FuturePromise<Stream> promise = new FuturePromise<>();
    session.newStream(new HeadersFrame(request, null, false), promise, new Stream.Listener.Adapter());
    Stream stream = promise.get(5, TimeUnit.SECONDS);
    // Send first chunk that exceeds the window.
    Callback.Completable completable = new Callback.Completable();
    stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(size * 2), false), completable);
    settingsLatch.await(5, TimeUnit.SECONDS);
    completable.thenRun(() -> {
        // Send the second chunk of data, must not arrive since we're flow control stalled on the client.
        stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(size * 2), true), Callback.NOOP);
    });
    Assert.assertFalse(dataLatch.await(1, TimeUnit.SECONDS));
    // Consume the data arrived to server, this will resume flow control on the client.
    callbackRef.get().succeeded();
    Assert.assertTrue(dataLatch.await(5, TimeUnit.SECONDS));
}
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) HTTP2Stream(org.eclipse.jetty.http2.HTTP2Stream) Stream(org.eclipse.jetty.http2.api.Stream) FuturePromise(org.eclipse.jetty.util.FuturePromise) AtomicReference(java.util.concurrent.atomic.AtomicReference) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) Callback(org.eclipse.jetty.util.Callback) FutureCallback(org.eclipse.jetty.util.FutureCallback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) HashMap(java.util.HashMap) 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 93 with Session

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

the class AbstractTest method newClient.

protected Session newClient(Session.Listener listener) throws Exception {
    String host = "localhost";
    int port = connector.getLocalPort();
    InetSocketAddress address = new InetSocketAddress(host, port);
    FuturePromise<Session> promise = new FuturePromise<>();
    client.connect(address, listener, promise);
    return promise.get(5, TimeUnit.SECONDS);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) FuturePromise(org.eclipse.jetty.util.FuturePromise) Session(org.eclipse.jetty.http2.api.Session)

Example 94 with Session

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

the class AsyncIOTest method testLastContentAvailableBeforeService.

@Test
public void testLastContentAvailableBeforeService() throws Exception {
    start(new HttpServlet() {

        @Override
        protected void service(final HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // Wait for the data to fully arrive.
            sleep(1000);
            final AsyncContext asyncContext = request.startAsync();
            asyncContext.setTimeout(0);
            request.getInputStream().setReadListener(new EmptyReadListener() {

                @Override
                public void onDataAvailable() throws IOException {
                    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);
    stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(16), true), Callback.NOOP);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
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) 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)

Example 95 with Session

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

the class AsyncIOTest method testLastContentAvailableAfterServiceReturns.

@Test
public void testLastContentAvailableAfterServiceReturns() throws Exception {
    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 {
                    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(16), true), Callback.NOOP);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
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) 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

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