8155888: java/net/httpclient/QuickResponses.java intermittently failed with java.util.ConcurrentModificationException
Reviewed-by: chegar
This commit is contained in:
parent
880dc078b3
commit
1eae872553
@ -71,6 +71,7 @@ public class QuickResponses {
|
||||
public static void main(String[] args) throws Exception {
|
||||
server = new Server(0);
|
||||
URI uri = new URI(server.getURL());
|
||||
server.start();
|
||||
|
||||
HttpRequest request = HttpRequest.create(uri)
|
||||
.GET();
|
||||
@ -79,7 +80,6 @@ public class QuickResponses {
|
||||
Server.Connection s1 = server.activity();
|
||||
s1.send(entireResponse());
|
||||
|
||||
|
||||
HttpResponse r = cf1.join();
|
||||
if (r.statusCode()!= 200 || !r.body(HttpResponse.asString()).equals(responses[0]))
|
||||
throw new RuntimeException("Failed on first response");
|
||||
|
@ -21,8 +21,6 @@
|
||||
* questions.
|
||||
*/
|
||||
|
||||
//package javaapplication16;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@ -32,6 +30,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
@ -44,13 +43,16 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
public class Server extends Thread {
|
||||
|
||||
ServerSocket ss;
|
||||
List<Connection> sockets;
|
||||
private final List<Connection> sockets;
|
||||
private final List<Connection> removals;
|
||||
private final List<Connection> additions;
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
|
||||
// waits up to 20 seconds for something to happen
|
||||
// dont use this unless certain activity coming.
|
||||
public Connection activity() {
|
||||
for (int i = 0; i < 80 * 100; i++) {
|
||||
doRemovalsAndAdditions();
|
||||
for (Connection c : sockets) {
|
||||
if (c.poll()) {
|
||||
return c;
|
||||
@ -59,11 +61,26 @@ public class Server extends Thread {
|
||||
try {
|
||||
Thread.sleep(250);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void doRemovalsAndAdditions() {
|
||||
if (removals.isEmpty() && additions.isEmpty())
|
||||
return;
|
||||
Iterator<Connection> i = removals.iterator();
|
||||
while (i.hasNext())
|
||||
sockets.remove(i.next());
|
||||
removals.clear();
|
||||
|
||||
i = additions.iterator();
|
||||
while (i.hasNext())
|
||||
sockets.add(i.next());
|
||||
additions.clear();
|
||||
}
|
||||
|
||||
// clears all current connections on Server.
|
||||
public void reset() {
|
||||
for (Connection c : sockets) {
|
||||
@ -84,7 +101,6 @@ public class Server extends Thread {
|
||||
incoming = new ArrayBlockingQueue<>(100);
|
||||
setName("Server-Connection");
|
||||
setDaemon(true);
|
||||
start();
|
||||
}
|
||||
final Socket socket;
|
||||
final int id;
|
||||
@ -210,16 +226,17 @@ public class Server extends Thread {
|
||||
try {
|
||||
socket.close();
|
||||
} catch (IOException e) {}
|
||||
sockets.remove(this);
|
||||
removals.add(this);
|
||||
}
|
||||
}
|
||||
|
||||
Server(int port) throws IOException {
|
||||
ss = new ServerSocket(port);
|
||||
sockets = Collections.synchronizedList(new LinkedList<>());
|
||||
removals = Collections.synchronizedList(new LinkedList<>());
|
||||
additions = Collections.synchronizedList(new LinkedList<>());
|
||||
setName("Test-Server");
|
||||
setDaemon(true);
|
||||
start();
|
||||
}
|
||||
|
||||
Server() throws IOException {
|
||||
@ -238,6 +255,7 @@ public class Server extends Thread {
|
||||
try {
|
||||
ss.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
for (Connection c : sockets) {
|
||||
c.close();
|
||||
@ -250,8 +268,10 @@ public class Server extends Thread {
|
||||
try {
|
||||
Socket s = ss.accept();
|
||||
Connection c = new Connection(s);
|
||||
sockets.add(c);
|
||||
c.start();
|
||||
additions.add(c);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -68,6 +68,7 @@ public class SplitResponse {
|
||||
public static void main(String[] args) throws Exception {
|
||||
server = new Server(0);
|
||||
URI uri = new URI(server.getURL());
|
||||
server.start();
|
||||
|
||||
HttpRequest request;
|
||||
HttpResponse r;
|
||||
|
Loading…
Reference in New Issue
Block a user