Skip to content

Commit 7b49c40

Browse files
Julia Boesdfuch
authored andcommitted
8171405: java/net/URLConnection/ResendPostBody.java failed with "Error while cleaning up threads after test"
Test cleaned up to improve safe termination Reviewed-by: michaelm, vtewari, dfuchs
1 parent 662348c commit 7b49c40

File tree

1 file changed

+73
-64
lines changed

1 file changed

+73
-64
lines changed

test/jdk/java/net/URLConnection/ResendPostBody.java

Lines changed: 73 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -51,130 +51,139 @@ public class ResendPostBody {
5151

5252
static class Server extends Thread {
5353

54-
InputStream in;
55-
OutputStream out;
56-
Socket sock;
57-
StringBuffer response;
58-
ServerSocket server;
59-
60-
Server (ServerSocket s) throws IOException
61-
{
54+
private InputStream in;
55+
private OutputStream out;
56+
private Socket sock;
57+
private StringBuffer response;
58+
private ServerSocket server;
59+
60+
Server(ServerSocket s) throws IOException {
6261
server = s;
6362
}
6463

65-
void waitFor (String s) throws IOException
66-
{
67-
byte[] w = s.getBytes ();
68-
for(int c=0; c<w.length; c++ ) {
64+
void waitFor(String s) throws IOException {
65+
byte[] w = s.getBytes();
66+
for (int c = 0; c < w.length; c++) {
6967
byte expected = w[c];
7068
int b = in.read();
7169
if (b == -1) {
72-
acceptConn ();
70+
acceptConn();
7371
}
74-
if ((byte)b != expected) {
72+
if ((byte) b != expected) {
7573
c = 0;
7674
}
7775
}
7876
}
7977

80-
boolean done = false;
78+
private boolean done = false;
8179

82-
public synchronized boolean finished () {
80+
public synchronized boolean finished() {
8381
return done;
8482
}
8583

86-
public synchronized void setFinished (boolean b) {
84+
public synchronized void setFinished(boolean b) throws IOException {
8785
done = b;
86+
this.closeConn();
87+
server.close();
88+
}
89+
90+
void acceptConn() throws IOException {
91+
sock = server.accept();
92+
in = sock.getInputStream();
93+
out = sock.getOutputStream();
8894
}
8995

90-
void acceptConn () throws IOException
91-
{
92-
sock = server.accept ();
93-
in = sock.getInputStream ();
94-
out = sock.getOutputStream ();
96+
void closeConn() throws IOException {
97+
in.close();
98+
out.close();
99+
sock.close();
95100
}
96101

97-
public void run () {
102+
public void run() {
98103
try {
99-
response = new StringBuffer (1024);
100-
acceptConn ();
101-
waitFor ("POST");
102-
waitFor ("ZZZ");
103-
Thread.sleep (500);
104-
sock.close ();
105-
acceptConn ();
106-
waitFor ("POST");
107-
waitFor ("ZZZ");
108-
response.append ("HTTP/1.1 200 OK\r\n");
109-
response.append ("Server: Microsoft-IIS/5.0");
110-
response.append ("Date: Wed, 26 Jul 2000 14:17:04 GMT\r\n\r\n");
111-
out.write (response.toString().getBytes());
104+
response = new StringBuffer(1024);
105+
acceptConn();
106+
waitFor("POST");
107+
waitFor("ZZZ");
108+
Thread.sleep(500);
109+
sock.close();
110+
acceptConn();
111+
waitFor("POST");
112+
waitFor("ZZZ");
113+
response.append("HTTP/1.1 200 OK\r\n");
114+
response.append("Server: Microsoft-IIS/5.0");
115+
response.append("Date: Wed, 26 Jul 2000 14:17:04 GMT\r\n\r\n");
116+
out.write(response.toString().getBytes());
117+
out.flush();
112118
while (!finished()) {
113-
Thread.sleep (1000);
119+
Thread.sleep(1000);
114120
}
115-
out.close();
116121
} catch (Exception e) {
117-
System.err.println ("Server Exception: " + e);
122+
if (!done) {
123+
System.err.println("Server Exception: " + e);
124+
}
118125
} finally {
119-
try { server.close(); } catch (IOException unused) {}
126+
try {
127+
closeConn();
128+
} catch (IOException ioe) {
129+
if (!done) {
130+
ioe.printStackTrace();
131+
}
132+
}
120133
}
121134
}
122135
}
123136

124-
ServerSocket ss;
125-
Server server;
137+
private ServerSocket ss;
138+
private Server server;
126139

127140
public static void main(String[] args) throws Exception {
128-
try {
129-
if (args.length == 1 && args[0].equals ("-i")) {
130-
System.out.println ("Press return when ready");
131-
System.in.read ();
132-
System.out.println ("Done");
133-
}
134-
ResendPostBody t = new ResendPostBody ();
135-
t. execute ();
136-
} catch (IOException e) {
137-
System.out.println ("IOException");
141+
if (args.length == 1 && args[0].equals("-i")) {
142+
System.out.println("Press return when ready");
143+
System.in.read();
144+
System.out.println("Done");
138145
}
146+
ResendPostBody t = new ResendPostBody();
147+
t.execute();
139148
}
140149

141-
public void execute () throws Exception {
142-
150+
public void execute() throws Exception {
143151
byte b[] = "X=ABCDEFGHZZZ".getBytes();
144152

145153
ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
146-
server = new Server (ss);
147-
server.start ();
154+
server = new Server(ss);
155+
server.start();
156+
148157
/* Get the URL */
149158
URL url = URIBuilder.newBuilder()
150159
.scheme("http")
151160
.loopback()
152161
.port(ss.getLocalPort())
153162
.path("/test")
154163
.toURL();
155-
HttpURLConnection conURL = (HttpURLConnection)url.openConnection();
164+
HttpURLConnection conURL = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
156165

157166
conURL.setDoOutput(true);
158167
conURL.setDoInput(true);
159168
conURL.setAllowUserInteraction(false);
160169
conURL.setUseCaches(false);
161170
conURL.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
162-
conURL.setRequestProperty("Content-Length", ""+b.length);
171+
conURL.setRequestProperty("Content-Length", "" + b.length);
163172
conURL.setRequestProperty("Connection", "Close");
164173

165174
/* POST some data */
166-
167175
DataOutputStream OutStream = new DataOutputStream(conURL.getOutputStream());
168-
OutStream.write(b, 0, b.length);
176+
OutStream.write(b, 0, b.length);
169177
OutStream.flush();
170178
OutStream.close();
171179

172180
/* Read the response */
181+
int resp = conURL.getResponseCode();
173182

174-
int resp = conURL.getResponseCode ();
175-
server.setFinished (true);
183+
server.setFinished(true); // Set finished and close ServerSocket
184+
server.join(); // Join server thread
176185

177186
if (resp != 200)
178-
throw new RuntimeException ("Response code was not 200: " + resp);
179-
}
187+
throw new RuntimeException("Response code was not 200: " + resp);
188+
}
180189
}

0 commit comments

Comments
 (0)