1 /*
   2  * Copyright (c) 2003, 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 4796166
  27  * @summary Linger interval delays usage of released file descriptor
  28  * @run main LingerTest
  29  * @run main/othervm -Djava.net.preferIPv4Stack=true LingerTest
  30  */
  31 
  32 import java.net.*;
  33 import java.io.*;
  34 
  35 public class LingerTest {
  36 
  37     static class Sender implements Runnable {
  38         Socket s;
  39 
  40         public Sender(Socket s) {
  41             this.s = s;
  42         }
  43 
  44         public void run() {
  45             System.out.println ("Sender starts");
  46             try {
  47                 s.getOutputStream().write(new byte[128*1024]);
  48             }
  49             catch (IOException ioe) {
  50             }
  51             System.out.println ("Sender ends");
  52         }
  53     }
  54 
  55     static class Closer implements Runnable {
  56         Socket s;
  57 
  58         public Closer(Socket s) {
  59             this.s = s;
  60         }
  61 
  62         public void run() {
  63             System.out.println ("Closer starts");
  64             try {
  65                 s.close();
  66             }
  67             catch (IOException ioe) {
  68             }
  69             System.out.println ("Closer ends");
  70         }
  71     }
  72 
  73     static class Other implements Runnable {
  74         int port;
  75         long delay;
  76         boolean connected = false;
  77 
  78         public Other(int port, long delay) {
  79             this.port = port;
  80             this.delay = delay;
  81         }
  82 
  83         public void run() {
  84             System.out.println ("Other starts: sleep " + delay);
  85             try {
  86                 Thread.sleep(delay);
  87                 System.out.println ("Other opening socket");
  88                 Socket s = new Socket("localhost", port);
  89                 synchronized (this) {
  90                     connected = true;
  91                 }
  92                 s.close();
  93             }
  94             catch (Exception ioe) {
  95                 ioe.printStackTrace();
  96             }
  97             System.out.println ("Other ends");
  98         }
  99 
 100         public synchronized boolean connected() {
 101             return connected;
 102         }
 103     }
 104 
 105     public static void main(String args[]) throws Exception {
 106         ServerSocket ss = new ServerSocket(0);
 107 
 108         Socket s1 = new Socket("localhost", ss.getLocalPort());
 109         Socket s2 = ss.accept();
 110 
 111         // setup conditions for untransmitted data and lengthy
 112             // linger interval
 113             s1.setSendBufferSize(128*1024);
 114         s1.setSoLinger(true, 30);
 115         s2.setReceiveBufferSize(1*1024);
 116 
 117         // start sender
 118             Thread thr = new Thread(new Sender(s1));
 119         thr.start();
 120 
 121         // other thread that will connect after 5 seconds.
 122             Other other = new Other(ss.getLocalPort(), 5000);
 123         thr = new Thread(other);
 124         thr.start();
 125 
 126         // give sender time to queue the data
 127             System.out.println ("Main sleep 1000");
 128             Thread.sleep(1000);
 129             System.out.println ("Main continue");
 130 
 131         // close the socket asynchronously
 132             (new Thread(new Closer(s1))).start();
 133 
 134             System.out.println ("Main sleep 15000");
 135         // give other time to run
 136             Thread.sleep(15000);
 137             System.out.println ("Main closing serversocket");
 138 
 139         ss.close();
 140         // check that other is done
 141             if (!other.connected()) {
 142             throw new RuntimeException("Other thread is blocked");
 143         }
 144         System.out.println ("Main ends");
 145     }
 146 }