Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/util/concurrent/ConcurrentQueues/IteratorWeakConsistency.java
+++ new/test/java/util/concurrent/ConcurrentQueues/IteratorWeakConsistency.java
1 1 /*
2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 3 *
4 4 * This code is free software; you can redistribute it and/or modify it
5 5 * under the terms of the GNU General Public License version 2 only, as
6 6 * published by the Free Software Foundation.
7 7 *
8 8 * This code is distributed in the hope that it will be useful, but WITHOUT
9 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 11 * version 2 for more details (a copy is included in the LICENSE file that
12 12 * accompanied this code).
13 13 *
14 14 * You should have received a copy of the GNU General Public License version
15 15 * 2 along with this work; if not, write to the Free Software Foundation,
16 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17 17 *
18 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19 19 * or visit www.oracle.com if you need additional information or have any
20 20 * questions.
21 21 */
22 22
23 23 /*
24 24 * This file is available under and governed by the GNU General Public
25 25 * License version 2 only, as published by the Free Software Foundation.
26 26 * However, the following notice accompanied the original version of this
27 27 * file:
28 28 *
29 29 * Written by Doug Lea with assistance from members of JCP JSR-166
30 30 * Expert Group and released to the public domain, as explained at
31 31 * http://creativecommons.org/licenses/publicdomain
32 32 */
33 33
34 34 import java.util.*;
35 35 import java.util.concurrent.*;
36 36
37 37 /*
38 38 * @test
39 39 * @bug 6805775 6815766
40 40 * @summary Check weak consistency of concurrent queue iterators
41 41 */
42 42
43 43 @SuppressWarnings({"unchecked", "rawtypes"})
44 44 public class IteratorWeakConsistency {
45 45
↓ open down ↓ |
45 lines elided |
↑ open up ↑ |
46 46 void test(String[] args) throws Throwable {
47 47 test(new LinkedBlockingQueue());
48 48 test(new LinkedBlockingQueue(20));
49 49 test(new LinkedBlockingDeque());
50 50 test(new LinkedBlockingDeque(20));
51 51 test(new ConcurrentLinkedDeque());
52 52 test(new ConcurrentLinkedQueue());
53 53 test(new LinkedTransferQueue());
54 54 // Other concurrent queues (e.g. ArrayBlockingQueue) do not
55 55 // currently have weakly consistent iterators.
56 - // test(new ArrayBlockingQueue(20));
56 + // As of 2010-09, ArrayBlockingQueue passes this test, but
57 + // does not fully implement weak consistency.
58 + test(new ArrayBlockingQueue(20));
57 59 }
58 60
59 61 void test(Queue q) {
60 62 // TODO: make this more general
61 63 try {
62 64 for (int i = 0; i < 10; i++)
63 65 q.add(i);
64 66 Iterator it = q.iterator();
65 67 q.poll();
66 68 q.poll();
67 69 q.poll();
68 70 q.remove(7);
69 71 List list = new ArrayList();
70 72 while (it.hasNext())
71 73 list.add(it.next());
72 74 equal(list, Arrays.asList(0, 3, 4, 5, 6, 8, 9));
73 75 check(! list.contains(null));
74 76 System.out.printf("%s: %s%n",
75 77 q.getClass().getSimpleName(),
76 78 list);
77 79 } catch (Throwable t) { unexpected(t); }
78 80
79 81 try {
80 82 q.clear();
81 83 q.add(1);
82 84 q.add(2);
83 85 q.add(3);
84 86 q.add(4);
85 87 Iterator it = q.iterator();
86 88 it.next();
87 89 q.remove(2);
88 90 q.remove(1);
89 91 q.remove(3);
90 92 boolean found4 = false;
91 93 while (it.hasNext()) {
92 94 found4 |= it.next().equals(4);
93 95 }
94 96 check(found4);
95 97 } catch (Throwable t) { unexpected(t); }
96 98
97 99 }
98 100
99 101 //--------------------- Infrastructure ---------------------------
100 102 volatile int passed = 0, failed = 0;
101 103 void pass() {passed++;}
102 104 void fail() {failed++; Thread.dumpStack();}
103 105 void fail(String msg) {System.err.println(msg); fail();}
104 106 void unexpected(Throwable t) {failed++; t.printStackTrace();}
105 107 void check(boolean cond) {if (cond) pass(); else fail();}
106 108 void equal(Object x, Object y) {
107 109 if (x == null ? y == null : x.equals(y)) pass();
108 110 else fail(x + " not equal to " + y);}
109 111 public static void main(String[] args) throws Throwable {
110 112 new IteratorWeakConsistency().instanceMain(args);}
111 113 public void instanceMain(String[] args) throws Throwable {
112 114 try {test(args);} catch (Throwable t) {unexpected(t);}
113 115 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
114 116 if (failed > 0) throw new AssertionError("Some tests failed");}
115 117 }
↓ open down ↓ |
49 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX