Author: remm
Date: Mon Feb 18 14:09:37 2019
New Revision: 1853802
URL: http://svn.apache.org/viewvc?rev=1853802&view=rev
Log:
Add test for 63182, which is probably valid, skip for NIO2
Modified:
tomcat/trunk/test/org/apache/catalina/nonblocking/TestNonBlockingAPI.java
Modified:
tomcat/trunk/test/org/apache/catalina/nonblocking/TestNonBlockingAPI.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/nonblocking/TestNonBlockingAPI.java?rev=1853802&r1=1853801&r2=1853802&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/nonblocking/TestNonBlockingAPI.java
(original)
+++ tomcat/trunk/test/org/apache/catalina/nonblocking/TestNonBlockingAPI.java
Mon Feb 18 14:09:37 2019
@@ -48,10 +48,12 @@ import javax.servlet.http.HttpServletReq
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
+import org.junit.Assume;
import org.junit.Ignore;
import org.junit.Test;
import org.apache.catalina.Context;
+import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.BytesStreamer;
import org.apache.catalina.startup.TesterServlet;
import org.apache.catalina.startup.Tomcat;
@@ -87,23 +89,34 @@ public class TestNonBlockingAPI extends
@Test
public void testNonBlockingRead() throws Exception {
- doTestNonBlockingRead(false);
+ doTestNonBlockingRead(false, false);
+ }
+
+
+ @Test
+ public void testNonBlockingReadAsync() throws Exception {
+ Tomcat tomcat = getTomcatInstance();
+ Connector connector = tomcat.getConnector();
+ // Skip for NIO2
+ Assume.assumeFalse("This test may fail for NIO2",
+ connector.getProtocolHandlerClassName().contains("Nio2"));
+ doTestNonBlockingRead(false, true);
}
@Test(expected=IOException.class)
public void testNonBlockingReadIgnoreIsReady() throws Exception {
- doTestNonBlockingRead(true);
+ doTestNonBlockingRead(true, false);
}
- private void doTestNonBlockingRead(boolean ignoreIsReady) throws Exception
{
+ private void doTestNonBlockingRead(boolean ignoreIsReady, boolean async)
throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
- NBReadServlet servlet = new NBReadServlet(ignoreIsReady);
+ NBReadServlet servlet = new NBReadServlet(ignoreIsReady, async);
String servletName = NBReadServlet.class.getName();
Tomcat.addServlet(ctx, servletName, servlet);
ctx.addServletMappingDecoded("/", servletName);
@@ -111,8 +124,8 @@ public class TestNonBlockingAPI extends
tomcat.start();
Map<String, List<String>> resHeaders = new HashMap<>();
- int rc = postUrl(true, new DataWriter(500), "http://localhost:" +
- getPort() + "/", new ByteChunk(), resHeaders, null);
+ int rc = postUrl(true, new DataWriter(async ? 0 : 500, async ? 1000000
: 5),
+ "http://localhost:" + getPort() + "/", new ByteChunk(),
resHeaders, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
}
@@ -408,24 +421,25 @@ public class TestNonBlockingAPI extends
public static class DataWriter implements BytesStreamer {
- private static final int MAX = 5;
+ int max = 5;
int count = 0;
long delay = 0;
byte[] b = "WANTMORE".getBytes(StandardCharsets.ISO_8859_1);
byte[] f = "FINISHED".getBytes(StandardCharsets.ISO_8859_1);
- public DataWriter(long delay) {
+ public DataWriter(long delay, int max) {
this.delay = delay;
+ this.max = max;
}
@Override
public int getLength() {
- return b.length * MAX;
+ return b.length * max;
}
@Override
public int available() {
- if (count < MAX) {
+ if (count < max) {
return b.length;
} else {
return 0;
@@ -434,7 +448,7 @@ public class TestNonBlockingAPI extends
@Override
public byte[] next() {
- if (count < MAX) {
+ if (count < max) {
if (count > 0)
try {
if (delay > 0)
@@ -442,7 +456,7 @@ public class TestNonBlockingAPI extends
} catch (Exception x) {
}
count++;
- if (count < MAX)
+ if (count < max)
return b;
else
return f;
@@ -456,10 +470,12 @@ public class TestNonBlockingAPI extends
@WebServlet(asyncSupported = true)
public class NBReadServlet extends TesterServlet {
private static final long serialVersionUID = 1L;
+ private final boolean async;
private final boolean ignoreIsReady;
public volatile TestReadListener listener;
- public NBReadServlet(boolean ignoreIsReady) {
+ public NBReadServlet(boolean ignoreIsReady, boolean async) {
+ this.async = async;
this.ignoreIsReady = ignoreIsReady;
}
@@ -496,7 +512,11 @@ public class TestNonBlockingAPI extends
});
// step 2 - notify on read
ServletInputStream in = req.getInputStream();
- listener = new TestReadListener(actx, false, ignoreIsReady);
+ if (async) {
+ listener = new TestAsyncReadListener(actx, false,
ignoreIsReady);
+ } else {
+ listener = new TestReadListener(actx, false, ignoreIsReady);
+ }
in.setReadListener(listener);
}
}
@@ -566,10 +586,10 @@ public class TestNonBlockingAPI extends
}
private class TestReadListener implements ReadListener {
- private final AsyncContext ctx;
- private final boolean usingNonBlockingWrite;
- private final boolean ignoreIsReady;
- private final StringBuilder body = new StringBuilder();
+ protected final AsyncContext ctx;
+ protected final boolean usingNonBlockingWrite;
+ protected final boolean ignoreIsReady;
+ protected final StringBuilder body = new StringBuilder();
public volatile boolean onErrorInvoked = false;
@@ -600,7 +620,7 @@ public class TestNonBlockingAPI extends
@Override
public void onAllDataRead() {
- log.info("onAllDataRead");
+ log.info("onAllDataRead totalData=" + body.toString().length());
// If non-blocking writes are being used, don't write here as it
// will inject unexpected data into the write output.
if (!usingNonBlockingWrite) {
@@ -627,6 +647,43 @@ public class TestNonBlockingAPI extends
}
}
+ private class TestAsyncReadListener extends TestReadListener {
+
+ public TestAsyncReadListener(AsyncContext ctx,
+ boolean usingNonBlockingWrite, boolean ignoreIsReady) {
+ super(ctx, usingNonBlockingWrite, ignoreIsReady);
+ }
+
+ @Override
+ public void onDataAvailable() throws IOException {
+ new Thread() {
+ @Override
+ public void run() {
+ try {
+ ServletInputStream in =
ctx.getRequest().getInputStream();
+ String s = "";
+ byte[] b = new byte[1024];
+ int read = in.read(b);
+ if (read == -1) {
+ return;
+ }
+ s += new String(b, 0, read);
+ synchronized (body) {
+ body.append(s);
+ }
+ if (ignoreIsReady || in.isReady()) {
+ onDataAvailable();
+ }
+ } catch (IOException e) {
+ onError(e);
+ }
+ }
+ }.start();
+ }
+
+
+ }
+
private class TestWriteListener implements WriteListener {
AsyncContext ctx;
int written = 0;
@@ -933,7 +990,7 @@ public class TestNonBlockingAPI extends
tomcat.start();
Map<String, List<String>> resHeaders = new HashMap<>();
- int rc = postUrl(true, new DataWriter(500), "http://localhost:" +
+ int rc = postUrl(true, new DataWriter(500, 5), "http://localhost:" +
getPort() + "/", new ByteChunk(), resHeaders, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]