test/jdk/com/sun/net/httpserver/SimpleHttpServerTest.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 54938 8c977741c3c8
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27720
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
     1
/*
54938
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
     2
 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
27720
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
     4
 *
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
     8
 *
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    13
 * accompanied this code).
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    14
 *
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    18
 *
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    21
 * questions.
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    22
 */
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    23
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    24
/**
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    25
 * @test
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    26
 * @bug 8015692
54938
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    27
 * @key intermittent
27720
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    28
 * @summary  Test HttpServer instantiation, start, and stop repeated in a loop
54938
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    29
 *           Testing for Bind exception on Windows. This test may fail
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    30
 *           intermittently if other tests / process manage to bind to
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    31
 *           the same port that the test is using in the short window
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    32
 *           time where the port might appear available again.
27720
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    33
 */
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    34
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    35
import java.net.InetSocketAddress;
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    36
import java.net.ServerSocket;
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    37
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    38
import com.sun.net.httpserver.HttpServer;
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    39
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    40
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    41
public class SimpleHttpServerTest {
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    42
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    44
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    45
        System.out.println(System.getProperty("java.version"));
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    46
        InetSocketAddress serverAddr = new InetSocketAddress(0);
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    47
        HttpServer server = HttpServer.create(serverAddr, 0);
54938
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    48
        int serverPort = server.getAddress().getPort();
27720
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    49
        server.start();
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    50
        server.stop(0);
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    51
        serverAddr = new InetSocketAddress(serverPort);
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    52
        int exceptionCount = 0;
54938
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    53
        boolean failedOnce = false;
27720
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    54
        System.out.println("Using serverPort == " + serverPort);
54938
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    55
        RETRY: while (exceptionCount == 0) {
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    56
            for (int i = 0; i < 100; i++) {
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    57
                try {
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    58
                    server = HttpServer.create(serverAddr, 0);
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    59
                    server.start();
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    60
                    server.stop(0);
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    61
                } catch (Exception ex) {
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    62
                    if (!failedOnce) {
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    63
                        failedOnce = true;
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    64
                        server = HttpServer.create(new InetSocketAddress(0), 0);
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    65
                        serverPort = server.getAddress().getPort();
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    66
                        server.start();
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    67
                        server.stop(0);
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    68
                        serverAddr = new InetSocketAddress(serverPort);
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    69
                        System.out.println("Retrying with serverPort == " + serverPort);
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    70
                        continue RETRY;
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    71
                    }
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    72
                    System.err.println("Got exception at iteration: " + i );
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    73
                    ex.printStackTrace();
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    74
                    exceptionCount++;
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    75
                }
27720
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    76
            }
54938
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    77
            break;
27720
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    78
        }
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    79
        if (exceptionCount > 0) {
54938
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    80
           throw new RuntimeException("Test Failed: got "
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    81
                 + exceptionCount + " exceptions.");
27720
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    82
        }
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    83
    }
aa3983c8fbee 8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
diff changeset
    84
}