# HG changeset patch # User rriggs # Date 1521231437 14400 # Node ID 55c0de67f375f1419c0ec9da1b522c7f66ad54a4 # Parent ff7c335430d4cdd4808b278c43023e2ee213b473 8193584: (testbug) TestSocketFactory fails intermittently with ConcurrentModificationException Reviewed-by: bpb, lancea diff -r ff7c335430d4 -r 55c0de67f375 test/jdk/java/rmi/testlibrary/TestSocketFactory.java --- a/test/jdk/java/rmi/testlibrary/TestSocketFactory.java Fri Mar 16 03:17:12 2018 -0700 +++ b/test/jdk/java/rmi/testlibrary/TestSocketFactory.java Fri Mar 16 16:17:17 2018 -0400 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2018, 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 @@ -135,7 +135,7 @@ * @param matchBytes bytes to match after the trigger has been seen * @param replaceBytes bytes to replace the matched bytes */ - public void setMatchReplaceBytes(byte[] triggerBytes, byte[] matchBytes, + public synchronized void setMatchReplaceBytes(byte[] triggerBytes, byte[] matchBytes, byte[] replaceBytes) { this.triggerBytes = Objects.requireNonNull(triggerBytes, "triggerBytes"); this.matchBytes = Objects.requireNonNull(matchBytes, "matchBytes"); @@ -147,7 +147,7 @@ } @Override - public Socket createSocket(String host, int port) throws IOException { + public synchronized Socket createSocket(String host, int port) throws IOException { Socket socket = RMISocketFactory.getDefaultSocketFactory() .createSocket(host, port); InterposeSocket s = new InterposeSocket(socket, @@ -160,13 +160,13 @@ * Return the current list of sockets. * @return Return a snapshot of the current list of sockets */ - public List getSockets() { + public synchronized List getSockets() { List snap = new ArrayList<>(sockets); return snap; } @Override - public ServerSocket createServerSocket(int port) throws IOException { + public synchronized ServerSocket createServerSocket(int port) throws IOException { ServerSocket serverSocket = RMISocketFactory.getDefaultSocketFactory() .createServerSocket(port); @@ -180,7 +180,7 @@ * Return the current list of server sockets. * @return Return a snapshot of the current list of server sockets */ - public List getServerSockets() { + public synchronized List getServerSockets() { List snap = new ArrayList<>(serverSockets); return snap; } @@ -202,7 +202,7 @@ private final ByteArrayOutputStream inLogStream; private final ByteArrayOutputStream outLogStream; private final String name; - private static volatile int num = 0; // index for created InterposeSockets + private static volatile int num = 0; // index for created Interpose509s /** * Construct a socket that interposes on a socket to match and replace. @@ -457,7 +457,7 @@ * @param matchBytes bytes to match after the trigger has been seen * @param replaceBytes bytes to replace the matched bytes */ - public void setMatchReplaceBytes(byte[] triggerBytes, byte[] matchBytes, + public synchronized void setMatchReplaceBytes(byte[] triggerBytes, byte[] matchBytes, byte[] replaceBytes) { this.triggerBytes = triggerBytes; this.matchBytes = matchBytes; @@ -468,7 +468,7 @@ * Return a snapshot of the current list of sockets created from this server socket. * @return Return a snapshot of the current list of sockets */ - public List getSockets() { + public synchronized List getSockets() { List snap = new ArrayList<>(sockets); return snap; } @@ -501,9 +501,12 @@ @Override public Socket accept() throws IOException { Socket s = socket.accept(); - InterposeSocket socket = new InterposeSocket(s, matchBytes, replaceBytes); - sockets.add(socket); - return socket; + synchronized(this) { + InterposeSocket aSocket = new InterposeSocket(s, matchBytes, + replaceBytes); + sockets.add(aSocket); + return aSocket; + } } @Override