1 /* 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 it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 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 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 */ 22 23 /* 24 * This file is available under and governed by the GNU General Public 25 * License version 2 only, as published by the Free Software Foundation. 26 * However, the following notice accompanied the original version of this 27 * file: 28 * 29 * Written by Doug Lea with assistance from members of JCP JSR-166 30 * Expert Group and released to the public domain, as explained at 31 * http://creativecommons.org/licenses/publicdomain 32 */ 33 34 import java.util.*; 35 import java.util.concurrent.*; 36 37 /* 38 * @test 39 * @bug 6805775 6815766 40 * @summary Check weak consistency of concurrent queue iterators 41 */ 42 43 @SuppressWarnings({"unchecked", "rawtypes"}) 44 public class IteratorWeakConsistency { 45 46 void test(String[] args) throws Throwable { 47 test(new LinkedBlockingQueue()); 48 test(new LinkedBlockingQueue(20)); 49 test(new LinkedBlockingDeque()); 50 test(new LinkedBlockingDeque(20)); 51 test(new ConcurrentLinkedDeque()); 52 test(new ConcurrentLinkedQueue()); 53 test(new LinkedTransferQueue()); 54 // Other concurrent queues (e.g. ArrayBlockingQueue) do not 55 // currently have weakly consistent iterators. 56 // test(new ArrayBlockingQueue(20)); 57 } 58 59 void test(Queue q) throws Throwable { 60 // TODO: make this more general 61 for (int i = 0; i < 10; i++) 62 q.add(i); 63 Iterator it = q.iterator(); 64 q.poll(); 65 q.poll(); 66 q.poll(); 67 q.remove(7); 68 List list = new ArrayList(); 69 while (it.hasNext()) 70 list.add(it.next()); 71 equal(list, Arrays.asList(0, 3, 4, 5, 6, 8, 9)); 72 check(! list.contains(null)); 73 System.out.printf("%s: %s%n", 74 q.getClass().getSimpleName(), 75 list); 76 } 77 78 //--------------------- Infrastructure --------------------------- 79 volatile int passed = 0, failed = 0; 80 void pass() {passed++;} 81 void fail() {failed++; Thread.dumpStack();} 82 void fail(String msg) {System.err.println(msg); fail();} 83 void unexpected(Throwable t) {failed++; t.printStackTrace();} 84 void check(boolean cond) {if (cond) pass(); else fail();} 85 void equal(Object x, Object y) { 86 if (x == null ? y == null : x.equals(y)) pass(); 87 else fail(x + " not equal to " + y);} 88 static Class<?> thisClass = new Object(){}.getClass().getEnclosingClass(); 89 public static void main(String[] args) throws Throwable { 90 new IteratorWeakConsistency().instanceMain(args);} 91 public void instanceMain(String[] args) throws Throwable { 92 try {test(args);} catch (Throwable t) {unexpected(t);} 93 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 94 if (failed > 0) throw new AssertionError("Some tests failed");} 95 }