Path: blob/master/test/jdk/java/net/SocketOption/TcpKeepAliveTest.java
41149 views
/*1* Copyright (c) 2018, 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 819429826* @summary Add support for per Socket configuration of TCP keepalive27* @modules jdk.net28* @run main TcpKeepAliveTest29*/30import java.io.IOException;31import java.net.DatagramSocket;32import java.net.InetAddress;33import java.net.InetSocketAddress;34import java.net.MulticastSocket;35import java.net.ServerSocket;36import java.net.Socket;37import jdk.net.ExtendedSocketOptions;3839public class TcpKeepAliveTest {4041private static final int DEFAULT_KEEP_ALIVE_PROBES = 7;42private static final int DEFAULT_KEEP_ALIVE_TIME = 1973;43private static final int DEFAULT_KEEP_ALIVE_INTVL = 53;4445public static void main(String args[]) throws IOException {46var loopback = InetAddress.getLoopbackAddress();47try (ServerSocket ss = boundServer(loopback);48Socket s = new Socket(loopback, ss.getLocalPort());49DatagramSocket ds = new DatagramSocket(0);50MulticastSocket mc = new MulticastSocket(0)) {51if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {52ss.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME);53if (ss.getOption(ExtendedSocketOptions.TCP_KEEPIDLE) != DEFAULT_KEEP_ALIVE_TIME) {54throw new RuntimeException("Test failed, TCP_KEEPIDLE should have been " + DEFAULT_KEEP_ALIVE_TIME);55}56}57if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) {58ss.setOption(ExtendedSocketOptions.TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES);59if (ss.getOption(ExtendedSocketOptions.TCP_KEEPCOUNT) != DEFAULT_KEEP_ALIVE_PROBES) {60throw new RuntimeException("Test failed, TCP_KEEPCOUNT should have been " + DEFAULT_KEEP_ALIVE_PROBES);61}62}63if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {64ss.setOption(ExtendedSocketOptions.TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL);65if (ss.getOption(ExtendedSocketOptions.TCP_KEEPINTERVAL) != DEFAULT_KEEP_ALIVE_INTVL) {66throw new RuntimeException("Test failed, TCP_KEEPINTERVAL should have been " + DEFAULT_KEEP_ALIVE_INTVL);67}68}69if (s.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {70s.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME);71if (s.getOption(ExtendedSocketOptions.TCP_KEEPIDLE) != DEFAULT_KEEP_ALIVE_TIME) {72throw new RuntimeException("Test failed, TCP_KEEPIDLE should have been " + DEFAULT_KEEP_ALIVE_TIME);73}74}75if (s.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) {76s.setOption(ExtendedSocketOptions.TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES);77if (s.getOption(ExtendedSocketOptions.TCP_KEEPCOUNT) != DEFAULT_KEEP_ALIVE_PROBES) {78throw new RuntimeException("Test failed, TCP_KEEPCOUNT should have been " + DEFAULT_KEEP_ALIVE_PROBES);79}80}81if (s.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {82s.setOption(ExtendedSocketOptions.TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL);83if (s.getOption(ExtendedSocketOptions.TCP_KEEPINTERVAL) != DEFAULT_KEEP_ALIVE_INTVL) {84throw new RuntimeException("Test failed, TCP_KEEPINTERVAL should have been " + DEFAULT_KEEP_ALIVE_INTVL);85}86}87if (ds.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) {88throw new RuntimeException("Test failed, TCP_KEEPCOUNT is applicable"89+ " for TCP Sockets only.");90}91if (ds.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {92throw new RuntimeException("Test failed, TCP_KEEPIDLE is applicable"93+ " for TCP Sockets only.");94}95if (ds.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {96throw new RuntimeException("Test failed, TCP_KEEPINTERVAL is applicable"97+ " for TCP Sockets only.");98}99if (mc.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) {100throw new RuntimeException("Test failed, TCP_KEEPCOUNT is applicable"101+ " for TCP Sockets only");102}103if (mc.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {104throw new RuntimeException("Test failed, TCP_KEEPIDLE is applicable"105+ " for TCP Sockets only");106}107if (mc.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {108throw new RuntimeException("Test failed, TCP_KEEPINTERVAL is applicable"109+ " for TCP Sockets only");110}111}112}113114private static ServerSocket boundServer(InetAddress address) throws IOException {115var socketAddress = new InetSocketAddress(address, 0);116var server = new ServerSocket();117server.bind(socketAddress);118return server;119}120}121122123