Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/SocketOption/TcpKeepAliveTest.java
38812 views
1
/*
2
* Copyright (c) 2018, 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 8194298
27
* @summary Add support for per Socket configuration of TCP keepalive
28
* @run main TcpKeepAliveTest
29
*/
30
import java.io.IOException;
31
import java.net.DatagramSocket;
32
import java.net.MulticastSocket;
33
import java.net.ServerSocket;
34
import java.net.Socket;
35
import java.net.SocketOption;
36
import java.util.Set;
37
import jdk.net.ExtendedSocketOptions;
38
import jdk.net.Sockets;
39
40
public class TcpKeepAliveTest {
41
42
private static final String LOCAL_HOST = "127.0.0.1";
43
private static final int DEFAULT_KEEP_ALIVE_PROBES = 7;
44
private static final int DEFAULT_KEEP_ALIVE_TIME = 1973;
45
private static final int DEFAULT_KEEP_ALIVE_INTVL = 53;
46
47
public static void main(String args[]) throws IOException {
48
49
try (ServerSocket ss = new ServerSocket(0);
50
Socket s = new Socket(LOCAL_HOST, ss.getLocalPort());
51
DatagramSocket ds = new DatagramSocket(0);
52
MulticastSocket mc = new MulticastSocket(0)) {
53
Set<SocketOption<?>> supportedOpts = Sockets.supportedOptions(ss.getClass());
54
Set<SocketOption<?>> supportedOptsClient = Sockets.supportedOptions(s.getClass());
55
Set<SocketOption<?>> supportedOptsDG = Sockets.supportedOptions(ds.getClass());
56
Set<SocketOption<?>> supportedOptsMC = Sockets.supportedOptions(mc.getClass());
57
if (supportedOpts.contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {
58
Sockets.setOption(ss, ExtendedSocketOptions.TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME);
59
if (Sockets.getOption(ss, ExtendedSocketOptions.TCP_KEEPIDLE) != DEFAULT_KEEP_ALIVE_TIME) {
60
throw new RuntimeException("Test failed, TCP_KEEPIDLE should have been " + DEFAULT_KEEP_ALIVE_TIME);
61
}
62
}
63
if (supportedOpts.contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) {
64
Sockets.setOption(ss, ExtendedSocketOptions.TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES);
65
if (Sockets.getOption(ss, ExtendedSocketOptions.TCP_KEEPCOUNT) != DEFAULT_KEEP_ALIVE_PROBES) {
66
throw new RuntimeException("Test failed, TCP_KEEPCOUNT should have been " + DEFAULT_KEEP_ALIVE_PROBES);
67
}
68
}
69
if (supportedOpts.contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {
70
Sockets.setOption(ss, ExtendedSocketOptions.TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL);
71
if (Sockets.getOption(ss, ExtendedSocketOptions.TCP_KEEPINTERVAL) != DEFAULT_KEEP_ALIVE_INTVL) {
72
throw new RuntimeException("Test failed, TCP_KEEPINTERVAL should have been " + DEFAULT_KEEP_ALIVE_INTVL);
73
}
74
}
75
if (supportedOptsClient.contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {
76
Sockets.setOption(s, ExtendedSocketOptions.TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME);
77
if (Sockets.getOption(s, ExtendedSocketOptions.TCP_KEEPIDLE) != DEFAULT_KEEP_ALIVE_TIME) {
78
throw new RuntimeException("Test failed, TCP_KEEPIDLE should have been " + DEFAULT_KEEP_ALIVE_TIME);
79
}
80
}
81
if (supportedOptsClient.contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) {
82
Sockets.setOption(s, ExtendedSocketOptions.TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES);
83
if (Sockets.getOption(s, ExtendedSocketOptions.TCP_KEEPCOUNT) != DEFAULT_KEEP_ALIVE_PROBES) {
84
throw new RuntimeException("Test failed, TCP_KEEPCOUNT should have been " + DEFAULT_KEEP_ALIVE_PROBES);
85
}
86
}
87
if (supportedOptsClient.contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {
88
Sockets.setOption(s, ExtendedSocketOptions.TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL);
89
if (Sockets.getOption(s, ExtendedSocketOptions.TCP_KEEPINTERVAL) != DEFAULT_KEEP_ALIVE_INTVL) {
90
throw new RuntimeException("Test failed, TCP_KEEPINTERVAL should have been " + DEFAULT_KEEP_ALIVE_INTVL);
91
}
92
}
93
if (supportedOptsDG.contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) {
94
throw new RuntimeException("Test failed, TCP_KEEPCOUNT is applicable"
95
+ " for TCP Sockets only.");
96
}
97
if (supportedOptsDG.contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {
98
throw new RuntimeException("Test failed, TCP_KEEPIDLE is applicable"
99
+ " for TCP Sockets only.");
100
}
101
if (supportedOptsDG.contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {
102
throw new RuntimeException("Test failed, TCP_KEEPINTERVAL is applicable"
103
+ " for TCP Sockets only.");
104
}
105
if (supportedOptsMC.contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) {
106
throw new RuntimeException("Test failed, TCP_KEEPCOUNT is applicable"
107
+ " for TCP Sockets only");
108
}
109
if (supportedOptsMC.contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {
110
throw new RuntimeException("Test failed, TCP_KEEPIDLE is applicable"
111
+ " for TCP Sockets only");
112
}
113
if (supportedOptsMC.contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {
114
throw new RuntimeException("Test failed, TCP_KEEPINTERVAL is applicable"
115
+ " for TCP Sockets only");
116
}
117
}
118
}
119
}
120
121