Print this page


Split Close
Expand all
Collapse all
          --- old/test/java/util/concurrent/BlockingQueue/LastElement.java
          +++ new/test/java/util/concurrent/BlockingQueue/LastElement.java
   1    1  /*
   2      - * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
        2 + * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
   3    3   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4    4   *
   5    5   * This code is free software; you can redistribute it and/or modify it
   6    6   * under the terms of the GNU General Public License version 2 only, as
   7    7   * published by the Free Software Foundation.
   8    8   *
   9    9   * This code is distributed in the hope that it will be useful, but WITHOUT
  10   10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11   11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12   12   * version 2 for more details (a copy is included in the LICENSE file that
↓ open down ↓ 3 lines elided ↑ open up ↑
  16   16   * 2 along with this work; if not, write to the Free Software Foundation,
  17   17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18   18   *
  19   19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20   20   * or visit www.oracle.com if you need additional information or have any
  21   21   * questions.
  22   22   */
  23   23  
  24   24  /*
  25   25   * @test
  26      - * @bug 6215625
       26 + * @bug 6215625 7161229
  27   27   * @summary Check correct behavior when last element is removed.
  28   28   * @author Martin Buchholz
  29   29   */
  30   30  
  31   31  import java.util.*;
  32   32  import java.util.concurrent.*;
  33   33  
  34   34  public class LastElement {
  35   35      void test(String[] args) throws Throwable {
  36   36          testQueue(new LinkedBlockingQueue<Integer>());
  37   37          testQueue(new LinkedBlockingDeque<Integer>());
  38   38          testQueue(new ArrayBlockingQueue<Integer>(10, true));
  39   39          testQueue(new ArrayBlockingQueue<Integer>(10, false));
  40   40          testQueue(new LinkedTransferQueue<Integer>());
  41      -
  42      -        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
  43      -        if (failed > 0) throw new Exception("Some tests failed");
       41 +        testQueue(new PriorityBlockingQueue<Integer>());
  44   42      }
  45   43  
  46   44      void testQueue(BlockingQueue<Integer> q) throws Throwable {
  47   45          Integer one = 1;
  48   46          Integer two = 2;
  49   47          Integer three = 3;
  50   48  
  51   49          // remove(Object)
  52   50          q.put(one);
  53   51          q.put(two);
  54   52          check(! q.isEmpty() && q.size() == 2);
  55   53          check(q.remove(one));
  56   54          check(q.remove(two));
  57   55          check(q.isEmpty() && q.size() == 0);
  58   56          q.put(three);
  59   57          try {check(q.take() == three);}
  60   58          catch (Throwable t) {unexpected(t);}
  61   59          check(q.isEmpty() && q.size() == 0);
       60 +        check(noRetention(q));
  62   61  
  63   62          // iterator().remove()
  64   63          q.clear();
  65   64          q.put(one);
  66   65          check(q.offer(two));
  67   66          check(! q.isEmpty() && q.size() == 2);
  68   67          Iterator<Integer> i = q.iterator();
  69   68          check(i.next() == one);
  70   69          i.remove();
  71   70          check(i.next() == two);
  72   71          i.remove();
  73   72          check(q.isEmpty() && q.size() == 0);
  74   73          q.put(three);
  75   74          try {check(q.take() == three);}
  76   75          catch (Throwable t) {unexpected(t);}
  77   76          check(q.isEmpty() && q.size() == 0);
  78   77      }
  79   78  
       79 +    boolean noRetention(BlockingQueue<?> q) {
       80 +        if (q instanceof PriorityBlockingQueue) {
       81 +            PriorityBlockingQueue<?> pbq = (PriorityBlockingQueue) q;
       82 +            try {
       83 +                java.lang.reflect.Field queue =
       84 +                    PriorityBlockingQueue.class.getDeclaredField("queue");
       85 +                queue.setAccessible(true);
       86 +                Object[] a = (Object[]) queue.get(pbq);
       87 +                return a[0] == null;
       88 +            }
       89 +            catch (NoSuchFieldException e) {
       90 +                unexpected(e);
       91 +            }
       92 +            catch (java.security.AccessControlException | IllegalAccessException e) {
       93 +                // ignore - security manager must be installed
       94 +            }
       95 +        }
       96 +        return true;
       97 +    }
       98 +
  80   99      //--------------------- Infrastructure ---------------------------
  81  100      volatile int passed = 0, failed = 0;
  82  101      void pass() {passed++;}
  83  102      void fail() {failed++; Thread.dumpStack();}
  84  103      void fail(String msg) {System.err.println(msg); fail();}
  85  104      void unexpected(Throwable t) {failed++; t.printStackTrace();}
  86  105      void check(boolean cond) {if (cond) pass(); else fail();}
  87  106      void equal(Object x, Object y) {
  88  107          if (x == null ? y == null : x.equals(y)) pass();
  89  108          else fail(x + " not equal to " + y);}
  90  109      public static void main(String[] args) throws Throwable {
  91  110          new LastElement().instanceMain(args);}
  92  111      public void instanceMain(String[] args) throws Throwable {
  93  112          try {test(args);} catch (Throwable t) {unexpected(t);}
  94  113          System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
  95  114          if (failed > 0) throw new AssertionError("Some tests failed");}
  96  115  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX