You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2015/02/22 19:50:02 UTC

svn commit: r1661522 - /tomcat/trunk/test/org/apache/coyote/http11/upgrade/TestUpgrade.java

Author: markt
Date: Sun Feb 22 18:50:01 2015
New Revision: 1661522

URL: http://svn.apache.org/r1661522
Log:
Update unit test to take account of recent changes to upgared behaviour
in light of EG discussions.

Modified:
    tomcat/trunk/test/org/apache/coyote/http11/upgrade/TestUpgrade.java

Modified: tomcat/trunk/test/org/apache/coyote/http11/upgrade/TestUpgrade.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/coyote/http11/upgrade/TestUpgrade.java?rev=1661522&r1=1661521&r2=1661522&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/coyote/http11/upgrade/TestUpgrade.java (original)
+++ tomcat/trunk/test/org/apache/coyote/http11/upgrade/TestUpgrade.java Sun Feb 22 18:50:01 2015
@@ -54,12 +54,16 @@ public class TestUpgrade extends TomcatB
 
     @Test
     public void testSimpleUpgradeBlocking() throws Exception {
-        doUpgrade(EchoBlocking.class);
+        UpgradeConnection uc = doUpgrade(EchoBlocking.class);
+        uc.shutdownInput();
+        uc.shutdownOutput();
     }
 
     @Test
     public void testSimpleUpgradeNonBlocking() throws Exception {
-        doUpgrade(EchoNonBlocking.class);
+        UpgradeConnection uc = doUpgrade(EchoNonBlocking.class);
+        uc.shutdownInput();
+        uc.shutdownOutput();
     }
 
     @Test
@@ -122,9 +126,9 @@ public class TestUpgrade extends TomcatB
     private void doTestMessages (
             Class<? extends HttpUpgradeHandler> upgradeHandlerClass)
             throws Exception {
-        UpgradeConnection conn = doUpgrade(upgradeHandlerClass);
-        PrintWriter pw = new PrintWriter(conn.getWriter());
-        BufferedReader reader = conn.getReader();
+        UpgradeConnection uc = doUpgrade(upgradeHandlerClass);
+        PrintWriter pw = new PrintWriter(uc.getWriter());
+        BufferedReader reader = uc.getReader();
 
         pw.println(MESSAGE);
         pw.flush();
@@ -134,12 +138,16 @@ public class TestUpgrade extends TomcatB
         pw.println(MESSAGE);
         pw.flush();
 
+        uc.shutdownOutput();
+
         // Note: BufferedReader.readLine() strips new lines
         //       ServletInputStream.readLine() does not strip new lines
         String response = reader.readLine();
         Assert.assertEquals(MESSAGE, response);
         response = reader.readLine();
         Assert.assertEquals(MESSAGE, response);
+
+        uc.shutdownInput();
     }
 
 
@@ -164,30 +172,26 @@ public class TestUpgrade extends TomcatB
 
         socket.setSoTimeout(5000);
 
-        InputStream is = socket.getInputStream();
-        OutputStream os = socket.getOutputStream();
-
-        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
-        Writer writer = new OutputStreamWriter(os);
+        UpgradeConnection uc = new UpgradeConnection(socket);
 
-        writer.write("GET / HTTP/1.1" + CRLF);
-        writer.write("Host: whatever" + CRLF);
-        writer.write(CRLF);
-        writer.flush();
+        uc.getWriter().write("GET / HTTP/1.1" + CRLF);
+        uc.getWriter().write("Host: whatever" + CRLF);
+        uc.getWriter().write(CRLF);
+        uc.getWriter().flush();
 
-        String status = reader.readLine();
+        String status = uc.getReader().readLine();
 
         Assert.assertNotNull(status);
         Assert.assertEquals("101", getStatusCode(status));
 
         // Skip the remaining response headers
-        String line = reader.readLine();
+        String line = uc.getReader().readLine();
         while (line != null && line.length() > 0) {
             // Skip
-            line = reader.readLine();
+            line = uc.getReader().readLine();
         }
 
-        return new UpgradeConnection(writer, reader);
+        return uc;
     }
 
     private static class UpgradeServlet extends HttpServlet {
@@ -209,10 +213,24 @@ public class TestUpgrade extends TomcatB
     }
 
     private static class UpgradeConnection {
+        private final Socket socket;
         private final Writer writer;
         private final BufferedReader reader;
 
-        public UpgradeConnection(Writer writer, BufferedReader reader) {
+        public UpgradeConnection(Socket socket) {
+            this.socket = socket;
+            InputStream is;
+            OutputStream os;
+            try {
+                is = socket.getInputStream();
+                os = socket.getOutputStream();
+            } catch (IOException ioe) {
+                throw new IllegalArgumentException(ioe);
+            }
+
+            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
+            Writer writer = new OutputStreamWriter(os);
+
             this.writer = writer;
             this.reader = reader;
         }
@@ -224,6 +242,15 @@ public class TestUpgrade extends TomcatB
         public BufferedReader getReader() {
             return reader;
         }
+
+        public void shutdownOutput() throws IOException {
+            writer.flush();
+            socket.shutdownOutput();
+        }
+
+        public void shutdownInput() throws IOException {
+            socket.shutdownInput();
+        }
     }
 
 
@@ -253,11 +280,10 @@ public class TestUpgrade extends TomcatB
 
     public static class EchoNonBlocking implements HttpUpgradeHandler {
 
-        private ServletInputStream sis;
-        private ServletOutputStream sos;
-
         @Override
         public void init(WebConnection connection) {
+            ServletInputStream sis;
+            ServletOutputStream sos;
 
             try {
                 sis = connection.getInputStream();
@@ -266,8 +292,9 @@ public class TestUpgrade extends TomcatB
                 throw new IllegalStateException(ioe);
             }
 
-            sis.setReadListener(new EchoReadListener());
-            sos.setWriteListener(new NoOpWriteListener());
+            EchoListener echoListener = new EchoListener(sis, sos);
+            sis.setReadListener(echoListener);
+            sos.setWriteListener(echoListener);
         }
 
         @Override
@@ -275,28 +302,52 @@ public class TestUpgrade extends TomcatB
             // NO-OP
         }
 
-        private class EchoReadListener extends NoOpReadListener {
 
-            private byte[] buffer = new byte[8192];
+        private class EchoListener implements ReadListener, WriteListener {
+
+            private final ServletInputStream sis;
+            private final ServletOutputStream sos;
+            private final byte[] buffer = new byte[8192];
+
+            public EchoListener(ServletInputStream sis, ServletOutputStream sos) {
+                this.sis = sis;
+                this.sos = sos;
+            }
 
             @Override
-            public void onDataAvailable() {
-                try {
+            public void onWritePossible() throws IOException {
+                if (sis.isFinished()) {
+                    sis.close();
+                    sos.close();
+                }
                     while (sis.isReady()) {
                         int read = sis.read(buffer);
                         if (read > 0) {
-                            if (sos.isReady()) {
                                 sos.write(buffer, 0, read);
-                            } else {
-                                throw new IOException("Unable to echo data. " +
-                                        "isReady() returned false");
+                        if (!sos.isReady()) {
+                            break;
                             }
-                            sos.flush();
                         }
                     }
-                } catch (IOException ioe) {
-                    throw new RuntimeException(ioe);
                 }
+
+            @Override
+            public void onDataAvailable() throws IOException {
+                if (sos.isReady()) {
+                    onWritePossible();
+                }
+            }
+
+            @Override
+            public void onAllDataRead() throws IOException {
+                if (sos.isReady()) {
+                    onWritePossible();
+                }
+            }
+
+            @Override
+            public void onError(Throwable throwable) {
+                throwable.printStackTrace();
             }
         }
     }



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