You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by hi...@apache.org on 2009/07/28 11:30:48 UTC
svn commit: r798469 [21/28] - in /harmony/enhanced/classlib/branches/java6:
./ depends/build/platform/ depends/files/ depends/jars/
depends/manifests/icu4j_4.0/ depends/manifests/icu4j_4.2.1/
depends/manifests/icu4j_4.2.1/META-INF/ make/ modules/access...
Modified: harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/CyclicBarrierTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/CyclicBarrierTest.java?rev=798469&r1=798468&r2=798469&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/CyclicBarrierTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/CyclicBarrierTest.java Tue Jul 28 09:30:33 2009
@@ -9,6 +9,8 @@
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) {
@@ -162,7 +164,7 @@
* 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 {
@@ -227,7 +229,7 @@
* 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 {
@@ -265,7 +267,7 @@
* 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 {
@@ -374,4 +376,249 @@
}
}
+ /**
+ * 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();
+ }
+ }
}
Modified: harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/DelayQueueTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/DelayQueueTest.java?rev=798469&r1=798468&r2=798469&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/DelayQueueTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/DelayQueueTest.java Tue Jul 28 09:30:33 2009
@@ -6,17 +6,13 @@
* Pat Fisher, Mike Judd.
*/
-/*
- * Modified in Apache Harmony.
- */
-
import junit.framework.*;
import java.util.*;
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() {
@@ -29,11 +25,11 @@
* 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; }
- public int compareTo(Delayed y) {
+ public int compareTo(PDelay y) {
int i = pseudodelay;
int j = ((PDelay)y).pseudodelay;
if (i < j) return -1;
@@ -41,7 +37,7 @@
return 0;
}
- public int compareTo(PDelay y) {
+ public int compareTo(Delayed y) {
int i = pseudodelay;
int j = ((PDelay)y).pseudodelay;
if (i < j) return -1;
@@ -73,12 +69,12 @@
/**
* 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;
}
- public int compareTo(Delayed y) {
+ public int compareTo(NanoDelay y) {
long i = trigger;
long j = ((NanoDelay)y).trigger;
if (i < j) return -1;
@@ -86,7 +82,7 @@
return 0;
}
- public int compareTo(NanoDelay y) {
+ public int compareTo(Delayed y) {
long i = trigger;
long j = ((NanoDelay)y).trigger;
if (i < j) return -1;
@@ -132,7 +128,7 @@
assertEquals(n, q.size());
return q;
}
-
+
/**
* A new queue has unbounded capacity
*/
@@ -233,7 +229,7 @@
DelayQueue q = new DelayQueue();
q.offer(null);
shouldThrow();
- } catch (NullPointerException success) { }
+ } catch (NullPointerException success) { }
}
/**
@@ -244,7 +240,7 @@
DelayQueue q = new DelayQueue();
q.add(null);
shouldThrow();
- } catch (NullPointerException success) { }
+ } catch (NullPointerException success) { }
}
/**
@@ -346,9 +342,9 @@
DelayQueue q = new DelayQueue();
q.put(null);
shouldThrow();
- }
+ }
catch (NullPointerException success){
- }
+ }
}
/**
@@ -416,7 +412,7 @@
} finally { }
}
});
-
+
try {
t.start();
Thread.sleep(SMALL_DELAY_MS);
@@ -438,7 +434,7 @@
}
} catch (InterruptedException e){
unexpectedException();
- }
+ }
}
/**
@@ -451,7 +447,7 @@
try {
q.take();
threadShouldThrow();
- } catch (InterruptedException success){ }
+ } catch (InterruptedException success){ }
}
});
try {
@@ -478,11 +474,11 @@
q.take();
threadShouldThrow();
} catch (InterruptedException success){
- }
+ }
}});
t.start();
- try {
- Thread.sleep(SHORT_DELAY_MS);
+ try {
+ Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
}
@@ -515,7 +511,7 @@
assertNull(q.poll(0, TimeUnit.MILLISECONDS));
} catch (InterruptedException e){
unexpectedException();
- }
+ }
}
/**
@@ -530,7 +526,7 @@
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
} catch (InterruptedException e){
unexpectedException();
- }
+ }
}
/**
@@ -547,11 +543,11 @@
}
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();
}
@@ -573,7 +569,7 @@
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
threadFail("Should block");
- } catch (InterruptedException success) { }
+ } catch (InterruptedException success) { }
}
});
try {
@@ -585,7 +581,7 @@
} catch (Exception e){
unexpectedException();
}
- }
+ }
/**
@@ -596,8 +592,10 @@
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());
}
@@ -630,7 +628,7 @@
q.remove();
shouldThrow();
} catch (NoSuchElementException success){
- }
+ }
}
/**
@@ -647,7 +645,7 @@
}
assertTrue(q.isEmpty());
}
-
+
/**
* contains(x) reports true when elements added but not yet removed
*/
@@ -669,8 +667,10 @@
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());
}
@@ -736,7 +736,7 @@
assertEquals(o[i], q.take());
} catch (InterruptedException e){
unexpectedException();
- }
+ }
}
/**
@@ -752,7 +752,7 @@
assertEquals(ints[i], q.take());
} catch (InterruptedException e){
unexpectedException();
- }
+ }
}
@@ -777,7 +777,7 @@
shouldThrow();
} catch(ArrayStoreException success){}
}
-
+
/**
* iterator iterates through all elements
*/
@@ -819,7 +819,7 @@
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 +875,7 @@
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 +885,41 @@
}
}
+ /**
+ * 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 +931,7 @@
/**
* drainTo(this) throws IAE
- */
+ */
public void testDrainToSelf() {
DelayQueue q = populatedQueue(SIZE);
try {
@@ -912,13 +943,30 @@
/**
* 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]);
}
/**
Modified: harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ExecutorCompletionServiceTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ExecutorCompletionServiceTest.java?rev=798469&r1=798468&r2=798469&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ExecutorCompletionServiceTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ExecutorCompletionServiceTest.java Tue Jul 28 09:30:33 2009
@@ -10,6 +10,7 @@
import junit.framework.*;
import java.util.*;
import java.util.concurrent.*;
+import java.util.concurrent.atomic.*;
import java.math.BigInteger;
import java.security.*;
@@ -87,7 +88,7 @@
Callable c = new StringTask();
ecs.submit(c);
Future f = ecs.take();
- assert(f.isDone());
+ assertTrue(f.isDone());
} catch (Exception ex) {
unexpectedException();
} finally {
@@ -127,7 +128,7 @@
for (;;) {
Future f = ecs.poll();
if (f != null) {
- assert(f.isDone());
+ assertTrue(f.isDone());
break;
}
}
@@ -150,12 +151,11 @@
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);
}
}
-
}
Modified: harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ExecutorsTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ExecutorsTest.java?rev=798469&r1=798468&r2=798469&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ExecutorsTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ExecutorsTest.java Tue Jul 28 09:30:33 2009
@@ -337,7 +337,7 @@
try {
Thread current = Thread.currentThread();
threadAssertTrue(!current.isDaemon());
- threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY);
+ threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
ThreadGroup g = current.getThreadGroup();
SecurityManager s = System.getSecurityManager();
if (s != null)
@@ -392,7 +392,7 @@
try {
Thread current = Thread.currentThread();
threadAssertTrue(!current.isDaemon());
- threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY);
+ threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
ThreadGroup g = current.getThreadGroup();
SecurityManager s = System.getSecurityManager();
if (s != null)
Modified: harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/JSR166TestCase.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/JSR166TestCase.java?rev=798469&r1=798468&r2=798469&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/JSR166TestCase.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/JSR166TestCase.java Tue Jul 28 09:30:33 2009
@@ -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.
*/
import junit.framework.*;
@@ -17,13 +17,13 @@
* 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.
@@ -44,7 +44,7 @@
* 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
@@ -63,7 +63,7 @@
* "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
@@ -88,10 +88,10 @@
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) {
@@ -104,26 +104,26 @@
/**
* Collects all JSR166 unit tests as one suite
- */
+ */
public static Test suite ( ) {
TestSuite suite = new TestSuite("JSR166 Unit Tests");
-
+
suite.addTest(new TestSuite(AbstractExecutorServiceTest.class));
suite.addTest(new TestSuite(AbstractQueueTest.class));
suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class));
suite.addTest(new TestSuite(ArrayBlockingQueueTest.class));
- suite.addTest(new TestSuite(AtomicBooleanTest.class));
- suite.addTest(new TestSuite(AtomicIntegerArrayTest.class));
- suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class));
- suite.addTest(new TestSuite(AtomicIntegerTest.class));
- suite.addTest(new TestSuite(AtomicLongArrayTest.class));
- suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class));
- suite.addTest(new TestSuite(AtomicLongTest.class));
- suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class));
- suite.addTest(new TestSuite(AtomicReferenceArrayTest.class));
- suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class));
- suite.addTest(new TestSuite(AtomicReferenceTest.class));
- suite.addTest(new TestSuite(AtomicStampedReferenceTest.class));
+ suite.addTest(new TestSuite(AtomicBooleanTest.class));
+ suite.addTest(new TestSuite(AtomicIntegerArrayTest.class));
+ suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class));
+ suite.addTest(new TestSuite(AtomicIntegerTest.class));
+ suite.addTest(new TestSuite(AtomicLongArrayTest.class));
+ suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class));
+ suite.addTest(new TestSuite(AtomicLongTest.class));
+ suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class));
+ suite.addTest(new TestSuite(AtomicReferenceArrayTest.class));
+ suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class));
+ suite.addTest(new TestSuite(AtomicReferenceTest.class));
+ suite.addTest(new TestSuite(AtomicStampedReferenceTest.class));
suite.addTest(new TestSuite(ConcurrentHashMapTest.class));
suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class));
suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class));
@@ -150,7 +150,7 @@
suite.addTest(new TestSuite(ThreadPoolExecutorTest.class));
suite.addTest(new TestSuite(ThreadTest.class));
suite.addTest(new TestSuite(TimeUnitTest.class));
-
+
return suite;
}
@@ -162,16 +162,16 @@
/**
- * 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();
@@ -186,23 +186,23 @@
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);
@@ -211,7 +211,7 @@
/**
* If expression not true, set status to indicate current testcase
* should fail
- */
+ */
public void threadAssertTrue(boolean b) {
if (!b) {
threadFailed = true;
@@ -222,7 +222,7 @@
/**
* If expression not false, set status to indicate current testcase
* should fail
- */
+ */
public void threadAssertFalse(boolean b) {
if (b) {
threadFailed = true;
@@ -233,7 +233,7 @@
/**
* If argument not null, set status to indicate current testcase
* should fail
- */
+ */
public void threadAssertNull(Object x) {
if (x != null) {
threadFailed = true;
@@ -244,7 +244,7 @@
/**
* If arguments not equal, set status to indicate current testcase
* should fail
- */
+ */
public void threadAssertEquals(long x, long y) {
if (x != y) {
threadFailed = true;
@@ -255,7 +255,7 @@
/**
* 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,8 +267,13 @@
* 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;
+ }
}
/**
@@ -279,6 +284,14 @@
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
@@ -297,7 +310,7 @@
/**
* fail with message "should throw exception"
- */
+ */
public void shouldThrow() {
fail("Should throw exception");
}
@@ -332,6 +345,7 @@
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);
@@ -387,7 +401,7 @@
Thread.sleep(SHORT_DELAY_MS);
}
catch(Exception e) {
- threadUnexpectedException();
+ threadUnexpectedException(e);
}
}
}
@@ -409,7 +423,7 @@
Thread.sleep(SMALL_DELAY_MS);
}
catch(Exception e) {
- threadUnexpectedException();
+ threadUnexpectedException(e);
}
}
}
@@ -430,7 +444,7 @@
Thread.sleep(SMALL_DELAY_MS);
}
catch(Exception e) {
- threadUnexpectedException();
+ threadUnexpectedException(e);
}
return Boolean.TRUE;
}
@@ -454,7 +468,7 @@
Thread.sleep(MEDIUM_DELAY_MS);
}
catch(Exception e) {
- threadUnexpectedException();
+ threadUnexpectedException(e);
}
}
}
@@ -496,7 +510,7 @@
static class SimpleThreadFactory implements ThreadFactory{
public Thread newThread(Runnable r){
return new Thread(r);
- }
+ }
}
static class TrackedShortRunnable implements Runnable {
@@ -556,8 +570,8 @@
* 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){}
}
-
-
+
+
}
Modified: harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/LinkedBlockingQueueTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/LinkedBlockingQueueTest.java?rev=798469&r1=798468&r2=798469&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/LinkedBlockingQueueTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/LinkedBlockingQueueTest.java Tue Jul 28 09:30:33 2009
@@ -614,6 +614,23 @@
}
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
@@ -638,6 +655,7 @@
assertEquals(SIZE, q.remainingCapacity());
q.add(one);
assertFalse(q.isEmpty());
+ assertTrue(q.contains(one));
q.clear();
assertTrue(q.isEmpty());
}
@@ -956,6 +974,17 @@
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));
}
/**
@@ -1014,15 +1043,18 @@
* 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);
+ assertEquals(q.size(), SIZE-k);
for (int j = 0; j < k; ++j)
assertEquals(l.get(j), new Integer(j));
+ while (q.poll() != null) ;
}
}
Modified: harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/LockSupportTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/LockSupportTest.java?rev=798469&r1=798468&r2=798469&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/LockSupportTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/LockSupportTest.java Tue Jul 28 09:30:33 2009
@@ -75,7 +75,6 @@
public void run() {
try {
LockSupport.park();
- threadAssertTrue(Thread.interrupted());
} catch(Exception e){
threadUnexpectedException();
}
Modified: harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/PriorityBlockingQueueTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/PriorityBlockingQueueTest.java?rev=798469&r1=798468&r2=798469&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/PriorityBlockingQueueTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/PriorityBlockingQueueTest.java Tue Jul 28 09:30:33 2009
@@ -626,9 +626,9 @@
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());
}
@@ -871,6 +871,17 @@
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));
}
/**
@@ -925,15 +936,18 @@
* 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) ;
}
}
Modified: harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ReentrantLockTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ReentrantLockTest.java?rev=798469&r1=798468&r2=798469&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ReentrantLockTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ReentrantLockTest.java Tue Jul 28 09:30:33 2009
@@ -409,7 +409,9 @@
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){
@@ -498,7 +500,7 @@
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) {
@@ -515,7 +517,7 @@
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) {
@@ -863,34 +865,61 @@
}
}
-
+ /** 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();
}
}
@@ -1039,6 +1068,61 @@
}
/**
+ * 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() {
Modified: harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ReentrantReadWriteLockTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ReentrantReadWriteLockTest.java?rev=798469&r1=798468&r2=798469&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ReentrantReadWriteLockTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ReentrantReadWriteLockTest.java Tue Jul 28 09:30:33 2009
@@ -126,7 +126,7 @@
/**
* getWriteHoldCount returns number of recursive holds
*/
- public void testGetHoldCount() {
+ public void testGetWriteHoldCount() {
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
for(int i = 1; i <= SIZE; i++) {
lock.writeLock().lock();
@@ -137,7 +137,7 @@
assertEquals(i-1,lock.getWriteHoldCount());
}
}
-
+
/**
* write-unlocking an unlocked lock throws IllegalMonitorStateException
@@ -169,7 +169,9 @@
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){
@@ -215,7 +217,9 @@
});
try {
t.start();
+ Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
+ Thread.sleep(SHORT_DELAY_MS);
lock.writeLock().unlock();
t.join();
} catch(Exception e){
@@ -376,6 +380,269 @@
}
}
+ /**
+ * 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
@@ -420,6 +687,50 @@
}
}
+
+ /**
+ * 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();
+ }
+ }
+
/**
@@ -493,7 +804,9 @@
});
try {
t.start();
+ Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
+ Thread.sleep(SHORT_DELAY_MS);
t.join();
lock.writeLock().unlock();
} catch(Exception e){
@@ -523,6 +836,7 @@
});
try {
t.start();
+ Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
lock.writeLock().unlock();
@@ -591,7 +905,6 @@
final Condition c = lock.writeLock().newCondition();
try {
lock.writeLock().lock();
- assertFalse(c.await(10, TimeUnit.MILLISECONDS));
lock.writeLock().unlock();
}
catch (Exception ex) {
@@ -608,7 +921,6 @@
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) {
@@ -649,32 +961,61 @@
}
}
+ /** 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();
}
}
Modified: harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/SynchronousQueueTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/SynchronousQueueTest.java?rev=798469&r1=798468&r2=798469&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/SynchronousQueueTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/SynchronousQueueTest.java Tue Jul 28 09:30:33 2009
@@ -755,6 +755,7 @@
while (!q.isEmpty())
assertEquals(q.remove(), r.remove());
} catch(Exception e){
+ e.printStackTrace();
unexpectedException();
}
}
Modified: harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ThreadPoolExecutorTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ThreadPoolExecutorTest.java?rev=798469&r1=798468&r2=798469&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ThreadPoolExecutorTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/ThreadPoolExecutorTest.java Tue Jul 28 09:30:33 2009
@@ -7,6 +7,7 @@
*/
import java.util.concurrent.*;
+import java.util.concurrent.atomic.*;
import junit.framework.*;
import java.util.*;
@@ -36,6 +37,15 @@
}
}
+ 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
*/
@@ -340,6 +350,9 @@
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 {
@@ -1498,5 +1511,58 @@
}
}
+ /**
+ * 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();
+ }
+ }
+
}
Modified: harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/TimeUnitTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/TimeUnitTest.java?rev=798469&r1=798468&r2=798469&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/TimeUnitTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/concurrent/src/test/java/TimeUnitTest.java Tue Jul 28 09:30:33 2009
@@ -20,54 +20,96 @@
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) {
+ for (long t = 0; t < 88888; ++t) {
+ assertEquals(t*60*60*24,
+ TimeUnit.SECONDS.convert(t,
+ TimeUnit.DAYS));
+ assertEquals(t*60*60,
+ TimeUnit.SECONDS.convert(t,
+ TimeUnit.HOURS));
+ assertEquals(t*60,
+ TimeUnit.SECONDS.convert(t,
+ TimeUnit.MINUTES));
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*60*60*24,
+ TimeUnit.MILLISECONDS.convert(t,
+ TimeUnit.DAYS));
+ assertEquals(1000L*t*60*60,
+ TimeUnit.MILLISECONDS.convert(t,
+ TimeUnit.HOURS));
+ assertEquals(1000L*t*60,
+ TimeUnit.MILLISECONDS.convert(t,
+ TimeUnit.MINUTES));
+ 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*60*60*24,
+ TimeUnit.MICROSECONDS.convert(t,
+ TimeUnit.DAYS));
+ assertEquals(1000000L*t*60*60,
+ TimeUnit.MICROSECONDS.convert(t,
+ TimeUnit.HOURS));
+ assertEquals(1000000L*t*60,
+ TimeUnit.MICROSECONDS.convert(t,
+ TimeUnit.MINUTES));
+ 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*60*60*24,
+ TimeUnit.NANOSECONDS.convert(t,
+ TimeUnit.DAYS));
+ assertEquals(1000000000L*t*60*60,
+ TimeUnit.NANOSECONDS.convert(t,
+ TimeUnit.HOURS));
+ assertEquals(1000000000L*t*60,
+ TimeUnit.NANOSECONDS.convert(t,
+ TimeUnit.MINUTES));
+ 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,
@@ -81,13 +123,18 @@
* nanoseconds
*/
public void testToNanos() {
- for (long t = 0; t < 10; ++t) {
- assertEquals(1000000000 * t,
+ for (long t = 0; t < 88888; ++t) {
+ assertEquals(t*1000000000L*60*60*24,
+ TimeUnit.DAYS.toNanos(t));
+ assertEquals(t*1000000000L*60*60,
+ TimeUnit.HOURS.toNanos(t));
+ assertEquals(t*1000000000L*60,
+ TimeUnit.MINUTES.toNanos(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));
@@ -99,16 +146,21 @@
* microseconds
*/
public void testToMicros() {
- for (long t = 0; t < 10; ++t) {
- assertEquals(1000000 * t,
+ for (long t = 0; t < 88888; ++t) {
+ assertEquals(t*1000000L*60*60*24,
+ TimeUnit.DAYS.toMicros(t));
+ assertEquals(t*1000000L*60*60,
+ TimeUnit.HOURS.toMicros(t));
+ assertEquals(t*1000000L*60,
+ TimeUnit.MINUTES.toMicros(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));
}
}
@@ -117,16 +169,21 @@
* milliseconds
*/
public void testToMillis() {
- for (long t = 0; t < 10; ++t) {
- assertEquals(1000 * t,
+ for (long t = 0; t < 88888; ++t) {
+ assertEquals(t*1000L*60*60*24,
+ TimeUnit.DAYS.toMillis(t));
+ assertEquals(t*1000L*60*60,
+ TimeUnit.HOURS.toMillis(t));
+ assertEquals(t*1000L*60,
+ TimeUnit.MINUTES.toMillis(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));
}
}
@@ -135,20 +192,24 @@
* seconds
*/
public void testToSeconds() {
- for (long t = 0; t < 10; ++t) {
+ for (long t = 0; t < 88888; ++t) {
+ assertEquals(t*60*60*24,
+ TimeUnit.DAYS.toSeconds(t));
+ assertEquals(t*60*60,
+ TimeUnit.HOURS.toSeconds(t));
+ assertEquals(t*60,
+ TimeUnit.MINUTES.toSeconds(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
@@ -160,6 +221,25 @@
assertEquals(Long.MIN_VALUE,
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
TimeUnit.SECONDS));
+ assertEquals(Long.MAX_VALUE,
+ TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
+ TimeUnit.MINUTES));
+ assertEquals(Long.MIN_VALUE,
+ TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
+ TimeUnit.MINUTES));
+ assertEquals(Long.MAX_VALUE,
+ TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
+ TimeUnit.HOURS));
+ assertEquals(Long.MIN_VALUE,
+ TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
+ TimeUnit.HOURS));
+ assertEquals(Long.MAX_VALUE,
+ TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
+ TimeUnit.DAYS));
+ assertEquals(Long.MIN_VALUE,
+ TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
+ TimeUnit.DAYS));
+
}
/**