Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/HttpExchange/AutoCloseableHttpExchange.java
41161 views
/*1* Copyright (c) 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*/2223import com.sun.net.httpserver.*;2425import java.io.IOException;26import java.io.InputStream;27import java.net.*;28import java.util.concurrent.CountDownLatch;29import java.util.concurrent.ExecutorService;30import java.util.concurrent.Executors;31import java.util.concurrent.atomic.AtomicBoolean;3233import jdk.test.lib.net.URIBuilder;34import sun.net.httpserver.HttpExchangeAccess;353637/**38* @test39* @bug 820303640* @library /test/lib41* @modules jdk.httpserver/sun.net.httpserver42* @build jdk.httpserver/sun.net.httpserver.HttpExchangeAccess AutoCloseableHttpExchange43* @run main/othervm AutoCloseableHttpExchange44* @summary Ensure that HttpExchange closes correctly when utilising the45* AutoCloseable interface e.g. both request InputStream and response OutputStream46* are closed, if not already.47*/4849public class AutoCloseableHttpExchange {5051static HttpServer testHttpServer;52static AtomicBoolean exchangeCloseFail = new AtomicBoolean(false);5354static class Handler implements HttpHandler {55private CountDownLatch latch;5657Handler(CountDownLatch latch) {58this.latch = latch;59}6061public void handle(HttpExchange t) throws IOException {62InputStream is = t.getRequestBody();63try (HttpExchange e = t) {64while (is.read() != -1) ;65t.sendResponseHeaders(200, -1);66}67if (!HttpExchangeAccess.isClosed(t)) {68exchangeCloseFail.set(true);69}70latch.countDown();71}72}7374static void connectAndCheck(String realm) throws Exception {75URL url = URIBuilder.newBuilder()76.scheme("http")77.loopback()78.port(testHttpServer.getAddress().getPort())79.path(realm)80.toURL();81HttpURLConnection testConnection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);82InputStream is = testConnection.getInputStream();83while (is.read() != -1) ;84is.close();85}8687public static void main(String[] args) throws Exception {88int CONNECTION_COUNT = 5;89CountDownLatch latch = new CountDownLatch(CONNECTION_COUNT);9091InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);92testHttpServer = HttpServer.create(addr, 0);93testHttpServer.createContext("/test", new Handler(latch));9495ExecutorService executor = Executors.newFixedThreadPool(CONNECTION_COUNT);96testHttpServer.setExecutor(executor);97testHttpServer.start();9899while (CONNECTION_COUNT-- != 0) {100connectAndCheck("/test");101}102latch.await();103testHttpServer.stop(2);104executor.shutdown();105106if (exchangeCloseFail.get())107throw new RuntimeException("The exchange was not closed properly");108}109}110111112