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