Path: blob/master/test/jdk/sun/net/www/protocol/http/AsyncDisconnect.java
41159 views
/*1* Copyright (c) 2006, 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 635853226* @library /test/lib27* @modules jdk.httpserver28* @run main/othervm AsyncDisconnect29* @run main/othervm -Djava.net.preferIPv6Addresses=true AsyncDisconnect30* @summary HttpURLConnection.disconnect doesn't really do the job31*/3233import java.net.*;34import java.io.*;35import com.sun.net.httpserver.*;36import java.util.concurrent.Executors;37import java.util.concurrent.ExecutorService;3839import jdk.test.lib.net.URIBuilder;4041public class AsyncDisconnect implements Runnable42{43com.sun.net.httpserver.HttpServer httpServer;44MyHandler httpHandler;45ExecutorService executorService;46HttpURLConnection uc;4748public static void main(String[] args) throws Exception {49new AsyncDisconnect();50}5152public AsyncDisconnect() throws Exception {53startHttpServer();54doClient();55}5657void doClient() throws Exception {58Thread t = new Thread(this);5960try {61InetSocketAddress address = httpServer.getAddress();62URL url = URIBuilder.newBuilder()63.scheme("http")64.host(address.getAddress())65.port(address.getPort())66.path("/test/")67.toURL();68uc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);6970// create a thread that will disconnect the connection71t.start();7273uc.getInputStream();7475// if we reach here then we have failed76throw new RuntimeException("Failed: We Expect a SocketException to be thrown");7778} catch (SocketException se) {79// this is what we expect to happen and is OK.80//System.out.println(se);81} finally {82httpServer.stop(1);83t.join();84executorService.shutdown();8586}87}8889public void run() {90// wait for the request to be sent to the server before calling disconnect91try { Thread.sleep(2000); }92catch (Exception e) {}9394uc.disconnect();95}9697/**98* Http Server99*/100public void startHttpServer() throws IOException {101InetAddress loopback = InetAddress.getLoopbackAddress();102InetSocketAddress address = new InetSocketAddress(loopback, 0);103httpServer = com.sun.net.httpserver.HttpServer.create(address, 0);104httpHandler = new MyHandler();105106HttpContext ctx = httpServer.createContext("/test/", httpHandler);107108executorService = Executors.newCachedThreadPool();109httpServer.setExecutor(executorService);110httpServer.start();111}112113class MyHandler implements HttpHandler {114public void handle(HttpExchange t) throws IOException {115// give the other thread a chance to close the connection116try { Thread.sleep(4000); }117catch (Exception e) {}118119t.sendResponseHeaders(400, -1);120t.close();121}122}123124}125126127