1 /*
   2  * Copyright (c) 1998, 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 /* @test
  25  * @bug 8195160
  26  * @summary  Socket.setSoTimeout(T) can cause incorrect delay of T
  27  *           under green threads
  28  * @requires (os.family == "linux")
  29  * @library .. /test/lib
  30  * @build RsocketTest
  31  * @run main/othervm -Djava.net.preferIPv4Stack=true SoTimeout
  32  */
  33 
  34 /*
  35  * This program depends a bit on the particular behaviour of the green
  36  * threads scheduler to produce the problem, but given that the underlying
  37  * bug a green threads bug, I think that's OK.
  38  */
  39 
  40 import java.net.*;
  41 import jdk.net.Sockets;
  42 
  43 public class SoTimeout implements Runnable {
  44     static ServerSocket serverSocket;
  45     static long timeWritten;
  46     static InetAddress addr;
  47     static int port;
  48 
  49     public static void main(String[] args) throws Exception {
  50         if (!RsocketTest.isRsocketAvailable())
  51             return;
  52 
  53         addr = InetAddress.getLocalHost();
  54 
  55         serverSocket = Sockets.openRdmaServerSocket();
  56         addr = InetAddress.getLocalHost();
  57         serverSocket.bind(new InetSocketAddress(addr, 0));
  58 
  59         port = serverSocket.getLocalPort();
  60 
  61         byte[] b = new byte[12];
  62         Thread t = new Thread(new SoTimeout());
  63         t.start();
  64 
  65         Socket s = serverSocket.accept();
  66         serverSocket.close();
  67 
  68         // set a 5 second timeout on the socket
  69         s.setSoTimeout(5000);
  70 
  71         s.getInputStream().read(b, 0, b.length);
  72         s.close();
  73 
  74         long waited = System.currentTimeMillis() - timeWritten;
  75 
  76         // this sequence should complete fairly quickly and if it
  77         // takes something resembling the the SoTimeout value then
  78         // we are probably incorrectly blocking and not waking up
  79         if (waited > 2000) {
  80             throw new Exception("shouldn't take " + waited + " to complete");
  81         }
  82     }
  83 
  84     public void run() {
  85         try {
  86             byte[] b = new byte[12];
  87             Socket s = Sockets.openRdmaSocket();
  88             s.connect(new InetSocketAddress(addr, port));
  89 
  90             Thread.yield();
  91             timeWritten = System.currentTimeMillis();
  92             s.getOutputStream().write(b, 0, 12);
  93             s.close();
  94         } catch (Exception e) {
  95             e.printStackTrace();
  96             throw new RuntimeException("Test Failed");
  97         }
  98     }
  99 
 100 }