Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be m…
…ore resilient to unexpected traffic

Backport-of: e0a87ef
  • Loading branch information
GoeLin committed Feb 13, 2023
1 parent 2c47c45 commit d0984df
Showing 1 changed file with 44 additions and 12 deletions.
56 changes: 44 additions & 12 deletions test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java
Expand Up @@ -42,6 +42,7 @@
import java.time.Duration;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;

public class MultiThreadTest extends Thread {

Expand All @@ -60,13 +61,11 @@ static void debug(String msg) {
System.out.println(msg);
}

static int reqnum = 0;
static final AtomicInteger reqnum = new AtomicInteger();

void doRequest(String uri) throws Exception {
URL url = new URL(uri + "?foo="+reqnum);
reqnum ++;
URL url = new URL(uri + "?foo="+reqnum.getAndIncrement());
HttpURLConnection http = (HttpURLConnection)url.openConnection();

InputStream in = http.getInputStream();
byte b[] = new byte[100];
int total = 0;
Expand Down Expand Up @@ -157,12 +156,37 @@ public static void main(String args[]) throws Exception {
int reqs = Worker.getRequests ();
MultiThreadTest.debug("Requests = " + reqs);
System.out.println ("Connection count = " + cnt + " Request count = " + reqs);
if (cnt > threads) { // could be less
throw new RuntimeException ("Expected "+threads + " connections: used " +cnt);

// We may have received traffic from something else than
// our client. We should only count those workers for which
// the expected header has been found.
int validConnections = 0;
for (Worker w : svr.workers()) {
if (w.headerFound) validConnections++;
}

if (validConnections > threads + 1 || validConnections == 0) { // could be less
throw new RuntimeException ("Expected " + threads + " connections: used " + validConnections);
}
if (reqs != threads*requests) {

// Sometimes the client drops a connection after a while and
// spawns a new one. Why this is happening is not clear,
// and JDK-8223783 is logged to follow up on this. For the sake
// of test stabilization we don't fail on `threads + 1` connections
// but log a warning instead.
if (validConnections == threads + 1) {
debug("WARNING: " + validConnections
+ " have been used, where only " + threads
+ " were expected!");
}

if (validConnections != cnt) {
debug("WARNING: got " + (cnt - validConnections) + " unexpected connections!");
}
if (validConnections == cnt && reqs != threads*requests) {
throw new RuntimeException ("Expected "+ threads*requests+ " requests: got " +reqs);
}

for (Thread worker : svr.workers()) {
worker.join(60_000);
}
Expand All @@ -179,7 +203,7 @@ class Server extends Thread {
ServerSocket ss;
int connectionCount;
boolean shutdown = false;
private Queue<Worker> workers = new ConcurrentLinkedQueue<>();
private final Queue<Worker> workers = new ConcurrentLinkedQueue<>();

Server(ServerSocket ss) {
this.ss = ss;
Expand Down Expand Up @@ -246,8 +270,10 @@ public void run() {
class Worker extends Thread {
Socket s;
int id;
volatile boolean headerFound;

Worker(Socket s, int id) {
super("Worker-" + id);
this.s = s;
this.id = id;
}
Expand All @@ -260,18 +286,20 @@ public static int getRequests () {
return requests;
}
}

public static void incRequests () {
synchronized (rlock) {
requests++;
}
}

int readUntil(InputStream in, char[] seq) throws IOException {
int readUntil(InputStream in, StringBuilder headers, char[] seq) throws IOException {
int i=0, count=0;
while (true) {
int c = in.read();
if (c == -1)
return -1;
headers.append((char)c);
count++;
if (c == seq[i]) {
i++;
Expand Down Expand Up @@ -300,14 +328,18 @@ public void run() {

// read entire request from client
int n=0;

n = readUntil(in, new char[] {'\r','\n', '\r','\n'});

StringBuilder headers = new StringBuilder();
n = readUntil(in, headers, new char[] {'\r','\n', '\r','\n'});
if (n <= 0) {
MultiThreadTest.debug("worker: " + id + ": Shutdown");
s.close();
return;
}
if (headers.toString().contains("/foo.html?foo=")) {
headerFound = true;
} else {
MultiThreadTest.debug("worker: " + id + ": Unexpected request received: " + headers);
}

MultiThreadTest.debug("worker " + id +
": Read request from client " +
Expand Down

1 comment on commit d0984df

@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.