J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | @bug 4156625 |
| 26 | @summary Socket.setSoTimeout(T) can cause incorrect delay of T |
| 27 | under green threads |
| 28 | @author Tom Rodriguez |
| 29 | */ |
| 30 | |
| 31 | /* |
| 32 | * This program depends a bit on the particular behaviour of the green |
| 33 | * threads scheduler to produce the problem, but given that the underlying |
| 34 | * bug a green threads bug, I think that's OK. |
| 35 | */ |
| 36 | |
| 37 | import java.net.*; |
| 38 | |
| 39 | public class SoTimeout implements Runnable { |
| 40 | static ServerSocket serverSocket; |
| 41 | static long timeWritten; |
| 42 | static InetAddress addr; |
| 43 | static int port; |
| 44 | |
| 45 | public static void main(String[] args) throws Exception { |
| 46 | addr = InetAddress.getLocalHost(); |
| 47 | serverSocket = new ServerSocket(0); |
| 48 | port = serverSocket.getLocalPort(); |
| 49 | |
| 50 | byte[] b = new byte[12]; |
| 51 | Thread t = new Thread(new SoTimeout()); |
| 52 | t.start(); |
| 53 | |
| 54 | Socket s = serverSocket.accept(); |
| 55 | |
| 56 | // set a 1 second timeout on the socket |
| 57 | s.setSoTimeout(1000); |
| 58 | |
| 59 | s.getInputStream().read(b, 0, b.length); |
| 60 | s.close(); |
| 61 | |
| 62 | long waited = System.currentTimeMillis() - timeWritten; |
| 63 | |
| 64 | // this sequence should complete fairly quickly and if it |
| 65 | // takes something resembling the the SoTimeout value then |
| 66 | // we are probably incorrectly blocking and not waking up |
| 67 | if (waited > 500) { |
| 68 | throw new Exception("shouldn't take " + waited + " to complete"); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public void run() { |
| 73 | try { |
| 74 | byte[] b = new byte[12]; |
| 75 | Socket s = new Socket(addr, port); |
| 76 | |
| 77 | Thread.yield(); |
| 78 | timeWritten = System.currentTimeMillis(); |
| 79 | s.getOutputStream().write(b, 0, 12); |
| 80 | s.close(); |
| 81 | } catch (Exception e) { |
| 82 | e.printStackTrace(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | } |