J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 6361557 |
| 27 | * @summary Lightweight HTTP server quickly runs out of file descriptors on Linux |
| 28 | */ |
| 29 | |
| 30 | import com.sun.net.httpserver.*; |
| 31 | |
| 32 | import java.util.*; |
| 33 | import java.util.concurrent.*; |
| 34 | import java.io.*; |
| 35 | import java.nio.*; |
| 36 | import java.nio.channels.*; |
| 37 | import java.net.*; |
| 38 | import java.security.*; |
| 39 | import java.security.cert.*; |
| 40 | import javax.net.ssl.*; |
| 41 | |
| 42 | /** |
| 43 | * The test simply opens 10,000 separate connections |
| 44 | * and invokes one http request on each. The client does |
| 45 | * not close any sockets until after they are closed |
| 46 | * by the server. This verifies the basic ability |
| 47 | * of the server to manage a reasonable number of connections |
| 48 | */ |
| 49 | public class B6361557 { |
| 50 | |
| 51 | public static boolean error = false; |
| 52 | |
| 53 | static class Handler implements HttpHandler { |
| 54 | int invocation = 1; |
| 55 | public void handle (HttpExchange t) |
| 56 | throws IOException |
| 57 | { |
| 58 | InputStream is = t.getRequestBody(); |
| 59 | Headers map = t.getRequestHeaders(); |
| 60 | Headers rmap = t.getResponseHeaders(); |
| 61 | while (is.read () != -1) ; |
| 62 | is.close(); |
| 63 | t.sendResponseHeaders (200, -1); |
| 64 | t.close(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public static void main (String[] args) throws Exception { |
| 69 | Handler handler = new Handler(); |
| 70 | InetSocketAddress addr = new InetSocketAddress (0); |
| 71 | HttpServer server = HttpServer.create (addr, 0); |
| 72 | HttpContext ctx = server.createContext ("/test", handler); |
| 73 | |
| 74 | ExecutorService executor = Executors.newCachedThreadPool(); |
| 75 | server.setExecutor (executor); |
| 76 | server.start (); |
| 77 | |
| 78 | final int NUM = 10000; |
| 79 | ByteBuffer buf = ByteBuffer.allocate (4096); |
| 80 | InetSocketAddress destaddr = new InetSocketAddress ( |
| 81 | "127.0.0.1", server.getAddress().getPort() |
| 82 | ); |
| 83 | System.out.println ("destaddr " + destaddr); |
| 84 | |
| 85 | Selector selector = Selector.open (); |
| 86 | int i = 0; |
| 87 | while (true) { |
| 88 | i ++; |
| 89 | int selres = selector.select (1); |
| 90 | Set<SelectionKey> selkeys = selector.selectedKeys(); |
| 91 | for (SelectionKey key : selkeys) { |
| 92 | if (key.isReadable()) { |
| 93 | SocketChannel chan = (SocketChannel)key.channel(); |
| 94 | buf.clear(); |
| 95 | try { |
| 96 | int x = chan.read (buf); |
| 97 | if (x == -1) { |
| 98 | chan.close(); |
| 99 | } |
| 100 | } catch (IOException e) {} |
| 101 | } |
| 102 | } |
| 103 | if (i< NUM) { |
| 104 | SocketChannel schan = SocketChannel.open (destaddr); |
| 105 | String cmd = "GET /test/foo.html HTTP/1.1\r\nContent-length: 0\r\n\r\n"; |
| 106 | buf.rewind (); |
| 107 | buf.put (cmd.getBytes()); |
| 108 | buf.flip(); |
| 109 | int c = 0; |
| 110 | while (buf.remaining() > 0) { |
| 111 | c += schan.write (buf); |
| 112 | } |
| 113 | schan.configureBlocking (false); |
| 114 | schan.register (selector, SelectionKey.OP_READ, null); |
| 115 | } else { |
| 116 | System.out.println ("Finished clients"); |
| 117 | server.stop (1); |
| 118 | executor.shutdown (); |
| 119 | return; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |