Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/AsyncDisconnect.java
38867 views
/*1* Copyright (c) 2006, 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 635853226* @run main/othervm AsyncDisconnect27* @summary HttpURLConnection.disconnect doesn't really do the job28*/2930import java.net.*;31import java.util.*;32import java.io.*;33import com.sun.net.httpserver.*;34import java.util.concurrent.Executors;35import java.util.concurrent.ExecutorService;3637public class AsyncDisconnect implements Runnable38{39com.sun.net.httpserver.HttpServer httpServer;40MyHandler httpHandler;41ExecutorService executorService;42HttpURLConnection uc;4344public static void main(String[] args) {45new AsyncDisconnect();46}4748public AsyncDisconnect() {49try {50startHttpServer();51doClient();52} catch (IOException ioe) {53System.err.println(ioe);54}55}5657void doClient() {58try {59InetSocketAddress address = httpServer.getAddress();60URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");61uc = (HttpURLConnection)url.openConnection();6263// create a thread that will disconnect the connection64(new Thread(this)).start();6566uc.getInputStream();6768// if we reach here then we have failed69throw new RuntimeException("Failed: We Expect a SocketException to be thrown");7071} catch (SocketException se) {72// this is what we expect to happen and is OK.73//System.out.println(se);74} catch (IOException e) {75e.printStackTrace();76} finally {77httpServer.stop(1);78executorService.shutdown();79}80}8182public void run() {83// wait for the request to be sent to the server before calling disconnect84try { Thread.sleep(2000); }85catch (Exception e) {}8687uc.disconnect();88}8990/**91* Http Server92*/93public void startHttpServer() throws IOException {94httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);95httpHandler = new MyHandler();9697HttpContext ctx = httpServer.createContext("/test/", httpHandler);9899executorService = Executors.newCachedThreadPool();100httpServer.setExecutor(executorService);101httpServer.start();102}103104class MyHandler implements HttpHandler {105public void handle(HttpExchange t) throws IOException {106// give the other thread a chance to close the connection107try { Thread.sleep(4000); }108catch (Exception e) {}109110t.sendResponseHeaders(400, -1);111t.close();112}113}114115}116117118