Path: blob/master/test/jdk/com/sun/net/httpserver/SimpleHttpServerTest.java
41152 views
/*1* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 801569226* @key intermittent27* @summary Test HttpServer instantiation, start, and stop repeated in a loop28* Testing for Bind exception on Windows. This test may fail29* intermittently if other tests / process manage to bind to30* the same port that the test is using in the short window31* time where the port might appear available again.32*/3334import java.net.InetSocketAddress;35import java.net.ServerSocket;3637import com.sun.net.httpserver.HttpServer;383940public class SimpleHttpServerTest {4142public static void main(String[] args) throws Exception {4344System.out.println(System.getProperty("java.version"));45InetSocketAddress serverAddr = new InetSocketAddress(0);46HttpServer server = HttpServer.create(serverAddr, 0);47int serverPort = server.getAddress().getPort();48server.start();49server.stop(0);50serverAddr = new InetSocketAddress(serverPort);51int exceptionCount = 0;52boolean failedOnce = false;53System.out.println("Using serverPort == " + serverPort);54RETRY: while (exceptionCount == 0) {55for (int i = 0; i < 100; i++) {56try {57server = HttpServer.create(serverAddr, 0);58server.start();59server.stop(0);60} catch (Exception ex) {61if (!failedOnce) {62failedOnce = true;63server = HttpServer.create(new InetSocketAddress(0), 0);64serverPort = server.getAddress().getPort();65server.start();66server.stop(0);67serverAddr = new InetSocketAddress(serverPort);68System.out.println("Retrying with serverPort == " + serverPort);69continue RETRY;70}71System.err.println("Got exception at iteration: " + i );72ex.printStackTrace();73exceptionCount++;74}75}76break;77}78if (exceptionCount > 0) {79throw new RuntimeException("Test Failed: got "80+ exceptionCount + " exceptions.");81}82}83}848586