Skip to content

Commit

Permalink
8326568: jdk/test/com/sun/net/httpserver/bugs/B6431193.java should us…
Browse files Browse the repository at this point in the history
…e try-with-resource and try-finally

Reviewed-by: dfuchs, jpai
  • Loading branch information
Darragh Clarke committed Apr 10, 2024
1 parent b49ba42 commit 86cb767
Showing 1 changed file with 30 additions and 41 deletions.
71 changes: 30 additions & 41 deletions test/jdk/com/sun/net/httpserver/bugs/B6431193.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -40,59 +40,48 @@

public class B6431193 {

static boolean error = false;
static boolean handlerIsDaemon = true;

public static void read (InputStream i) throws IOException {
while (i.read() != -1);
i.close();
}

/**
* @param args
*/
public static void main(String[] args) {
public static void main(String[] args) throws IOException {
class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
InputStream is = t.getRequestBody();
read(is);
// .. read the request body
try (InputStream is = t.getRequestBody();
OutputStream os = t.getResponseBody()) {
is.readAllBytes();
// .. read the request body
String response = "This is the response";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
error = Thread.currentThread().isDaemon();
t.sendResponseHeaders(200, response.length());
os.write(response.getBytes());
} finally {
handlerIsDaemon = Thread.currentThread().isDaemon();
}
}
}

InetAddress loopback = InetAddress.getLoopbackAddress();
HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
server.createContext("/apps", new MyHandler());
server.setExecutor(null);
server.start();

HttpServer server;
try {
InetAddress loopback = InetAddress.getLoopbackAddress();
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);

server.createContext("/apps", new MyHandler());
server.setExecutor(null);
// creates a default executor
server.start();
int port = server.getAddress().getPort();
String s = "http://localhost:"+port+"/apps/foo";
URL url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(port)
.path("/apps/foo")
.toURL();
InputStream is = url.openConnection(Proxy.NO_PROXY).getInputStream();
read (is);
server.stop(0);
if (error) {
throw new RuntimeException ("error in test");
.scheme("http")
.loopback()
.port(port)
.path("/apps/foo")
.toURL();
try (InputStream is = url.openConnection(Proxy.NO_PROXY).getInputStream()) {
is.readAllBytes();
}

}
catch (Exception e) {
if (handlerIsDaemon) {
throw new RuntimeException("request was handled by a daemon thread");
}
} catch (Exception e) {
throw new AssertionError("Unexpected exception: " + e, e);
} finally {
server.stop(0);
}
}
}

0 comments on commit 86cb767

Please sign in to comment.