Skip to content

Commit

Permalink
8171405: java/net/URLConnection/ResendPostBody.java failed with "Erro…
Browse files Browse the repository at this point in the history
…r while cleaning up threads after test"

Test cleaned up to improve safe termination

Reviewed-by: clanger
Backport-of: 7b49c40
  • Loading branch information
GoeLin committed Feb 16, 2023
1 parent 22be061 commit 5c1748a
Showing 1 changed file with 72 additions and 63 deletions.
135 changes: 72 additions & 63 deletions test/jdk/java/net/URLConnection/ResendPostBody.java
Expand Up @@ -48,127 +48,136 @@ public class ResendPostBody {

static class Server extends Thread {

InputStream in;
OutputStream out;
Socket sock;
StringBuffer response;
ServerSocket server;

Server (ServerSocket s) throws IOException
{
private InputStream in;
private OutputStream out;
private Socket sock;
private StringBuffer response;
private ServerSocket server;

Server(ServerSocket s) throws IOException {
server = s;
}

void waitFor (String s) throws IOException
{
byte[] w = s.getBytes ();
for(int c=0; c<w.length; c++ ) {
void waitFor(String s) throws IOException {
byte[] w = s.getBytes();
for (int c = 0; c < w.length; c++) {
byte expected = w[c];
int b = in.read();
if (b == -1) {
acceptConn ();
acceptConn();
}
if ((byte)b != expected) {
c = 0;
}
}
}

boolean done = false;
private boolean done = false;

public synchronized boolean finished () {
public synchronized boolean finished() {
return done;
}

public synchronized void setFinished (boolean b) {
public synchronized void setFinished(boolean b) throws IOException {
done = b;
this.closeConn();
server.close();
}

void acceptConn() throws IOException {
sock = server.accept();
in = sock.getInputStream();
out = sock.getOutputStream();
}

void acceptConn () throws IOException
{
sock = server.accept ();
in = sock.getInputStream ();
out = sock.getOutputStream ();
void closeConn() throws IOException {
in.close();
out.close();
sock.close();
}

public void run () {
public void run() {
try {
response = new StringBuffer (1024);
acceptConn ();
waitFor ("POST");
waitFor ("ZZZ");
Thread.sleep (500);
sock.close ();
acceptConn ();
waitFor ("POST");
waitFor ("ZZZ");
response.append ("HTTP/1.1 200 OK\r\n");
response.append ("Server: Microsoft-IIS/5.0");
response.append ("Date: Wed, 26 Jul 2000 14:17:04 GMT\r\n\r\n");
out.write (response.toString().getBytes());
response = new StringBuffer(1024);
acceptConn();
waitFor("POST");
waitFor("ZZZ");
Thread.sleep(500);
sock.close();
acceptConn();
waitFor("POST");
waitFor("ZZZ");
response.append("HTTP/1.1 200 OK\r\n");
response.append("Server: Microsoft-IIS/5.0");
response.append("Date: Wed, 26 Jul 2000 14:17:04 GMT\r\n\r\n");
out.write(response.toString().getBytes());
out.flush();
while (!finished()) {
Thread.sleep (1000);
Thread.sleep(1000);
}
out.close();
} catch (Exception e) {
System.err.println ("Server Exception: " + e);
if (!done) {
System.err.println("Server Exception: " + e);
}
} finally {
try { server.close(); } catch (IOException unused) {}
try {
closeConn();
} catch (IOException ioe) {
if (!done) {
ioe.printStackTrace();
}
}
}
}
}

ServerSocket ss;
Server server;
private ServerSocket ss;
private Server server;

public static void main(String[] args) throws Exception {
try {
if (args.length == 1 && args[0].equals ("-i")) {
System.out.println ("Press return when ready");
System.in.read ();
System.out.println ("Done");
}
ResendPostBody t = new ResendPostBody ();
t. execute ();
} catch (IOException e) {
System.out.println ("IOException");
if (args.length == 1 && args[0].equals("-i")) {
System.out.println("Press return when ready");
System.in.read();
System.out.println("Done");
}
ResendPostBody t = new ResendPostBody();
t.execute();
}

public void execute () throws Exception {

public void execute() throws Exception {
byte b[] = "X=ABCDEFGHZZZ".getBytes();

ss = new ServerSocket (0);
server = new Server (ss);
server.start ();
server = new Server(ss);
server.start();

/* Get the URL */

String s = "http://localhost:"+ss.getLocalPort()+"/test";
URL url = new URL(s);
HttpURLConnection conURL = (HttpURLConnection)url.openConnection();
HttpURLConnection conURL = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);

conURL.setDoOutput(true);
conURL.setDoInput(true);
conURL.setAllowUserInteraction(false);
conURL.setUseCaches(false);
conURL.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conURL.setRequestProperty("Content-Length", ""+b.length);
conURL.setRequestProperty("Content-Length", "" + b.length);
conURL.setRequestProperty("Connection", "Close");

/* POST some data */

DataOutputStream OutStream = new DataOutputStream(conURL.getOutputStream());
OutStream.write(b, 0, b.length);
OutStream.write(b, 0, b.length);
OutStream.flush();
OutStream.close();

/* Read the response */
int resp = conURL.getResponseCode();

int resp = conURL.getResponseCode ();
server.setFinished (true);
server.setFinished(true); // Set finished and close ServerSocket
server.join(); // Join server thread

if (resp != 200)
throw new RuntimeException ("Response code was not 200: " + resp);
}
throw new RuntimeException("Response code was not 200: " + resp);
}
}

1 comment on commit 5c1748a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.