From 1eae87255325503962d15ebd3f5c9967b01a02e9 Mon Sep 17 00:00:00 2001 From: Michael McMahon Date: Mon, 9 May 2016 10:28:24 +0100 Subject: [PATCH] 8155888: java/net/httpclient/QuickResponses.java intermittently failed with java.util.ConcurrentModificationException Reviewed-by: chegar --- .../java/net/httpclient/QuickResponses.java | 2 +- jdk/test/java/net/httpclient/Server.java | 34 +++++++++++++++---- .../java/net/httpclient/SplitResponse.java | 1 + 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/jdk/test/java/net/httpclient/QuickResponses.java b/jdk/test/java/net/httpclient/QuickResponses.java index 5de1f687552..50d32b8dfbb 100644 --- a/jdk/test/java/net/httpclient/QuickResponses.java +++ b/jdk/test/java/net/httpclient/QuickResponses.java @@ -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"); diff --git a/jdk/test/java/net/httpclient/Server.java b/jdk/test/java/net/httpclient/Server.java index d103e12d54c..ffc98956a2d 100644 --- a/jdk/test/java/net/httpclient/Server.java +++ b/jdk/test/java/net/httpclient/Server.java @@ -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 sockets; + private final List sockets; + private final List removals; + private final List 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 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(); } } } diff --git a/jdk/test/java/net/httpclient/SplitResponse.java b/jdk/test/java/net/httpclient/SplitResponse.java index cf887c19a0f..a26ee01e159 100644 --- a/jdk/test/java/net/httpclient/SplitResponse.java +++ b/jdk/test/java/net/httpclient/SplitResponse.java @@ -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;