jdk/test/sun/net/www/http/KeepAliveStream/InfiniteLoop.java
author sla
Mon, 07 Apr 2014 08:09:55 +0200
changeset 23723 46b29c0e9656
parent 14903 7181b5ef130e
child 41965 015dea372cb3
permissions -rw-r--r--
8039256: Add sun/jvmstat/monitor/MonitoredVm/CR6672135.java to ProblemList.txt Reviewed-by: mgronlun, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14903
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
     1
/*
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
     4
 *
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
     8
 *
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    13
 * accompanied this code).
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    14
 *
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    18
 *
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    21
 * questions.
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    22
 */
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    23
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    24
/*
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    25
 * @test
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    26
 * @bug 8004863
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    27
 * @summary Checks for proper close code in KeepAliveStream
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    28
 */
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    29
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    30
import com.sun.net.httpserver.HttpExchange;
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    31
import com.sun.net.httpserver.HttpHandler;
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    32
import com.sun.net.httpserver.HttpServer;
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    33
import java.io.*;
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    34
import java.net.*;
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    35
import java.util.concurrent.Phaser;
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    36
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    37
// Racey test, will not always fail, but if it does then we have a problem.
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    38
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    39
public class InfiniteLoop {
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    40
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    42
        HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    43
        server.createContext("/test/InfiniteLoop", new RespHandler());
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    44
        server.start();
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    45
        try {
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    46
            InetSocketAddress address = server.getAddress();
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    47
            URL url = new URL("http://localhost:" + address.getPort()
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    48
                              + "/test/InfiniteLoop");
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    49
            final Phaser phaser = new Phaser(2);
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    50
            for (int i=0; i<10; i++) {
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    51
                HttpURLConnection uc = (HttpURLConnection)url.openConnection();
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    52
                final InputStream is = uc.getInputStream();
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    53
                final Thread thread = new Thread() {
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    54
                    public void run() {
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    55
                        try {
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    56
                            phaser.arriveAndAwaitAdvance();
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    57
                            while (is.read() != -1)
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    58
                                Thread.sleep(50);
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    59
                        } catch (Exception x) { x.printStackTrace(); }
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    60
                    }};
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    61
                thread.start();
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    62
                phaser.arriveAndAwaitAdvance();
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    63
                is.close();
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    64
                System.out.println("returned from close");
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    65
                thread.join();
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    66
            }
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    67
        } finally {
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    68
            server.stop(0);
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    69
        }
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    70
    }
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    71
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    72
    static class RespHandler implements HttpHandler {
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    73
        static final int RESP_LENGTH = 32 * 1024;
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    74
        @Override
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    75
        public void handle(HttpExchange t) throws IOException {
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    76
            InputStream is  = t.getRequestBody();
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    77
            byte[] ba = new byte[8192];
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    78
            while(is.read(ba) != -1);
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    79
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    80
            t.sendResponseHeaders(200, RESP_LENGTH);
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    81
            try (OutputStream os = t.getResponseBody()) {
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    82
                os.write(new byte[RESP_LENGTH]);
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    83
            }
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    84
            t.close();
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    85
        }
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    86
    }
7181b5ef130e 8004863: Infinite Loop in KeepAliveStream
martin
parents:
diff changeset
    87
}