test/java/util/concurrent/ConcurrentQueues/IteratorWeakConsistency.java
Print this page
@@ -54,12 +54,13 @@
// Other concurrent queues (e.g. ArrayBlockingQueue) do not
// currently have weakly consistent iterators.
// test(new ArrayBlockingQueue(20));
}
- void test(Queue q) throws Throwable {
+ void test(Queue q) {
// TODO: make this more general
+ try {
for (int i = 0; i < 10; i++)
q.add(i);
Iterator it = q.iterator();
q.poll();
q.poll();
@@ -71,12 +72,32 @@
equal(list, Arrays.asList(0, 3, 4, 5, 6, 8, 9));
check(! list.contains(null));
System.out.printf("%s: %s%n",
q.getClass().getSimpleName(),
list);
+ } catch (Throwable t) { unexpected(t); }
+
+ try {
+ q.clear();
+ q.add(1);
+ q.add(2);
+ q.add(3);
+ q.add(4);
+ Iterator it = q.iterator();
+ it.next();
+ q.remove(2);
+ q.remove(1);
+ q.remove(3);
+ boolean found4 = false;
+ while (it.hasNext()) {
+ found4 |= it.next().equals(4);
}
+ 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();}
@@ -83,11 +104,10 @@
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);}
- static Class<?> thisClass = new Object(){}.getClass().getEnclosingClass();
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);