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
↓ open down ↓ 48 lines elided ↑ open up ↑
  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   56          // test(new ArrayBlockingQueue(20));
  57   57      }
  58   58  
  59      -    void test(Queue q) throws Throwable {
       59 +    void test(Queue q) {
  60   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);
       61 +        try {
       62 +            for (int i = 0; i < 10; i++)
       63 +                q.add(i);
       64 +            Iterator it = q.iterator();
       65 +            q.poll();
       66 +            q.poll();
       67 +            q.poll();
       68 +            q.remove(7);
       69 +            List list = new ArrayList();
       70 +            while (it.hasNext())
       71 +                list.add(it.next());
       72 +            equal(list, Arrays.asList(0, 3, 4, 5, 6, 8, 9));
       73 +            check(! list.contains(null));
       74 +            System.out.printf("%s: %s%n",
       75 +                              q.getClass().getSimpleName(),
       76 +                              list);
       77 +        } catch (Throwable t) { unexpected(t); }
       78 +
       79 +        try {
       80 +            q.clear();
       81 +            q.add(1);
       82 +            q.add(2);
       83 +            q.add(3);
       84 +            q.add(4);
       85 +            Iterator it = q.iterator();
       86 +            it.next();
       87 +            q.remove(2);
       88 +            q.remove(1);
       89 +            q.remove(3);
       90 +            boolean found4 = false;
       91 +            while (it.hasNext()) {
       92 +                found4 |= it.next().equals(4);
       93 +            }
       94 +            check(found4);
       95 +        } catch (Throwable t) { unexpected(t); }
       96 +
  76   97      }
  77   98  
  78   99      //--------------------- Infrastructure ---------------------------
  79  100      volatile int passed = 0, failed = 0;
  80  101      void pass() {passed++;}
  81  102      void fail() {failed++; Thread.dumpStack();}
  82  103      void fail(String msg) {System.err.println(msg); fail();}
  83  104      void unexpected(Throwable t) {failed++; t.printStackTrace();}
  84  105      void check(boolean cond) {if (cond) pass(); else fail();}
  85  106      void equal(Object x, Object y) {
  86  107          if (x == null ? y == null : x.equals(y)) pass();
  87  108          else fail(x + " not equal to " + y);}
  88      -    static Class<?> thisClass = new Object(){}.getClass().getEnclosingClass();
  89  109      public static void main(String[] args) throws Throwable {
  90  110          new IteratorWeakConsistency().instanceMain(args);}
  91  111      public void instanceMain(String[] args) throws Throwable {
  92  112          try {test(args);} catch (Throwable t) {unexpected(t);}
  93  113          System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
  94  114          if (failed > 0) throw new AssertionError("Some tests failed");}
  95  115  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX