From be127ee0cfaab8ed806773b28ad24a3297824ffa Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Wed, 4 Feb 2015 10:58:57 +1100 Subject: [PATCH] added test for async IO isNotReadyAtEOF --- .../jetty/servlet/AsyncIOServletTest.java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncIOServletTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncIOServletTest.java index 79140ef0979..7031e7f3f49 100644 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncIOServletTest.java +++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncIOServletTest.java @@ -27,6 +27,7 @@ import java.nio.charset.StandardCharsets; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; + import javax.servlet.AsyncContext; import javax.servlet.ReadListener; import javax.servlet.ServletException; @@ -411,4 +412,90 @@ public class AsyncIOServletTest if (!latch.await(5, TimeUnit.SECONDS)) Assert.fail(); } + + + @Test + public void testIsNotReadyAtEOF() throws Exception + { + final CountDownLatch latch = new CountDownLatch(1); + String text = "Now is the winter of our discontent. How Now Brown Cow. The quick brown fox jumped over the lazy dog.\n"; + final byte[] data = text.getBytes(StandardCharsets.ISO_8859_1); + + startServer(new HttpServlet() + { + @Override + protected void service(HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException + { + response.flushBuffer(); + + final AsyncContext async = request.startAsync(); + final ServletInputStream in = request.getInputStream(); + final ServletOutputStream out = response.getOutputStream(); + + in.setReadListener(new ReadListener() + { + transient int _i=0; + transient boolean _minusOne=false;; + transient boolean _finished=false;; + + @Override + public void onError(Throwable t) + { + t.printStackTrace(); + async.complete(); + } + + @Override + public void onDataAvailable() throws IOException + { + while(in.isReady()) + { + int b = in.read(); + if (b==-1) + _minusOne=true; + else if (data[_i++]!=b) + throw new IllegalStateException(); + } + + if (in.isFinished()) + _finished=true; + } + + @Override + public void onAllDataRead() throws IOException + { + out.write(String.format("i=%d eof=%b finished=%b",_i,_minusOne,_finished).getBytes(StandardCharsets.ISO_8859_1)); + async.complete(); + } + }); + } + }); + + String request = "GET " + path + " HTTP/1.1\r\n" + + "Host: localhost:" + connector.getLocalPort() + "\r\n" + + "Content-Type: text/plain\r\n"+ + "Content-Length: "+data.length+"\r\n" + + "\r\n"; + + try (Socket client = new Socket("localhost", connector.getLocalPort())) + { + OutputStream output = client.getOutputStream(); + output.write(request.getBytes("UTF-8")); + output.write(data); + output.flush(); + + BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); + String line=in.readLine(); + assertThat(line, containsString("200 OK")); + while (line.length()>0) + line=in.readLine(); + line=in.readLine(); + assertThat(line, not(containsString(" "))); + line=in.readLine(); + assertThat(line, containsString("i="+data.length+" eof=false finished=true")); + } + + if (!latch.await(5, TimeUnit.SECONDS)) + Assert.fail(); + } }