Search in sources :

Example 6 with WritePendingException

use of java.nio.channels.WritePendingException in project jetty.project by eclipse.

the class HTTP2Test method testInvalidAPIUsageOnClient.

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

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

                @Override
                public void onData(Stream stream, DataFrame frame, Callback callback) {
                    callback.succeeded();
                    if (frame.isEndStream()) {
                        completable.thenRun(() -> {
                            DataFrame endFrame = new DataFrame(stream.getId(), BufferUtil.EMPTY_BUFFER, true);
                            stream.data(endFrame, Callback.NOOP);
                        });
                    }
                }
            };
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    HeadersFrame frame = new HeadersFrame(metaData, null, false);
    Promise.Completable<Stream> completable = new Promise.Completable<>();
    CountDownLatch completeLatch = new CountDownLatch(2);
    session.newStream(frame, completable, new Stream.Listener.Adapter() {

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            callback.succeeded();
            if (frame.isEndStream())
                completeLatch.countDown();
        }
    });
    Stream stream = completable.get(5, TimeUnit.SECONDS);
    long sleep = 1000;
    DataFrame data1 = new DataFrame(stream.getId(), ByteBuffer.allocate(1024), false) {

        @Override
        public ByteBuffer getData() {
            sleep(2 * sleep);
            return super.getData();
        }
    };
    DataFrame data2 = new DataFrame(stream.getId(), BufferUtil.EMPTY_BUFFER, true);
    new Thread(() -> {
        // The first data() call is legal, but slow.
        stream.data(data1, new Callback() {

            @Override
            public void succeeded() {
                stream.data(data2, NOOP);
            }
        });
    }).start();
    // Wait for the first data() call to happen.
    sleep(sleep);
    // This data call is illegal because it does not
    // wait for the previous callback to complete.
    stream.data(data2, new Callback() {

        @Override
        public void failed(Throwable x) {
            if (x instanceof WritePendingException) {
                // Expected.
                completeLatch.countDown();
            }
        }
    });
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) WritePendingException(java.nio.channels.WritePendingException) 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) 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) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Aggregations

WritePendingException (java.nio.channels.WritePendingException)6 HttpFields (org.eclipse.jetty.http.HttpFields)3 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 MetaData (org.eclipse.jetty.http.MetaData)2 Session (org.eclipse.jetty.http2.api.Session)2 Stream (org.eclipse.jetty.http2.api.Stream)2 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)2 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)2 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)2 Callback (org.eclipse.jetty.util.Callback)2 FuturePromise (org.eclipse.jetty.util.FuturePromise)2 Promise (org.eclipse.jetty.util.Promise)2 Test (org.junit.Test)2 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 SSLEngineResult (javax.net.ssl.SSLEngineResult)1 CloseCode (javax.websocket.CloseReason.CloseCode)1