Path: blob/master/test/jdk/java/net/Socket/SoTimeout.java
41149 views
/*1* Copyright (c) 1998, 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/* @test24@bug 415662525@summary Socket.setSoTimeout(T) can cause incorrect delay of T26under green threads27@author Tom Rodriguez28*/2930/*31* This program depends a bit on the particular behaviour of the green32* threads scheduler to produce the problem, but given that the underlying33* bug a green threads bug, I think that's OK.34*/3536import java.net.*;3738public class SoTimeout implements Runnable {39static ServerSocket serverSocket;40static long timeWritten;41static InetAddress addr;42static int port;4344public static void main(String[] args) throws Exception {45addr = InetAddress.getLocalHost();46serverSocket = new ServerSocket();47serverSocket.bind(new InetSocketAddress(addr, 0));48port = serverSocket.getLocalPort();4950byte[] b = new byte[12];51Thread t = new Thread(new SoTimeout());52t.start();5354Socket s = serverSocket.accept();55serverSocket.close();5657// set a 5 second timeout on the socket58s.setSoTimeout(5000);5960s.getInputStream().read(b, 0, b.length);61s.close();6263long waited = System.currentTimeMillis() - timeWritten;6465// this sequence should complete fairly quickly and if it66// takes something resembling the the SoTimeout value then67// we are probably incorrectly blocking and not waking up68if (waited > 2000) {69throw new Exception("shouldn't take " + waited + " to complete");70}71}7273public void run() {74try {75byte[] b = new byte[12];76Socket s = new Socket(addr, port);7778Thread.yield();79timeWritten = System.currentTimeMillis();80s.getOutputStream().write(b, 0, 12);81s.close();82} catch (Exception e) {83e.printStackTrace();84}85}8687}888990