diff options
author | Przemyslaw Szczepaniak <pszczepaniak@google.com> | 2016-03-11 15:59:10 +0000 |
---|---|---|
committer | Przemyslaw Szczepaniak <pszczepaniak@google.com> | 2016-03-14 15:40:14 +0000 |
commit | 5328e07d282bef36ac8b757bbee16a761415b2c4 (patch) | |
tree | d7fd9980442e0d647e1454f54f3d037f4b2280e7 /jsr166-tests/src/test/java/jsr166/DelayQueueTest.java | |
parent | 8d28fc1d531b4b39def9598de89311aff112f955 (diff) |
JSR-166 update
Adapted from sources taken from CVS using:
cvs -d ':pserver:anonymous@gee.cs.oswego.edu/home/jsr166/jsr166' checkout -D "03/03/2016 10:00:00 GMT" jsr166
Bug: 27426599
Change-Id: Ic9ba278929f8747d58b69e7d67ec325064588bff
Diffstat (limited to 'jsr166-tests/src/test/java/jsr166/DelayQueueTest.java')
-rw-r--r-- | jsr166-tests/src/test/java/jsr166/DelayQueueTest.java | 84 |
1 files changed, 44 insertions, 40 deletions
diff --git a/jsr166-tests/src/test/java/jsr166/DelayQueueTest.java b/jsr166-tests/src/test/java/jsr166/DelayQueueTest.java index 7619b48946..e42ac2dfbc 100644 --- a/jsr166-tests/src/test/java/jsr166/DelayQueueTest.java +++ b/jsr166-tests/src/test/java/jsr166/DelayQueueTest.java @@ -25,25 +25,26 @@ import java.util.concurrent.TimeUnit; import junit.framework.Test; -// android-changed: Extend BlockingQueueTest directly. -public class DelayQueueTest extends BlockingQueueTest { +public class DelayQueueTest extends JSR166TestCase { // android-changed: Extend BlockingQueueTest directly instead of creating // an inner class and its associated suite. // // public static class Generic extends BlockingQueueTest { - // protected BlockingQueue emptyCollection() { + // protected BlockingQueue emptyCollection() { // return new DelayQueue(); // } // protected PDelay makeElement(int i) { // return new PDelay(i); // } // } + + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. // // public static void main(String[] args) { // main(suite(), args); // } - // // public static Test suite() { // return newTestSuite(DelayQueueTest.class, // new Generic().testSuite()); @@ -138,7 +139,7 @@ public class DelayQueueTest extends BlockingQueueTest { private DelayQueue<PDelay> populatedQueue(int n) { DelayQueue<PDelay> q = new DelayQueue<PDelay>(); assertTrue(q.isEmpty()); - for (int i = n-1; i >= 0; i -= 2) + for (int i = n - 1; i >= 0; i -= 2) assertTrue(q.offer(new PDelay(i))); for (int i = (n & 1); i < n; i += 2) assertTrue(q.offer(new PDelay(i))); @@ -170,8 +171,7 @@ public class DelayQueueTest extends BlockingQueueTest { */ public void testConstructor4() { try { - PDelay[] ints = new PDelay[SIZE]; - new DelayQueue(Arrays.asList(ints)); + new DelayQueue(Arrays.asList(new PDelay[SIZE])); shouldThrow(); } catch (NullPointerException success) {} } @@ -180,11 +180,11 @@ public class DelayQueueTest extends BlockingQueueTest { * Initializing from Collection with some null elements throws NPE */ public void testConstructor5() { + PDelay[] a = new PDelay[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) + a[i] = new PDelay(i); try { - PDelay[] ints = new PDelay[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new PDelay(i); - new DelayQueue(Arrays.asList(ints)); + new DelayQueue(Arrays.asList(a)); shouldThrow(); } catch (NullPointerException success) {} } @@ -223,7 +223,7 @@ public class DelayQueueTest extends BlockingQueueTest { BlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); assertTrue(q.remove() instanceof PDelay); } for (int i = 0; i < SIZE; ++i) { @@ -257,8 +257,8 @@ public class DelayQueueTest extends BlockingQueueTest { * addAll(this) throws IAE */ public void testAddAllSelf() { + DelayQueue q = populatedQueue(SIZE); try { - DelayQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); } catch (IllegalArgumentException success) {} @@ -269,12 +269,12 @@ public class DelayQueueTest extends BlockingQueueTest { * possibly adding some elements */ public void testAddAll3() { + DelayQueue q = new DelayQueue(); + PDelay[] a = new PDelay[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) + a[i] = new PDelay(i); try { - DelayQueue q = new DelayQueue(); - PDelay[] ints = new PDelay[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new PDelay(i); - q.addAll(Arrays.asList(ints)); + q.addAll(Arrays.asList(a)); shouldThrow(); } catch (NullPointerException success) {} } @@ -285,7 +285,7 @@ public class DelayQueueTest extends BlockingQueueTest { public void testAddAll5() { PDelay[] empty = new PDelay[0]; PDelay[] ints = new PDelay[SIZE]; - for (int i = SIZE-1; i >= 0; --i) + for (int i = SIZE - 1; i >= 0; --i) ints[i] = new PDelay(i); DelayQueue q = new DelayQueue(); assertFalse(q.addAll(Arrays.asList(empty))); @@ -427,11 +427,13 @@ public class DelayQueueTest extends BlockingQueueTest { */ public void testInterruptedTimedPoll() throws InterruptedException { final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + final DelayQueue q = populatedQueue(SIZE); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - DelayQueue q = populatedQueue(SIZE); + long startTime = System.nanoTime(); for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); + assertEquals(new PDelay(i), + ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); } Thread.currentThread().interrupt(); @@ -447,12 +449,14 @@ public class DelayQueueTest extends BlockingQueueTest { shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); await(pleaseInterrupt); assertThreadStaysAlive(t); t.interrupt(); awaitTermination(t); + checkEmpty(q); } /** @@ -557,7 +561,7 @@ public class DelayQueueTest extends BlockingQueueTest { assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); p.remove(); } } @@ -570,7 +574,7 @@ public class DelayQueueTest extends BlockingQueueTest { DelayQueue q = populatedQueue(SIZE); DelayQueue p = populatedQueue(i); assertTrue(q.removeAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); for (int j = 0; j < i; ++j) { PDelay x = (PDelay)(p.remove()); assertFalse(q.contains(x)); @@ -668,22 +672,22 @@ public class DelayQueueTest extends BlockingQueueTest { public void testPollInExecutor() { final DelayQueue q = new DelayQueue(); final CheckedBarrier threadsStarted = new CheckedBarrier(2); - ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - assertNull(q.poll()); - threadsStarted.await(); - assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); - checkEmpty(q); - }}); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - q.put(new PDelay(1)); - }}); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertNull(q.poll()); + threadsStarted.await(); + assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); + checkEmpty(q); + }}); - joinPool(executor); + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + q.put(new PDelay(1)); + }}); + } } /** @@ -768,7 +772,7 @@ public class DelayQueueTest extends BlockingQueueTest { final DelayQueue q = populatedQueue(SIZE); Thread t = new Thread(new CheckedRunnable() { public void realRun() { - q.put(new PDelay(SIZE+1)); + q.put(new PDelay(SIZE + 1)); }}); t.start(); @@ -788,7 +792,7 @@ public class DelayQueueTest extends BlockingQueueTest { ArrayList l = new ArrayList(); q.drainTo(l, i); int k = (i < SIZE) ? i : SIZE; - assertEquals(SIZE-k, q.size()); + assertEquals(SIZE - k, q.size()); assertEquals(k, l.size()); } } |