Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/SimpleHttpServerTest.java
41152 views
1
/*
2
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/**
25
* @test
26
* @bug 8015692
27
* @key intermittent
28
* @summary Test HttpServer instantiation, start, and stop repeated in a loop
29
* Testing for Bind exception on Windows. This test may fail
30
* intermittently if other tests / process manage to bind to
31
* the same port that the test is using in the short window
32
* time where the port might appear available again.
33
*/
34
35
import java.net.InetSocketAddress;
36
import java.net.ServerSocket;
37
38
import com.sun.net.httpserver.HttpServer;
39
40
41
public class SimpleHttpServerTest {
42
43
public static void main(String[] args) throws Exception {
44
45
System.out.println(System.getProperty("java.version"));
46
InetSocketAddress serverAddr = new InetSocketAddress(0);
47
HttpServer server = HttpServer.create(serverAddr, 0);
48
int serverPort = server.getAddress().getPort();
49
server.start();
50
server.stop(0);
51
serverAddr = new InetSocketAddress(serverPort);
52
int exceptionCount = 0;
53
boolean failedOnce = false;
54
System.out.println("Using serverPort == " + serverPort);
55
RETRY: while (exceptionCount == 0) {
56
for (int i = 0; i < 100; i++) {
57
try {
58
server = HttpServer.create(serverAddr, 0);
59
server.start();
60
server.stop(0);
61
} catch (Exception ex) {
62
if (!failedOnce) {
63
failedOnce = true;
64
server = HttpServer.create(new InetSocketAddress(0), 0);
65
serverPort = server.getAddress().getPort();
66
server.start();
67
server.stop(0);
68
serverAddr = new InetSocketAddress(serverPort);
69
System.out.println("Retrying with serverPort == " + serverPort);
70
continue RETRY;
71
}
72
System.err.println("Got exception at iteration: " + i );
73
ex.printStackTrace();
74
exceptionCount++;
75
}
76
}
77
break;
78
}
79
if (exceptionCount > 0) {
80
throw new RuntimeException("Test Failed: got "
81
+ exceptionCount + " exceptions.");
82
}
83
}
84
}
85
86