diff options
author | Jesse Wilson <jessewilson@google.com> | 2009-07-28 17:34:57 -0700 |
---|---|---|
committer | Jesse Wilson <jessewilson@google.com> | 2009-07-28 17:35:12 -0700 |
commit | bba8d1acd6dfff06c94d761c67a30154ca5ca5df (patch) | |
tree | f68043d73ad474a98471ed8c4081b3ffa274c8f6 /concurrent/src/test/java/tests | |
parent | b5d658bb3d92e590f2320058064b807c4034860c (diff) | |
download | libcore-bba8d1acd6dfff06c94d761c67a30154ca5ca5df.zip libcore-bba8d1acd6dfff06c94d761c67a30154ca5ca5df.tar.gz libcore-bba8d1acd6dfff06c94d761c67a30154ca5ca5df.tar.bz2 |
Update concurrent module to Harmony r798021.
commit 56bcdc2a3b881883409267c3bd16294d80716859
Merge: 903691a f429dca
Author: Jesse Wilson <jessewilson@google.com>
Date: Mon Jul 27 17:33:19 2009 -0700
Merge branch 'concurrent_798021' into concurrent_dalvik
Conflicts:
libcore/concurrent/.classpath
libcore/concurrent/build.xml
libcore/concurrent/make/run-test.xml
libcore/concurrent/src/main/java/java/util/concurrent/ArrayBlockingQueue.java
libcore/concurrent/src/main/java/java/util/concurrent/BlockingQueue.java
libcore/concurrent/src/main/java/java/util/concurrent/ConcurrentHashMap.java
libcore/concurrent/src/main/java/java/util/concurrent/ConcurrentLinkedQueue.java
libcore/concurrent/src/main/java/java/util/concurrent/ConcurrentMap.java
libcore/concurrent/src/main/java/java/util/concurrent/CopyOnWriteArrayList.java
libcore/concurrent/src/main/java/java/util/concurrent/CopyOnWriteArraySet.java
libcore/concurrent/src/main/java/java/util/concurrent/DelayQueue.java
libcore/concurrent/src/main/java/java/util/concurrent/LinkedBlockingQueue.java
libcore/concurrent/src/main/java/java/util/concurrent/PriorityBlockingQueue.java
libcore/concurrent/src/main/java/java/util/concurrent/ScheduledThreadPoolExecutor.java
libcore/concurrent/src/main/java/java/util/concurrent/Semaphore.java
libcore/concurrent/src/main/java/java/util/concurrent/SynchronousQueue.java
libcore/concurrent/src/main/java/java/util/concurrent/ThreadPoolExecutor.java
libcore/concurrent/src/main/java/java/util/concurrent/TimeUnit.java
libcore/concurrent/src/main/java/java/util/concurrent/atomic/AtomicBoolean.java
libcore/concurrent/src/main/java/java/util/concurrent/atomic/AtomicInteger.java
libcore/concurrent/src/main/java/java/util/concurrent/atomic/AtomicIntegerArray.java
libcore/concurrent/src/main/java/java/util/concurrent/atomic/AtomicIntegerFieldUpdater.java
libcore/concurrent/src/main/java/java/util/concurrent/atomic/AtomicLong.java
libcore/concurrent/src/main/java/java/util/concurrent/atomic/AtomicLongArray.java
libcore/concurrent/src/main/java/java/util/concurrent/atomic/AtomicLongFieldUpdater.java
libcore/concurrent/src/main/java/java/util/concurrent/atomic/AtomicReference.java
libcore/concurrent/src/main/java/java/util/concurrent/atomic/AtomicReferenceArray.java
libcore/concurrent/src/main/java/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.java
libcore/concurrent/src/main/java/java/util/concurrent/atomic/package.html
libcore/concurrent/src/main/java/java/util/concurrent/locks/AbstractQueuedSynchronizer.java
libcore/concurrent/src/main/java/java/util/concurrent/locks/LockSupport.java
libcore/concurrent/src/main/java/java/util/concurrent/locks/ReentrantReadWriteLock.java
libcore/concurrent/src/main/java/java/util/concurrent/locks/package.html
libcore/concurrent/src/main/java/java/util/concurrent/package.html
libcore/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicLongFieldUpdaterTest.java
libcore/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicReferenceFieldUpdaterTest.java
libcore/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicReferenceTest.java
libcore/concurrent/src/test/java/tests/api/java/util/concurrent/ConcurrentHashMapTest.java
libcore/concurrent/src/test/java/tests/api/java/util/concurrent/DelayQueueTest.java
libcore/concurrent/src/test/java/tests/api/java/util/concurrent/ExecutorsTest.java
libcore/concurrent/src/test/java/tests/api/java/util/concurrent/JSR166TestCase.java
libcore/concurrent/src/test/java/tests/api/java/util/concurrent/LinkedBlockingQueueTest.java
libcore/concurrent/src/test/java/tests/api/java/util/concurrent/LockSupportTest.java
libcore/concurrent/src/test/java/tests/api/java/util/concurrent/ReentrantLockTest.java
libcore/concurrent/src/test/java/tests/api/java/util/concurrent/ReentrantReadWriteLockTest.java
commit 903691ae71cff640d5487a3d34a20e8767dbfb66
Author: Jesse Wilson <jessewilson@google.com>
Date: Mon Jul 27 16:09:58 2009 -0700
Dalvik Concurrent
commit f429dca21c408ee62e688f60d5e110718374e944
Author: Jesse Wilson <jessewilson@google.com>
Date: Mon Jul 27 16:08:25 2009 -0700
Concurrent 798021
commit b2c76fdd1056113000140bc4af57300c87469d2d
Author: Jesse Wilson <jessewilson@google.com>
Date: Mon Jul 27 16:03:45 2009 -0700
Concurrent 527399
Diffstat (limited to 'concurrent/src/test/java/tests')
22 files changed, 1494 insertions, 567 deletions
diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/AbstractQueuedSynchronizerTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/AbstractQueuedSynchronizerTest.java index 7102c14..db89645 100644 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/AbstractQueuedSynchronizerTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/AbstractQueuedSynchronizerTest.java @@ -462,7 +462,9 @@ public class AbstractQueuedSynchronizerTest extends JSR166TestCase { Thread t = new Thread(new InterruptedSyncRunnable(sync)); try { t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); + Thread.sleep(SHORT_DELAY_MS); sync.release(1); t.join(); } catch(Exception e){ @@ -951,7 +953,6 @@ public class AbstractQueuedSynchronizerTest extends JSR166TestCase { sync.acquire(1); c.signal(); sync.release(1); - assert(t.isInterrupted()); t.join(SHORT_DELAY_MS); assertFalse(t.isAlive()); } diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/ArrayBlockingQueueTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/ArrayBlockingQueueTest.java index da6988f..b650ede 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/ArrayBlockingQueueTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/ArrayBlockingQueueTest.java @@ -651,6 +651,7 @@ public class ArrayBlockingQueueTest extends JSR166TestCase { assertEquals(SIZE, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); + assertTrue(q.contains(one)); q.clear(); assertTrue(q.isEmpty()); } @@ -971,6 +972,17 @@ public class ArrayBlockingQueueTest extends JSR166TestCase { assertEquals(l.size(), SIZE); 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)); } /** @@ -1029,15 +1041,18 @@ public class ArrayBlockingQueueTest extends JSR166TestCase { * 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); + assertEquals(q.size(), SIZE-k); for (int j = 0; j < k; ++j) assertEquals(l.get(j), new Integer(j)); + while (q.poll() != null) ; } } diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicIntegerFieldUpdaterTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicIntegerFieldUpdaterTest.java index 03fd2c8..4c3ecb4 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicIntegerFieldUpdaterTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicIntegerFieldUpdaterTest.java @@ -62,6 +62,27 @@ public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase { catch (RuntimeException rt) {} } + static class Base { + protected volatile int f = 0; + } + static class Sub1 extends Base { + AtomicIntegerFieldUpdater<Base> fUpdater + = AtomicIntegerFieldUpdater.newUpdater(Base.class, "f"); + } + static class Sub2 extends Base {} + + public void testProtectedFieldOnAnotherSubtype() { + Sub1 sub1 = new Sub1(); + Sub2 sub2 = new Sub2(); + + sub1.fUpdater.set(sub1, 1); + try { + sub1.fUpdater.set(sub2, 2); + shouldThrow(); + } + catch (RuntimeException rt) {} + } + /** * get returns the last value set or assigned */ @@ -80,6 +101,7 @@ public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase { assertEquals(-3,a.get(this)); } + /** * compareAndSet succeeds in changing value if equal to expected else fails */ diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicIntegerTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicIntegerTest.java index 1771d80..d484785 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicIntegerTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicIntegerTest.java @@ -48,6 +48,7 @@ public class AtomicIntegerTest extends JSR166TestCase { assertEquals(-3,ai.get()); } + /** * compareAndSet succeeds in changing value if equal to expected else fails */ diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicLongFieldUpdaterTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicLongFieldUpdaterTest.java index 8076f7b..9310795 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicLongFieldUpdaterTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicLongFieldUpdaterTest.java @@ -29,7 +29,7 @@ public class AtomicLongFieldUpdaterTest extends JSR166TestCase { */ public void testConstructor(){ try{ - AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> + AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater (AtomicLongFieldUpdaterTest.class, "y"); shouldThrow(); @@ -42,7 +42,7 @@ public class AtomicLongFieldUpdaterTest extends JSR166TestCase { */ public void testConstructor2(){ try{ - AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> + AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater (AtomicLongFieldUpdaterTest.class, "z"); shouldThrow(); @@ -55,7 +55,7 @@ public class AtomicLongFieldUpdaterTest extends JSR166TestCase { */ public void testConstructor3(){ try{ - AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> + AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater (AtomicLongFieldUpdaterTest.class, "w"); shouldThrow(); @@ -64,6 +64,27 @@ public class AtomicLongFieldUpdaterTest extends JSR166TestCase { catch (RuntimeException rt) {} } + static class Base { + protected volatile long f = 0; + } + static class Sub1 extends Base { + AtomicLongFieldUpdater<Base> fUpdater + = AtomicLongFieldUpdater.newUpdater(Base.class, "f"); + } + static class Sub2 extends Base {} + + public void testProtectedFieldOnAnotherSubtype() { + Sub1 sub1 = new Sub1(); + Sub2 sub2 = new Sub2(); + + sub1.fUpdater.set(sub1, 1); + try { + sub1.fUpdater.set(sub2, 2); + shouldThrow(); + } + catch (RuntimeException rt) {} + } + /** * get returns the last value set or assigned */ @@ -75,12 +96,12 @@ public class AtomicLongFieldUpdaterTest extends JSR166TestCase { return; } x = 1; - assertEquals(1,a.get(this)); - a.set(this,2); - assertEquals(2,a.get(this)); - a.set(this,-3); - assertEquals(-3,a.get(this)); - + assertEquals(1,a.get(this)); + a.set(this,2); + assertEquals(2,a.get(this)); + a.set(this,-3); + assertEquals(-3,a.get(this)); + } /** * compareAndSet succeeds in changing value if equal to expected else fails @@ -134,7 +155,7 @@ public class AtomicLongFieldUpdaterTest extends JSR166TestCase { /** * repeated weakCompareAndSet succeeds in changing value when equal - * to expected + * to expected */ public void testWeakCompareAndSet(){ AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicLongTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicLongTest.java index 55b8a49..143c84a 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicLongTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicLongTest.java @@ -48,6 +48,7 @@ public class AtomicLongTest extends JSR166TestCase { assertEquals(-3,ai.get()); } + /** * compareAndSet succeeds in changing value if equal to expected else fails */ diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicReferenceFieldUpdaterTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicReferenceFieldUpdaterTest.java index 183ca21..feddce7 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicReferenceFieldUpdaterTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicReferenceFieldUpdaterTest.java @@ -64,6 +64,27 @@ public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase{ catch (RuntimeException rt) {} } + static class Base { + protected volatile Object f = null; + } + static class Sub1 extends Base { + AtomicReferenceFieldUpdater<Base, Object> fUpdater + = AtomicReferenceFieldUpdater.newUpdater(Base.class, Object.class, "f"); + } + static class Sub2 extends Base {} + + public void testProtectedFieldOnAnotherSubtype() { + Sub1 sub1 = new Sub1(); + Sub2 sub2 = new Sub2(); + + sub1.fUpdater.set(sub1, "f"); + try { + sub1.fUpdater.set(sub2, "g"); + shouldThrow(); + } + catch (RuntimeException rt) {} + } + /** * get returns the last value set or assigned */ @@ -75,14 +96,11 @@ public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase{ return; } x = one; - assertEquals(one,a.get(this)); - a.set(this,two); - assertEquals(two,a.get(this)); - a.set(this,-3); - // BEGIN android-changed - assertEquals(new Integer(-3),a.get(this)); - // END android-changed - + assertEquals(one,a.get(this)); + a.set(this,two); + assertEquals(two,a.get(this)); + a.set(this,m3); + assertEquals(m3,a.get(this)); } /** * compareAndSet succeeds in changing value if equal to expected else fails @@ -135,7 +153,7 @@ public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase{ /** * repeated weakCompareAndSet succeeds in changing value when equal - * to expected + * to expected */ public void testWeakCompareAndSet(){ AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicReferenceTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicReferenceTest.java index 0aa48cd..33da30d 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicReferenceTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/AtomicReferenceTest.java @@ -46,7 +46,7 @@ public class AtomicReferenceTest extends JSR166TestCase { assertEquals(two,ai.get()); ai.set(m3); assertEquals(m3,ai.get()); - + } /** * compareAndSet succeeds in changing value if equal to expected else fails @@ -86,7 +86,7 @@ public class AtomicReferenceTest extends JSR166TestCase { /** * repeated weakCompareAndSet succeeds in changing value when equal - * to expected + * to expected */ public void testWeakCompareAndSet(){ AtomicReference ai = new AtomicReference(one); diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/ConcurrentHashMapTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/ConcurrentHashMapTest.java index 9b3ec52..d7f2210 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/ConcurrentHashMapTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/ConcurrentHashMapTest.java @@ -16,59 +16,16 @@ import java.io.*; public class ConcurrentHashMapTest 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(ConcurrentHashMapTest.class); } - // BEGIN android-added - static class MyConcurrentHashMap<V, K> extends ConcurrentHashMap<K, V> - implements Cloneable { - - public MyConcurrentHashMap() { - super(); - } - - public MyConcurrentHashMap(int initialCapacity, float loadFactor, - int concurrencyLevel) { - super(initialCapacity, loadFactor, concurrencyLevel); - } - - public MyConcurrentHashMap(int initialCapacity) { - super(initialCapacity); - } - - public MyConcurrentHashMap(Map<? extends K, ? extends V> t) { - super(t); - } - - @Override - protected Object clone() throws CloneNotSupportedException { - return super.clone(); - } - - } /** * Create a map from Integers 1-5 to Strings "A"-"E". */ - private static MyConcurrentHashMap myMap5() { - MyConcurrentHashMap map = new MyConcurrentHashMap(5); - assertTrue(map.isEmpty()); - map.put(one, "A"); - map.put(two, "B"); - map.put(three, "C"); - map.put(four, "D"); - map.put(five, "E"); - assertFalse(map.isEmpty()); - assertEquals(5, map.size()); - return map; - } - // END android-added - /** - * Create a map from Integers 1-5 to Strings "A"-"E". - */ - private static ConcurrentHashMap map5() { + private static ConcurrentHashMap map5() { ConcurrentHashMap map = new ConcurrentHashMap(5); assertTrue(map.isEmpty()); map.put(one, "A"); @@ -111,7 +68,7 @@ public class ConcurrentHashMapTest extends JSR166TestCase{ assertTrue(map.contains("A")); assertFalse(map.contains("Z")); } - + /** * containsKey returns true for contained key */ @@ -126,8 +83,8 @@ public class ConcurrentHashMapTest extends JSR166TestCase{ */ public void testContainsValue() { ConcurrentHashMap map = map5(); - assertTrue(map.contains("A")); - assertFalse(map.contains("Z")); + assertTrue(map.containsValue("A")); + assertFalse(map.containsValue("Z")); } /** @@ -146,21 +103,6 @@ public class ConcurrentHashMapTest extends JSR166TestCase{ } /** - * Clone creates an equal map - */ - public void testClone() { - // BEGIN android-changed - MyConcurrentHashMap map =myMap5(); - try { - MyConcurrentHashMap m2 = (MyConcurrentHashMap)(map.clone()); - assertEquals(map, m2); - } catch (CloneNotSupportedException e) { - fail("clone not supported"); - } - // END android-changed - } - - /** * get returns the correct element at the given key, * or null if not present */ @@ -210,6 +152,49 @@ public class ConcurrentHashMapTest extends JSR166TestCase{ } /** + * keySet.toArray returns contains all keys + */ + public void testKeySetToArray() { + ConcurrentHashMap map = map5(); + Set s = map.keySet(); + Object[] ar = s.toArray(); + assertTrue(s.containsAll(Arrays.asList(ar))); + assertEquals(5, ar.length); + ar[0] = m10; + assertFalse(s.containsAll(Arrays.asList(ar))); + } + + /** + * Values.toArray contains all values + */ + public void testValuesToArray() { + ConcurrentHashMap map = map5(); + Collection v = map.values(); + Object[] ar = v.toArray(); + ArrayList s = new ArrayList(Arrays.asList(ar)); + assertEquals(5, ar.length); + assertTrue(s.contains("A")); + assertTrue(s.contains("B")); + assertTrue(s.contains("C")); + assertTrue(s.contains("D")); + assertTrue(s.contains("E")); + } + + /** + * entrySet.toArray contains all entries + */ + public void testEntrySetToArray() { + ConcurrentHashMap map = map5(); + Set s = map.entrySet(); + Object[] ar = s.toArray(); + assertEquals(5, ar.length); + for (int i = 0; i < 5; ++i) { + assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); + assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); + } + } + + /** * values collection contains all values */ public void testValues() { @@ -233,7 +218,7 @@ public class ConcurrentHashMapTest extends JSR166TestCase{ Iterator it = s.iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry) it.next(); - assertTrue( + assertTrue( (e.getKey().equals(one) && e.getValue().equals("A")) || (e.getKey().equals(two) && e.getValue().equals("B")) || (e.getKey().equals(three) && e.getValue().equals("C")) || @@ -357,12 +342,12 @@ public class ConcurrentHashMapTest extends JSR166TestCase{ for (int i = 1; i <= 5; ++i) { assertTrue(s.indexOf(String.valueOf(i)) >= 0); } - } + } // Exception tests - + /** - * Cannot create with negative capacity + * Cannot create with negative capacity */ public void testConstructor1() { try { @@ -561,6 +546,19 @@ public class ConcurrentHashMapTest extends JSR166TestCase{ } /** + * remove(x, null) returns false + */ + public void testRemove3() { + try { + ConcurrentHashMap c = new ConcurrentHashMap(5); + c.put("sadsdf", "asdads"); + assertFalse(c.remove("sadsdf", null)); + } catch(NullPointerException e){ + fail(); + } + } + + /** * A deserialized map equals original */ public void testSerialization() { diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/CyclicBarrierTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/CyclicBarrierTest.java index 9eac1a7..ecd6e45 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/CyclicBarrierTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/CyclicBarrierTest.java @@ -11,6 +11,8 @@ package tests.api.java.util.concurrent; import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import java.util.concurrent.locks.*; +import java.util.concurrent.atomic.*; public class CyclicBarrierTest extends JSR166TestCase{ public static void main(String[] args) { @@ -164,7 +166,7 @@ public class CyclicBarrierTest extends JSR166TestCase{ * throw BrokenBarrierException */ public void testAwait2_Interrupted_BrokenBarrier() { - final CyclicBarrier c = new CyclicBarrier(3); + final CyclicBarrier c = new CyclicBarrier(3); Thread t1 = new Thread(new Runnable() { public void run() { try { @@ -229,7 +231,7 @@ public class CyclicBarrierTest extends JSR166TestCase{ * throw BrokenBarrierException */ public void testAwait4_Timeout_BrokenBarrier() { - final CyclicBarrier c = new CyclicBarrier(3); + final CyclicBarrier c = new CyclicBarrier(3); Thread t1 = new Thread(new Runnable() { public void run() { try { @@ -267,7 +269,7 @@ public class CyclicBarrierTest extends JSR166TestCase{ * throw BrokenBarrierException */ public void testAwait5_Timeout_BrokenBarrier() { - final CyclicBarrier c = new CyclicBarrier(3); + final CyclicBarrier c = new CyclicBarrier(3); Thread t1 = new Thread(new Runnable() { public void run() { try { @@ -376,4 +378,249 @@ public class CyclicBarrierTest extends JSR166TestCase{ } } + /** + * All threads block while a barrier is broken. + */ + public void testReset_Leakage() { + try { + final CyclicBarrier c = new CyclicBarrier(2); + final AtomicBoolean done = new AtomicBoolean(); + Thread t = new Thread() { + public void run() { + while (!done.get()) { + try { + while (c.isBroken()) + c.reset(); + + c.await(); + threadFail("await should not return"); + } + catch (BrokenBarrierException e) { + } + catch (InterruptedException ie) { + } + } + } + }; + + t.start(); + for( int i = 0; i < 4; i++) { + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + } + done.set(true); + t.interrupt(); + } + catch (Exception ex) { + unexpectedException(); + } + } + + /** + * Reset of a non-broken barrier does not break barrier + */ + public void testResetWithoutBreakage() { + try { + final CyclicBarrier start = new CyclicBarrier(3); + final CyclicBarrier barrier = new CyclicBarrier(3); + for (int i = 0; i < 3; i++) { + Thread t1 = new Thread(new Runnable() { + public void run() { + try { start.await(); } + catch (Exception ie) { + threadFail("start barrier"); + } + try { barrier.await(); } + catch (Throwable thrown) { + unexpectedException(); + }}}); + + Thread t2 = new Thread(new Runnable() { + public void run() { + try { start.await(); } + catch (Exception ie) { + threadFail("start barrier"); + } + try { barrier.await(); } + catch (Throwable thrown) { + unexpectedException(); + }}}); + + + t1.start(); + t2.start(); + try { start.await(); } + catch (Exception ie) { threadFail("start barrier"); } + barrier.await(); + t1.join(); + t2.join(); + assertFalse(barrier.isBroken()); + assertEquals(0, barrier.getNumberWaiting()); + if (i == 1) barrier.reset(); + assertFalse(barrier.isBroken()); + assertEquals(0, barrier.getNumberWaiting()); + } + } + catch (Exception ex) { + unexpectedException(); + } + } + + /** + * Reset of a barrier after interruption reinitializes it. + */ + public void testResetAfterInterrupt() { + try { + final CyclicBarrier start = new CyclicBarrier(3); + final CyclicBarrier barrier = new CyclicBarrier(3); + for (int i = 0; i < 2; i++) { + Thread t1 = new Thread(new Runnable() { + public void run() { + try { start.await(); } + catch (Exception ie) { + threadFail("start barrier"); + } + try { barrier.await(); } + catch(InterruptedException ok) {} + catch (Throwable thrown) { + unexpectedException(); + }}}); + + Thread t2 = new Thread(new Runnable() { + public void run() { + try { start.await(); } + catch (Exception ie) { + threadFail("start barrier"); + } + try { barrier.await(); } + catch(BrokenBarrierException ok) {} + catch (Throwable thrown) { + unexpectedException(); + }}}); + + t1.start(); + t2.start(); + try { start.await(); } + catch (Exception ie) { threadFail("start barrier"); } + t1.interrupt(); + t1.join(); + t2.join(); + assertTrue(barrier.isBroken()); + assertEquals(0, barrier.getNumberWaiting()); + barrier.reset(); + assertFalse(barrier.isBroken()); + assertEquals(0, barrier.getNumberWaiting()); + } + } + catch (Exception ex) { + unexpectedException(); + } + } + + /** + * Reset of a barrier after timeout reinitializes it. + */ + public void testResetAfterTimeout() { + try { + final CyclicBarrier start = new CyclicBarrier(3); + final CyclicBarrier barrier = new CyclicBarrier(3); + for (int i = 0; i < 2; i++) { + Thread t1 = new Thread(new Runnable() { + public void run() { + try { start.await(); } + catch (Exception ie) { + threadFail("start barrier"); + } + try { barrier.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); } + catch(TimeoutException ok) {} + catch (Throwable thrown) { + unexpectedException(); + }}}); + + Thread t2 = new Thread(new Runnable() { + public void run() { + try { start.await(); } + catch (Exception ie) { + threadFail("start barrier"); + } + try { barrier.await(); } + catch(BrokenBarrierException ok) {} + catch (Throwable thrown) { + unexpectedException(); + }}}); + + t1.start(); + t2.start(); + try { start.await(); } + catch (Exception ie) { threadFail("start barrier"); } + t1.join(); + t2.join(); + assertTrue(barrier.isBroken()); + assertEquals(0, barrier.getNumberWaiting()); + barrier.reset(); + assertFalse(barrier.isBroken()); + assertEquals(0, barrier.getNumberWaiting()); + } + } + catch (Exception ex) { + unexpectedException(); + } + } + + + /** + * Reset of a barrier after a failed command reinitializes it. + */ + public void testResetAfterCommandException() { + try { + final CyclicBarrier start = new CyclicBarrier(3); + final CyclicBarrier barrier = + new CyclicBarrier(3, new Runnable() { + public void run() { + throw new NullPointerException(); }}); + for (int i = 0; i < 2; i++) { + Thread t1 = new Thread(new Runnable() { + public void run() { + try { start.await(); } + catch (Exception ie) { + threadFail("start barrier"); + } + try { barrier.await(); } + catch(BrokenBarrierException ok) {} + catch (Throwable thrown) { + unexpectedException(); + }}}); + + Thread t2 = new Thread(new Runnable() { + public void run() { + try { start.await(); } + catch (Exception ie) { + threadFail("start barrier"); + } + try { barrier.await(); } + catch(BrokenBarrierException ok) {} + catch (Throwable thrown) { + unexpectedException(); + }}}); + + t1.start(); + t2.start(); + try { start.await(); } + catch (Exception ie) { threadFail("start barrier"); } + while (barrier.getNumberWaiting() < 2) { Thread.yield(); } + try { barrier.await(); } + catch (Exception ok) { } + t1.join(); + t2.join(); + assertTrue(barrier.isBroken()); + assertEquals(0, barrier.getNumberWaiting()); + barrier.reset(); + assertFalse(barrier.isBroken()); + assertEquals(0, barrier.getNumberWaiting()); + } + } + catch (Exception ex) { + unexpectedException(); + } + } } diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/DelayQueueTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/DelayQueueTest.java index e332513..978edb4 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/DelayQueueTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/DelayQueueTest.java @@ -14,11 +14,11 @@ import java.util.concurrent.*; public class DelayQueueTest 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(DelayQueueTest.class); + return new TestSuite(DelayQueueTest.class); } private static final int NOCAP = Integer.MAX_VALUE; @@ -27,20 +27,19 @@ public class DelayQueueTest extends JSR166TestCase { * A delayed implementation for testing. * Most tests use Pseudodelays, where delays are all elapsed * (so, no blocking solely for delays) but are still ordered - */ - static class PDelay implements Delayed { + */ + static class PDelay implements Delayed { int pseudodelay; PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } - // BEGIN android-changed - public int compareTo(Delayed y) { + public int compareTo(PDelay y) { int i = pseudodelay; int j = ((PDelay)y).pseudodelay; if (i < j) return -1; if (i > j) return 1; return 0; } - // END android-changed - public int compareTo(PDelay y) { + + public int compareTo(Delayed y) { int i = pseudodelay; int j = ((PDelay)y).pseudodelay; if (i < j) return -1; @@ -72,21 +71,20 @@ public class DelayQueueTest extends JSR166TestCase { /** * Delayed implementation that actually delays */ - static class NanoDelay implements Delayed { + static class NanoDelay implements Delayed { long trigger; - NanoDelay(long i) { + NanoDelay(long i) { trigger = System.nanoTime() + i; } - // BEGIN android-changed - public int compareTo(Delayed y) { + public int compareTo(NanoDelay y) { long i = trigger; long j = ((NanoDelay)y).trigger; if (i < j) return -1; if (i > j) return 1; return 0; } - // END android-changed - public int compareTo(NanoDelay y) { + + public int compareTo(Delayed y) { long i = trigger; long j = ((NanoDelay)y).trigger; if (i < j) return -1; @@ -123,16 +121,16 @@ public class DelayQueueTest extends JSR166TestCase { private DelayQueue populatedQueue(int n) { DelayQueue q = new DelayQueue(); assertTrue(q.isEmpty()); - 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))); + 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))); assertFalse(q.isEmpty()); assertEquals(NOCAP, q.remainingCapacity()); - assertEquals(n, q.size()); + assertEquals(n, q.size()); return q; } - + /** * A new queue has unbounded capacity */ @@ -229,22 +227,22 @@ public class DelayQueueTest extends JSR166TestCase { * offer(null) throws NPE */ public void testOfferNull() { - try { + try { DelayQueue q = new DelayQueue(); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } } /** * add(null) throws NPE */ public void testAddNull() { - try { + try { DelayQueue q = new DelayQueue(); q.add(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } } /** @@ -342,13 +340,13 @@ public class DelayQueueTest extends JSR166TestCase { * put(null) throws NPE */ public void testPutNull() { - try { + try { DelayQueue q = new DelayQueue(); q.put(null); shouldThrow(); - } + } catch (NullPointerException success){ - } + } } /** @@ -416,7 +414,7 @@ public class DelayQueueTest extends JSR166TestCase { } finally { } } }); - + try { t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -431,14 +429,14 @@ public class DelayQueueTest extends JSR166TestCase { * take retrieves elements in priority order */ public void testTake() { - try { + try { DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(new PDelay(i), ((PDelay)q.take())); } } catch (InterruptedException e){ - unexpectedException(); - } + unexpectedException(); + } } /** @@ -450,8 +448,8 @@ public class DelayQueueTest extends JSR166TestCase { public void run() { try { q.take(); - threadShouldThrow(); - } catch (InterruptedException success){ } + threadShouldThrow(); + } catch (InterruptedException success){ } } }); try { @@ -478,16 +476,16 @@ public class DelayQueueTest extends JSR166TestCase { q.take(); threadShouldThrow(); } catch (InterruptedException success){ - } + } }}); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); + try { + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } catch (InterruptedException ie) { - unexpectedException(); + unexpectedException(); } } @@ -500,7 +498,7 @@ public class DelayQueueTest extends JSR166TestCase { for (int i = 0; i < SIZE; ++i) { assertEquals(new PDelay(i), ((PDelay)q.poll())); } - assertNull(q.poll()); + assertNull(q.poll()); } /** @@ -514,8 +512,8 @@ public class DelayQueueTest extends JSR166TestCase { } assertNull(q.poll(0, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ - unexpectedException(); - } + unexpectedException(); + } } /** @@ -529,8 +527,8 @@ public class DelayQueueTest extends JSR166TestCase { } assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ - unexpectedException(); - } + unexpectedException(); + } } /** @@ -547,16 +545,16 @@ public class DelayQueueTest extends JSR166TestCase { } threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); } catch (InterruptedException success){ - } + } }}); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); + try { + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } catch (InterruptedException ie) { - unexpectedException(); + unexpectedException(); } } @@ -572,8 +570,8 @@ public class DelayQueueTest extends JSR166TestCase { threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadFail("Should block"); - } catch (InterruptedException success) { } + threadFail("Should block"); + } catch (InterruptedException success) { } } }); try { @@ -585,7 +583,7 @@ public class DelayQueueTest extends JSR166TestCase { } catch (Exception e){ unexpectedException(); } - } + } /** @@ -596,10 +594,12 @@ public class DelayQueueTest extends JSR166TestCase { for (int i = 0; i < SIZE; ++i) { assertEquals(new PDelay(i), ((PDelay)q.peek())); q.poll(); - assertTrue(q.peek() == null || - i != ((PDelay)q.peek()).intValue()); + if (q.isEmpty()) + assertNull(q.peek()); + else + assertTrue(i != ((PDelay)q.peek()).intValue()); } - assertNull(q.peek()); + assertNull(q.peek()); } /** @@ -630,7 +630,7 @@ public class DelayQueueTest extends JSR166TestCase { q.remove(); shouldThrow(); } catch (NoSuchElementException success){ - } + } } /** @@ -647,7 +647,7 @@ public class DelayQueueTest extends JSR166TestCase { } assertTrue(q.isEmpty()); } - + /** * contains(x) reports true when elements added but not yet removed */ @@ -669,8 +669,10 @@ public class DelayQueueTest extends JSR166TestCase { assertTrue(q.isEmpty()); assertEquals(0, q.size()); assertEquals(NOCAP, q.remainingCapacity()); - q.add(new PDelay(1)); + PDelay x = new PDelay(1); + q.add(x); assertFalse(q.isEmpty()); + assertTrue(q.contains(x)); q.clear(); assertTrue(q.isEmpty()); } @@ -729,14 +731,14 @@ public class DelayQueueTest extends JSR166TestCase { */ public void testToArray() { DelayQueue q = populatedQueue(SIZE); - Object[] o = q.toArray(); + Object[] o = q.toArray(); Arrays.sort(o); - try { - for(int i = 0; i < o.length; i++) - assertEquals(o[i], q.take()); - } catch (InterruptedException e){ - unexpectedException(); - } + try { + for(int i = 0; i < o.length; i++) + assertEquals(o[i], q.take()); + } catch (InterruptedException e){ + unexpectedException(); + } } /** @@ -744,15 +746,15 @@ public class DelayQueueTest extends JSR166TestCase { */ public void testToArray2() { DelayQueue q = populatedQueue(SIZE); - PDelay[] ints = new PDelay[SIZE]; - ints = (PDelay[])q.toArray(ints); + PDelay[] ints = new PDelay[SIZE]; + ints = (PDelay[])q.toArray(ints); Arrays.sort(ints); - try { - for(int i = 0; i < ints.length; i++) - assertEquals(ints[i], q.take()); - } catch (InterruptedException e){ - unexpectedException(); - } + try { + for(int i = 0; i < ints.length; i++) + assertEquals(ints[i], q.take()); + } catch (InterruptedException e){ + unexpectedException(); + } } @@ -760,31 +762,31 @@ public class DelayQueueTest extends JSR166TestCase { * toArray(null) throws NPE */ public void testToArray_BadArg() { - try { + try { DelayQueue 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 { DelayQueue 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() { DelayQueue q = populatedQueue(SIZE); int i = 0; - Iterator it = q.iterator(); + Iterator it = q.iterator(); while(it.hasNext()) { assertTrue(q.contains(it.next())); ++i; @@ -819,7 +821,7 @@ public class DelayQueueTest extends JSR166TestCase { for (int i = 0; i < SIZE; ++i) { assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0); } - } + } /** * offer transfers elements across Executor tasks @@ -875,7 +877,7 @@ public class DelayQueueTest extends JSR166TestCase { NanoDelay e = (NanoDelay)(q.take()); long tt = e.getTriggerTime(); assertTrue(tt <= System.nanoTime()); - if (i != 0) + if (i != 0) assertTrue(tt >= last); last = tt; } @@ -885,10 +887,41 @@ public class DelayQueueTest extends JSR166TestCase { } } + /** + * peek of a non-empty queue returns non-null even if not expired + */ + public void testPeekDelayed() { + DelayQueue q = new DelayQueue(); + q.add(new NanoDelay(Long.MAX_VALUE)); + assert(q.peek() != null); + } + + + /** + * poll of a non-empty queue returns null if no expired elements. + */ + public void testPollDelayed() { + DelayQueue q = new DelayQueue(); + q.add(new NanoDelay(Long.MAX_VALUE)); + assertNull(q.poll()); + } + + /** + * timed poll of a non-empty queue returns null if no expired elements. + */ + public void testTimedPollDelayed() { + DelayQueue q = new DelayQueue(); + q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); + try { + assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + } catch (Exception ex) { + unexpectedException(); + } + } /** * drainTo(null) throws NPE - */ + */ public void testDrainToNull() { DelayQueue q = populatedQueue(SIZE); try { @@ -900,7 +933,7 @@ public class DelayQueueTest extends JSR166TestCase { /** * drainTo(this) throws IAE - */ + */ public void testDrainToSelf() { DelayQueue q = populatedQueue(SIZE); try { @@ -912,13 +945,30 @@ public class DelayQueueTest extends JSR166TestCase { /** * drainTo(c) empties queue into another collection c - */ + */ public void testDrainTo() { - DelayQueue q = populatedQueue(SIZE); + DelayQueue q = new DelayQueue(); + PDelay[] elems = new PDelay[SIZE]; + for (int i = 0; i < SIZE; ++i) { + elems[i] = new PDelay(i); + q.add(elems[i]); + } ArrayList l = new ArrayList(); q.drainTo(l); assertEquals(q.size(), 0); - assertEquals(l.size(), SIZE); + for (int i = 0; i < SIZE; ++i) + assertEquals(l.get(i), elems[i]); + q.add(elems[0]); + q.add(elems[1]); + assertFalse(q.isEmpty()); + assertTrue(q.contains(elems[0])); + assertTrue(q.contains(elems[1])); + l.clear(); + q.drainTo(l); + assertEquals(q.size(), 0); + assertEquals(l.size(), 2); + for (int i = 0; i < 2; ++i) + assertEquals(l.get(i), elems[i]); } /** diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/ExecutorCompletionServiceTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/ExecutorCompletionServiceTest.java index 2e237e2..b1988cc 100644 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/ExecutorCompletionServiceTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/ExecutorCompletionServiceTest.java @@ -11,6 +11,7 @@ package tests.api.java.util.concurrent; import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import java.util.concurrent.atomic.*; import java.math.BigInteger; import java.security.*; @@ -88,7 +89,7 @@ public class ExecutorCompletionServiceTest extends JSR166TestCase{ Callable c = new StringTask(); ecs.submit(c); Future f = ecs.take(); - assert(f.isDone()); + assertTrue(f.isDone()); } catch (Exception ex) { unexpectedException(); } finally { @@ -128,7 +129,7 @@ public class ExecutorCompletionServiceTest extends JSR166TestCase{ for (;;) { Future f = ecs.poll(); if (f != null) { - assert(f.isDone()); + assertTrue(f.isDone()); break; } } @@ -151,12 +152,11 @@ public class ExecutorCompletionServiceTest extends JSR166TestCase{ ecs.submit(c); Future f = ecs.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); if (f != null) - assert(f.isDone()); + assertTrue(f.isDone()); } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); } } - } diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/ExecutorsTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/ExecutorsTest.java index 40e23e4..e8fc7e5 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/ExecutorsTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/ExecutorsTest.java @@ -16,7 +16,7 @@ import java.security.*; public class ExecutorsTest 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(ExecutorsTest.class); @@ -26,13 +26,13 @@ public class ExecutorsTest extends JSR166TestCase{ private final ExecutorService exec; private final Callable<T> func; private final long msecs; - + TimedCallable(ExecutorService exec, Callable<T> func, long msecs) { this.exec = exec; this.func = func; this.msecs = msecs; } - + public T call() throws Exception { Future<T> ftask = exec.submit(func); try { @@ -295,13 +295,13 @@ public class ExecutorsTest extends JSR166TestCase{ List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N); try { long startTime = System.currentTimeMillis(); - + long i = 0; while (tasks.size() < N) { tasks.add(new TimedCallable<BigInteger>(executor, new Fib(i), 1)); i += 10; } - + int iters = 0; BigInteger sum = BigInteger.ZERO; for (Iterator<Callable<BigInteger>> it = tasks.iterator(); it.hasNext();) { @@ -326,7 +326,7 @@ public class ExecutorsTest extends JSR166TestCase{ } } - + /** * ThreadPoolExecutor using defaultThreadFactory has * specified group, priority, daemon status, and name @@ -335,31 +335,31 @@ public class ExecutorsTest extends JSR166TestCase{ final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); Runnable r = new Runnable() { public void run() { - try { - Thread current = Thread.currentThread(); - threadAssertTrue(!current.isDaemon()); - threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY); - ThreadGroup g = current.getThreadGroup(); - SecurityManager s = System.getSecurityManager(); - if (s != null) - threadAssertTrue(g == s.getThreadGroup()); - else - threadAssertTrue(g == egroup); - String name = current.getName(); - threadAssertTrue(name.endsWith("thread-1")); - } catch (SecurityException ok) { - // Also pass if not allowed to change setting - } + try { + Thread current = Thread.currentThread(); + threadAssertTrue(!current.isDaemon()); + threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); + ThreadGroup g = current.getThreadGroup(); + SecurityManager s = System.getSecurityManager(); + if (s != null) + threadAssertTrue(g == s.getThreadGroup()); + else + threadAssertTrue(g == egroup); + String name = current.getName(); + threadAssertTrue(name.endsWith("thread-1")); + } catch (SecurityException ok) { + // Also pass if not allowed to change setting + } } }; ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); - + e.execute(r); try { e.shutdown(); } catch(SecurityException ok) { } - + try { Thread.sleep(SHORT_DELAY_MS); } catch (Exception eX) { @@ -390,27 +390,27 @@ public class ExecutorsTest extends JSR166TestCase{ final AccessControlContext thisacc = AccessController.getContext(); Runnable r = new Runnable() { public void run() { - try { - Thread current = Thread.currentThread(); - threadAssertTrue(!current.isDaemon()); - threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY); - ThreadGroup g = current.getThreadGroup(); - SecurityManager s = System.getSecurityManager(); - if (s != null) - threadAssertTrue(g == s.getThreadGroup()); - else - threadAssertTrue(g == egroup); - String name = current.getName(); - threadAssertTrue(name.endsWith("thread-1")); - threadAssertTrue(thisccl == current.getContextClassLoader()); - threadAssertTrue(thisacc.equals(AccessController.getContext())); - } catch(SecurityException ok) { - // Also pass if not allowed to change settings - } + try { + Thread current = Thread.currentThread(); + threadAssertTrue(!current.isDaemon()); + threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); + ThreadGroup g = current.getThreadGroup(); + SecurityManager s = System.getSecurityManager(); + if (s != null) + threadAssertTrue(g == s.getThreadGroup()); + else + threadAssertTrue(g == egroup); + String name = current.getName(); + threadAssertTrue(name.endsWith("thread-1")); + threadAssertTrue(thisccl == current.getContextClassLoader()); + threadAssertTrue(thisacc.equals(AccessController.getContext())); + } catch(SecurityException ok) { + // Also pass if not allowed to change settings + } } }; ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); - + Policy.setPolicy(savedPolicy); e.execute(r); try { @@ -460,7 +460,7 @@ public class ExecutorsTest extends JSR166TestCase{ Policy.setPolicy(savedPolicy); return; } catch(AccessControlException ok) { - } + } try { Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); @@ -468,7 +468,7 @@ public class ExecutorsTest extends JSR166TestCase{ } catch(AccessControlException success) { } catch(Exception ex) { unexpectedException(); - } + } finally { Policy.setPolicy(savedPolicy); } @@ -489,13 +489,13 @@ public class ExecutorsTest extends JSR166TestCase{ } catch (AccessControlException ok) { return; } - + try { Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); task.call(); } catch(Exception ex) { unexpectedException(); - } + } finally { Policy.setPolicy(savedPolicy); } @@ -520,7 +520,7 @@ public class ExecutorsTest extends JSR166TestCase{ return; // program has too few permissions to set up test } - // Make sure that program doesn't have too many permissions + // Make sure that program doesn't have too many permissions try { AccessController.doPrivileged(new PrivilegedAction() { public Object run() { @@ -538,7 +538,7 @@ public class ExecutorsTest extends JSR166TestCase{ } catch(AccessControlException success) { } catch(Exception ex) { unexpectedException(); - } + } } /** diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/JSR166TestCase.java b/concurrent/src/test/java/tests/api/java/util/concurrent/JSR166TestCase.java index 70dafbc..c900616 100644 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/JSR166TestCase.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/JSR166TestCase.java @@ -2,8 +2,8 @@ * 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. */ package tests.api.java.util.concurrent; @@ -19,13 +19,13 @@ import java.security.*; * utility methods and classes, as well as a simple framework for * helping to make sure that assertions failing in generated threads * cause the associated test that generated them to itself fail (which - * JUnit doe not otherwise arrange). The rules for creating such + * JUnit does not otherwise arrange). The rules for creating such * tests are: * * <ol> * * <li> All assertions in code running in generated threads must use - * the forms {@link #threadFail} , {@link #threadAssertTrue} {@link + * the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link * #threadAssertEquals}, or {@link #threadAssertNull}, (not * <tt>fail</tt>, <tt>assertTrue</tt>, etc.) It is OK (but not * particularly recommended) for other code to use these forms too. @@ -46,7 +46,7 @@ import java.security.*; * is always discriminable as larger than SHORT and smaller than * MEDIUM. And so on. These constants are set to conservative values, * but even so, if there is ever any doubt, they can all be increased - * in one spot to rerun tests on slower platforms</li> + * in one spot to rerun tests on slower platforms.</li> * * <li> All threads generated must be joined inside each test case * method (or <tt>fail</tt> to do so) before returning from the @@ -65,7 +65,7 @@ import java.security.*; * "normal" behaviors differ significantly. And sometimes testcases * cover multiple methods when they cannot be tested in * isolation.</li> - * + * * <li> The documentation style for testcases is to provide as javadoc * a simple sentence or two describing the property that the testcase * method purports to test. The javadocs do not say anything about how @@ -90,10 +90,10 @@ import java.security.*; public class JSR166TestCase extends TestCase { /** * Runs all JSR166 unit tests using junit.textui.TestRunner - */ + */ public static void main (String[] args) { int iters = 1; - if (args.length > 0) + if (args.length > 0) iters = Integer.parseInt(args[0]); Test s = suite(); for (int i = 0; i < iters; ++i) { @@ -106,7 +106,7 @@ public class JSR166TestCase extends TestCase { /** * Collects all JSR166 unit tests as one suite - */ + */ public static Test suite ( ) { TestSuite suite = tests.TestSuiteFactory.createTestSuite("JSR166 Unit Tests"); // BEGIN android-changed @@ -114,18 +114,18 @@ public class JSR166TestCase extends TestCase { suite.addTest(AbstractQueueTest.suite()); suite.addTest(AbstractQueuedSynchronizerTest.suite()); suite.addTest(ArrayBlockingQueueTest.suite()); - suite.addTest(AtomicBooleanTest.suite()); - suite.addTest(AtomicIntegerArrayTest.suite()); - suite.addTest(AtomicIntegerFieldUpdaterTest.suite()); - suite.addTest(AtomicIntegerTest.suite()); - suite.addTest(AtomicLongArrayTest.suite()); - suite.addTest(AtomicLongFieldUpdaterTest.suite()); - suite.addTest(AtomicLongTest.suite()); - suite.addTest(AtomicMarkableReferenceTest.suite()); - suite.addTest(AtomicReferenceArrayTest.suite()); - suite.addTest(AtomicReferenceFieldUpdaterTest.suite()); - suite.addTest(AtomicReferenceTest.suite()); - suite.addTest(AtomicStampedReferenceTest.suite()); + suite.addTest(AtomicBooleanTest.suite()); + suite.addTest(AtomicIntegerArrayTest.suite()); + suite.addTest(AtomicIntegerFieldUpdaterTest.suite()); + suite.addTest(AtomicIntegerTest.suite()); + suite.addTest(AtomicLongArrayTest.suite()); + suite.addTest(AtomicLongFieldUpdaterTest.suite()); + suite.addTest(AtomicLongTest.suite()); + suite.addTest(AtomicMarkableReferenceTest.suite()); + suite.addTest(AtomicReferenceArrayTest.suite()); + suite.addTest(AtomicReferenceFieldUpdaterTest.suite()); + suite.addTest(AtomicReferenceTest.suite()); + suite.addTest(AtomicStampedReferenceTest.suite()); suite.addTest(ConcurrentHashMapTest.suite()); suite.addTest(ConcurrentLinkedQueueTest.suite()); suite.addTest(CopyOnWriteArrayListTest.suite()); @@ -164,16 +164,16 @@ public class JSR166TestCase extends TestCase { /** - * Return the shortest timed delay. This could + * Returns the shortest timed delay. This could * be reimplemented to use for example a Property. - */ + */ protected long getShortDelay() { return 50; } /** - * Set delays as multiples of SHORT_DELAY. + * Sets delays as multiples of SHORT_DELAY. */ protected void setDelays() { SHORT_DELAY_MS = getShortDelay(); @@ -188,23 +188,23 @@ public class JSR166TestCase extends TestCase { volatile boolean threadFailed; /** - * Initialize test to indicate that no thread assertions have failed + * Initializes test to indicate that no thread assertions have failed */ - public void setUp() { + public void setUp() { setDelays(); - threadFailed = false; + threadFailed = false; } /** - * Trigger test case failure if any thread assertions have failed + * Triggers test case failure if any thread assertions have failed */ - public void tearDown() { - assertFalse(threadFailed); + public void tearDown() { + assertFalse(threadFailed); } /** * Fail, also setting status to indicate current testcase should fail - */ + */ public void threadFail(String reason) { threadFailed = true; fail(reason); @@ -213,7 +213,7 @@ public class JSR166TestCase extends TestCase { /** * If expression not true, set status to indicate current testcase * should fail - */ + */ public void threadAssertTrue(boolean b) { if (!b) { threadFailed = true; @@ -224,7 +224,7 @@ public class JSR166TestCase extends TestCase { /** * If expression not false, set status to indicate current testcase * should fail - */ + */ public void threadAssertFalse(boolean b) { if (b) { threadFailed = true; @@ -235,7 +235,7 @@ public class JSR166TestCase extends TestCase { /** * If argument not null, set status to indicate current testcase * should fail - */ + */ public void threadAssertNull(Object x) { if (x != null) { threadFailed = true; @@ -246,7 +246,7 @@ public class JSR166TestCase extends TestCase { /** * If arguments not equal, set status to indicate current testcase * should fail - */ + */ public void threadAssertEquals(long x, long y) { if (x != y) { threadFailed = true; @@ -257,7 +257,7 @@ public class JSR166TestCase extends TestCase { /** * If arguments not equal, set status to indicate current testcase * should fail - */ + */ public void threadAssertEquals(Object x, Object y) { if (x != y && (x == null || !x.equals(y))) { threadFailed = true; @@ -267,10 +267,15 @@ public class JSR166TestCase extends TestCase { /** * threadFail with message "should throw exception" - */ + */ public void threadShouldThrow() { - threadFailed = true; - fail("should throw exception"); + try { + threadFailed = true; + fail("should throw exception"); + } catch (AssertionFailedError e) { + e.printStackTrace(); + throw e; + } } /** @@ -281,6 +286,14 @@ public class JSR166TestCase extends TestCase { fail("Unexpected exception"); } + /** + * threadFail with message "Unexpected exception", with argument + */ + public void threadUnexpectedException(Throwable ex) { + threadFailed = true; + ex.printStackTrace(); + fail("Unexpected exception: " + ex); + } /** * Wait out termination of a thread pool or fail doing so @@ -299,7 +312,7 @@ public class JSR166TestCase extends TestCase { /** * fail with message "should throw exception" - */ + */ public void shouldThrow() { fail("Should throw exception"); } @@ -334,6 +347,7 @@ public class JSR166TestCase extends TestCase { static final Integer m3 = new Integer(-3); static final Integer m4 = new Integer(-4); static final Integer m5 = new Integer(-5); + static final Integer m6 = new Integer(-6); static final Integer m10 = new Integer(-10); @@ -389,7 +403,7 @@ public class JSR166TestCase extends TestCase { Thread.sleep(SHORT_DELAY_MS); } catch(Exception e) { - threadUnexpectedException(); + threadUnexpectedException(e); } } } @@ -411,7 +425,7 @@ public class JSR166TestCase extends TestCase { Thread.sleep(SMALL_DELAY_MS); } catch(Exception e) { - threadUnexpectedException(); + threadUnexpectedException(e); } } } @@ -432,7 +446,7 @@ public class JSR166TestCase extends TestCase { Thread.sleep(SMALL_DELAY_MS); } catch(Exception e) { - threadUnexpectedException(); + threadUnexpectedException(e); } return Boolean.TRUE; } @@ -456,7 +470,7 @@ public class JSR166TestCase extends TestCase { Thread.sleep(MEDIUM_DELAY_MS); } catch(Exception e) { - threadUnexpectedException(); + threadUnexpectedException(e); } } } @@ -498,7 +512,7 @@ public class JSR166TestCase extends TestCase { static class SimpleThreadFactory implements ThreadFactory{ public Thread newThread(Runnable r){ return new Thread(r); - } + } } static class TrackedShortRunnable implements Runnable { @@ -558,8 +572,8 @@ public class JSR166TestCase extends TestCase { * For use as RejectedExecutionHandler in constructors */ static class NoOpREHandler implements RejectedExecutionHandler{ - public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} + public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} } - - + + } diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/LinkedBlockingQueueTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/LinkedBlockingQueueTest.java index 7496a4a..6648afb 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/LinkedBlockingQueueTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/LinkedBlockingQueueTest.java @@ -16,7 +16,7 @@ import java.io.*; public class LinkedBlockingQueueTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run (suite()); } public static Test suite() { @@ -38,7 +38,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { assertEquals(n, q.size()); return q; } - + /** * A new queue has the indicated capacity, or Integer.MAX_VALUE if * none given @@ -151,7 +151,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { LinkedBlockingQueue q = new LinkedBlockingQueue(1); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } } /** @@ -162,7 +162,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { LinkedBlockingQueue q = new LinkedBlockingQueue(1); q.add(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } } /** @@ -186,7 +186,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { assertEquals(0, q.remainingCapacity()); q.add(new Integer(SIZE)); } catch (IllegalStateException success){ - } + } } /** @@ -280,9 +280,9 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); q.put(null); shouldThrow(); - } + } catch (NullPointerException success){ - } + } catch (InterruptedException ie) { unexpectedException(); } @@ -323,11 +323,11 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { threadShouldThrow(); } catch (InterruptedException ie){ threadAssertEquals(added, SIZE); - } + } }}); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); + try { + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -386,7 +386,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { } catch (InterruptedException success){} } }); - + try { t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -408,7 +408,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { } } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -421,7 +421,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { try { q.take(); threadShouldThrow(); - } catch (InterruptedException success){ } + } catch (InterruptedException success){ } } }); try { @@ -448,11 +448,11 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { q.take(); threadShouldThrow(); } catch (InterruptedException success){ - } + } }}); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); + try { + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -485,7 +485,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { assertNull(q.poll(0, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -500,7 +500,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -517,11 +517,11 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { } threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); } catch (InterruptedException success){ - } + } }}); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); + try { + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -543,7 +543,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); threadShouldThrow(); - } catch (InterruptedException success) { } + } catch (InterruptedException success) { } } }); try { @@ -555,7 +555,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { } catch (Exception e){ unexpectedException(); } - } + } /** * peek returns next element, or null if empty @@ -599,7 +599,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { q.remove(); shouldThrow(); } catch (NoSuchElementException success){ - } + } } /** @@ -616,7 +616,24 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { } assertTrue(q.isEmpty()); } - + + /** + * An add following remove(x) succeeds + */ + public void testRemoveElementAndAdd() { + try { + LinkedBlockingQueue q = new LinkedBlockingQueue(); + assertTrue(q.add(new Integer(1))); + assertTrue(q.add(new Integer(2))); + assertTrue(q.remove(new Integer(1))); + assertTrue(q.remove(new Integer(2))); + assertTrue(q.add(new Integer(3))); + assertTrue(q.take() != null); + } catch (Exception e){ + unexpectedException(); + } + } + /** * contains(x) reports true when elements added but not yet removed */ @@ -640,6 +657,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { assertEquals(SIZE, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); + assertTrue(q.contains(one)); q.clear(); assertTrue(q.isEmpty()); } @@ -704,7 +722,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { assertEquals(o[i], q.take()); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -719,7 +737,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { assertEquals(ints[i], q.take()); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -744,7 +762,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { } catch(ArrayStoreException success){} } - + /** * iterator iterates through all elements */ @@ -757,7 +775,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { } } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -772,7 +790,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { Iterator it = q.iterator(); it.next(); it.remove(); - + it = q.iterator(); assertEquals(it.next(), one); assertEquals(it.next(), three); @@ -827,7 +845,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { for (int i = 0; i < SIZE; ++i) { assertTrue(s.indexOf(String.valueOf(i)) >= 0); } - } + } /** @@ -862,7 +880,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { } } }); - + joinPool(executor); } @@ -896,7 +914,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { } } }); - + joinPool(executor); } @@ -916,7 +934,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); assertEquals(q.size(), r.size()); - while (!q.isEmpty()) + while (!q.isEmpty()) assertEquals(q.remove(), r.remove()); } catch(Exception e){ unexpectedException(); @@ -925,7 +943,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { /** * drainTo(null) throws NPE - */ + */ public void testDrainToNull() { LinkedBlockingQueue q = populatedQueue(SIZE); try { @@ -937,7 +955,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { /** * drainTo(this) throws IAE - */ + */ public void testDrainToSelf() { LinkedBlockingQueue q = populatedQueue(SIZE); try { @@ -949,27 +967,38 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { /** * drainTo(c) empties queue into another collection c - */ + */ public void testDrainTo() { LinkedBlockingQueue 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() { final LinkedBlockingQueue q = populatedQueue(SIZE); Thread t = new Thread(new Runnable() { public void run() { try { q.put(new Integer(SIZE+1)); - } catch (InterruptedException ie){ + } catch (InterruptedException ie){ threadUnexpectedException(); } } @@ -979,7 +1008,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { ArrayList l = new ArrayList(); q.drainTo(l); assertTrue(l.size() >= SIZE); - for (int i = 0; i < SIZE; ++i) + for (int i = 0; i < SIZE; ++i) assertEquals(l.get(i), new Integer(i)); t.join(); assertTrue(q.size() + l.size() >= SIZE); @@ -990,7 +1019,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { /** * drainTo(null, n) throws NPE - */ + */ public void testDrainToNullN() { LinkedBlockingQueue q = populatedQueue(SIZE); try { @@ -1002,7 +1031,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { /** * drainTo(this, n) throws IAE - */ + */ public void testDrainToSelfN() { LinkedBlockingQueue q = populatedQueue(SIZE); try { @@ -1014,17 +1043,20 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { /** * drainTo(c, n) empties first max {n, size} elements of queue into c - */ + */ public void testDrainToN() { + LinkedBlockingQueue q = new LinkedBlockingQueue(); for (int i = 0; i < SIZE + 2; ++i) { - LinkedBlockingQueue 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) ; } } diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/LockSupportTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/LockSupportTest.java index 8d74a3a..b39db2e 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/LockSupportTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/LockSupportTest.java @@ -15,7 +15,7 @@ import java.util.concurrent.locks.*; public class LockSupportTest 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(LockSupportTest.class); @@ -24,7 +24,7 @@ public class LockSupportTest extends JSR166TestCase{ /** * park is released by unpark occurring after park */ - public void testPark() { + public void testPark() { Thread t = new Thread(new Runnable() { public void run() { try { @@ -48,7 +48,7 @@ public class LockSupportTest extends JSR166TestCase{ /** * park is released by unpark occurring before park */ - public void testPark2() { + public void testPark2() { Thread t = new Thread(new Runnable() { public void run() { try { @@ -70,15 +70,14 @@ public class LockSupportTest extends JSR166TestCase{ } /** - * park is released by interrupt + * park is released by interrupt */ - public void testPark3() { - Thread t = new Thread(new Runnable() { - public void run() { - try { - LockSupport.park(); - threadAssertTrue(Thread.interrupted()); - } catch(Exception e){ + public void testPark3() { + Thread t = new Thread(new Runnable() { + public void run() { + try { + LockSupport.park(); + } catch(Exception e){ threadUnexpectedException(); } } @@ -97,7 +96,7 @@ public class LockSupportTest extends JSR166TestCase{ /** * park returns if interrupted before park */ - public void testPark4() { + public void testPark4() { final ReentrantLock lock = new ReentrantLock(); lock.lock(); Thread t = new Thread(new Runnable() { @@ -124,7 +123,7 @@ public class LockSupportTest extends JSR166TestCase{ /** * parkNanos times out if not unparked */ - public void testParkNanos() { + public void testParkNanos() { Thread t = new Thread(new Runnable() { public void run() { try { @@ -147,7 +146,7 @@ public class LockSupportTest extends JSR166TestCase{ /** * parkUntil times out if not unparked */ - public void testParkUntil() { + public void testParkUntil() { Thread t = new Thread(new Runnable() { public void run() { try { diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/PriorityBlockingQueueTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/PriorityBlockingQueueTest.java index b258963..3857e0f 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/PriorityBlockingQueueTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/PriorityBlockingQueueTest.java @@ -628,9 +628,9 @@ public class PriorityBlockingQueueTest extends JSR166TestCase { q.clear(); assertTrue(q.isEmpty()); assertEquals(0, q.size()); - assertEquals(NOCAP, q.remainingCapacity()); - q.add(new Integer(1)); + q.add(one); assertFalse(q.isEmpty()); + assertTrue(q.contains(one)); q.clear(); assertTrue(q.isEmpty()); } @@ -873,6 +873,17 @@ public class PriorityBlockingQueueTest extends JSR166TestCase { assertEquals(l.size(), SIZE); 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)); } /** @@ -927,15 +938,18 @@ public class PriorityBlockingQueueTest extends JSR166TestCase { * drainTo(c, n) empties first max {n, size} elements of queue into c */ public void testDrainToN() { + PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2); for (int i = 0; i < SIZE + 2; ++i) { - PriorityBlockingQueue 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); + assertEquals(q.size(), SIZE-k); for (int j = 0; j < k; ++j) - assertTrue(l.contains(new Integer(j))); + assertEquals(l.get(j), new Integer(j)); + while (q.poll() != null) ; } } diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/ReentrantLockTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/ReentrantLockTest.java index fdc9f31..c50482d 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/ReentrantLockTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/ReentrantLockTest.java @@ -16,7 +16,7 @@ import java.io.*; public class ReentrantLockTest 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(ReentrantLockTest.class); @@ -56,11 +56,11 @@ public class ReentrantLockTest extends JSR166TestCase { */ static class PublicReentrantLock extends ReentrantLock { PublicReentrantLock() { super(); } - public Collection<Thread> getQueuedThreads() { - return super.getQueuedThreads(); + public Collection<Thread> getQueuedThreads() { + return super.getQueuedThreads(); } - public Collection<Thread> getWaitingThreads(Condition c) { - return super.getWaitingThreads(c); + public Collection<Thread> getWaitingThreads(Condition c) { + return super.getWaitingThreads(c); } @@ -69,7 +69,7 @@ public class ReentrantLockTest extends JSR166TestCase { /** * Constructor sets given fairness */ - public void testConstructor() { + public void testConstructor() { ReentrantLock rl = new ReentrantLock(); assertFalse(rl.isFair()); ReentrantLock r2 = new ReentrantLock(true); @@ -79,7 +79,7 @@ public class ReentrantLockTest extends JSR166TestCase { /** * locking an unlocked lock succeeds */ - public void testLock() { + public void testLock() { ReentrantLock rl = new ReentrantLock(); rl.lock(); assertTrue(rl.isLocked()); @@ -89,7 +89,7 @@ public class ReentrantLockTest extends JSR166TestCase { /** * locking an unlocked fair lock succeeds */ - public void testFairLock() { + public void testFairLock() { ReentrantLock rl = new ReentrantLock(true); rl.lock(); assertTrue(rl.isLocked()); @@ -99,7 +99,7 @@ public class ReentrantLockTest extends JSR166TestCase { /** * Unlocking an unlocked lock throws IllegalMonitorStateException */ - public void testUnlock_IllegalMonitorStateException() { + public void testUnlock_IllegalMonitorStateException() { ReentrantLock rl = new ReentrantLock(); try { rl.unlock(); @@ -111,7 +111,7 @@ public class ReentrantLockTest extends JSR166TestCase { /** * tryLock on an unlocked lock succeeds */ - public void testTryLock() { + public void testTryLock() { ReentrantLock rl = new ReentrantLock(); assertTrue(rl.tryLock()); assertTrue(rl.isLocked()); @@ -122,7 +122,7 @@ public class ReentrantLockTest extends JSR166TestCase { /** * hasQueuedThreads reports whether there are waiting threads */ - public void testhasQueuedThreads() { + public void testhasQueuedThreads() { final ReentrantLock lock = new ReentrantLock(); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); @@ -146,12 +146,12 @@ public class ReentrantLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } /** * getQueueLength reports number of waiting threads */ - public void testGetQueueLength() { + public void testGetQueueLength() { final ReentrantLock lock = new ReentrantLock(); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); @@ -175,12 +175,12 @@ public class ReentrantLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } /** * getQueueLength reports number of waiting threads */ - public void testGetQueueLength_fair() { + public void testGetQueueLength_fair() { final ReentrantLock lock = new ReentrantLock(true); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); @@ -204,12 +204,12 @@ public class ReentrantLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } /** * hasQueuedThread(null) throws NPE */ - public void testHasQueuedThreadNPE() { + public void testHasQueuedThreadNPE() { final ReentrantLock sync = new ReentrantLock(); try { sync.hasQueuedThread(null); @@ -221,7 +221,7 @@ public class ReentrantLockTest extends JSR166TestCase { /** * hasQueuedThread reports whether a thread is queued. */ - public void testHasQueuedThread() { + public void testHasQueuedThread() { final ReentrantLock sync = new ReentrantLock(); Thread t1 = new Thread(new InterruptedLockRunnable(sync)); Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); @@ -250,13 +250,13 @@ public class ReentrantLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } /** * getQueuedThreads includes waiting threads */ - public void testGetQueuedThreads() { + public void testGetQueuedThreads() { final PublicReentrantLock lock = new PublicReentrantLock(); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); @@ -283,13 +283,13 @@ public class ReentrantLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } /** * timed tryLock is interruptible. */ - public void testInterruptedException2() { + public void testInterruptedException2() { final ReentrantLock lock = new ReentrantLock(); lock.lock(); Thread t = new Thread(new Runnable() { @@ -312,7 +312,7 @@ public class ReentrantLockTest extends JSR166TestCase { /** * TryLock on a locked lock fails */ - public void testTryLockWhenLocked() { + public void testTryLockWhenLocked() { final ReentrantLock lock = new ReentrantLock(); lock.lock(); Thread t = new Thread(new Runnable() { @@ -327,12 +327,12 @@ public class ReentrantLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } /** * Timed tryLock on a locked lock times out */ - public void testTryLock_Timeout() { + public void testTryLock_Timeout() { final ReentrantLock lock = new ReentrantLock(); lock.lock(); Thread t = new Thread(new Runnable() { @@ -351,8 +351,8 @@ public class ReentrantLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } - + } + /** * getHoldCount returns number of recursive holds */ @@ -367,8 +367,8 @@ public class ReentrantLockTest extends JSR166TestCase { assertEquals(i-1,lock.getHoldCount()); } } - - + + /** * isLocked is true when locked and false when not */ @@ -378,7 +378,7 @@ public class ReentrantLockTest extends JSR166TestCase { assertTrue(lock.isLocked()); lock.unlock(); assertFalse(lock.isLocked()); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { lock.lock(); try { @@ -405,25 +405,27 @@ public class ReentrantLockTest extends JSR166TestCase { /** * lockInterruptibly is interruptible. */ - public void testLockInterruptibly1() { + public void testLockInterruptibly1() { final ReentrantLock lock = new ReentrantLock(); lock.lock(); Thread t = new Thread(new InterruptedLockRunnable(lock)); try { t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); + Thread.sleep(SHORT_DELAY_MS); lock.unlock(); t.join(); } catch(Exception e){ unexpectedException(); } - } + } /** * lockInterruptibly succeeds when unlocked, else is interruptible */ public void testLockInterruptibly2() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); try { lock.lockInterruptibly(); } catch(Exception e) { @@ -445,7 +447,7 @@ public class ReentrantLockTest extends JSR166TestCase { * Calling await without holding lock throws IllegalMonitorStateException */ public void testAwait_IllegalMonitor() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); try { c.await(); @@ -462,7 +464,7 @@ public class ReentrantLockTest extends JSR166TestCase { * Calling signal without holding lock throws IllegalMonitorStateException */ public void testSignal_IllegalMonitor() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); try { c.signal(); @@ -479,7 +481,7 @@ public class ReentrantLockTest extends JSR166TestCase { * awaitNanos without a signal times out */ public void testAwaitNanos_Timeout() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); try { lock.lock(); @@ -496,11 +498,11 @@ public class ReentrantLockTest extends JSR166TestCase { * timed await without a signal times out */ public void testAwait_Timeout() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); try { lock.lock(); - assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); lock.unlock(); } catch (Exception ex) { @@ -512,12 +514,12 @@ public class ReentrantLockTest extends JSR166TestCase { * awaitUntil without a signal times out */ public void testAwaitUntil_Timeout() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); try { lock.lock(); java.util.Date d = new java.util.Date(); - assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); + c.awaitUntil(new java.util.Date(d.getTime() + 10)); lock.unlock(); } catch (Exception ex) { @@ -529,9 +531,9 @@ public class ReentrantLockTest extends JSR166TestCase { * await returns when signalled */ public void testAwait() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { lock.lock(); @@ -670,9 +672,9 @@ public class ReentrantLockTest extends JSR166TestCase { * getWaitingThreads throws IAE if not owned */ public void testGetWaitingThreadsIAE() { - final PublicReentrantLock lock = new PublicReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); final Condition c = (lock.newCondition()); - final PublicReentrantLock lock2 = new PublicReentrantLock(); + final PublicReentrantLock lock2 = new PublicReentrantLock(); try { lock2.getWaitingThreads(c); shouldThrow(); @@ -686,7 +688,7 @@ public class ReentrantLockTest extends JSR166TestCase { * getWaitingThreads throws IMSE if not locked */ public void testGetWaitingThreadsIMSE() { - final PublicReentrantLock lock = new PublicReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); final Condition c = (lock.newCondition()); try { lock.getWaitingThreads(c); @@ -703,9 +705,9 @@ public class ReentrantLockTest extends JSR166TestCase { * hasWaiters returns true when a thread is waiting, else false */ public void testHasWaiters() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { lock.lock(); @@ -745,9 +747,9 @@ public class ReentrantLockTest extends JSR166TestCase { * getWaitQueueLength returns number of waiting threads */ public void testGetWaitQueueLength() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t1 = new Thread(new Runnable() { + Thread t1 = new Thread(new Runnable() { public void run() { try { lock.lock(); @@ -762,7 +764,7 @@ public class ReentrantLockTest extends JSR166TestCase { } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { lock.lock(); @@ -806,9 +808,9 @@ public class ReentrantLockTest extends JSR166TestCase { * getWaitingThreads returns only and all waiting threads */ public void testGetWaitingThreads() { - final PublicReentrantLock lock = new PublicReentrantLock(); + final PublicReentrantLock lock = new PublicReentrantLock(); final Condition c = lock.newCondition(); - Thread t1 = new Thread(new Runnable() { + Thread t1 = new Thread(new Runnable() { public void run() { try { lock.lock(); @@ -822,7 +824,7 @@ public class ReentrantLockTest extends JSR166TestCase { } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { lock.lock(); @@ -865,34 +867,61 @@ public class ReentrantLockTest extends JSR166TestCase { } } + /** A helper class for uninterruptible wait tests */ + class UninterruptableThread extends Thread { + private ReentrantLock lock; + private Condition c; + + public volatile boolean canAwake = false; + public volatile boolean interrupted = false; + public volatile boolean lockStarted = false; + + public UninterruptableThread(ReentrantLock lock, Condition c) { + this.lock = lock; + this.c = c; + } + + public synchronized void run() { + lock.lock(); + lockStarted = true; + while (!canAwake) { + c.awaitUninterruptibly(); + } + + interrupted = isInterrupted(); + lock.unlock(); + } + } /** * awaitUninterruptibly doesn't abort on interrupt */ public void testAwaitUninterruptibly() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new Runnable() { - public void run() { - lock.lock(); - c.awaitUninterruptibly(); - lock.unlock(); - } - }); + UninterruptableThread thread = new UninterruptableThread(lock, c); try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); + thread.start(); + + while (!thread.lockStarted) { + Thread.sleep(100); + } + lock.lock(); - c.signal(); - lock.unlock(); - assert(t.isInterrupted()); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); - } - catch (Exception ex) { + try { + thread.interrupt(); + thread.canAwake = true; + c.signal(); + } finally { + lock.unlock(); + } + + thread.join(); + assertTrue(thread.interrupted); + assertFalse(thread.isAlive()); + } catch (Exception ex) { unexpectedException(); } } @@ -901,9 +930,9 @@ public class ReentrantLockTest extends JSR166TestCase { * await is interruptible */ public void testAwait_Interrupt() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { lock.lock(); @@ -932,9 +961,9 @@ public class ReentrantLockTest extends JSR166TestCase { * awaitNanos is interruptible */ public void testAwaitNanos_Interrupt() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { lock.lock(); @@ -963,9 +992,9 @@ public class ReentrantLockTest extends JSR166TestCase { * awaitUntil is interruptible */ public void testAwaitUntil_Interrupt() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { lock.lock(); @@ -995,9 +1024,9 @@ public class ReentrantLockTest extends JSR166TestCase { * signalAll wakes up all threads */ public void testSignalAll() { - final ReentrantLock lock = new ReentrantLock(); + final ReentrantLock lock = new ReentrantLock(); final Condition c = lock.newCondition(); - Thread t1 = new Thread(new Runnable() { + Thread t1 = new Thread(new Runnable() { public void run() { try { lock.lock(); @@ -1010,7 +1039,7 @@ public class ReentrantLockTest extends JSR166TestCase { } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { lock.lock(); @@ -1041,6 +1070,61 @@ public class ReentrantLockTest extends JSR166TestCase { } /** + * await after multiple reentrant locking preserves lock count + */ + public void testAwaitLockCount() { + final ReentrantLock lock = new ReentrantLock(); + final Condition c = lock.newCondition(); + Thread t1 = new Thread(new Runnable() { + public void run() { + try { + lock.lock(); + threadAssertEquals(1, lock.getHoldCount()); + c.await(); + threadAssertEquals(1, lock.getHoldCount()); + lock.unlock(); + } + catch(InterruptedException e) { + threadUnexpectedException(); + } + } + }); + + Thread t2 = new Thread(new Runnable() { + public void run() { + try { + lock.lock(); + lock.lock(); + threadAssertEquals(2, lock.getHoldCount()); + c.await(); + threadAssertEquals(2, lock.getHoldCount()); + lock.unlock(); + lock.unlock(); + } + catch(InterruptedException e) { + threadUnexpectedException(); + } + } + }); + + try { + t1.start(); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + lock.lock(); + c.signalAll(); + lock.unlock(); + t1.join(SHORT_DELAY_MS); + t2.join(SHORT_DELAY_MS); + assertFalse(t1.isAlive()); + assertFalse(t2.isAlive()); + } + catch (Exception ex) { + unexpectedException(); + } + } + + /** * A serialized lock deserializes as unlocked */ public void testSerialization() { diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/ReentrantReadWriteLockTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/ReentrantReadWriteLockTest.java index 8925b42..e38165a 100644 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/ReentrantReadWriteLockTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/ReentrantReadWriteLockTest.java @@ -16,7 +16,7 @@ import java.util.*; public class ReentrantReadWriteLockTest 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(ReentrantReadWriteLockTest.class); @@ -56,18 +56,18 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { */ static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock { PublicReentrantReadWriteLock() { super(); } - public Collection<Thread> getQueuedThreads() { - return super.getQueuedThreads(); + public Collection<Thread> getQueuedThreads() { + return super.getQueuedThreads(); } - public Collection<Thread> getWaitingThreads(Condition c) { - return super.getWaitingThreads(c); + public Collection<Thread> getWaitingThreads(Condition c) { + return super.getWaitingThreads(c); } } /** * Constructor sets given fairness, and is in unlocked state */ - public void testConstructor() { + public void testConstructor() { ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); assertFalse(rl.isFair()); assertFalse(rl.isWriteLocked()); @@ -81,7 +81,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { /** * write-locking and read-locking an unlocked lock succeed */ - public void testLock() { + public void testLock() { ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); rl.writeLock().lock(); assertTrue(rl.isWriteLocked()); @@ -105,7 +105,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { /** * locking an unlocked fair lock succeeds */ - public void testFairLock() { + public void testFairLock() { ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true); rl.writeLock().lock(); assertTrue(rl.isWriteLocked()); @@ -128,23 +128,23 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { /** * getWriteHoldCount returns number of recursive holds */ - public void testGetHoldCount() { - ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - for(int i = 1; i <= SIZE; i++) { - lock.writeLock().lock(); - assertEquals(i,lock.getWriteHoldCount()); - } - for(int i = SIZE; i > 0; i--) { - lock.writeLock().unlock(); - assertEquals(i-1,lock.getWriteHoldCount()); - } + public void testGetWriteHoldCount() { + ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + for(int i = 1; i <= SIZE; i++) { + lock.writeLock().lock(); + assertEquals(i,lock.getWriteHoldCount()); + } + for(int i = SIZE; i > 0; i--) { + lock.writeLock().unlock(); + assertEquals(i-1,lock.getWriteHoldCount()); + } } - + /** * write-unlocking an unlocked lock throws IllegalMonitorStateException */ - public void testUnlock_IllegalMonitorStateException() { + public void testUnlock_IllegalMonitorStateException() { ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); try { rl.writeLock().unlock(); @@ -156,7 +156,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { /** * write-lockInterruptibly is interruptible */ - public void testWriteLockInterruptibly_Interrupted() { + public void testWriteLockInterruptibly_Interrupted() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); Thread t = new Thread(new Runnable() { public void run() { @@ -171,18 +171,20 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { try { lock.writeLock().lock(); t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); + Thread.sleep(SHORT_DELAY_MS); lock.writeLock().unlock(); t.join(); } catch(Exception e){ unexpectedException(); } - } + } /** * timed write-tryLock is interruptible */ - public void testWriteTryLock_Interrupted() { + public void testWriteTryLock_Interrupted() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); Thread t = new Thread(new Runnable() { @@ -205,7 +207,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { /** * read-lockInterruptibly is interruptible */ - public void testReadLockInterruptibly_Interrupted() { + public void testReadLockInterruptibly_Interrupted() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); Thread t = new Thread(new Runnable() { @@ -217,18 +219,20 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { }); try { t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); + Thread.sleep(SHORT_DELAY_MS); lock.writeLock().unlock(); t.join(); } catch(Exception e){ unexpectedException(); } - } + } /** * timed read-tryLock is interruptible */ - public void testReadTryLock_Interrupted() { + public void testReadTryLock_Interrupted() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); Thread t = new Thread(new Runnable() { @@ -248,11 +252,11 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } } - + /** * write-tryLock fails if locked */ - public void testWriteTryLockWhenLocked() { + public void testWriteTryLockWhenLocked() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); Thread t = new Thread(new Runnable() { @@ -267,12 +271,12 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } /** * read-tryLock fails if locked */ - public void testReadTryLockWhenLocked() { + public void testReadTryLockWhenLocked() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); Thread t = new Thread(new Runnable() { @@ -287,12 +291,12 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } /** * Multiple threads can hold a read lock when not write-locked */ - public void testMultipleReadLocks() { + public void testMultipleReadLocks() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().lock(); Thread t = new Thread(new Runnable() { @@ -308,12 +312,12 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } /** * A writelock succeeds after reading threads unlock */ - public void testWriteAfterMultipleReadLocks() { + public void testWriteAfterMultipleReadLocks() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().lock(); Thread t1 = new Thread(new Runnable() { @@ -338,16 +342,16 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { t2.join(MEDIUM_DELAY_MS); assertTrue(!t1.isAlive()); assertTrue(!t2.isAlive()); - + } catch(Exception e){ unexpectedException(); } - } + } /** * Readlocks succeed after a writing thread unlocks */ - public void testReadAfterWriteLock() { + public void testReadAfterWriteLock() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); Thread t1 = new Thread(new Runnable() { @@ -372,17 +376,280 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { t2.join(MEDIUM_DELAY_MS); assertTrue(!t1.isAlive()); assertTrue(!t2.isAlive()); - + + } catch(Exception e){ + unexpectedException(); + } + } + + /** + * Read trylock succeeds if write locked by current thread + */ + public void testReadHoldingWriteLock() { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + lock.writeLock().lock(); + assertTrue(lock.readLock().tryLock()); + lock.readLock().unlock(); + lock.writeLock().unlock(); + } + + /** + * Read lock succeeds if write locked by current thread even if + * other threads are waiting for readlock + */ + public void testReadHoldingWriteLock2() { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + lock.writeLock().lock(); + Thread t1 = new Thread(new Runnable() { + public void run() { + lock.readLock().lock(); + lock.readLock().unlock(); + } + }); + Thread t2 = new Thread(new Runnable() { + public void run() { + lock.readLock().lock(); + lock.readLock().unlock(); + } + }); + + try { + t1.start(); + t2.start(); + lock.readLock().lock(); + lock.readLock().unlock(); + Thread.sleep(SHORT_DELAY_MS); + lock.readLock().lock(); + lock.readLock().unlock(); + lock.writeLock().unlock(); + t1.join(MEDIUM_DELAY_MS); + t2.join(MEDIUM_DELAY_MS); + assertTrue(!t1.isAlive()); + assertTrue(!t2.isAlive()); + + } catch(Exception e){ + unexpectedException(); + } + } + + /** + * Read lock succeeds if write locked by current thread even if + * other threads are waiting for writelock + */ + public void testReadHoldingWriteLock3() { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + lock.writeLock().lock(); + Thread t1 = new Thread(new Runnable() { + public void run() { + lock.writeLock().lock(); + lock.writeLock().unlock(); + } + }); + Thread t2 = new Thread(new Runnable() { + public void run() { + lock.writeLock().lock(); + lock.writeLock().unlock(); + } + }); + + try { + t1.start(); + t2.start(); + lock.readLock().lock(); + lock.readLock().unlock(); + Thread.sleep(SHORT_DELAY_MS); + lock.readLock().lock(); + lock.readLock().unlock(); + lock.writeLock().unlock(); + t1.join(MEDIUM_DELAY_MS); + t2.join(MEDIUM_DELAY_MS); + assertTrue(!t1.isAlive()); + assertTrue(!t2.isAlive()); + + } catch(Exception e){ + unexpectedException(); + } + } + + + /** + * Write lock succeeds if write locked by current thread even if + * other threads are waiting for writelock + */ + public void testWriteHoldingWriteLock4() { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + lock.writeLock().lock(); + Thread t1 = new Thread(new Runnable() { + public void run() { + lock.writeLock().lock(); + lock.writeLock().unlock(); + } + }); + Thread t2 = new Thread(new Runnable() { + public void run() { + lock.writeLock().lock(); + lock.writeLock().unlock(); + } + }); + + try { + t1.start(); + t2.start(); + lock.writeLock().lock(); + lock.writeLock().unlock(); + Thread.sleep(SHORT_DELAY_MS); + lock.writeLock().lock(); + lock.writeLock().unlock(); + lock.writeLock().unlock(); + t1.join(MEDIUM_DELAY_MS); + t2.join(MEDIUM_DELAY_MS); + assertTrue(!t1.isAlive()); + assertTrue(!t2.isAlive()); + + } catch(Exception e){ + unexpectedException(); + } + } + + + /** + * Fair Read trylock succeeds if write locked by current thread + */ + public void testReadHoldingWriteLockFair() { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); + lock.writeLock().lock(); + assertTrue(lock.readLock().tryLock()); + lock.readLock().unlock(); + lock.writeLock().unlock(); + } + + /** + * Fair Read lock succeeds if write locked by current thread even if + * other threads are waiting for readlock + */ + public void testReadHoldingWriteLockFair2() { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); + lock.writeLock().lock(); + Thread t1 = new Thread(new Runnable() { + public void run() { + lock.readLock().lock(); + lock.readLock().unlock(); + } + }); + Thread t2 = new Thread(new Runnable() { + public void run() { + lock.readLock().lock(); + lock.readLock().unlock(); + } + }); + + try { + t1.start(); + t2.start(); + lock.readLock().lock(); + lock.readLock().unlock(); + Thread.sleep(SHORT_DELAY_MS); + lock.readLock().lock(); + lock.readLock().unlock(); + lock.writeLock().unlock(); + t1.join(MEDIUM_DELAY_MS); + t2.join(MEDIUM_DELAY_MS); + assertTrue(!t1.isAlive()); + assertTrue(!t2.isAlive()); + + } catch(Exception e){ + unexpectedException(); + } + } + + + /** + * Fair Read lock succeeds if write locked by current thread even if + * other threads are waiting for writelock + */ + public void testReadHoldingWriteLockFair3() { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); + lock.writeLock().lock(); + Thread t1 = new Thread(new Runnable() { + public void run() { + lock.writeLock().lock(); + lock.writeLock().unlock(); + } + }); + Thread t2 = new Thread(new Runnable() { + public void run() { + lock.writeLock().lock(); + lock.writeLock().unlock(); + } + }); + + try { + t1.start(); + t2.start(); + lock.readLock().lock(); + lock.readLock().unlock(); + Thread.sleep(SHORT_DELAY_MS); + lock.readLock().lock(); + lock.readLock().unlock(); + lock.writeLock().unlock(); + t1.join(MEDIUM_DELAY_MS); + t2.join(MEDIUM_DELAY_MS); + assertTrue(!t1.isAlive()); + assertTrue(!t2.isAlive()); + } catch(Exception e){ unexpectedException(); } - } + } + + + /** + * Fair Write lock succeeds if write locked by current thread even if + * other threads are waiting for writelock + */ + public void testWriteHoldingWriteLockFair4() { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); + lock.writeLock().lock(); + Thread t1 = new Thread(new Runnable() { + public void run() { + lock.writeLock().lock(); + lock.writeLock().unlock(); + } + }); + Thread t2 = new Thread(new Runnable() { + public void run() { + lock.writeLock().lock(); + lock.writeLock().unlock(); + } + }); + + try { + t1.start(); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.isWriteLockedByCurrentThread()); + assertTrue(lock.getWriteHoldCount() == 1); + lock.writeLock().lock(); + assertTrue(lock.getWriteHoldCount() == 2); + lock.writeLock().unlock(); + lock.writeLock().lock(); + lock.writeLock().unlock(); + lock.writeLock().unlock(); + t1.join(MEDIUM_DELAY_MS); + t2.join(MEDIUM_DELAY_MS); + assertTrue(!t1.isAlive()); + assertTrue(!t2.isAlive()); + + } catch(Exception e){ + unexpectedException(); + } + } /** * Read tryLock succeeds if readlocked but not writelocked */ - public void testTryLockWhenReadLocked() { + public void testTryLockWhenReadLocked() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().lock(); Thread t = new Thread(new Runnable() { @@ -398,14 +665,14 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } + - /** * write tryLock fails when readlocked */ - public void testWriteTryLockWhenReadLocked() { + public void testWriteTryLockWhenReadLocked() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().lock(); Thread t = new Thread(new Runnable() { @@ -420,14 +687,58 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } + + + /** + * Fair Read tryLock succeeds if readlocked but not writelocked + */ + public void testTryLockWhenReadLockedFair() { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); + lock.readLock().lock(); + Thread t = new Thread(new Runnable() { + public void run() { + threadAssertTrue(lock.readLock().tryLock()); + lock.readLock().unlock(); + } + }); + try { + t.start(); + t.join(); + lock.readLock().unlock(); + } catch(Exception e){ + unexpectedException(); + } + } + + + + /** + * Fair write tryLock fails when readlocked + */ + public void testWriteTryLockWhenReadLockedFair() { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); + lock.readLock().lock(); + Thread t = new Thread(new Runnable() { + public void run() { + threadAssertFalse(lock.writeLock().tryLock()); + } + }); + try { + t.start(); + t.join(); + lock.readLock().unlock(); + } catch(Exception e){ + unexpectedException(); + } + } + - /** * write timed tryLock times out if locked */ - public void testWriteTryLock_Timeout() { + public void testWriteTryLock_Timeout() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); Thread t = new Thread(new Runnable() { @@ -446,12 +757,12 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } /** * read timed tryLock times out if write-locked */ - public void testReadTryLock_Timeout() { + public void testReadTryLock_Timeout() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); Thread t = new Thread(new Runnable() { @@ -470,7 +781,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } /** @@ -483,7 +794,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } catch(Exception e) { unexpectedException(); } - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { lock.writeLock().lockInterruptibly(); @@ -495,7 +806,9 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { }); try { t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); + Thread.sleep(SHORT_DELAY_MS); t.join(); lock.writeLock().unlock(); } catch(Exception e){ @@ -513,7 +826,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } catch(Exception e) { unexpectedException(); } - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { lock.readLock().lockInterruptibly(); @@ -525,6 +838,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { }); try { t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); lock.writeLock().unlock(); @@ -537,7 +851,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { * Calling await without holding lock throws IllegalMonitorStateException */ public void testAwait_IllegalMonitor() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); try { c.await(); @@ -554,7 +868,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { * Calling signal without holding lock throws IllegalMonitorStateException */ public void testSignal_IllegalMonitor() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); try { c.signal(); @@ -571,7 +885,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { * awaitNanos without a signal times out */ public void testAwaitNanos_Timeout() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); try { lock.writeLock().lock(); @@ -589,11 +903,10 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { * timed await without a signal times out */ public void testAwait_Timeout() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); try { lock.writeLock().lock(); - assertFalse(c.await(10, TimeUnit.MILLISECONDS)); lock.writeLock().unlock(); } catch (Exception ex) { @@ -605,12 +918,11 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { * awaitUntil without a signal times out */ public void testAwaitUntil_Timeout() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); try { lock.writeLock().lock(); java.util.Date d = new java.util.Date(); - assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); lock.writeLock().unlock(); } catch (Exception ex) { @@ -622,9 +934,9 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { * await returns when signalled */ public void testAwait() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { lock.writeLock().lock(); @@ -651,32 +963,61 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } } + /** A helper class for uninterruptible wait tests */ + class UninterruptableThread extends Thread { + private Lock lock; + private Condition c; + + public volatile boolean canAwake = false; + public volatile boolean interrupted = false; + public volatile boolean lockStarted = false; + + public UninterruptableThread(Lock lock, Condition c) { + this.lock = lock; + this.c = c; + } + + public synchronized void run() { + lock.lock(); + lockStarted = true; + + while (!canAwake) { + c.awaitUninterruptibly(); + } + + interrupted = isInterrupted(); + lock.unlock(); + } + } + /** * awaitUninterruptibly doesn't abort on interrupt */ public void testAwaitUninterruptibly() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); - Thread t = new Thread(new Runnable() { - public void run() { - lock.writeLock().lock(); - c.awaitUninterruptibly(); - lock.writeLock().unlock(); - } - }); + UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c); try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); + thread.start(); + + while (!thread.lockStarted) { + Thread.sleep(100); + } + lock.writeLock().lock(); - c.signal(); - lock.writeLock().unlock(); - assert(t.isInterrupted()); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); - } - catch (Exception ex) { + try { + thread.interrupt(); + thread.canAwake = true; + c.signal(); + } finally { + lock.writeLock().unlock(); + } + + thread.join(); + assertTrue(thread.interrupted); + assertFalse(thread.isAlive()); + } catch (Exception ex) { unexpectedException(); } } @@ -685,9 +1026,9 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { * await is interruptible */ public void testAwait_Interrupt() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { lock.writeLock().lock(); @@ -716,9 +1057,9 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { * awaitNanos is interruptible */ public void testAwaitNanos_Interrupt() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { lock.writeLock().lock(); @@ -747,9 +1088,9 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { * awaitUntil is interruptible */ public void testAwaitUntil_Interrupt() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { lock.writeLock().lock(); @@ -779,9 +1120,9 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { * signalAll wakes up all threads */ public void testSignalAll() { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); - Thread t1 = new Thread(new Runnable() { + Thread t1 = new Thread(new Runnable() { public void run() { try { lock.writeLock().lock(); @@ -794,7 +1135,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { lock.writeLock().lock(); @@ -852,7 +1193,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { /** * hasQueuedThreads reports whether there are waiting threads */ - public void testhasQueuedThreads() { + public void testhasQueuedThreads() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); @@ -876,12 +1217,12 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } /** * hasQueuedThread(null) throws NPE */ - public void testHasQueuedThreadNPE() { + public void testHasQueuedThreadNPE() { final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); try { sync.hasQueuedThread(null); @@ -893,7 +1234,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { /** * hasQueuedThread reports whether a thread is queued. */ - public void testHasQueuedThread() { + public void testHasQueuedThread() { final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); Thread t1 = new Thread(new InterruptedLockRunnable(sync)); Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); @@ -922,13 +1263,13 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } /** * getQueueLength reports number of waiting threads */ - public void testGetQueueLength() { + public void testGetQueueLength() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); @@ -952,12 +1293,12 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } /** * getQueuedThreads includes waiting threads */ - public void testGetQueuedThreads() { + public void testGetQueuedThreads() { final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); Thread t1 = new Thread(new InterruptedLockRunnable(lock)); Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); @@ -984,7 +1325,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } catch(Exception e){ unexpectedException(); } - } + } /** * hasWaiters throws NPE if null @@ -1097,9 +1438,9 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { * getWaitingThreads throws IAE if not owned */ public void testGetWaitingThreadsIAE() { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); final Condition c = (lock.writeLock().newCondition()); - final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock(); + final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock(); try { lock2.getWaitingThreads(c); shouldThrow(); @@ -1113,7 +1454,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { * getWaitingThreads throws IMSE if not locked */ public void testGetWaitingThreadsIMSE() { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); final Condition c = (lock.writeLock().newCondition()); try { lock.getWaitingThreads(c); @@ -1131,7 +1472,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { public void testHasWaiters() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = (lock.writeLock().newCondition()); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { lock.writeLock().lock(); @@ -1173,7 +1514,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { public void testGetWaitQueueLength() { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = (lock.writeLock().newCondition()); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { lock.writeLock().lock(); @@ -1214,9 +1555,9 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { * getWaitingThreads returns only and all waiting threads */ public void testGetWaitingThreads() { - final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); + final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); - Thread t1 = new Thread(new Runnable() { + Thread t1 = new Thread(new Runnable() { public void run() { try { lock.writeLock().lock(); @@ -1230,7 +1571,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { lock.writeLock().lock(); diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/SynchronousQueueTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/SynchronousQueueTest.java index 19bdede..debce5d 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/SynchronousQueueTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/SynchronousQueueTest.java @@ -757,6 +757,7 @@ public class SynchronousQueueTest extends JSR166TestCase { while (!q.isEmpty()) assertEquals(q.remove(), r.remove()); } catch(Exception e){ + e.printStackTrace(); unexpectedException(); } } diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/ThreadPoolExecutorTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/ThreadPoolExecutorTest.java index ca4ba78..4f7cc46 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/ThreadPoolExecutorTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/ThreadPoolExecutorTest.java @@ -9,6 +9,7 @@ package tests.api.java.util.concurrent; import java.util.concurrent.*; +import java.util.concurrent.atomic.*; import junit.framework.*; import java.util.*; @@ -38,6 +39,15 @@ public class ThreadPoolExecutorTest extends JSR166TestCase { } } + static class FailingThreadFactory implements ThreadFactory{ + int calls = 0; + public Thread newThread(Runnable r){ + if (++calls > 1) return null; + return new Thread(r); + } + } + + /** * execute successfully executes a runnable */ @@ -342,6 +352,9 @@ public class ThreadPoolExecutorTest extends JSR166TestCase { assertSame(q, wq); assertFalse(wq.contains(tasks[0])); assertTrue(wq.contains(tasks[4])); + for (int i = 1; i < 5; ++i) + tasks[i].cancel(true); + p1.shutdownNow(); } catch(Exception e) { unexpectedException(); } finally { @@ -1500,5 +1513,58 @@ public class ThreadPoolExecutorTest extends JSR166TestCase { } } + /** + * Execution continues if there is at least one thread even if + * thread factory fails to create more + */ + public void testFailingThreadFactory() { + ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); + try { + ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); + for (int k = 0; k < 100; ++k) { + e.execute(new NoOpRunnable()); + } + Thread.sleep(LONG_DELAY_MS); + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + /** + * execute allows the same task to be submitted multiple times, even + * if rejected + */ + public void testRejectedRecycledTask() { + final int nTasks = 1000; + final AtomicInteger nRun = new AtomicInteger(0); + final Runnable recycledTask = new Runnable() { + public void run() { + nRun.getAndIncrement(); + } }; + final ThreadPoolExecutor p = + new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, + new ArrayBlockingQueue(30)); + try { + for (int i = 0; i < nTasks; ++i) { + for (;;) { + try { + p.execute(recycledTask); + break; + } + catch (RejectedExecutionException ignore) { + } + } + } + Thread.sleep(5000); // enough time to run all tasks + assertEquals(nRun.get(), nTasks); + } catch(Exception ex) { + ex.printStackTrace(); + unexpectedException(); + } finally { + p.shutdown(); + } + } + } diff --git a/concurrent/src/test/java/tests/api/java/util/concurrent/TimeUnitTest.java b/concurrent/src/test/java/tests/api/java/util/concurrent/TimeUnitTest.java index 1a9a04a..54fdc69 100755 --- a/concurrent/src/test/java/tests/api/java/util/concurrent/TimeUnitTest.java +++ b/concurrent/src/test/java/tests/api/java/util/concurrent/TimeUnitTest.java @@ -21,54 +21,60 @@ public class TimeUnitTest extends JSR166TestCase { return new TestSuite(TimeUnitTest.class); } + // (loops to 88888 check increments at all time divisions.) + /** - * convert correctly converts sample values across the four units + * convert correctly converts sample values across the units */ public void testConvert() { - for (long t = 0; t < 10; ++t) { - assertEquals(t, + for (long t = 0; t < 88888; ++t) { + assertEquals(t, TimeUnit.SECONDS.convert(t, TimeUnit.SECONDS)); assertEquals(t, - TimeUnit.SECONDS.convert(1000 * t, + TimeUnit.SECONDS.convert(1000L*t, TimeUnit.MILLISECONDS)); assertEquals(t, - TimeUnit.SECONDS.convert(1000000 * t, + TimeUnit.SECONDS.convert(1000000L*t, TimeUnit.MICROSECONDS)); assertEquals(t, - TimeUnit.SECONDS.convert(1000000000 * t, + TimeUnit.SECONDS.convert(1000000000L*t, TimeUnit.NANOSECONDS)); - assertEquals(1000 * t, + + + assertEquals(1000L*t, TimeUnit.MILLISECONDS.convert(t, TimeUnit.SECONDS)); assertEquals(t, TimeUnit.MILLISECONDS.convert(t, TimeUnit.MILLISECONDS)); assertEquals(t, - TimeUnit.MILLISECONDS.convert(1000 * t, + TimeUnit.MILLISECONDS.convert(1000L*t, TimeUnit.MICROSECONDS)); assertEquals(t, - TimeUnit.MILLISECONDS.convert(1000000 * t, + TimeUnit.MILLISECONDS.convert(1000000L*t, TimeUnit.NANOSECONDS)); - assertEquals(1000000 * t, + + assertEquals(1000000L*t, TimeUnit.MICROSECONDS.convert(t, TimeUnit.SECONDS)); - assertEquals(1000 * t, + assertEquals(1000L*t, TimeUnit.MICROSECONDS.convert(t, TimeUnit.MILLISECONDS)); assertEquals(t, TimeUnit.MICROSECONDS.convert(t, TimeUnit.MICROSECONDS)); assertEquals(t, - TimeUnit.MICROSECONDS.convert(1000 * t, + TimeUnit.MICROSECONDS.convert(1000L*t, TimeUnit.NANOSECONDS)); - assertEquals(1000000000 * t, + + assertEquals(1000000000L*t, TimeUnit.NANOSECONDS.convert(t, TimeUnit.SECONDS)); - assertEquals(1000000 * t, + assertEquals(1000000L*t, TimeUnit.NANOSECONDS.convert(t, TimeUnit.MILLISECONDS)); - assertEquals(1000 * t, + assertEquals(1000L*t, TimeUnit.NANOSECONDS.convert(t, TimeUnit.MICROSECONDS)); assertEquals(t, @@ -82,13 +88,12 @@ public class TimeUnitTest extends JSR166TestCase { * nanoseconds */ public void testToNanos() { - for (long t = 0; t < 10; ++t) { - assertEquals(1000000000 * t, + for (long t = 0; t < 88888; ++t) { + assertEquals(1000000000L*t, TimeUnit.SECONDS.toNanos(t)); - - assertEquals(1000000 * t, + assertEquals(1000000L*t, TimeUnit.MILLISECONDS.toNanos(t)); - assertEquals(1000 * t, + assertEquals(1000L*t, TimeUnit.MICROSECONDS.toNanos(t)); assertEquals(t, TimeUnit.NANOSECONDS.toNanos(t)); @@ -100,16 +105,15 @@ public class TimeUnitTest extends JSR166TestCase { * microseconds */ public void testToMicros() { - for (long t = 0; t < 10; ++t) { - assertEquals(1000000 * t, + for (long t = 0; t < 88888; ++t) { + assertEquals(1000000L*t, TimeUnit.SECONDS.toMicros(t)); - - assertEquals(1000 * t, + assertEquals(1000L*t, TimeUnit.MILLISECONDS.toMicros(t)); assertEquals(t, TimeUnit.MICROSECONDS.toMicros(t)); assertEquals(t, - TimeUnit.NANOSECONDS.toMicros(t * 1000)); + TimeUnit.NANOSECONDS.toMicros(t*1000L)); } } @@ -118,16 +122,15 @@ public class TimeUnitTest extends JSR166TestCase { * milliseconds */ public void testToMillis() { - for (long t = 0; t < 10; ++t) { - assertEquals(1000 * t, + for (long t = 0; t < 88888; ++t) { + assertEquals(1000L*t, TimeUnit.SECONDS.toMillis(t)); - assertEquals(t, TimeUnit.MILLISECONDS.toMillis(t)); assertEquals(t, - TimeUnit.MICROSECONDS.toMillis(t * 1000)); + TimeUnit.MICROSECONDS.toMillis(t*1000L)); assertEquals(t, - TimeUnit.NANOSECONDS.toMillis(t * 1000000)); + TimeUnit.NANOSECONDS.toMillis(t*1000000L)); } } @@ -136,20 +139,18 @@ public class TimeUnitTest extends JSR166TestCase { * seconds */ public void testToSeconds() { - for (long t = 0; t < 10; ++t) { - assertEquals(t, + for (long t = 0; t < 88888; ++t) { + assertEquals(t, TimeUnit.SECONDS.toSeconds(t)); - assertEquals(t, - TimeUnit.MILLISECONDS.toSeconds(t * 1000)); + TimeUnit.MILLISECONDS.toSeconds(t*1000L)); assertEquals(t, - TimeUnit.MICROSECONDS.toSeconds(t * 1000000)); + TimeUnit.MICROSECONDS.toSeconds(t*1000000L)); assertEquals(t, - TimeUnit.NANOSECONDS.toSeconds(t * 1000000000)); + TimeUnit.NANOSECONDS.toSeconds(t*1000000000L)); } } - /** * convert saturates positive too-large values to Long.MAX_VALUE * and negative to LONG.MIN_VALUE @@ -161,6 +162,7 @@ public class TimeUnitTest extends JSR166TestCase { assertEquals(Long.MIN_VALUE, TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, TimeUnit.SECONDS)); + } /** |