Path: blob/master/test/jdk/java/net/Socket/LingerTest.java
41152 views
/*1* Copyright (c) 2003, 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 479616626* @library /test/lib27* @summary Linger interval delays usage of released file descriptor28* @run main LingerTest29* @run main/othervm -Djava.net.preferIPv4Stack=true LingerTest30*/3132import java.net.*;33import java.io.*;34import jdk.test.lib.net.IPSupport;3536public class LingerTest {3738static class Sender implements Runnable {39Socket s;4041public Sender(Socket s) {42this.s = s;43}4445public void run() {46System.out.println ("Sender starts");47try {48s.getOutputStream().write(new byte[128*1024]);49}50catch (IOException ioe) {51}52System.out.println ("Sender ends");53}54}5556static class Closer implements Runnable {57Socket s;5859public Closer(Socket s) {60this.s = s;61}6263public void run() {64System.out.println ("Closer starts");65try {66s.close();67}68catch (IOException ioe) {69}70System.out.println ("Closer ends");71}72}7374static class Other implements Runnable {75final InetAddress address;76final int port;77final long delay;78boolean connected = false;7980public Other(InetAddress address, int port, long delay) {81this.address = address;82this.port = port;83this.delay = delay;84}8586public void run() {87System.out.println ("Other starts: sleep " + delay);88try {89Thread.sleep(delay);90System.out.println ("Other opening socket");91Socket s = new Socket(address, port);92synchronized (this) {93connected = true;94}95s.close();96}97catch (Exception ioe) {98ioe.printStackTrace();99}100System.out.println ("Other ends");101}102103public synchronized boolean connected() {104return connected;105}106}107108public static void main(String args[]) throws Exception {109IPSupport.throwSkippedExceptionIfNonOperational();110111InetAddress loopback = InetAddress.getLoopbackAddress();112ServerSocket ss = new ServerSocket(0, 50, loopback);113114Socket s1 = new Socket(loopback, ss.getLocalPort());115Socket s2 = ss.accept();116117// setup conditions for untransmitted data and lengthy118// linger interval119s1.setSendBufferSize(128*1024);120s1.setSoLinger(true, 30);121s2.setReceiveBufferSize(1*1024);122123// start sender124Thread senderThread = new Thread(new Sender(s1));125senderThread.start();126127// other thread that will connect after 5 seconds.128Other other = new Other(loopback, ss.getLocalPort(), 5000);129Thread otherThread = new Thread(other);130otherThread.start();131132// give sender time to queue the data133System.out.println ("Main sleep 1000");134Thread.sleep(1000);135System.out.println ("Main continue");136137// close the socket asynchronously138Thread closerThread = new Thread(new Closer(s1));139closerThread.start();140141System.out.println ("Main sleep 15000");142// give other time to run143Thread.sleep(15000);144System.out.println ("Main closing serversocket");145146ss.close();147// check that other is done148if (!other.connected()) {149throw new RuntimeException("Other thread is blocked");150}151152// await termination of all test related threads153senderThread.join(60_000);154otherThread.join(60_000);155closerThread.join(60_000);156157System.out.println ("Main ends");158}159}160161162