Path: blob/master/test/jdk/sun/net/www/http/KeepAliveStream/InfiniteLoop.java
41154 views
/*1* Copyright (c) 2012, 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 800486326* @modules jdk.httpserver27* @summary Checks for proper close code in KeepAliveStream28* @library /test/lib29* @run main InfiniteLoop30* @run main/othervm -Djava.net.preferIPv6Addresses=true InfiniteLoop31*/3233import com.sun.net.httpserver.HttpExchange;34import com.sun.net.httpserver.HttpHandler;35import com.sun.net.httpserver.HttpServer;36import java.io.InputStream;37import java.io.IOException;38import java.io.OutputStream;39import java.net.HttpURLConnection;40import java.net.InetAddress;41import java.net.InetSocketAddress;42import java.net.Proxy;43import java.net.URL;44import java.util.concurrent.Phaser;4546import jdk.test.lib.net.URIBuilder;4748// Racey test, will not always fail, but if it does then we have a problem.4950public class InfiniteLoop {5152public static void main(String[] args) throws Exception {53InetAddress loopback = InetAddress.getLoopbackAddress();54HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 0);55server.createContext("/test/InfiniteLoop", new RespHandler());56server.start();57try {58InetSocketAddress address = server.getAddress();59URL url = URIBuilder.newBuilder()60.scheme("http")61.host(server.getAddress().getAddress())62.port(server.getAddress().getPort())63.path("/test/InfiniteLoop")64.toURL();65final Phaser phaser = new Phaser(2);66for (int i=0; i<10; i++) {67HttpURLConnection uc = (HttpURLConnection)68url.openConnection(Proxy.NO_PROXY);69final InputStream is = uc.getInputStream();70final Thread thread = new Thread() {71public void run() {72try {73phaser.arriveAndAwaitAdvance();74while (is.read() != -1)75Thread.sleep(50);76} catch (Exception x) { x.printStackTrace(); }77}};78thread.start();79phaser.arriveAndAwaitAdvance();80is.close();81System.out.println("returned from close");82thread.join();83}84} finally {85server.stop(0);86}87}8889static class RespHandler implements HttpHandler {90static final int RESP_LENGTH = 32 * 1024;91@Override92public void handle(HttpExchange t) throws IOException {93InputStream is = t.getRequestBody();94byte[] ba = new byte[8192];95while(is.read(ba) != -1);9697t.sendResponseHeaders(200, RESP_LENGTH);98try (OutputStream os = t.getResponseBody()) {99os.write(new byte[RESP_LENGTH]);100}101t.close();102}103}104}105106107