Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/LingerTest.java
38812 views
/*1* Copyright (c) 2003, 2013, 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* @summary Linger interval delays usage of released file descriptor27*/2829import java.net.*;30import java.io.*;3132public class LingerTest {3334static class Sender implements Runnable {35Socket s;3637public Sender(Socket s) {38this.s = s;39}4041public void run() {42System.out.println ("Sender starts");43try {44s.getOutputStream().write(new byte[128*1024]);45}46catch (IOException ioe) {47}48System.out.println ("Sender ends");49}50}5152static class Closer implements Runnable {53Socket s;5455public Closer(Socket s) {56this.s = s;57}5859public void run() {60System.out.println ("Closer starts");61try {62s.close();63}64catch (IOException ioe) {65}66System.out.println ("Closer ends");67}68}6970static class Other implements Runnable {71int port;72long delay;73boolean connected = false;7475public Other(int port, long delay) {76this.port = port;77this.delay = delay;78}7980public void run() {81System.out.println ("Other starts: sleep " + delay);82try {83Thread.sleep(delay);84System.out.println ("Other opening socket");85Socket s = new Socket("localhost", port);86synchronized (this) {87connected = true;88}89s.close();90}91catch (Exception ioe) {92ioe.printStackTrace();93}94System.out.println ("Other ends");95}9697public synchronized boolean connected() {98return connected;99}100}101102public static void main(String args[]) throws Exception {103ServerSocket ss = new ServerSocket(0);104105Socket s1 = new Socket("localhost", ss.getLocalPort());106Socket s2 = ss.accept();107108// setup conditions for untransmitted data and lengthy109// linger interval110s1.setSendBufferSize(128*1024);111s1.setSoLinger(true, 30);112s2.setReceiveBufferSize(1*1024);113114// start sender115Thread thr = new Thread(new Sender(s1));116thr.start();117118// other thread that will connect after 5 seconds.119Other other = new Other(ss.getLocalPort(), 5000);120thr = new Thread(other);121thr.start();122123// give sender time to queue the data124System.out.println ("Main sleep 1000");125Thread.sleep(1000);126System.out.println ("Main continue");127128// close the socket asynchronously129(new Thread(new Closer(s1))).start();130131System.out.println ("Main sleep 15000");132// give other time to run133Thread.sleep(15000);134System.out.println ("Main closing serversocket");135136ss.close();137// check that other is done138if (!other.connected()) {139throw new RuntimeException("Other thread is blocked");140}141System.out.println ("Main ends");142}143}144145146