--- jsr166/src/test/tck/PriorityBlockingQueueTest.java 2003/09/07 20:39:11 1.2 +++ jsr166/src/test/tck/PriorityBlockingQueueTest.java 2003/09/14 20:42:40 1.3 @@ -10,14 +10,7 @@ import java.util.*; import java.util.concurrent.*; import java.io.*; -public class PriorityBlockingQueueTest extends TestCase { - - private static final int N = 10; - private static final long SHORT_DELAY_MS = 100; - private static final long MEDIUM_DELAY_MS = 1000; - private static final long LONG_DELAY_MS = 10000; - private static final int NOCAP = Integer.MAX_VALUE; - +public class PriorityBlockingQueueTest extends JSR166TestCase { public static void main(String[] args) { junit.textui.TestRunner.run (suite()); } @@ -26,6 +19,9 @@ public class PriorityBlockingQueueTest e return new TestSuite(PriorityBlockingQueueTest.class); } + private static final int NOCAP = Integer.MAX_VALUE; + + /** Sample Comparator */ static class MyReverseComparator implements Comparator { public int compare(Object x, Object y) { int i = ((Integer)x).intValue(); @@ -41,7 +37,7 @@ public class PriorityBlockingQueueTest e * Create a queue of given size containing consecutive * Integers 0 ... n. */ - private PriorityBlockingQueue fullQueue(int n) { + private PriorityBlockingQueue populatedQueue(int n) { PriorityBlockingQueue q = new PriorityBlockingQueue(n); assertTrue(q.isEmpty()); for(int i = n-1; i >= 0; i-=2) @@ -55,7 +51,7 @@ public class PriorityBlockingQueueTest e } public void testConstructor1(){ - assertEquals(NOCAP, new PriorityBlockingQueue(N).remainingCapacity()); + assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity()); } public void testConstructor2(){ @@ -77,7 +73,7 @@ public class PriorityBlockingQueueTest e public void testConstructor4(){ try { - Integer[] ints = new Integer[N]; + Integer[] ints = new Integer[SIZE]; PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); fail("Cannot make with null elements"); } @@ -86,8 +82,8 @@ public class PriorityBlockingQueueTest e public void testConstructor5(){ try { - Integer[] ints = new Integer[N]; - for (int i = 0; i < N-1; ++i) + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE-1; ++i) ints[i] = new Integer(i); PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); fail("Cannot make with null elements"); @@ -97,11 +93,11 @@ public class PriorityBlockingQueueTest e public void testConstructor6(){ try { - Integer[] ints = new Integer[N]; - for (int i = 0; i < N; ++i) + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) ints[i] = new Integer(i); PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); - for (int i = 0; i < N; ++i) + for (int i = 0; i < SIZE; ++i) assertEquals(ints[i], q.poll()); } finally {} @@ -109,12 +105,12 @@ public class PriorityBlockingQueueTest e public void testConstructor7(){ try { - PriorityBlockingQueue q = new PriorityBlockingQueue(N, new MyReverseComparator()); - Integer[] ints = new Integer[N]; - for (int i = 0; i < N; ++i) + PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, new MyReverseComparator()); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); - for (int i = N-1; i >= 0; --i) + for (int i = SIZE-1; i >= 0; --i) assertEquals(ints[i], q.poll()); } finally {} @@ -133,13 +129,13 @@ public class PriorityBlockingQueueTest e } public void testRemainingCapacity(){ - PriorityBlockingQueue q = fullQueue(N); - for (int i = 0; i < N; ++i) { + PriorityBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { assertEquals(NOCAP, q.remainingCapacity()); - assertEquals(N-i, q.size()); + assertEquals(SIZE-i, q.size()); q.remove(); } - for (int i = 0; i < N; ++i) { + for (int i = 0; i < SIZE; ++i) { assertEquals(NOCAP, q.remainingCapacity()); assertEquals(i, q.size()); q.add(new Integer(i)); @@ -172,8 +168,8 @@ public class PriorityBlockingQueueTest e } public void testAdd(){ - PriorityBlockingQueue q = new PriorityBlockingQueue(N); - for (int i = 0; i < N; ++i) { + PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.size()); assertTrue(q.add(new Integer(i))); } @@ -189,8 +185,8 @@ public class PriorityBlockingQueueTest e } public void testAddAll2(){ try { - PriorityBlockingQueue q = new PriorityBlockingQueue(N); - Integer[] ints = new Integer[N]; + PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); + Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); fail("Cannot add null elements"); } @@ -198,9 +194,9 @@ public class PriorityBlockingQueueTest e } public void testAddAll3(){ try { - PriorityBlockingQueue q = new PriorityBlockingQueue(N); - Integer[] ints = new Integer[N]; - for (int i = 0; i < N-1; ++i) + PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE-1; ++i) ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); fail("Cannot add null elements"); @@ -211,13 +207,13 @@ public class PriorityBlockingQueueTest e public void testAddAll5(){ try { Integer[] empty = new Integer[0]; - Integer[] ints = new Integer[N]; - for (int i = N-1; i >= 0; --i) + Integer[] ints = new Integer[SIZE]; + for (int i = SIZE-1; i >= 0; --i) ints[i] = new Integer(i); - PriorityBlockingQueue q = new PriorityBlockingQueue(N); + PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); assertFalse(q.addAll(Arrays.asList(empty))); assertTrue(q.addAll(Arrays.asList(ints))); - for (int i = 0; i < N; ++i) + for (int i = 0; i < SIZE; ++i) assertEquals(ints[i], q.poll()); } finally {} @@ -225,7 +221,7 @@ public class PriorityBlockingQueueTest e public void testPutNull() { try { - PriorityBlockingQueue q = new PriorityBlockingQueue(N); + PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); q.put(null); fail("put should throw NPE"); } @@ -235,13 +231,13 @@ public class PriorityBlockingQueueTest e public void testPut() { try { - PriorityBlockingQueue q = new PriorityBlockingQueue(N); - for (int i = 0; i < N; ++i) { + PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { Integer I = new Integer(i); q.put(I); assertTrue(q.contains(I)); } - assertEquals(N, q.size()); + assertEquals(SIZE, q.size()); } finally { } @@ -261,7 +257,7 @@ public class PriorityBlockingQueueTest e ++added; q.put(new Integer(0)); ++added; - assertTrue(added == 4); + threadAssertTrue(added == 4); } finally { } } @@ -284,15 +280,15 @@ public class PriorityBlockingQueueTest e try { q.put(new Integer(0)); q.put(new Integer(0)); - assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); - assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); + threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); } finally { } } }); try { t.start(); - Thread.sleep(SHORT_DELAY_MS); + Thread.sleep(SMALL_DELAY_MS); t.interrupt(); t.join(); } catch (Exception e){ @@ -302,8 +298,8 @@ public class PriorityBlockingQueueTest e public void testTake(){ try { - PriorityBlockingQueue q = fullQueue(N); - for (int i = 0; i < N; ++i) { + PriorityBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { assertEquals(i, ((Integer)q.take()).intValue()); } } catch (InterruptedException e){ @@ -317,7 +313,7 @@ public class PriorityBlockingQueueTest e public void run(){ try { q.take(); - fail("Should block"); + threadFail("Should block"); } catch (InterruptedException success){ } } }); @@ -335,12 +331,12 @@ public class PriorityBlockingQueueTest e Thread t = new Thread(new Runnable() { public void run() { try { - PriorityBlockingQueue q = fullQueue(N); - for (int i = 0; i < N; ++i) { - assertEquals(i, ((Integer)q.take()).intValue()); + PriorityBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + threadAssertEquals(i, ((Integer)q.take()).intValue()); } q.take(); - fail("take should block"); + threadFail("take should block"); } catch (InterruptedException success){ } }}); @@ -357,8 +353,8 @@ public class PriorityBlockingQueueTest e public void testPoll(){ - PriorityBlockingQueue q = fullQueue(N); - for (int i = 0; i < N; ++i) { + PriorityBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { assertEquals(i, ((Integer)q.poll()).intValue()); } assertNull(q.poll()); @@ -366,8 +362,8 @@ public class PriorityBlockingQueueTest e public void testTimedPoll0() { try { - PriorityBlockingQueue q = fullQueue(N); - for (int i = 0; i < N; ++i) { + PriorityBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); } assertNull(q.poll(0, TimeUnit.MILLISECONDS)); @@ -378,8 +374,8 @@ public class PriorityBlockingQueueTest e public void testTimedPoll() { try { - PriorityBlockingQueue q = fullQueue(N); - for (int i = 0; i < N; ++i) { + PriorityBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); } assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); @@ -392,11 +388,11 @@ public class PriorityBlockingQueueTest e Thread t = new Thread(new Runnable() { public void run() { try { - PriorityBlockingQueue q = fullQueue(N); - for (int i = 0; i < N; ++i) { - assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); + PriorityBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); } - assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); } catch (InterruptedException success){ } }}); @@ -416,16 +412,16 @@ public class PriorityBlockingQueueTest e Thread t = new Thread(new Runnable() { public void run(){ try { - assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - fail("Should block"); + threadFail("Should block"); } catch (InterruptedException success) { } } }); try { t.start(); - Thread.sleep(SHORT_DELAY_MS * 2); + Thread.sleep(SMALL_DELAY_MS); assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); t.interrupt(); t.join(); @@ -436,8 +432,8 @@ public class PriorityBlockingQueueTest e public void testPeek(){ - PriorityBlockingQueue q = fullQueue(N); - for (int i = 0; i < N; ++i) { + PriorityBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { assertEquals(i, ((Integer)q.peek()).intValue()); q.poll(); assertTrue(q.peek() == null || @@ -447,8 +443,8 @@ public class PriorityBlockingQueueTest e } public void testElement(){ - PriorityBlockingQueue q = fullQueue(N); - for (int i = 0; i < N; ++i) { + PriorityBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { assertEquals(i, ((Integer)q.element()).intValue()); q.poll(); } @@ -460,8 +456,8 @@ public class PriorityBlockingQueueTest e } public void testRemove(){ - PriorityBlockingQueue q = fullQueue(N); - for (int i = 0; i < N; ++i) { + PriorityBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { assertEquals(i, ((Integer)q.remove()).intValue()); } try { @@ -472,11 +468,11 @@ public class PriorityBlockingQueueTest e } public void testRemoveElement(){ - PriorityBlockingQueue q = fullQueue(N); - for (int i = 1; i < N; i+=2) { + PriorityBlockingQueue q = populatedQueue(SIZE); + for (int i = 1; i < SIZE; i+=2) { assertTrue(q.remove(new Integer(i))); } - for (int i = 0; i < N; i+=2) { + for (int i = 0; i < SIZE; i+=2) { assertTrue(q.remove(new Integer(i))); assertFalse(q.remove(new Integer(i+1))); } @@ -484,8 +480,8 @@ public class PriorityBlockingQueueTest e } public void testContains(){ - PriorityBlockingQueue q = fullQueue(N); - for (int i = 0; i < N; ++i) { + PriorityBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { assertTrue(q.contains(new Integer(i))); q.poll(); assertFalse(q.contains(new Integer(i))); @@ -493,7 +489,7 @@ public class PriorityBlockingQueueTest e } public void testClear(){ - PriorityBlockingQueue q = fullQueue(N); + PriorityBlockingQueue q = populatedQueue(SIZE); q.clear(); assertTrue(q.isEmpty()); assertEquals(0, q.size()); @@ -505,9 +501,9 @@ public class PriorityBlockingQueueTest e } public void testContainsAll(){ - PriorityBlockingQueue q = fullQueue(N); - PriorityBlockingQueue p = new PriorityBlockingQueue(N); - for (int i = 0; i < N; ++i) { + PriorityBlockingQueue q = populatedQueue(SIZE); + PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { assertTrue(q.containsAll(p)); assertFalse(p.containsAll(q)); p.add(new Integer(i)); @@ -516,9 +512,9 @@ public class PriorityBlockingQueueTest e } public void testRetainAll(){ - PriorityBlockingQueue q = fullQueue(N); - PriorityBlockingQueue p = fullQueue(N); - for (int i = 0; i < N; ++i) { + PriorityBlockingQueue q = populatedQueue(SIZE); + PriorityBlockingQueue p = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { boolean changed = q.retainAll(p); if (i == 0) assertFalse(changed); @@ -526,17 +522,17 @@ public class PriorityBlockingQueueTest e assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(N-i, q.size()); + assertEquals(SIZE-i, q.size()); p.remove(); } } public void testRemoveAll(){ - for (int i = 1; i < N; ++i) { - PriorityBlockingQueue q = fullQueue(N); - PriorityBlockingQueue p = fullQueue(i); + for (int i = 1; i < SIZE; ++i) { + PriorityBlockingQueue q = populatedQueue(SIZE); + PriorityBlockingQueue p = populatedQueue(i); assertTrue(q.removeAll(p)); - assertEquals(N-i, q.size()); + assertEquals(SIZE-i, q.size()); for (int j = 0; j < i; ++j) { Integer I = (Integer)(p.remove()); assertFalse(q.contains(I)); @@ -545,7 +541,7 @@ public class PriorityBlockingQueueTest e } public void testToArray(){ - PriorityBlockingQueue q = fullQueue(N); + PriorityBlockingQueue q = populatedQueue(SIZE); Object[] o = q.toArray(); Arrays.sort(o); try { @@ -557,8 +553,8 @@ public class PriorityBlockingQueueTest e } public void testToArray2(){ - PriorityBlockingQueue q = fullQueue(N); - Integer[] ints = new Integer[N]; + PriorityBlockingQueue q = populatedQueue(SIZE); + Integer[] ints = new Integer[SIZE]; ints = (Integer[])q.toArray(ints); Arrays.sort(ints); try { @@ -570,14 +566,14 @@ public class PriorityBlockingQueueTest e } public void testIterator(){ - PriorityBlockingQueue q = fullQueue(N); + PriorityBlockingQueue q = populatedQueue(SIZE); int i = 0; Iterator it = q.iterator(); while(it.hasNext()) { assertTrue(q.contains(it.next())); ++i; } - assertEquals(i, N); + assertEquals(i, SIZE); } public void testIteratorRemove () { @@ -600,9 +596,9 @@ public class PriorityBlockingQueueTest e public void testToString(){ - PriorityBlockingQueue q = fullQueue(N); + PriorityBlockingQueue q = populatedQueue(SIZE); String s = q.toString(); - for (int i = 0; i < N; ++i) { + for (int i = 0; i < SIZE; ++i) { assertTrue(s.indexOf(String.valueOf(i)) >= 0); } } @@ -615,13 +611,13 @@ public class PriorityBlockingQueueTest e executor.execute(new Runnable() { public void run() { - assertNull("poll should fail", q.poll()); + threadAssertNull(q.poll()); try { - assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); - assertTrue(q.isEmpty()); + threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); + threadAssertTrue(q.isEmpty()); } catch (InterruptedException e) { - fail("should not be interrupted"); + threadFail("should not be interrupted"); } } }); @@ -629,21 +625,21 @@ public class PriorityBlockingQueueTest e executor.execute(new Runnable() { public void run() { try { - Thread.sleep(MEDIUM_DELAY_MS); + Thread.sleep(SMALL_DELAY_MS); q.put(new Integer(1)); } catch (InterruptedException e) { - fail("should not be interrupted"); + threadFail("should not be interrupted"); } } }); - executor.shutdown(); + joinPool(executor); } public void testSerialization() { - PriorityBlockingQueue q = fullQueue(N); + PriorityBlockingQueue q = populatedQueue(SIZE); try { ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));