Search in sources :

Example 21 with PrefaceFrame

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

the class CloseTest method testServerSendsGoAwayClientDoesNotCloseServerIdleTimeout.

@Test
public void testServerSendsGoAwayClientDoesNotCloseServerIdleTimeout() throws Exception {
    final long idleTimeout = 1000;
    final AtomicReference<Session> sessionRef = new AtomicReference<>();
    startServer(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            stream.setIdleTimeout(10 * idleTimeout);
            sessionRef.set(stream.getSession());
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
            stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
            stream.getSession().close(ErrorCode.NO_ERROR.code, "OK", Callback.NOOP);
            return null;
        }
    });
    connector.setIdleTimeout(idleTimeout);
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    generator.control(lease, new HeadersFrame(1, metaData, null, true));
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            output.write(BufferUtil.toArray(buffer));
        }
        final CountDownLatch responseLatch = new CountDownLatch(1);
        final CountDownLatch closeLatch = new CountDownLatch(1);
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onHeaders(HeadersFrame frame) {
                responseLatch.countDown();
            }

            @Override
            public void onGoAway(GoAwayFrame frame) {
                closeLatch.countDown();
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        Assert.assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
        Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
        // Don't close the connection.
        // Wait for the server to idle timeout.
        Thread.sleep(2 * idleTimeout);
        // Client received the TCP FIN from server.
        Assert.assertEquals(-1, client.getInputStream().read());
        // Server is closed.
        Session session = sessionRef.get();
        Assert.assertTrue(session.isClosed());
        Assert.assertTrue(((HTTP2Session) session).isDisconnected());
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) 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) OutputStream(java.io.OutputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Socket(java.net.Socket) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Aggregations

PrefaceFrame (org.eclipse.jetty.http2.frames.PrefaceFrame)21 SettingsFrame (org.eclipse.jetty.http2.frames.SettingsFrame)20 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)20 Test (org.junit.Test)20 HashMap (java.util.HashMap)19 ByteBuffer (java.nio.ByteBuffer)18 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)17 HttpFields (org.eclipse.jetty.http.HttpFields)16 MetaData (org.eclipse.jetty.http.MetaData)16 Parser (org.eclipse.jetty.http2.parser.Parser)13 OutputStream (java.io.OutputStream)12 Socket (java.net.Socket)12 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 HttpServlet (javax.servlet.http.HttpServlet)6 IOException (java.io.IOException)5 Session (org.eclipse.jetty.http2.api.Session)5 Generator (org.eclipse.jetty.http2.generator.Generator)5 MappedByteBufferPool (org.eclipse.jetty.io.MappedByteBufferPool)5