IteratorWeakConsistency.java revision 3059
1468N/A/*
1468N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1468N/A *
1468N/A * This code is free software; you can redistribute it and/or modify it
1468N/A * under the terms of the GNU General Public License version 2 only, as
1468N/A * published by the Free Software Foundation.
1468N/A *
1468N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1468N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1468N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1468N/A * version 2 for more details (a copy is included in the LICENSE file that
1468N/A * accompanied this code).
1468N/A *
1468N/A * You should have received a copy of the GNU General Public License version
1468N/A * 2 along with this work; if not, write to the Free Software Foundation,
1468N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1468N/A *
1468N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1468N/A * or visit www.oracle.com if you need additional information or have any
1468N/A * questions.
1468N/A */
1468N/A
1468N/A/*
1468N/A * This file is available under and governed by the GNU General Public
1468N/A * License version 2 only, as published by the Free Software Foundation.
1468N/A * However, the following notice accompanied the original version of this
1468N/A * file:
1468N/A *
1468N/A * Written by Doug Lea with assistance from members of JCP JSR-166
1468N/A * Expert Group and released to the public domain, as explained at
1468N/A * http://creativecommons.org/licenses/publicdomain
1468N/A */
1468N/A
1468N/Aimport java.util.*;
1468N/Aimport java.util.concurrent.*;
1468N/A
1468N/A/*
1468N/A * @test
1468N/A * @bug 6805775 6815766
1468N/A * @summary Check weak consistency of concurrent queue iterators
1468N/A */
1468N/A
1468N/A@SuppressWarnings({"unchecked", "rawtypes"})
1468N/Apublic class IteratorWeakConsistency {
1468N/A
1468N/A void test(String[] args) throws Throwable {
1468N/A test(new LinkedBlockingQueue());
1468N/A test(new LinkedBlockingQueue(20));
1468N/A test(new LinkedBlockingDeque());
1468N/A test(new LinkedBlockingDeque(20));
1468N/A test(new ConcurrentLinkedDeque());
1771N/A test(new ConcurrentLinkedQueue());
1468N/A test(new LinkedTransferQueue());
1468N/A // Other concurrent queues (e.g. ArrayBlockingQueue) do not
1468N/A // currently have weakly consistent iterators.
1468N/A // test(new ArrayBlockingQueue(20));
1468N/A }
1468N/A
1468N/A void test(Queue q) {
1468N/A // TODO: make this more general
1468N/A try {
1468N/A for (int i = 0; i < 10; i++)
1468N/A q.add(i);
1468N/A Iterator it = q.iterator();
1468N/A q.poll();
1468N/A q.poll();
1468N/A q.poll();
1468N/A q.remove(7);
1468N/A List list = new ArrayList();
1468N/A while (it.hasNext())
1468N/A list.add(it.next());
1468N/A equal(list, Arrays.asList(0, 3, 4, 5, 6, 8, 9));
1468N/A check(! list.contains(null));
1468N/A System.out.printf("%s: %s%n",
1468N/A q.getClass().getSimpleName(),
1468N/A list);
1468N/A } catch (Throwable t) { unexpected(t); }
1468N/A
1468N/A try {
1468N/A q.clear();
1468N/A q.add(1);
1468N/A q.add(2);
1468N/A q.add(3);
1468N/A q.add(4);
1468N/A Iterator it = q.iterator();
1468N/A it.next();
1468N/A q.remove(2);
1468N/A q.remove(1);
1468N/A q.remove(3);
1468N/A boolean found4 = false;
1468N/A while (it.hasNext()) {
1468N/A found4 |= it.next().equals(4);
1468N/A }
1468N/A check(found4);
} catch (Throwable t) { unexpected(t); }
}
//--------------------- Infrastructure ---------------------------
volatile int passed = 0, failed = 0;
void pass() {passed++;}
void fail() {failed++; Thread.dumpStack();}
void fail(String msg) {System.err.println(msg); fail();}
void unexpected(Throwable t) {failed++; t.printStackTrace();}
void check(boolean cond) {if (cond) pass(); else fail();}
void equal(Object x, Object y) {
if (x == null ? y == null : x.equals(y)) pass();
else fail(x + " not equal to " + y);}
public static void main(String[] args) throws Throwable {
new IteratorWeakConsistency().instanceMain(args);}
public void instanceMain(String[] args) throws Throwable {
try {test(args);} catch (Throwable t) {unexpected(t);}
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new AssertionError("Some tests failed");}
}