Path: blob/master/test/jdk/java/net/URLConnection/TimeoutTest.java
41149 views
/*1* Copyright (c) 2001, 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/*24* @test25* @bug 438997626* @library /test/lib27* @summary can't unblock read() of InputStream from URL connection28* @run main/timeout=40/othervm -Dsun.net.client.defaultReadTimeout=2000 TimeoutTest29*/3031import java.io.*;32import java.net.*;33import jdk.test.lib.net.URIBuilder;3435public class TimeoutTest {3637class Server extends Thread {38ServerSocket server;39Server (ServerSocket server) {40super ();41this.server = server;42}43public void run () {44try {45Socket s = server.accept ();46while (!finished ()) {47Thread.sleep (1000);48}49s.close();50} catch (Exception e) {51}52}53boolean isFinished = false;5455synchronized boolean finished () {56return (isFinished);57}58synchronized void done () {59isFinished = true;60}61}6263public static void main(String[] args) throws Exception {64TimeoutTest t = new TimeoutTest ();65t.test ();66}6768public void test() throws Exception {69ServerSocket ss = new ServerSocket();70ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));71Server s = new Server (ss);72try{73URL url = URIBuilder.newBuilder()74.scheme("http")75.loopback()76.port(ss.getLocalPort())77.toURL();78System.out.println("URL: " + url);79URLConnection urlc = url.openConnection ();80InputStream is = urlc.getInputStream ();81throw new RuntimeException("Should have received timeout");82} catch (SocketTimeoutException e) {83return;84} finally {85s.done();86ss.close();87}88}89}909192