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