8210130: java/net/httpclient/UnknownBodyLengthTest.java failed

The fix improves the test robustness by making it retry once in case of IO/Connect exception.

Reviewed-by: chegar
This commit is contained in:
Daniel Fuchs
2019-01-28 15:24:41 +00:00
parent 12cda6f859
commit 453d95eaf2

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019, 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
@@ -42,10 +42,14 @@ import jdk.testlibrary.SimpleSSLContext;
* @bug 8207966
* @library /lib/testlibrary
* @build jdk.testlibrary.SimpleSSLContext
* @run main/othervm -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest plain false
* @run main/othervm -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest SSL false
* @run main/othervm -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest plain true
* @run main/othervm -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest SSL true
* @run main/othervm -Djdk.httpclient.enableAllMethodRetry
* -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest plain false
* @run main/othervm -Djdk.httpclient.enableAllMethodRetry
* -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest SSL false
* @run main/othervm -Djdk.httpclient.enableAllMethodRetry
* -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest plain true
* @run main/othervm -Djdk.httpclient.enableAllMethodRetry
* -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest SSL true
*/
public class UnknownBodyLengthTest {
@@ -84,46 +88,55 @@ public class UnknownBodyLengthTest {
throw new RuntimeException("error at position " + i);
}
volatile boolean stopped;
void server(final boolean withContentLength) {
fillBuf(BUF);
try {
Socket s = ss.accept();
s.setTcpNoDelay(true);
s.setSoLinger(true, 1);
System.out.println("Accepted: "+s.getRemoteSocketAddress());
System.out.println("Accepted: "+s);
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
boolean done = false;
byte[] buf = new byte[1024];
String rsp = "";
while (!done) {
int c = is.read(buf);
if (c < 0) break;
String s1 = new String(buf, 0, c, "ISO-8859-1");
rsp += s1;
done = rsp.endsWith("!#!#");
while (!stopped) {
try (Socket s = ss.accept()) {
s.setTcpNoDelay(true);
s.setSoLinger(true, 1);
System.out.println("Accepted: " + s.getRemoteSocketAddress());
System.out.println("Accepted: " + s);
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
boolean done = false;
byte[] buf = new byte[1024];
String rsp = "";
while (!done) {
int c = is.read(buf);
if (c < 0) break;
String s1 = new String(buf, 0, c, "ISO-8859-1");
rsp += s1;
done = rsp.endsWith("!#!#");
}
String r = "HTTP/1.0 200 OK\r\nConnection: close\r\nContent-Type:" +
" text/xml; charset=UTF-8\r\n";
os.write(r.getBytes());
String chdr = "Content-Length: " + Integer.toString(BUF.length) +
"\r\n";
System.out.println(chdr);
if (withContentLength)
os.write(chdr.getBytes());
os.write("\r\n".getBytes());
os.write(BUF);
if (is.available() > 0)
is.read(buf);
os.flush();
os.close();
s.shutdownOutput();
} catch(Exception e) {
if (!stopped) {
System.out.println("Unexpected server exception: " + e);
e.printStackTrace();
}
}
}
String r = "HTTP/1.0 200 OK\r\nConnection: close\r\nContent-Type:" +
" text/xml; charset=UTF-8\r\n";
os.write(r.getBytes());
String chdr = "Content-Length: " + Integer.toString(BUF.length) +
"\r\n";
System.out.println(chdr);
if(withContentLength)
os.write(chdr.getBytes());
os.write("\r\n".getBytes());
os.write(BUF);
if (is.available() > 0)
is.read(buf);
os.flush();
os.close();
s.shutdownOutput();
s.close();
} catch(final Throwable t) {
t.printStackTrace();
if (!stopped) t.printStackTrace();
} finally {
try {ss.close(); } catch (Exception e) {}
try {stopped = true; ss.close(); } catch (Exception e) {}
}
}
@@ -154,11 +167,20 @@ public class UnknownBodyLengthTest {
boolean ssl = args[0].equals("SSL");
boolean fixedlen = args[1].equals("true");
UnknownBodyLengthTest test = new UnknownBodyLengthTest(ssl);
test.run(ssl, fixedlen);
try {
test.run(ssl, fixedlen);
} finally {
test.stop();
}
}
public void run(boolean ssl, boolean fixedlen) throws Exception {
new Thread(()->server(fixedlen)).start();
client(ssl);
}
public void stop() {
stopped = true;
try { ss.close(); } catch (Throwable t) { }
}
}