--- jsr166/src/test/tck/PriorityBlockingQueueTest.java 2003/08/31 19:24:55 1.1 +++ jsr166/src/test/tck/PriorityBlockingQueueTest.java 2003/09/20 00:31:57 1.4 @@ -8,15 +8,9 @@ import junit.framework.*; 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()); } @@ -25,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(); @@ -40,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) @@ -54,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(){ @@ -76,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"); } @@ -85,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"); @@ -96,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 {} @@ -108,12 +105,14 @@ 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) + MyReverseComparator cmp = new MyReverseComparator(); + PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp); + assertEquals(cmp, q.comparator()); + 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 {} @@ -132,13 +131,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)); @@ -171,8 +170,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))); } @@ -188,8 +187,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"); } @@ -197,9 +196,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"); @@ -210,13 +209,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 {} @@ -224,7 +223,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"); } @@ -234,13 +233,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 { } @@ -260,7 +259,7 @@ public class PriorityBlockingQueueTest e ++added; q.put(new Integer(0)); ++added; - assertTrue(added == 4); + threadAssertTrue(added == 4); } finally { } } @@ -283,15 +282,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){ @@ -301,8 +300,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){ @@ -316,7 +315,7 @@ public class PriorityBlockingQueueTest e public void run(){ try { q.take(); - fail("Should block"); + threadFail("Should block"); } catch (InterruptedException success){ } } }); @@ -334,12 +333,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){ } }}); @@ -356,8 +355,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()); @@ -365,8 +364,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)); @@ -377,8 +376,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)); @@ -391,11 +390,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){ } }}); @@ -415,16 +414,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(); @@ -435,8 +434,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 || @@ -446,8 +445,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(); } @@ -459,8 +458,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 { @@ -471,20 +470,20 @@ 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))); } - assert(q.isEmpty()); + assertTrue(q.isEmpty()); } 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))); @@ -492,7 +491,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()); @@ -504,9 +503,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)); @@ -515,9 +514,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); @@ -525,17 +524,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)); @@ -544,7 +543,7 @@ public class PriorityBlockingQueueTest e } public void testToArray(){ - PriorityBlockingQueue q = fullQueue(N); + PriorityBlockingQueue q = populatedQueue(SIZE); Object[] o = q.toArray(); Arrays.sort(o); try { @@ -556,8 +555,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 { @@ -569,14 +568,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 () { @@ -599,9 +598,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); } } @@ -614,13 +613,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"); } } }); @@ -628,17 +627,36 @@ 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 = populatedQueue(SIZE); + try { + ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); + ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); + out.writeObject(q); + out.close(); + + ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); + ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); + PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject(); + assertEquals(q.size(), r.size()); + while (!q.isEmpty()) + assertEquals(q.remove(), r.remove()); + } catch(Exception e){ + fail("unexpected exception"); + } } }