--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2003/12/29 19:05:40 1.8 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2009/11/21 19:11:53 1.21 @@ -2,22 +2,23 @@ * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/licenses/publicdomain - * Other contributors include Andrew Wright, Jeffrey Hayes, - * Pat Fisher, Mike Judd. + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.io.*; public class ArrayBlockingQueueTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run (suite()); } public static Test suite() { - return new TestSuite(ArrayBlockingQueueTest.class); + return new TestSuite(ArrayBlockingQueueTest.class); } /** @@ -27,14 +28,14 @@ public class ArrayBlockingQueueTest exte private ArrayBlockingQueue populatedQueue(int n) { ArrayBlockingQueue q = new ArrayBlockingQueue(n); assertTrue(q.isEmpty()); - for(int i = 0; i < n; i++) - assertTrue(q.offer(new Integer(i))); + for (int i = 0; i < n; i++) + assertTrue(q.offer(new Integer(i))); assertFalse(q.isEmpty()); assertEquals(0, q.remainingCapacity()); - assertEquals(n, q.size()); + assertEquals(n, q.size()); return q; } - + /** * A new queue has the indicated capacity */ @@ -43,14 +44,13 @@ public class ArrayBlockingQueueTest exte } /** - * Constructor throws IAE if capacity argument nonpositive + * Constructor throws IAE if capacity argument nonpositive */ public void testConstructor2() { try { ArrayBlockingQueue q = new ArrayBlockingQueue(0); shouldThrow(); - } - catch (IllegalArgumentException success) {} + } catch (IllegalArgumentException success) {} } /** @@ -60,8 +60,7 @@ public class ArrayBlockingQueueTest exte try { ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** @@ -72,8 +71,7 @@ public class ArrayBlockingQueueTest exte Integer[] ints = new Integer[SIZE]; ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** @@ -86,8 +84,7 @@ public class ArrayBlockingQueueTest exte ints[i] = new Integer(i); ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** @@ -100,23 +97,19 @@ public class ArrayBlockingQueueTest exte ints[i] = new Integer(i); ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints)); shouldThrow(); - } - catch (IllegalArgumentException success) {} + } catch (IllegalArgumentException success) {} } /** * Queue contains all elements of collection used to initialize */ public void testConstructor7() { - try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); - for (int i = 0; i < SIZE; ++i) - assertEquals(ints[i], q.poll()); - } - finally {} + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = new Integer(i); + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); + for (int i = 0; i < SIZE; ++i) + assertEquals(ints[i], q.poll()); } /** @@ -155,22 +148,22 @@ public class ArrayBlockingQueueTest exte * offer(null) throws NPE */ public void testOfferNull() { - try { + try { ArrayBlockingQueue q = new ArrayBlockingQueue(1); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) {} } /** * add(null) throws NPE */ public void testAddNull() { - try { + try { ArrayBlockingQueue q = new ArrayBlockingQueue(1); q.add(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) {} } /** @@ -186,15 +179,15 @@ public class ArrayBlockingQueueTest exte * add succeeds if not full; throws ISE if full */ public void testAdd() { - try { + try { ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertTrue(q.add(new Integer(i))); } assertEquals(0, q.remainingCapacity()); q.add(new Integer(SIZE)); - } catch (IllegalStateException success){ - } + shouldThrow(); + } catch (IllegalStateException success) {} } /** @@ -205,8 +198,7 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = new ArrayBlockingQueue(1); q.addAll(null); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** @@ -217,8 +209,7 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); - } - catch (IllegalArgumentException success) {} + } catch (IllegalArgumentException success) {} } @@ -231,8 +222,7 @@ public class ArrayBlockingQueueTest exte Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** * addAll of a collection with any null elements throws NPE after @@ -246,8 +236,7 @@ public class ArrayBlockingQueueTest exte ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** * addAll throws ISE if not enough room @@ -260,214 +249,164 @@ public class ArrayBlockingQueueTest exte ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); shouldThrow(); - } - catch (IllegalStateException success) {} + } catch (IllegalStateException success) {} } /** * Queue contains all elements, in traversal order, of successful addAll */ public void testAddAll5() { - try { - Integer[] empty = new Integer[0]; - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); - assertFalse(q.addAll(Arrays.asList(empty))); - assertTrue(q.addAll(Arrays.asList(ints))); - for (int i = 0; i < SIZE; ++i) - assertEquals(ints[i], q.poll()); - } - finally {} + Integer[] empty = new Integer[0]; + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = new Integer(i); + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); + assertFalse(q.addAll(Arrays.asList(empty))); + assertTrue(q.addAll(Arrays.asList(ints))); + for (int i = 0; i < SIZE; ++i) + assertEquals(ints[i], q.poll()); } /** * put(null) throws NPE */ - public void testPutNull() { - try { + public void testPutNull() throws InterruptedException { + try { ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); q.put(null); shouldThrow(); - } - catch (NullPointerException success){ - } - catch (InterruptedException ie) { - unexpectedException(); - } + } catch (NullPointerException success) {} } /** * all elements successfully put are contained */ - public void testPut() { - try { - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.put(I); - assertTrue(q.contains(I)); - } - assertEquals(0, q.remainingCapacity()); - } - catch (InterruptedException ie) { - unexpectedException(); + public void testPut() throws InterruptedException { + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + Integer I = new Integer(i); + q.put(I); + assertTrue(q.contains(I)); } + assertEquals(0, q.remainingCapacity()); } /** * put blocks interruptibly if full */ - public void testBlockingPut() { - Thread t = new Thread(new Runnable() { - public void run() { - int added = 0; - try { - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - q.put(new Integer(i)); - ++added; - } - q.put(new Integer(SIZE)); - threadShouldThrow(); - } catch (InterruptedException ie){ - threadAssertEquals(added, SIZE); - } - }}); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } + public void testBlockingPut() throws InterruptedException { + final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() { + int added = 0; + try { + for (int i = 0; i < SIZE; ++i) { + q.put(new Integer(i)); + ++added; + } + q.put(new Integer(SIZE)); + threadShouldThrow(); + } catch (InterruptedException success) { + threadAssertEquals(added, SIZE); + }}}); + + t.start(); + Thread.sleep(MEDIUM_DELAY_MS); + t.interrupt(); + t.join(); } /** * put blocks waiting for take when full */ - public void testPutWithTake() { + public void testPutWithTake() throws InterruptedException { final ArrayBlockingQueue q = new ArrayBlockingQueue(2); - Thread t = new Thread(new Runnable() { - public void run() { - int added = 0; - try { - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - threadShouldThrow(); - } catch (InterruptedException e){ - threadAssertTrue(added >= 2); - } - } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - q.take(); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + Thread t = new Thread(new CheckedRunnable() { + public void realRun() { + int added = 0; + try { + q.put(new Object()); + ++added; + q.put(new Object()); + ++added; + q.put(new Object()); + ++added; + q.put(new Object()); + ++added; + threadShouldThrow(); + } catch (InterruptedException success) { + threadAssertTrue(added >= 2); + } + }}); + + t.start(); + Thread.sleep(SHORT_DELAY_MS); + q.take(); + t.interrupt(); + t.join(); } /** * timed offer times out if full and elements not taken */ - public void testTimedOffer() { + public void testTimedOffer() throws InterruptedException { final ArrayBlockingQueue q = new ArrayBlockingQueue(2); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.put(new Object()); - q.put(new Object()); - threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); - q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success){} - } - }); - - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + Thread t = new ThreadShouldThrow(InterruptedException.class) { + public void realRun() throws InterruptedException { + q.put(new Object()); + q.put(new Object()); + threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); + q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); + }}; + + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } /** * take retrieves elements in FIFO order */ - public void testTake() { - try { - ArrayBlockingQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.take()).intValue()); - } - } catch (InterruptedException e){ - unexpectedException(); - } + public void testTake() throws InterruptedException { + ArrayBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, ((Integer)q.take()).intValue()); + } } /** * take blocks interruptibly when empty */ - public void testTakeFromEmpty() { + public void testTakeFromEmpty() throws InterruptedException { final ArrayBlockingQueue q = new ArrayBlockingQueue(2); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.take(); - threadShouldThrow(); - } catch (InterruptedException success){ } - } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + Thread t = new ThreadShouldThrow(InterruptedException.class) { + public void realRun() throws InterruptedException { + q.take(); + }}; + + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } /** * Take removes existing elements until empty, then blocks interruptibly */ - public void testBlockingTake() { - Thread t = new Thread(new Runnable() { - public void run() { - try { - ArrayBlockingQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.take()).intValue()); - } - q.take(); - threadShouldThrow(); - } catch (InterruptedException success){ - } - }}); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); + public void testBlockingTake() throws InterruptedException { + Thread t = new ThreadShouldThrow(InterruptedException.class) { + public void realRun() throws InterruptedException { + ArrayBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + threadAssertEquals(i, ((Integer)q.take()).intValue()); + } + q.take(); + }}; + + t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } } @@ -479,92 +418,72 @@ public class ArrayBlockingQueueTest exte for (int i = 0; i < SIZE; ++i) { assertEquals(i, ((Integer)q.poll()).intValue()); } - assertNull(q.poll()); + assertNull(q.poll()); } /** * timed pool with zero timeout succeeds when non-empty, else times out */ - public void testTimedPoll0() { - try { - ArrayBlockingQueue 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)); - } catch (InterruptedException e){ - unexpectedException(); - } + public void testTimedPoll0() throws InterruptedException { + ArrayBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); + } + assertNull(q.poll(0, MILLISECONDS)); } /** * timed pool with nonzero timeout succeeds when non-empty, else times out */ - public void testTimedPoll() { - try { - ArrayBlockingQueue 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)); - } catch (InterruptedException e){ - unexpectedException(); - } + public void testTimedPoll() throws InterruptedException { + ArrayBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + } + assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); } /** * Interrupted timed poll throws InterruptedException instead of * returning timeout status */ - public void testInterruptedTimedPoll() { - Thread t = new Thread(new Runnable() { - public void run() { - try { - ArrayBlockingQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); - } - threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException success){ - } - }}); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } + public void testInterruptedTimedPoll() throws InterruptedException { + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + ArrayBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + } + try { + q.poll(SMALL_DELAY_MS, MILLISECONDS); + } catch (InterruptedException success) {} + }}); + + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } /** * timed poll before a delayed offer fails; after offer succeeds; * on interruption throws */ - public void testTimedPollWithOffer() { + public void testTimedPollWithOffer() throws InterruptedException { final ArrayBlockingQueue q = new ArrayBlockingQueue(2); - Thread t = new Thread(new Runnable() { - public void run() { - try { - threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success) { } - } - }); - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } - } + Thread t = new ThreadShouldThrow(InterruptedException.class) { + public void realRun() throws InterruptedException { + threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + q.poll(LONG_DELAY_MS, MILLISECONDS); + q.poll(LONG_DELAY_MS, MILLISECONDS); + }}; + + t.start(); + Thread.sleep(SMALL_DELAY_MS); + assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); + t.interrupt(); + t.join(); + } /** @@ -578,7 +497,7 @@ public class ArrayBlockingQueueTest exte assertTrue(q.peek() == null || i != ((Integer)q.peek()).intValue()); } - assertNull(q.peek()); + assertNull(q.peek()); } /** @@ -593,8 +512,7 @@ public class ArrayBlockingQueueTest exte try { q.element(); shouldThrow(); - } - catch (NoSuchElementException success) {} + } catch (NoSuchElementException success) {} } /** @@ -608,8 +526,7 @@ public class ArrayBlockingQueueTest exte try { q.remove(); shouldThrow(); - } catch (NoSuchElementException success){ - } + } catch (NoSuchElementException success) {} } /** @@ -626,7 +543,7 @@ public class ArrayBlockingQueueTest exte } assertTrue(q.isEmpty()); } - + /** * contains(x) reports true when elements added but not yet removed */ @@ -650,6 +567,7 @@ public class ArrayBlockingQueueTest exte assertEquals(SIZE, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); + assertTrue(q.contains(one)); q.clear(); assertTrue(q.isEmpty()); } @@ -706,68 +624,56 @@ public class ArrayBlockingQueueTest exte /** * toArray contains all elements */ - public void testToArray() { + public void testToArray() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(SIZE); - Object[] o = q.toArray(); - try { - for(int i = 0; i < o.length; i++) - assertEquals(o[i], q.take()); - } catch (InterruptedException e){ - unexpectedException(); - } + Object[] o = q.toArray(); + for (int i = 0; i < o.length; i++) + assertEquals(o[i], q.take()); } /** * toArray(a) contains all elements */ - public void testToArray2() { + public void testToArray2() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(SIZE); - Integer[] ints = new Integer[SIZE]; - ints = (Integer[])q.toArray(ints); - try { - for(int i = 0; i < ints.length; i++) - assertEquals(ints[i], q.take()); - } catch (InterruptedException e){ - unexpectedException(); - } + Integer[] ints = new Integer[SIZE]; + ints = (Integer[])q.toArray(ints); + for (int i = 0; i < ints.length; i++) + assertEquals(ints[i], q.take()); } /** * toArray(null) throws NPE */ public void testToArray_BadArg() { - try { + try { ArrayBlockingQueue q = populatedQueue(SIZE); - Object o[] = q.toArray(null); - shouldThrow(); - } catch(NullPointerException success){} + Object o[] = q.toArray(null); + shouldThrow(); + } catch (NullPointerException success) {} } /** * toArray with incompatible array type throws CCE */ public void testToArray1_BadArg() { - try { + try { ArrayBlockingQueue q = populatedQueue(SIZE); - Object o[] = q.toArray(new String[10] ); - shouldThrow(); - } catch(ArrayStoreException success){} + Object o[] = q.toArray(new String[10] ); + shouldThrow(); + } catch (ArrayStoreException success) {} } - + /** * iterator iterates through all elements */ - public void testIterator() { + public void testIterator() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(SIZE); - Iterator it = q.iterator(); - try { - while(it.hasNext()){ - assertEquals(it.next(), q.take()); - } - } catch (InterruptedException e){ - unexpectedException(); - } + Iterator it = q.iterator(); + while (it.hasNext()) { + assertEquals(it.next(), q.take()); + } } /** @@ -782,7 +688,7 @@ public class ArrayBlockingQueueTest exte Iterator it = q.iterator(); it.next(); it.remove(); - + it = q.iterator(); assertEquals(it.next(), one); assertEquals(it.next(), three); @@ -816,14 +722,9 @@ public class ArrayBlockingQueueTest exte q.add(one); q.add(two); q.add(three); - try { - for (Iterator it = q.iterator(); it.hasNext();) { - q.remove(); - it.next(); - } - } - catch (ConcurrentModificationException e) { - unexpectedException(); + for (Iterator it = q.iterator(); it.hasNext();) { + q.remove(); + it.next(); } assertEquals(0, q.size()); } @@ -838,7 +739,7 @@ public class ArrayBlockingQueueTest exte for (int i = 0; i < SIZE; ++i) { assertTrue(s.indexOf(String.valueOf(i)) >= 0); } - } + } /** @@ -849,31 +750,19 @@ public class ArrayBlockingQueueTest exte q.add(one); q.add(two); ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { - public void run() { + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { threadAssertFalse(q.offer(three)); - try { - threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); - threadAssertEquals(0, q.remainingCapacity()); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); + threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); + threadAssertEquals(0, q.remainingCapacity()); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + Thread.sleep(SMALL_DELAY_MS); + threadAssertEquals(one, q.take()); + }}); - executor.execute(new Runnable() { - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); - threadAssertEquals(one, q.take()); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); - joinPool(executor); } @@ -884,161 +773,146 @@ public class ArrayBlockingQueueTest exte public void testPollInExecutor() { final ArrayBlockingQueue q = new ArrayBlockingQueue(2); ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { - public void run() { + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { threadAssertNull(q.poll()); - try { - threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); - threadAssertTrue(q.isEmpty()); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); + threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + threadAssertTrue(q.isEmpty()); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + Thread.sleep(SMALL_DELAY_MS); + q.put(one); + }}); - executor.execute(new Runnable() { - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); - q.put(one); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); - joinPool(executor); } /** * A deserialized serialized queue has same elements in same order */ - public void testSerialization() { + public void testSerialization() throws Exception { ArrayBlockingQueue 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)); - ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); - assertEquals(q.size(), r.size()); - while (!q.isEmpty()) - assertEquals(q.remove(), r.remove()); - } catch(Exception e){ - unexpectedException(); - } + 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)); + ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); + assertEquals(q.size(), r.size()); + while (!q.isEmpty()) + assertEquals(q.remove(), r.remove()); } /** * drainTo(null) throws NPE - */ + */ public void testDrainToNull() { ArrayBlockingQueue q = populatedQueue(SIZE); try { q.drainTo(null); shouldThrow(); - } catch(NullPointerException success) { - } + } catch (NullPointerException success) {} } /** * drainTo(this) throws IAE - */ + */ public void testDrainToSelf() { ArrayBlockingQueue q = populatedQueue(SIZE); try { q.drainTo(q); shouldThrow(); - } catch(IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** * drainTo(c) empties queue into another collection c - */ + */ public void testDrainTo() { ArrayBlockingQueue q = populatedQueue(SIZE); ArrayList l = new ArrayList(); q.drainTo(l); assertEquals(q.size(), 0); assertEquals(l.size(), SIZE); - for (int i = 0; i < SIZE; ++i) + for (int i = 0; i < SIZE; ++i) + assertEquals(l.get(i), new Integer(i)); + q.add(zero); + q.add(one); + assertFalse(q.isEmpty()); + assertTrue(q.contains(zero)); + assertTrue(q.contains(one)); + l.clear(); + q.drainTo(l); + assertEquals(q.size(), 0); + assertEquals(l.size(), 2); + for (int i = 0; i < 2; ++i) assertEquals(l.get(i), new Integer(i)); } /** * drainTo empties full queue, unblocking a waiting put. - */ - public void testDrainToWithActivePut() { + */ + public void testDrainToWithActivePut() throws InterruptedException { final ArrayBlockingQueue q = populatedQueue(SIZE); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.put(new Integer(SIZE+1)); - } catch (InterruptedException ie){ - threadUnexpectedException(); - } - } - }); - try { - t.start(); - ArrayList l = new ArrayList(); - q.drainTo(l); - assertTrue(l.size() >= SIZE); - for (int i = 0; i < SIZE; ++i) - assertEquals(l.get(i), new Integer(i)); - t.join(); - assertTrue(q.size() + l.size() == SIZE+1); - } catch(Exception e){ - unexpectedException(); - } + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.put(new Integer(SIZE+1)); + }}); + + t.start(); + ArrayList l = new ArrayList(); + q.drainTo(l); + assertTrue(l.size() >= SIZE); + for (int i = 0; i < SIZE; ++i) + assertEquals(l.get(i), new Integer(i)); + t.join(); + assertTrue(q.size() + l.size() >= SIZE); } /** * drainTo(null, n) throws NPE - */ + */ public void testDrainToNullN() { ArrayBlockingQueue q = populatedQueue(SIZE); try { q.drainTo(null, 0); shouldThrow(); - } catch(NullPointerException success) { - } + } catch (NullPointerException success) {} } /** * drainTo(this, n) throws IAE - */ + */ public void testDrainToSelfN() { ArrayBlockingQueue q = populatedQueue(SIZE); try { q.drainTo(q, 0); shouldThrow(); - } catch(IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** * drainTo(c, n) empties first max {n, size} elements of queue into c - */ + */ public void testDrainToN() { + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); for (int i = 0; i < SIZE + 2; ++i) { - ArrayBlockingQueue q = populatedQueue(SIZE); + for (int j = 0; j < SIZE; j++) + assertTrue(q.offer(new Integer(j))); ArrayList l = new ArrayList(); q.drainTo(l, i); int k = (i < SIZE)? i : SIZE; - assertEquals(q.size(), SIZE-k); assertEquals(l.size(), k); - for (int j = 0; j < k; ++j) + assertEquals(q.size(), SIZE-k); + for (int j = 0; j < k; ++j) assertEquals(l.get(j), new Integer(j)); + while (q.poll() != null) ; } } - }