6510892: com/sun/net/httpserver/bugs/B6361557.java fails
authormichaelm
Fri, 30 Jul 2010 18:16:30 +0100
changeset 6132 bb7d59eb0f18
parent 6131 faa55a02c27a
child 6133 72f74948ab78
child 6292 9da761dff0c7
6510892: com/sun/net/httpserver/bugs/B6361557.java fails Reviewed-by: chegar
jdk/test/com/sun/net/httpserver/bugs/B6361557.java
--- a/jdk/test/com/sun/net/httpserver/bugs/B6361557.java	Thu Jul 29 22:02:45 2010 -0700
+++ b/jdk/test/com/sun/net/httpserver/bugs/B6361557.java	Fri Jul 30 18:16:30 2010 +0100
@@ -24,6 +24,7 @@
 /**
  * @test
  * @bug 6361557
+ * @run main/othervm B6361557
  * @summary  Lightweight HTTP server quickly runs out of file descriptors on Linux
  */
 
@@ -63,6 +64,9 @@
         }
     }
 
+    final static String request = "GET /test/foo.html HTTP/1.1\r\nContent-length: 0\r\n\r\n";
+    final static ByteBuffer requestBuf = ByteBuffer.allocate(64).put(request.getBytes());
+
     public static void main (String[] args) throws Exception {
         Handler handler = new Handler();
         InetSocketAddress addr = new InetSocketAddress (0);
@@ -73,48 +77,72 @@
         server.setExecutor (executor);
         server.start ();
 
-        ByteBuffer buf = ByteBuffer.allocate (4096);
         InetSocketAddress destaddr = new InetSocketAddress (
                 "127.0.0.1", server.getAddress().getPort()
         );
         System.out.println ("destaddr " + destaddr);
 
         Selector selector = Selector.open ();
-        int i = 0;
+        int requests = 0;
+        int responses = 0;
         while (true) {
-            i ++;
             int selres = selector.select (1);
             Set<SelectionKey> selkeys = selector.selectedKeys();
             for (SelectionKey key : selkeys) {
                 if (key.isReadable()) {
                     SocketChannel chan = (SocketChannel)key.channel();
-                    buf.clear();
+                    ByteBuffer buf = (ByteBuffer)key.attachment();
                     try {
-                        int x = chan.read (buf);
-                        if (x == -1) {
+                        int x = chan.read(buf);
+                        if (x == -1 || responseComplete(buf)) {
+                            key.attach(null);
                             chan.close();
+                            responses++;
                         }
                     } catch (IOException e) {}
                 }
             }
-            if (i< NUM) {
-                SocketChannel schan = SocketChannel.open (destaddr);
-                String cmd = "GET /test/foo.html HTTP/1.1\r\nContent-length: 0\r\n\r\n";
-                buf.rewind ();
-                buf.put (cmd.getBytes());
-                buf.flip();
+            if (requests < NUM) {
+                SocketChannel schan = SocketChannel.open(destaddr);
+                requestBuf.rewind();
                 int c = 0;
-                while (buf.remaining() > 0) {
-                    c += schan.write (buf);
+                while (requestBuf.remaining() > 0) {
+                    c += schan.write(requestBuf);
                 }
-                schan.configureBlocking (false);
-                schan.register (selector, SelectionKey.OP_READ, null);
-            } else {
+                schan.configureBlocking(false);
+                schan.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(100));
+                requests++;
+            }
+            if (responses == NUM) {
                 System.out.println ("Finished clients");
-                server.stop (1);
-                executor.shutdown ();
-                return;
+                break;
             }
         }
+        server.stop (1);
+        selector.close();
+        executor.shutdown ();
+
+    }
+
+    /* Look for CR LF CR LF */
+    static boolean responseComplete(ByteBuffer buf) {
+        int pos = buf.position();
+        buf.flip();
+        byte[] lookingFor = new byte[] {'\r', '\n', '\r', '\n' };
+        int lookingForCount = 0;
+        while (buf.hasRemaining()) {
+            byte b = buf.get();
+            if (b == lookingFor[lookingForCount]) {
+                lookingForCount++;
+                if (lookingForCount == 4) {
+                    return true;
+                }
+            } else {
+                lookingForCount = 0;
+            }
+        }
+        buf.position(pos);
+        buf.limit(buf.capacity());
+        return false;
     }
 }