Path: blob/master/test/jdk/sun/net/www/protocol/http/RetryUponTimeout.java
41159 views
/*1* Copyright (c) 2003, 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 477207726* @library /test/lib27* @summary using defaultReadTimeout appear to retry request upon timeout28* @modules java.base/sun.net.www29*/3031import java.net.*;32import java.io.*;33import jdk.test.lib.net.URIBuilder;34import sun.net.www.*;3536public class RetryUponTimeout implements Runnable {37// run server38public void run(){39Socket socket = null;40try {41for (int i = 0; i < 2; i++) {42socket = server.accept();43InputStream is = socket.getInputStream ();44MessageHeader header = new MessageHeader (is);45count++;46}47} catch (Exception ex) {48// ignored49} finally {50try {51socket.close();52server.close();53} catch (IOException ioex) {54}55}5657}5859static ServerSocket server;60static int count = 0;61public static void main(String[] args) throws Exception {62try {63server = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());64int port = server.getLocalPort ();65new Thread(new RetryUponTimeout()).start ();6667URL url = URIBuilder.newBuilder()68.scheme("http")69.loopback()70.port(port)71.toURL();72System.out.println("URL: " + url);73java.net.URLConnection uc = url.openConnection();74uc.setReadTimeout(1000);75uc.getInputStream();76} catch (SocketTimeoutException stex) {77// expected exception78server.close();79}8081if (count > 1) {82throw new RuntimeException("Server received "+count+" requests instead of one.");83}8485}86}878889