Skip to content

Commit

Permalink
Add a test for maxConcurrentStreams
Browse files Browse the repository at this point in the history
Also expands capabilities of Http2TestBase to include
- large requests
- setting maxConcurrentStreams

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1684771 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Jun 10, 2015
1 parent 76c182e commit 80b8958
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 5 deletions.
61 changes: 57 additions & 4 deletions test/org/apache/coyote/http2/Http2TestBase.java
Expand Up @@ -114,10 +114,30 @@ protected void sendSimpleRequest(int streamId) throws IOException {
}


protected void sendLargeRequest(int streamId) throws IOException {
byte[] frameHeader = new byte[9];
ByteBuffer headersPayload = ByteBuffer.allocate(128);

buildLargeRequest(frameHeader, headersPayload, streamId);
writeFrame(frameHeader, headersPayload);
}


protected void buildSimpleRequest(byte[] frameHeader, ByteBuffer headersPayload, int streamId) {
buildRequest(frameHeader, headersPayload, streamId, "/simple");
}


protected void buildLargeRequest(byte[] frameHeader, ByteBuffer headersPayload, int streamId) {
buildRequest(frameHeader, headersPayload, streamId, "/large");
}


protected void buildRequest(byte[] frameHeader, ByteBuffer headersPayload, int streamId,
String url) {
MimeHeaders headers = new MimeHeaders();
headers.addValue(":method").setString("GET");
headers.addValue(":path").setString("/any");
headers.addValue(":path").setString(url);
headers.addValue(":authority").setString("localhost:" + getPort());
hpackEncoder.encode(headers, headersPayload);

Expand All @@ -137,7 +157,7 @@ protected void buildSimpleRequestPart1(byte[] frameHeader, ByteBuffer headersPay
int streamId) {
MimeHeaders headers = new MimeHeaders();
headers.addValue(":method").setString("GET");
headers.addValue(":path").setString("/any");
headers.addValue(":path").setString("/simple");
hpackEncoder.encode(headers, headersPayload);

headersPayload.flip();
Expand Down Expand Up @@ -204,12 +224,17 @@ protected String getSimpleResponseTrace(int streamId) {


protected void enableHttp2() {
enableHttp2(200);
}

protected void enableHttp2(long maxConcurrentStreams) {
Connector connector = getTomcatInstance().getConnector();
Http2Protocol http2Protocol = new Http2Protocol();
// Short timeouts for now. May need to increase these for CI systems.
http2Protocol.setReadTimeout(2000);
http2Protocol.setKeepAliveTimeout(5000);
http2Protocol.setWriteTimeout(2000);
http2Protocol.setMaxConcurrentStreams(maxConcurrentStreams);
connector.addUpgradeProtocol(http2Protocol);
}

Expand All @@ -219,7 +244,9 @@ protected void configureAndStartWebApplication() throws LifecycleException {

Context ctxt = tomcat.addContext("", null);
Tomcat.addServlet(ctxt, "simple", new SimpleServlet());
ctxt.addServletMapping("/*", "simple");
ctxt.addServletMapping("/simple", "simple");
Tomcat.addServlet(ctxt, "large", new LargeServlet());
ctxt.addServletMapping("/large", "large");

tomcat.start();
}
Expand All @@ -245,7 +272,7 @@ protected void doHttpUpgrade() throws IOException {

protected void doHttpUpgrade(String connection, String upgrade, String settings,
boolean validate) throws IOException {
byte[] upgradeRequest = ("GET / HTTP/1.1\r\n" +
byte[] upgradeRequest = ("GET /simple HTTP/1.1\r\n" +
"Host: localhost:" + getPort() + "\r\n" +
"Connection: "+ connection + "\r\n" +
"Upgrade: " + upgrade + "\r\n" +
Expand Down Expand Up @@ -615,4 +642,30 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
}
}
}


private static class LargeServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// Generate content with a simple known format that will exceed the
// default flow control window for a stream.
resp.setContentType("application/octet-stream");

int count = 128 * 1024;
// Two bytes per entry
resp.setContentLengthLong(count * 2);

OutputStream os = resp.getOutputStream();
byte[] data = new byte[2];
for (int i = 0; i < count; i++) {
data[0] = (byte) (i & 0xFF);
data[1] = (byte) ((i >> 8) & 0xFF);
os.write(data);
}
}
}
}
41 changes: 40 additions & 1 deletion test/org/apache/coyote/http2/TestHttp2Section_5_1.java
Expand Up @@ -204,5 +204,44 @@ public void testImplicitClose() throws Exception {
output.getTrace().startsWith("0-Goaway-[2147483647]-[" +
Http2Error.PROTOCOL_ERROR.getCode() + "]-["));
}
// TODO 5.1.2 tests


@Test
public void testExceedMaxActiveStreams() throws Exception {
hpackEncoder = new HpackEncoder(ConnectionSettings.DEFAULT_HEADER_TABLE_SIZE);

// http2Connect() - modified
enableHttp2(1);
configureAndStartWebApplication();
openClientConnection();
doHttpUpgrade();
sendClientPreface();
validateHttp2InitialResponse();

sendLargeRequest(3);

sendSimpleRequest(5);

// 1 * headers
// 64k of body (8 * 8k)
// 1 * error (could be in any order)
for (int i = 0; i < 9; i++) {
parser.readFrame(true);
}
parser.readFrame(true);

// Release the remaining body
sendWindowUpdate(0, (1 << 31) - 1);
sendWindowUpdate(3, (1 << 31) - 1);

// 192k of body (24 * 8k)
// 1 * error (could be in any order)
for (int i = 0; i < 24; i++) {
parser.readFrame(true);
}

Assert.assertTrue(output.getTrace(),
output.getTrace().contains("5-RST-[" +
Http2Error.REFUSED_STREAM.getCode() + "]"));
}
}

0 comments on commit 80b8958

Please sign in to comment.