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 *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/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
3984N/A * http://creativecommons.org/publicdomain/zero/1.0/
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));
2803N/A test(new ConcurrentLinkedDeque());
1468N/A test(new ConcurrentLinkedQueue());
1771N/A test(new LinkedTransferQueue());
1468N/A // Other concurrent queues (e.g. ArrayBlockingQueue) do not
1468N/A // currently have weakly consistent iterators.
3387N/A // As of 2010-09, ArrayBlockingQueue passes this test, but
3387N/A // does not fully implement weak consistency.
3387N/A test(new ArrayBlockingQueue(20));
1468N/A }
1468N/A
3059N/A void test(Queue q) {
1468N/A // TODO: make this more general
3059N/A try {
3059N/A for (int i = 0; i < 10; i++)
3059N/A q.add(i);
3059N/A Iterator it = q.iterator();
3059N/A q.poll();
3059N/A q.poll();
3059N/A q.poll();
3059N/A q.remove(7);
3059N/A List list = new ArrayList();
3059N/A while (it.hasNext())
3059N/A list.add(it.next());
3059N/A equal(list, Arrays.asList(0, 3, 4, 5, 6, 8, 9));
3059N/A check(! list.contains(null));
3059N/A System.out.printf("%s: %s%n",
3059N/A q.getClass().getSimpleName(),
3059N/A list);
3059N/A } catch (Throwable t) { unexpected(t); }
3059N/A
3059N/A try {
3059N/A q.clear();
3059N/A q.add(1);
3059N/A q.add(2);
3059N/A q.add(3);
3059N/A q.add(4);
3059N/A Iterator it = q.iterator();
3059N/A it.next();
3059N/A q.remove(2);
3059N/A q.remove(1);
3059N/A q.remove(3);
3059N/A boolean found4 = false;
3059N/A while (it.hasNext()) {
3059N/A found4 |= it.next().equals(4);
3059N/A }
3059N/A check(found4);
3059N/A } catch (Throwable t) { unexpected(t); }
3059N/A
1468N/A }
1468N/A
1468N/A //--------------------- Infrastructure ---------------------------
1468N/A volatile int passed = 0, failed = 0;
1468N/A void pass() {passed++;}
1468N/A void fail() {failed++; Thread.dumpStack();}
1468N/A void fail(String msg) {System.err.println(msg); fail();}
1468N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
1468N/A void check(boolean cond) {if (cond) pass(); else fail();}
1468N/A void equal(Object x, Object y) {
1468N/A if (x == null ? y == null : x.equals(y)) pass();
1468N/A else fail(x + " not equal to " + y);}
1468N/A public static void main(String[] args) throws Throwable {
1468N/A new IteratorWeakConsistency().instanceMain(args);}
1468N/A public void instanceMain(String[] args) throws Throwable {
1468N/A try {test(args);} catch (Throwable t) {unexpected(t);}
1468N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
1468N/A if (failed > 0) throw new AssertionError("Some tests failed");}
1468N/A}