Path: blob/master/test/jdk/java/util/Collection/HotPotatoes.java
41149 views
/*1* Copyright (c) 2005, 2006, 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 6355660 6347106 639400426* @summary methods taking concurrently mutating collection should work27* @author Martin Buchholz28*/2930import java.lang.reflect.Constructor;31import java.util.ArrayList;32import java.util.Arrays;33import java.util.Collection;34import java.util.Collections;35import java.util.List;36import java.util.PriorityQueue;37import java.util.Vector;38import java.util.concurrent.CopyOnWriteArrayList;39import java.util.concurrent.PriorityBlockingQueue;4041@SuppressWarnings("unchecked")42public class HotPotatoes {43private static void realMain(String[] args) throws Throwable {44testImplementation(Vector.class);45testImplementation(ArrayList.class);46testImplementation(PriorityQueue.class);47testImplementation(PriorityBlockingQueue.class);48}4950private static void testImplementation(Class<? extends Collection> implClazz)51throws Throwable52{53testPotato(implClazz, Vector.class);54testPotato(implClazz, CopyOnWriteArrayList.class);5556final Constructor<? extends Collection> constr57= implClazz.getConstructor(Collection.class);58final Collection<Object> coll59= constr.newInstance(Arrays.asList(new String[] {}));60coll.add(1);61equal(coll.toString(), "[1]");62}6364private static void testPotato(Class<? extends Collection> implClazz,65Class<? extends List> argClazz)66throws Throwable67{68try {69System.out.printf("implClazz=%s, argClazz=%s\n",70implClazz.getName(), argClazz.getName());71final int iterations = 100000;72final List<Integer> list = (List<Integer>)73argClazz.getDeclaredConstructor().newInstance();74final Integer one = Integer.valueOf(1);75final List<Integer> oneElementList = Collections.singletonList(one);76final Constructor<? extends Collection> constr77= implClazz.getConstructor(Collection.class);78final Thread t = new CheckedThread() { public void realRun() {79for (int i = 0; i < iterations; i++) {80list.add(one);81list.remove(one);82}}};83t.setDaemon(true);84t.start();8586for (int i = 0; i < iterations; i++) {87Collection<?> coll = constr.newInstance(list);88Object[] elts = coll.toArray();89check(elts.length == 0 ||90(elts.length == 1 && elts[0] == one));91}92} catch (Throwable t) { unexpected(t); }93}9495//--------------------- Infrastructure ---------------------------96static volatile int passed = 0, failed = 0;97static void pass() {passed++;}98static void fail() {failed++; Thread.dumpStack();}99static void fail(String msg) {System.out.println(msg); fail();}100static void unexpected(Throwable t) {failed++; t.printStackTrace();}101static void check(boolean cond) {if (cond) pass(); else fail();}102static void equal(Object x, Object y) {103if (x == null ? y == null : x.equals(y)) pass();104else fail(x + " not equal to " + y);}105public static void main(String[] args) throws Throwable {106try {realMain(args);} catch (Throwable t) {unexpected(t);}107System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);108if (failed > 0) throw new AssertionError("Some tests failed");}109private abstract static class CheckedThread extends Thread {110public abstract void realRun() throws Throwable;111public void run() {112try { realRun(); } catch (Throwable t) { unexpected(t); }}}113}114115116