Author: markt
Date: Thu Jun 11 14:51:28 2015
New Revision: 1684910

URL: http://svn.apache.org/r1684910
Log:
Add unit tests for section 5.2
Fix a bug where the end of stream flag was incorrectly set when flow control 
was applied to the response body after it had been fully written by the app but 
before it had been fully written to the client.

Added:
    tomcat/trunk/test/org/apache/coyote/http2/TestHttp2Section_5_2.java   (with 
props)
Modified:
    tomcat/trunk/java/org/apache/coyote/http2/Http2UpgradeHandler.java
    tomcat/trunk/java/org/apache/coyote/http2/Stream.java
    tomcat/trunk/java/org/apache/coyote/http2/StreamProcessor.java
    tomcat/trunk/test/org/apache/coyote/http2/Http2TestBase.java

Modified: tomcat/trunk/java/org/apache/coyote/http2/Http2UpgradeHandler.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http2/Http2UpgradeHandler.java?rev=1684910&r1=1684909&r2=1684910&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http2/Http2UpgradeHandler.java 
(original)
+++ tomcat/trunk/java/org/apache/coyote/http2/Http2UpgradeHandler.java Thu Jun 
11 14:51:28 2015
@@ -453,7 +453,7 @@ public class Http2UpgradeHandler extends
     }
 
 
-    void writeBody(Stream stream, ByteBuffer data, int len) throws IOException 
{
+    void writeBody(Stream stream, ByteBuffer data, int len, boolean finished) 
throws IOException {
         if (log.isDebugEnabled()) {
             log.debug(sm.getString("upgradeHandler.writeBody", connectionId, 
stream.getIdentifier(),
                     Integer.toString(data.remaining())));
@@ -462,7 +462,7 @@ public class Http2UpgradeHandler extends
             byte[] header = new byte[9];
             ByteUtil.setThreeBytes(header, 0, len);
             header[3] = FrameType.DATA.getIdByte();
-            if (stream.getOutputBuffer().isFinished()) {
+            if (finished) {
                 header[4] = FLAG_END_OF_STREAM;
                 stream.sentEndOfStream();
                 if (!stream.isActive()) {

Modified: tomcat/trunk/java/org/apache/coyote/http2/Stream.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http2/Stream.java?rev=1684910&r1=1684909&r2=1684910&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http2/Stream.java (original)
+++ tomcat/trunk/java/org/apache/coyote/http2/Stream.java Thu Jun 11 14:51:28 
2015
@@ -275,11 +275,11 @@ public class Stream extends AbstractStre
 
         private final ByteBuffer buffer = ByteBuffer.allocate(8 * 1024);
         private volatile long written = 0;
-        private volatile boolean finished = false;
+        private volatile boolean closed = false;
 
         @Override
         public int doWrite(ByteChunk chunk) throws IOException {
-            if (finished) {
+            if (closed) {
                 // TODO i18n
                 throw new IllegalStateException();
             }
@@ -293,7 +293,7 @@ public class Stream extends AbstractStre
                 if (len > 0 && !buffer.hasRemaining()) {
                     // Only flush if we have more data to write and the buffer
                     // is full
-                    flush();
+                    flush(true);
                 }
             }
             written += offset;
@@ -301,6 +301,10 @@ public class Stream extends AbstractStre
         }
 
         public void flush() throws IOException {
+            flush(false);
+        }
+
+        private void flush(boolean writeInProgress) throws IOException {
             if (!coyoteResponse.isCommitted()) {
                 coyoteResponse.sendHeaders();
             }
@@ -348,7 +352,8 @@ public class Stream extends AbstractStre
                 decrementWindowSize(thisWrite);
 
                 // Do the write
-                handler.writeBody(Stream.this, buffer, thisWrite);
+                handler.writeBody(Stream.this, buffer, thisWrite,
+                        !writeInProgress && closed && left == thisWrite);
                 left -= thisWrite;
                 buffer.position(buffer.position() + thisWrite);
             }
@@ -360,12 +365,12 @@ public class Stream extends AbstractStre
             return written;
         }
 
-        public void finished() {
-            finished = true;
+        public void close() {
+            closed = true;
         }
 
-        public boolean isFinished() {
-            return finished;
+        public boolean isClosed() {
+            return closed;
         }
 
         /**
@@ -373,7 +378,7 @@ public class Stream extends AbstractStre
          *         response has no body.
          */
         public boolean hasNoBody() {
-            return ((written == 0) && finished);
+            return ((written == 0) && closed);
         }
     }
 

Modified: tomcat/trunk/java/org/apache/coyote/http2/StreamProcessor.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http2/StreamProcessor.java?rev=1684910&r1=1684909&r2=1684910&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http2/StreamProcessor.java (original)
+++ tomcat/trunk/java/org/apache/coyote/http2/StreamProcessor.java Thu Jun 11 
14:51:28 2015
@@ -76,7 +76,7 @@ public class StreamProcessor extends Abs
         }
         case CLOSE: {
             // Tell the output buffer there will be no more data
-            stream.getOutputBuffer().finished();
+            stream.getOutputBuffer().close();
             // Then flush it
             action(ActionCode.CLIENT_FLUSH, null);
             break;

Modified: tomcat/trunk/test/org/apache/coyote/http2/Http2TestBase.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/coyote/http2/Http2TestBase.java?rev=1684910&r1=1684909&r2=1684910&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/coyote/http2/Http2TestBase.java (original)
+++ tomcat/trunk/test/org/apache/coyote/http2/Http2TestBase.java Thu Jun 11 
14:51:28 2015
@@ -456,6 +456,24 @@ public abstract class Http2TestBase exte
     }
 
 
+    void sendSetting(int settingId, long value) throws IOException {
+        byte[] settingFrame = new byte[15];
+        // length
+        ByteUtil.setThreeBytes(settingFrame, 0, 6);
+        // type
+        settingFrame[3] = FrameType.SETTINGS.getIdByte();
+        // No flags
+        // Stream 0
+
+        // Payload
+        ByteUtil.setTwoBytes(settingFrame, 9, settingId);
+        ByteUtil.setFourBytes(settingFrame, 11, value);
+
+        os.write(settingFrame);
+        os.flush();
+    }
+
+
     private static class TestInput implements Http2Parser.Input {
 
         private final InputStream is;

Added: tomcat/trunk/test/org/apache/coyote/http2/TestHttp2Section_5_2.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/coyote/http2/TestHttp2Section_5_2.java?rev=1684910&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/coyote/http2/TestHttp2Section_5_2.java (added)
+++ tomcat/trunk/test/org/apache/coyote/http2/TestHttp2Section_5_2.java Thu Jun 
11 14:51:28 2015
@@ -0,0 +1,116 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.coyote.http2;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit tests for Section 5.2 of
+ * <a href="https://tools.ietf.org/html/rfc7540";>RFC 7540</a>.
+ * <br>
+ * The order of tests in this class is aligned with the order of the
+ * requirements in the RFC.
+ */
+public class TestHttp2Section_5_2 extends Http2TestBase {
+
+    /*
+     * Get the connection to a point where 1k of 8k response body has been
+     * read and the flow control for the stream has no capacity left.
+     */
+    @Override
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+
+        http2Connect();
+
+        // Set the default window size to 1024 bytes
+        sendSetting(4, 1024);
+        // Wait for the ack
+        parser.readFrame(true);
+        output.clearTrace();
+
+        // Headers + 8k response
+        sendSimpleRequest(3);
+
+        // Headers
+        parser.readFrame(true);
+        // First 1k of body
+        parser.readFrame(true);
+        output.clearTrace();
+    }
+
+
+    @Test
+    public void testFlowControlLimits01() throws Exception {
+        readBytes(20);
+        clearRemainder();
+    }
+
+
+    @Test
+    public void testFlowControlLimits02() throws Exception {
+        readBytes(1);
+        readBytes(1);
+        readBytes(1024);
+        readBytes(1);
+        clearRemainder();
+    }
+
+
+    @Test
+    public void testFlowControlLimits03() throws Exception {
+        readBytes(8192,7168);
+    }
+
+
+    @Test
+    public void testFlowControlLimits04() throws Exception {
+        readBytes(7168, 7168, true);
+    }
+
+
+    private void readBytes(int len) throws Exception {
+        readBytes(len, len);
+    }
+
+
+    private void readBytes(int len, int expected) throws Exception {
+        readBytes(len, expected, len > expected);
+    }
+
+
+    private void readBytes(int len, int expected, boolean eos) throws 
Exception {
+        sendWindowUpdate(3, len);
+        parser.readFrame(true);
+        String expectedTrace = "3-Body-" + expected + "\n";
+        if (eos) {
+            expectedTrace += "3-EndOfStream\n";
+        }
+        Assert.assertEquals(expectedTrace, output.getTrace());
+        output.clearTrace();
+    }
+
+
+    private void clearRemainder() throws Exception {
+        // Remainder
+        sendWindowUpdate(3, 8192);
+        parser.readFrame(true);
+    }
+}

Propchange: tomcat/trunk/test/org/apache/coyote/http2/TestHttp2Section_5_2.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tomcat/trunk/test/org/apache/coyote/http2/TestHttp2Section_5_2.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to