Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/RacingCollections.java
38821 views
/*1* Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6360946 636094826* @summary Test various operations on concurrently mutating collections27* @author Martin Buchholz28*/2930import static java.util.Collections.*;31import java.util.*;32import java.util.concurrent.*;3334public class RacingCollections {35/**36* How long to run each "race" (in milliseconds).37* Turn this up to some higher value like 1000 for stress testing:38* java -Dmillis=1000 RacingCollections39*/40final static long defaultWorkTimeMillis = Long.getLong("millis", 10L);4142/**43* Whether to print debug information.44*/45final static boolean debug = Boolean.getBoolean("debug");4647final static Integer one = 1;48final static Integer two = 2;4950/**51* A thread that mutates an object forever, alternating between52* being empty and containing singleton "two"53*/54static class Frobber extends CheckedThread {55volatile boolean done = false;56boolean keepGoing(int i) { return (i % 128 != 0) || ! done; }5758final Object elLoco;59Frobber(Object elLoco) {60this.elLoco = elLoco;61this.start();62}6364@SuppressWarnings("unchecked") void clear(Object o) {65if (o instanceof Collection)66((Collection<?>)o).clear();67else68((Map<?,?>)o).clear();69}7071@SuppressWarnings("unchecked") void realRun() {72// Mutate elLoco wildly forever, checking occasionally for "done"73clear(elLoco);74if (elLoco instanceof List) {75List<Integer> l = (List<Integer>) elLoco;76for (int i = 0; keepGoing(i); i++) {77switch (i%2) {78case 0: l.add(two); break;79case 1: l.add(0, two); break;80}81switch (i%2) {82case 0: l.remove(two); break;83case 1: l.remove(0); break;84}}}85else if (elLoco instanceof Deque) {86Deque<Integer> q = (Deque<Integer>) elLoco;87for (int i = 0; keepGoing(i); i++) {88switch (i%6) {89case 0: q.add(two); break;90case 1: q.addFirst(two); break;91case 2: q.addLast(two); break;92case 3: q.offer(two); break;93case 4: q.offerFirst(two); break;94case 5: q.offerLast(two); break;95}96switch (i%6) {97case 0: q.remove(two); break;98case 1: q.removeFirst(); break;99case 2: q.removeLast(); break;100case 3: q.poll(); break;101case 4: q.pollFirst(); break;102case 5: q.pollLast(); break;103}}}104else if (elLoco instanceof Queue) {105Queue<Integer> q = (Queue<Integer>) elLoco;106for (int i = 0; keepGoing(i); i++) {107switch (i%2) {108case 0: q.add(two); break;109case 1: q.offer(two); break;110}111switch (i%2) {112case 0: q.remove(two); break;113case 1: q.poll(); break;114}}}115else if (elLoco instanceof Map) {116Map<Integer, Boolean> m = (Map<Integer, Boolean>) elLoco;117for (int i = 0; keepGoing(i); i++) {118m.put(two, true);119m.remove(two);120}}121else if (elLoco instanceof Collection) {122Collection<Integer> c = (Collection<Integer>) elLoco;123for (int i = 0; keepGoing(i); i++) {124c.add(two);125c.remove(two);126}}127else { throw new Error("Huh? " + elLoco); }128}129130void enoughAlready() {131done = true;132try { join(); } catch (Throwable t) { unexpected(t); }133}134}135136private static void checkEqualSanity(Object theRock, Object elLoco) {137//equal(theRock, theRock);138equal(elLoco, elLoco);139140// It would be nice someday to have theRock and elLoco never "equal",141// although the meaning of "equal" for mutating collections142// is a bit fuzzy. Uncomment when/if we fix:143// 6374942: Improve thread safety of collection .equals() methods144//notEqual(theRock, elLoco);145//notEqual(elLoco, theRock);146147notEqual(theRock.toString(), elLoco.toString());148}149150static class Looper {151final long quittingTime;152int i = 0;153Looper() { this(defaultWorkTimeMillis); }154Looper(long workTimeMillis) {155quittingTime = System.nanoTime() + workTimeMillis * 1024 * 1024;156}157boolean keepGoing() {158return (i++ % 128 != 0) || (System.nanoTime() < quittingTime);159}160}161162private static void frob(Object theRock, Object elLoco) {163Frobber frobber = new Frobber(elLoco);164try {165if (theRock instanceof Collection) {166@SuppressWarnings("unchecked")167Collection<Integer> c = (Collection<Integer>) theRock;168if (! c.contains(one))169c.add(one);170} else {171@SuppressWarnings("unchecked")172Map<Integer, Boolean> m = (Map<Integer, Boolean>) theRock;173if (! m.containsKey(one))174m.put(one, true);175}176for (Looper looper = new Looper(); looper.keepGoing(); )177checkEqualSanity(theRock, elLoco);178}179catch (Throwable t) { unexpected(t); }180finally { frobber.enoughAlready(); }181}182183private static List<Map<Integer, Boolean>> newConcurrentMaps() {184List<Map<Integer, Boolean>> list =185new ArrayList<Map<Integer, Boolean>>();186list.add(new ConcurrentHashMap<Integer, Boolean>());187list.add(new ConcurrentSkipListMap<Integer, Boolean>());188return list;189}190191private static List<Map<Integer, Boolean>> maps() {192List<Map<Integer, Boolean>> list = newConcurrentMaps();193list.add(new Hashtable<Integer, Boolean>());194list.add(new HashMap<Integer, Boolean>());195list.add(new TreeMap<Integer, Boolean>());196Comparator<Integer> cmp = new Comparator<Integer>() {197public int compare(Integer x, Integer y) {198return x - y;199}};200list.add(new TreeMap<Integer, Boolean>(Collections.reverseOrder(cmp)));201return list;202}203204private static List<Set<Integer>> newConcurrentSets() {205List<Set<Integer>> list = new ArrayList<Set<Integer>>();206list.add(new ConcurrentSkipListSet<Integer>());207list.add(new CopyOnWriteArraySet<Integer>());208return list;209}210211private static List<Set<Integer>> newSets() {212List<Set<Integer>> list = newConcurrentSets();213list.add(new HashSet<Integer>());214list.add(new TreeSet<Integer>());215list.add(new TreeSet<Integer>(Collections.reverseOrder()));216return list;217}218219private static List<List<Integer>> newConcurrentLists() {220List<List<Integer>> list = new ArrayList<List<Integer>>();221list.add(new CopyOnWriteArrayList<Integer>());222return list;223}224225private static List<List<Integer>> newLists() {226List<List<Integer>> list = newConcurrentLists();227list.add(new Vector<Integer>());228list.add(new ArrayList<Integer>());229return list;230}231232private static List<Queue<Integer>> newConcurrentQueues() {233List<Queue<Integer>> list =234new ArrayList<Queue<Integer>>(newConcurrentDeques());235list.add(new LinkedBlockingQueue<Integer>(10));236list.add(new LinkedTransferQueue<Integer>());237list.add(new ConcurrentLinkedQueue<Integer>());238return list;239}240241private static List<Queue<Integer>> newQueues() {242List<Queue<Integer>> list =243new ArrayList<Queue<Integer>>(newDeques());244list.add(new LinkedBlockingQueue<Integer>(10));245return list;246}247248private static List<Deque<Integer>> newConcurrentDeques() {249List<Deque<Integer>> list = new ArrayList<Deque<Integer>>();250list.add(new LinkedBlockingDeque<Integer>(10));251list.add(new ConcurrentLinkedDeque<Integer>());252return list;253}254255private static List<Deque<Integer>> newDeques() {256List<Deque<Integer>> list = newConcurrentDeques();257list.add(new ArrayDeque<Integer>());258list.add(new LinkedList<Integer>());259return list;260}261262private static void describe(Class<?> k, Object x, Object y) {263if (debug)264System.out.printf("%s: %s, %s%n", k.getSimpleName(),265x.getClass().getSimpleName(),266y.getClass().getSimpleName());267}268269private static void realMain(String[] args) {270for (Map<Integer, Boolean> x : maps())271for (Map<Integer, Boolean> y : newConcurrentMaps()) {272describe(Map.class, x, y);273x.put(one, true);274frob(x, y);275frob(unmodifiableMap(x), y);276frob(synchronizedMap(x), y);277frob(x, synchronizedMap(y));278frob(checkedMap(x, Integer.class, Boolean.class), y);279frob(x, checkedMap(y, Integer.class, Boolean.class));280x.clear();281frob(newSetFromMap(x), newSetFromMap(y));282frob(x.keySet(), newSetFromMap(y));283}284285for (Set<Integer> x : newSets())286for (Set<Integer> y : newConcurrentSets()) {287describe(Set.class, x, y);288frob(x, y);289frob(unmodifiableSet(x), y);290frob(synchronizedSet(x), y);291frob(x, synchronizedSet(y));292frob(checkedSet(x, Integer.class), y);293frob(x, checkedSet(y, Integer.class));294}295296for (List<Integer> x : newLists())297for (List<Integer> y : newConcurrentLists()) {298describe(List.class, x, y);299frob(x, y);300frob(unmodifiableList(x), y);301frob(synchronizedList(x), y);302frob(x, synchronizedList(y));303frob(checkedList(x, Integer.class), y);304frob(x, checkedList(y, Integer.class));305}306307for (Queue<Integer> x : newQueues())308for (Queue<Integer> y : newConcurrentQueues()) {309describe(Queue.class, x, y);310frob(x, y);311}312313for (Deque<Integer> x : newDeques())314for (Deque<Integer> y : newConcurrentDeques()) {315describe(Deque.class, x, y);316frob(asLifoQueue(x), y);317frob(x, asLifoQueue(y));318}319}320321//--------------------- Infrastructure ---------------------------322static volatile int passed = 0, failed = 0;323static void pass() {passed++;}324static void fail() {failed++; Thread.dumpStack();}325static void fail(String msg) {System.out.println(msg); fail();}326static void unexpected(Throwable t) {failed++; t.printStackTrace();}327static void check(boolean cond) {if (cond) pass(); else fail();}328static String toString(Object x) {329return ((x instanceof Collection) || (x instanceof Map)) ?330x.getClass().getName() : x.toString();}331static void equal(Object x, Object y) {332if (x == null ? y == null : x.equals(y)) pass();333else fail(toString(x) + " not equal to " + toString(y));}334static void notEqual(Object x, Object y) {335if (x == null ? y == null : x.equals(y))336fail(toString(x) + " equal to " + toString(y));337else pass();}338public static void main(String[] args) throws Throwable {339try {realMain(args);} catch (Throwable t) {unexpected(t);}340System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);341if (failed > 0) throw new AssertionError("Some tests failed");}342private static abstract class CheckedThread extends Thread {343abstract void realRun() throws Throwable;344public void run() {345try { realRun(); } catch (Throwable t) { unexpected(t); }}}346}347348349