Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/SoTimeout.java
38812 views
/*1* Copyright (c) 1998, 2010, 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(0);47port = serverSocket.getLocalPort();4849byte[] b = new byte[12];50Thread t = new Thread(new SoTimeout());51t.start();5253Socket s = serverSocket.accept();54serverSocket.close();5556// set a 5 second timeout on the socket57s.setSoTimeout(5000);5859s.getInputStream().read(b, 0, b.length);60s.close();6162long waited = System.currentTimeMillis() - timeWritten;6364// this sequence should complete fairly quickly and if it65// takes something resembling the the SoTimeout value then66// we are probably incorrectly blocking and not waking up67if (waited > 2000) {68throw new Exception("shouldn't take " + waited + " to complete");69}70}7172public void run() {73try {74byte[] b = new byte[12];75Socket s = new Socket(addr, port);7677Thread.yield();78timeWritten = System.currentTimeMillis();79s.getOutputStream().write(b, 0, 12);80s.close();81} catch (Exception e) {82e.printStackTrace();83}84}8586}878889