You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/08/24 05:42:33 UTC

svn commit: r434296 [18/19] - in /incubator/harmony/enhanced/classlib/trunk: make/ modules/concurrent/ modules/concurrent/.settings/ modules/concurrent/META-INF/ modules/concurrent/make/ modules/concurrent/src/ modules/concurrent/src/main/ modules/conc...

Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SemaphoreTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SemaphoreTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SemaphoreTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SemaphoreTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,917 @@
+/*
+ * 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. 
+ */
+
+import junit.framework.*;
+import java.util.*;
+import java.util.concurrent.*;
+import java.io.*;
+
+public class SemaphoreTest extends JSR166TestCase {
+    public static void main(String[] args) {
+	junit.textui.TestRunner.run (suite());	
+    }
+    public static Test suite() {
+	return new TestSuite(SemaphoreTest.class);
+    }
+
+    /**
+     * Subclass to expose protected methods
+     */
+    static class PublicSemaphore extends Semaphore {
+        PublicSemaphore(int p, boolean f) { super(p, f); }
+        public Collection<Thread> getQueuedThreads() { 
+            return super.getQueuedThreads(); 
+        }
+        public void reducePermits(int p) { 
+            super.reducePermits(p);
+        }
+    }
+
+    /**
+     * A runnable calling acquire
+     */
+    class InterruptibleLockRunnable implements Runnable {
+        final Semaphore lock;
+        InterruptibleLockRunnable(Semaphore l) { lock = l; }
+        public void run() {
+            try {
+                lock.acquire();
+            } catch(InterruptedException success){}
+        }
+    }
+
+
+    /**
+     * A runnable calling acquire that expects to be
+     * interrupted
+     */
+    class InterruptedLockRunnable implements Runnable {
+        final Semaphore lock;
+        InterruptedLockRunnable(Semaphore l) { lock = l; }
+        public void run() {
+            try {
+                lock.acquire();
+                threadShouldThrow();
+            } catch(InterruptedException success){}
+        }
+    }
+
+    /**
+     * Zero, negative, and positive initial values are allowed in constructor
+     */
+    public void testConstructor() {
+        Semaphore s0 = new Semaphore(0, false);
+        assertEquals(0, s0.availablePermits());
+        assertFalse(s0.isFair());
+        Semaphore s1 = new Semaphore(-1, false);
+        assertEquals(-1, s1.availablePermits());
+        assertFalse(s1.isFair());
+        Semaphore s2 = new Semaphore(-1, false);
+        assertEquals(-1, s2.availablePermits());
+        assertFalse(s2.isFair());
+    }
+
+    /**
+     * Constructor without fairness argument behaves as nonfair
+     */
+    public void testConstructor2() {
+        Semaphore s0 = new Semaphore(0);
+        assertEquals(0, s0.availablePermits());
+        assertFalse(s0.isFair());
+        Semaphore s1 = new Semaphore(-1);
+        assertEquals(-1, s1.availablePermits());
+        assertFalse(s1.isFair());
+        Semaphore s2 = new Semaphore(-1);
+        assertEquals(-1, s2.availablePermits());
+        assertFalse(s2.isFair());
+    }
+
+    /**
+     * tryAcquire succeeds when sufficient permits, else fails
+     */
+    public void testTryAcquireInSameThread() {
+        Semaphore s = new Semaphore(2, false);
+        assertEquals(2, s.availablePermits());
+        assertTrue(s.tryAcquire());
+        assertTrue(s.tryAcquire());
+        assertEquals(0, s.availablePermits());
+        assertFalse(s.tryAcquire());
+    }
+
+    /**
+     * Acquire and release of semaphore succeed if initially available
+     */
+    public void testAcquireReleaseInSameThread() {
+        Semaphore s = new Semaphore(1, false);
+        try {
+            s.acquire();
+            s.release();
+            s.acquire();
+            s.release();
+            s.acquire();
+            s.release();
+            s.acquire();
+            s.release();
+            s.acquire();
+            s.release();
+            assertEquals(1, s.availablePermits());
+	} catch( InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * Uninterruptible acquire and release of semaphore succeed if
+     * initially available
+     */
+    public void testAcquireUninterruptiblyReleaseInSameThread() {
+        Semaphore s = new Semaphore(1, false);
+        try {
+            s.acquireUninterruptibly();
+            s.release();
+            s.acquireUninterruptibly();
+            s.release();
+            s.acquireUninterruptibly();
+            s.release();
+            s.acquireUninterruptibly();
+            s.release();
+            s.acquireUninterruptibly();
+            s.release();
+            assertEquals(1, s.availablePermits());
+	} finally {
+        }
+    }
+
+    /**
+     * Timed Acquire and release of semaphore succeed if
+     * initially available
+     */
+    public void testTimedAcquireReleaseInSameThread() {
+        Semaphore s = new Semaphore(1, false);
+        try {
+            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release();
+            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release();
+            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release();
+            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release();
+            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release();
+            assertEquals(1, s.availablePermits());
+	} catch( InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * A release in one thread enables an acquire in another thread
+     */
+    public void testAcquireReleaseInDifferentThreads() {
+        final Semaphore s = new Semaphore(0, false);
+	Thread t = new Thread(new Runnable() {
+		public void run() {
+		    try {
+			s.acquire();
+                        s.release();
+                        s.release();
+                        s.acquire();
+		    } catch(InterruptedException ie){
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            s.release();
+            s.release();
+            s.acquire();
+            s.acquire();
+            s.release();
+            t.join();
+	} catch( InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * A release in one thread enables an uninterruptible acquire in another thread
+     */
+    public void testUninterruptibleAcquireReleaseInDifferentThreads() {
+        final Semaphore s = new Semaphore(0, false);
+	Thread t = new Thread(new Runnable() {
+		public void run() {
+                    s.acquireUninterruptibly();
+                    s.release();
+                    s.release();
+                    s.acquireUninterruptibly();
+		}
+	    });
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            s.release();
+            s.release();
+            s.acquireUninterruptibly();
+            s.acquireUninterruptibly();
+            s.release();
+            t.join();
+	} catch( InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+
+    /**
+     *  A release in one thread enables a timed acquire in another thread
+     */
+    public void testTimedAcquireReleaseInDifferentThreads() {
+        final Semaphore s = new Semaphore(1, false);
+	Thread t = new Thread(new Runnable() {
+		public void run() {
+		    try {
+                        s.release();
+                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+                        s.release();
+                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+
+		    } catch(InterruptedException ie){
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+        try {
+            t.start();
+            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release();
+            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release();
+            s.release();
+            t.join();
+	} catch( InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * A waiting acquire blocks interruptibly
+     */
+    public void testAcquire_InterruptedException() {
+	final Semaphore s = new Semaphore(0, false);
+	Thread t = new Thread(new Runnable() {
+		public void run() {
+		    try {
+			s.acquire();
+			threadShouldThrow();
+		    } catch(InterruptedException success){}
+		}
+	    });
+	t.start();
+	try {
+	    Thread.sleep(SHORT_DELAY_MS);
+            t.interrupt();
+            t.join();
+        } catch(InterruptedException e){
+            unexpectedException();
+        }
+    }
+    
+    /**
+     *  A waiting timed acquire blocks interruptibly
+     */
+    public void testTryAcquire_InterruptedException() {
+	final Semaphore s = new Semaphore(0, false);
+	Thread t = new Thread(new Runnable() {
+		public void run() {
+		    try {
+			s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
+			threadShouldThrow();
+		    } catch(InterruptedException success){
+                    }
+		}
+	    });
+	t.start();
+	try {
+	    Thread.sleep(SHORT_DELAY_MS);
+            t.interrupt();
+            t.join();
+        } catch(InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * hasQueuedThreads reports whether there are waiting threads
+     */
+    public void testHasQueuedThreads() { 
+	final Semaphore lock = new Semaphore(1, false);
+        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
+        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
+        try {
+            assertFalse(lock.hasQueuedThreads());
+            lock.acquireUninterruptibly();
+            t1.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(lock.hasQueuedThreads());
+            t2.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(lock.hasQueuedThreads());
+            t1.interrupt();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(lock.hasQueuedThreads());
+            lock.release();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertFalse(lock.hasQueuedThreads());
+            t1.join();
+            t2.join();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+    /**
+     * getQueueLength reports number of waiting threads
+     */
+    public void testGetQueueLength() { 
+	final Semaphore lock = new Semaphore(1, false);
+        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
+        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
+        try {
+            assertEquals(0, lock.getQueueLength());
+            lock.acquireUninterruptibly();
+            t1.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertEquals(1, lock.getQueueLength());
+            t2.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertEquals(2, lock.getQueueLength());
+            t1.interrupt();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertEquals(1, lock.getQueueLength());
+            lock.release();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertEquals(0, lock.getQueueLength());
+            t1.join();
+            t2.join();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+    /**
+     * getQueuedThreads includes waiting threads
+     */
+    public void testGetQueuedThreads() { 
+	final PublicSemaphore lock = new PublicSemaphore(1, false);
+        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
+        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
+        try {
+            assertTrue(lock.getQueuedThreads().isEmpty());
+            lock.acquireUninterruptibly();
+            assertTrue(lock.getQueuedThreads().isEmpty());
+            t1.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(lock.getQueuedThreads().contains(t1));
+            t2.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(lock.getQueuedThreads().contains(t1));
+            assertTrue(lock.getQueuedThreads().contains(t2));
+            t1.interrupt();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertFalse(lock.getQueuedThreads().contains(t1));
+            assertTrue(lock.getQueuedThreads().contains(t2));
+            lock.release();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(lock.getQueuedThreads().isEmpty());
+            t1.join();
+            t2.join();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+    /**
+     * drainPermits reports and removes given number of permits
+     */
+    public void testDrainPermits() {
+        Semaphore s = new Semaphore(0, false);
+        assertEquals(0, s.availablePermits());
+        assertEquals(0, s.drainPermits());
+        s.release(10);
+        assertEquals(10, s.availablePermits());
+        assertEquals(10, s.drainPermits());
+        assertEquals(0, s.availablePermits());
+        assertEquals(0, s.drainPermits());
+    }
+
+    /**
+     * reducePermits reduces number of permits
+     */
+    public void testReducePermits() {
+        PublicSemaphore s = new PublicSemaphore(10, false);
+        assertEquals(10, s.availablePermits());
+        s.reducePermits(1);
+        assertEquals(9, s.availablePermits());
+        s.reducePermits(10);
+        assertEquals(-1, s.availablePermits());
+    }
+
+    /**
+     * a deserialized serialized semaphore has same number of permits
+     */
+    public void testSerialization() {
+        Semaphore l = new Semaphore(3, false);
+        try {
+            l.acquire();
+            l.release();
+            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
+            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
+            out.writeObject(l);
+            out.close();
+
+            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
+            Semaphore r = (Semaphore) in.readObject();
+            assertEquals(3, r.availablePermits());
+            assertFalse(r.isFair());
+            r.acquire();
+            r.release();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    }
+
+
+    /**
+     * Zero, negative, and positive initial values are allowed in constructor
+     */
+    public void testConstructor_fair() {
+        Semaphore s0 = new Semaphore(0, true);
+        assertEquals(0, s0.availablePermits());
+        assertTrue(s0.isFair());
+        Semaphore s1 = new Semaphore(-1, true);
+        assertEquals(-1, s1.availablePermits());
+        Semaphore s2 = new Semaphore(-1, true);
+        assertEquals(-1, s2.availablePermits());
+    }
+
+    /**
+     * tryAcquire succeeds when sufficient permits, else fails
+     */
+    public void testTryAcquireInSameThread_fair() {
+        Semaphore s = new Semaphore(2, true);
+        assertEquals(2, s.availablePermits());
+        assertTrue(s.tryAcquire());
+        assertTrue(s.tryAcquire());
+        assertEquals(0, s.availablePermits());
+        assertFalse(s.tryAcquire());
+    }
+
+    /**
+     * tryAcquire(n) succeeds when sufficient permits, else fails
+     */
+    public void testTryAcquireNInSameThread_fair() {
+        Semaphore s = new Semaphore(2, true);
+        assertEquals(2, s.availablePermits());
+        assertTrue(s.tryAcquire(2));
+        assertEquals(0, s.availablePermits());
+        assertFalse(s.tryAcquire());
+    }
+
+    /**
+     * Acquire and release of semaphore succeed if initially available
+     */
+    public void testAcquireReleaseInSameThread_fair() {
+        Semaphore s = new Semaphore(1, true);
+        try {
+            s.acquire();
+            s.release();
+            s.acquire();
+            s.release();
+            s.acquire();
+            s.release();
+            s.acquire();
+            s.release();
+            s.acquire();
+            s.release();
+            assertEquals(1, s.availablePermits());
+	} catch( InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * Acquire(n) and release(n) of semaphore succeed if initially available
+     */
+    public void testAcquireReleaseNInSameThread_fair() {
+        Semaphore s = new Semaphore(1, true);
+        try {
+            s.release(1);
+            s.acquire(1);
+            s.release(2);
+            s.acquire(2);
+            s.release(3);
+            s.acquire(3);
+            s.release(4);
+            s.acquire(4);
+            s.release(5);
+            s.acquire(5);
+            assertEquals(1, s.availablePermits());
+	} catch( InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * Acquire(n) and release(n) of semaphore succeed if initially available
+     */
+    public void testAcquireUninterruptiblyReleaseNInSameThread_fair() {
+        Semaphore s = new Semaphore(1, true);
+        try {
+            s.release(1);
+            s.acquireUninterruptibly(1);
+            s.release(2);
+            s.acquireUninterruptibly(2);
+            s.release(3);
+            s.acquireUninterruptibly(3);
+            s.release(4);
+            s.acquireUninterruptibly(4);
+            s.release(5);
+            s.acquireUninterruptibly(5);
+            assertEquals(1, s.availablePermits());
+	} finally {
+        }
+    }
+
+    /**
+     * release(n) in one thread enables timed acquire(n) in another thread
+     */
+    public void testTimedAcquireReleaseNInSameThread_fair() {
+        Semaphore s = new Semaphore(1, true);
+        try {
+            s.release(1);
+            assertTrue(s.tryAcquire(1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release(2);
+            assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release(3);
+            assertTrue(s.tryAcquire(3, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release(4);
+            assertTrue(s.tryAcquire(4, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release(5);
+            assertTrue(s.tryAcquire(5, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            assertEquals(1, s.availablePermits());
+	} catch( InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * release in one thread enables timed acquire in another thread
+     */
+    public void testTimedAcquireReleaseInSameThread_fair() {
+        Semaphore s = new Semaphore(1, true);
+        try {
+            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release();
+            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release();
+            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release();
+            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release();
+            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release();
+            assertEquals(1, s.availablePermits());
+	} catch( InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * A release in one thread enables an acquire in another thread
+     */
+    public void testAcquireReleaseInDifferentThreads_fair() {
+        final Semaphore s = new Semaphore(0, true);
+	Thread t = new Thread(new Runnable() {
+		public void run() {
+		    try {
+			s.acquire();
+                        s.acquire();
+                        s.acquire();
+                        s.acquire();
+		    } catch(InterruptedException ie){
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            s.release();
+            s.release();
+            s.release();
+            s.release();
+            s.release();
+            s.release();
+            t.join();
+            assertEquals(2, s.availablePermits());
+	} catch( InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * release(n) in one thread enables acquire(n) in another thread
+     */
+    public void testAcquireReleaseNInDifferentThreads_fair() {
+        final Semaphore s = new Semaphore(0, true);
+	Thread t = new Thread(new Runnable() {
+		public void run() {
+		    try {
+			s.acquire();
+                        s.release(2);
+                        s.acquire();
+		    } catch(InterruptedException ie){
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            s.release(2);
+            s.acquire(2);
+            s.release(1);
+            t.join();
+	} catch( InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * release(n) in one thread enables acquire(n) in another thread
+     */
+    public void testAcquireReleaseNInDifferentThreads_fair2() {
+        final Semaphore s = new Semaphore(0, true);
+	Thread t = new Thread(new Runnable() {
+		public void run() {
+		    try {
+                        s.acquire(2);
+                        s.acquire(2);
+                        s.release(4);
+		    } catch(InterruptedException ie){
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            s.release(6);
+            s.acquire(2);
+            s.acquire(2);
+            s.release(2);
+            t.join();
+	} catch( InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+
+
+
+
+    /**
+     * release in one thread enables timed acquire in another thread
+     */
+    public void testTimedAcquireReleaseInDifferentThreads_fair() {
+        final Semaphore s = new Semaphore(1, true);
+	Thread t = new Thread(new Runnable() {
+		public void run() {
+		    try {
+                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+
+		    } catch(InterruptedException ie){
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+	t.start();
+        try {
+            s.release();
+            s.release();
+            s.release();
+            s.release();
+            s.release();
+            t.join();
+	} catch( InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * release(n) in one thread enables timed acquire(n) in another thread
+     */
+    public void testTimedAcquireReleaseNInDifferentThreads_fair() {
+        final Semaphore s = new Semaphore(2, true);
+	Thread t = new Thread(new Runnable() {
+		public void run() {
+		    try {
+                        threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+                        s.release(2);
+                        threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+                        s.release(2);
+		    } catch(InterruptedException ie){
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+	t.start();
+        try {
+            assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release(2);
+            assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            s.release(2);
+            t.join();
+	} catch( InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * A waiting acquire blocks interruptibly
+     */
+    public void testAcquire_InterruptedException_fair() {
+	final Semaphore s = new Semaphore(0, true);
+	Thread t = new Thread(new Runnable() {
+		public void run() {
+		    try {
+			s.acquire();
+			threadShouldThrow();
+		    } catch(InterruptedException success){}
+		}
+	    });
+	t.start();
+	try {
+	    Thread.sleep(SHORT_DELAY_MS);
+            t.interrupt();
+            t.join();
+        } catch(InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * A waiting acquire(n) blocks interruptibly
+     */
+    public void testAcquireN_InterruptedException_fair() {
+	final Semaphore s = new Semaphore(2, true);
+	Thread t = new Thread(new Runnable() {
+		public void run() {
+		    try {
+			s.acquire(3);
+			threadShouldThrow();
+		    } catch(InterruptedException success){}
+		}
+	    });
+	t.start();
+	try {
+	    Thread.sleep(SHORT_DELAY_MS);
+            t.interrupt();
+            t.join();
+        } catch(InterruptedException e){
+            unexpectedException();
+        }
+    }
+    
+    /**
+     *  A waiting tryAcquire blocks interruptibly
+     */
+    public void testTryAcquire_InterruptedException_fair() {
+	final Semaphore s = new Semaphore(0, true);
+	Thread t = new Thread(new Runnable() {
+		public void run() {
+		    try {
+			s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
+			threadShouldThrow();
+		    } catch(InterruptedException success){
+                    }
+		}
+	    });
+	t.start();
+	try {
+	    Thread.sleep(SHORT_DELAY_MS);
+            t.interrupt();
+            t.join();
+        } catch(InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     *  A waiting tryAcquire(n) blocks interruptibly
+     */
+    public void testTryAcquireN_InterruptedException_fair() {
+	final Semaphore s = new Semaphore(1, true);
+	Thread t = new Thread(new Runnable() {
+		public void run() {
+		    try {
+			s.tryAcquire(4, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
+			threadShouldThrow();
+		    } catch(InterruptedException success){
+                    }
+		}
+	    });
+	t.start();
+	try {
+	    Thread.sleep(SHORT_DELAY_MS);
+            t.interrupt();
+            t.join();
+        } catch(InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * getQueueLength reports number of waiting threads
+     */
+    public void testGetQueueLength_fair() { 
+	final Semaphore lock = new Semaphore(1, true);
+        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
+        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
+        try {
+            assertEquals(0, lock.getQueueLength());
+            lock.acquireUninterruptibly();
+            t1.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertEquals(1, lock.getQueueLength());
+            t2.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertEquals(2, lock.getQueueLength());
+            t1.interrupt();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertEquals(1, lock.getQueueLength());
+            lock.release();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertEquals(0, lock.getQueueLength());
+            t1.join();
+            t2.join();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+
+    /**
+     * a deserialized serialized semaphore has same number of permits
+     */
+    public void testSerialization_fair() {
+        Semaphore l = new Semaphore(3, true);
+
+        try {
+            l.acquire();
+            l.release();
+            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
+            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
+            out.writeObject(l);
+            out.close();
+
+            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
+            Semaphore r = (Semaphore) in.readObject();
+            assertEquals(3, r.availablePermits());
+            assertTrue(r.isFair());
+            r.acquire();
+            r.release();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * toString indicates current number of permits
+     */
+    public void testToString() {
+        Semaphore s = new Semaphore(0);
+        String us = s.toString();
+        assertTrue(us.indexOf("Permits = 0") >= 0);
+        s.release();
+        String s1 = s.toString();
+        assertTrue(s1.indexOf("Permits = 1") >= 0);
+        s.release();
+        String s2 = s.toString();
+        assertTrue(s2.indexOf("Permits = 2") >= 0);
+    }
+
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SemaphoreTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SynchronousQueueTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SynchronousQueueTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SynchronousQueueTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SynchronousQueueTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,893 @@
+/*
+ * 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. 
+ */
+
+import junit.framework.*;
+import java.util.*;
+import java.util.concurrent.*;
+import java.io.*;
+
+public class SynchronousQueueTest extends JSR166TestCase {
+
+    public static void main(String[] args) {
+	junit.textui.TestRunner.run (suite());	
+    }
+
+    public static Test suite() {
+	return new TestSuite(SynchronousQueueTest.class);
+    }
+
+    /**
+     * A SynchronousQueue is both empty and full
+     */
+    public void testEmptyFull() {
+        SynchronousQueue q = new SynchronousQueue();
+        assertTrue(q.isEmpty());
+	assertEquals(0, q.size());
+        assertEquals(0, q.remainingCapacity());
+        assertFalse(q.offer(zero));
+    }
+
+    /**
+     * A fair SynchronousQueue is both empty and full
+     */
+    public void testFairEmptyFull() {
+        SynchronousQueue q = new SynchronousQueue(true);
+        assertTrue(q.isEmpty());
+	assertEquals(0, q.size());
+        assertEquals(0, q.remainingCapacity());
+        assertFalse(q.offer(zero));
+    }
+
+    /**
+     * offer(null) throws NPE
+     */
+    public void testOfferNull() {
+	try {
+            SynchronousQueue q = new SynchronousQueue();
+            q.offer(null);
+            shouldThrow();
+        } catch (NullPointerException success) { }   
+    }
+
+    /**
+     * add(null) throws NPE
+     */
+    public void testAddNull() {
+	try {
+            SynchronousQueue q = new SynchronousQueue();
+            q.add(null);
+            shouldThrow();
+        } catch (NullPointerException success) { }   
+    }
+
+    /**
+     * offer fails if no active taker
+     */
+    public void testOffer() {
+        SynchronousQueue q = new SynchronousQueue();
+        assertFalse(q.offer(one));
+    }
+
+    /**
+     * add throws ISE if no active taker
+     */
+    public void testAdd() {
+	try {
+            SynchronousQueue q = new SynchronousQueue();
+            assertEquals(0, q.remainingCapacity());
+            q.add(one);
+            shouldThrow();
+        } catch (IllegalStateException success){
+	}   
+    }
+
+    /**
+     * addAll(null) throws NPE
+     */
+    public void testAddAll1() {
+        try {
+            SynchronousQueue q = new SynchronousQueue();
+            q.addAll(null);
+            shouldThrow();
+        }
+        catch (NullPointerException success) {}
+    }
+
+    /**
+     * addAll(this) throws IAE
+     */
+    public void testAddAllSelf() {
+        try {
+            SynchronousQueue q = new SynchronousQueue();
+            q.addAll(q);
+            shouldThrow();
+        }
+        catch (IllegalArgumentException success) {}
+    }
+
+    /**
+     * addAll of a collection with null elements throws NPE
+     */
+    public void testAddAll2() {
+        try {
+            SynchronousQueue q = new SynchronousQueue();
+            Integer[] ints = new Integer[1];
+            q.addAll(Arrays.asList(ints));
+            shouldThrow();
+        }
+        catch (NullPointerException success) {}
+    }
+    /**
+     * addAll throws ISE if no active taker
+     */
+    public void testAddAll4() {
+        try {
+            SynchronousQueue q = new SynchronousQueue();
+            Integer[] ints = new Integer[1];
+            for (int i = 0; i < 1; ++i)
+                ints[i] = new Integer(i);
+            q.addAll(Arrays.asList(ints));
+            shouldThrow();
+        }
+        catch (IllegalStateException success) {}
+    }
+
+    /**
+     * put(null) throws NPE
+     */
+    public void testPutNull() {
+	try {
+            SynchronousQueue q = new SynchronousQueue();
+            q.put(null);
+            shouldThrow();
+        } 
+        catch (NullPointerException success){
+	}   
+        catch (InterruptedException ie) {
+	    unexpectedException();
+        }
+     }
+
+    /**
+     * put blocks interruptibly if no active taker
+     */
+    public void testBlockingPut() {
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        SynchronousQueue q = new SynchronousQueue();
+                        q.put(zero);
+                        threadShouldThrow();
+                    } catch (InterruptedException ie){
+                    }   
+                }});
+        t.start();
+        try { 
+           Thread.sleep(SHORT_DELAY_MS); 
+           t.interrupt();
+           t.join();
+        }
+        catch (InterruptedException ie) {
+	    unexpectedException();
+        }
+    }
+
+    /**
+     * put blocks waiting for take 
+     */
+    public void testPutWithTake() {
+        final SynchronousQueue q = new SynchronousQueue();
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    int added = 0;
+                    try {
+                        q.put(new Object());
+                        ++added;
+                        q.put(new Object());
+                        ++added;
+                        q.put(new Object());
+                        ++added;
+                        q.put(new Object());
+                        ++added;
+			threadShouldThrow();
+                    } catch (InterruptedException e){
+                        assertTrue(added >= 1);
+                    }
+                }
+            });
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            q.take();
+            Thread.sleep(SHORT_DELAY_MS);
+            t.interrupt();
+            t.join();
+        } catch (Exception e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * timed offer times out if elements not taken
+     */
+    public void testTimedOffer() {
+        final SynchronousQueue q = new SynchronousQueue();
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+
+                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
+			threadShouldThrow();
+                    } catch (InterruptedException success){}
+                }
+            });
+        
+        try {
+            t.start();
+            Thread.sleep(SMALL_DELAY_MS);
+            t.interrupt();
+            t.join();
+        } catch (Exception e){
+            unexpectedException();
+        }
+    }
+
+
+    /**
+     * take blocks interruptibly when empty
+     */
+    public void testTakeFromEmpty() {
+        final SynchronousQueue q = new SynchronousQueue();
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        q.take();
+			threadShouldThrow();
+                    } catch (InterruptedException success){ }                
+                }
+            });
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            t.interrupt();
+            t.join();
+        } catch (Exception e){
+            unexpectedException();
+        }
+    }
+
+
+    /**
+     * put blocks interruptibly if no active taker
+     */
+    public void testFairBlockingPut() {
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        SynchronousQueue q = new SynchronousQueue(true);
+                        q.put(zero);
+                        threadShouldThrow();
+                    } catch (InterruptedException ie){
+                    }   
+                }});
+        t.start();
+        try { 
+           Thread.sleep(SHORT_DELAY_MS); 
+           t.interrupt();
+           t.join();
+        }
+        catch (InterruptedException ie) {
+	    unexpectedException();
+        }
+    }
+
+    /**
+     * put blocks waiting for take 
+     */
+    public void testFairPutWithTake() {
+        final SynchronousQueue q = new SynchronousQueue(true);
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    int added = 0;
+                    try {
+                        q.put(new Object());
+                        ++added;
+                        q.put(new Object());
+                        ++added;
+                        q.put(new Object());
+                        ++added;
+                        q.put(new Object());
+                        ++added;
+			threadShouldThrow();
+                    } catch (InterruptedException e){
+                        assertTrue(added >= 1);
+                    }
+                }
+            });
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            q.take();
+            Thread.sleep(SHORT_DELAY_MS);
+            t.interrupt();
+            t.join();
+        } catch (Exception e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * timed offer times out if elements not taken
+     */
+    public void testFairTimedOffer() {
+        final SynchronousQueue q = new SynchronousQueue(true);
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+
+                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
+			threadShouldThrow();
+                    } catch (InterruptedException success){}
+                }
+            });
+        
+        try {
+            t.start();
+            Thread.sleep(SMALL_DELAY_MS);
+            t.interrupt();
+            t.join();
+        } catch (Exception e){
+            unexpectedException();
+        }
+    }
+
+
+    /**
+     * take blocks interruptibly when empty
+     */
+    public void testFairTakeFromEmpty() {
+        final SynchronousQueue q = new SynchronousQueue(true);
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        q.take();
+			threadShouldThrow();
+                    } catch (InterruptedException success){ }                
+                }
+            });
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            t.interrupt();
+            t.join();
+        } catch (Exception e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * poll fails unless active taker
+     */
+    public void testPoll() {
+        SynchronousQueue q = new SynchronousQueue();
+	assertNull(q.poll());
+    }
+
+    /**
+     * timed pool with zero timeout times out if no active taker
+     */
+    public void testTimedPoll0() {
+        try {
+            SynchronousQueue q = new SynchronousQueue();
+            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
+        } catch (InterruptedException e){
+	    unexpectedException();
+	}   
+    }
+
+    /**
+     * timed pool with nonzero timeout times out if no active taker
+     */
+    public void testTimedPoll() {
+        try {
+            SynchronousQueue q = new SynchronousQueue();
+            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+        } catch (InterruptedException e){
+	    unexpectedException();
+	}   
+    }
+
+    /**
+     * Interrupted timed poll throws InterruptedException instead of
+     * returning timeout status
+     */
+    public void testInterruptedTimedPoll() {
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        SynchronousQueue q = new SynchronousQueue();
+                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+                    } catch (InterruptedException success){
+                    }   
+                }});
+        t.start();
+        try { 
+           Thread.sleep(SHORT_DELAY_MS); 
+           t.interrupt();
+           t.join();
+        }
+        catch (InterruptedException ie) {
+	    unexpectedException();
+        }
+    }
+
+    /**
+     *  timed poll before a delayed offer fails; after offer succeeds;
+     *  on interruption throws
+     */
+    public void testTimedPollWithOffer() {
+        final SynchronousQueue q = new SynchronousQueue();
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
+                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
+			threadShouldThrow();
+                    } catch (InterruptedException success) { }                
+                }
+            });
+        try {
+            t.start();
+            Thread.sleep(SMALL_DELAY_MS);
+            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            t.interrupt();
+            t.join();
+        } catch (Exception e){
+            unexpectedException();
+        }
+    }  
+
+    /**
+     * Interrupted timed poll throws InterruptedException instead of
+     * returning timeout status
+     */
+    public void testFairInterruptedTimedPoll() {
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        SynchronousQueue q = new SynchronousQueue(true);
+                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+                    } catch (InterruptedException success){
+                    }   
+                }});
+        t.start();
+        try { 
+           Thread.sleep(SHORT_DELAY_MS); 
+           t.interrupt();
+           t.join();
+        }
+        catch (InterruptedException ie) {
+	    unexpectedException();
+        }
+    }
+
+    /**
+     *  timed poll before a delayed offer fails; after offer succeeds;
+     *  on interruption throws
+     */
+    public void testFairTimedPollWithOffer() {
+        final SynchronousQueue q = new SynchronousQueue(true);
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
+                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
+			threadShouldThrow();
+                    } catch (InterruptedException success) { }                
+                }
+            });
+        try {
+            t.start();
+            Thread.sleep(SMALL_DELAY_MS);
+            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            t.interrupt();
+            t.join();
+        } catch (Exception e){
+            unexpectedException();
+        }
+    }  
+
+
+    /**
+     * peek returns null
+     */
+    public void testPeek() {
+        SynchronousQueue q = new SynchronousQueue();
+	assertNull(q.peek());
+    }
+
+    /**
+     * element throws NSEE
+     */
+    public void testElement() {
+        SynchronousQueue q = new SynchronousQueue();
+        try {
+            q.element();
+            shouldThrow();
+        }
+        catch (NoSuchElementException success) {}
+    }
+
+    /**
+     * remove throws NSEE if no active taker
+     */
+    public void testRemove() {
+        SynchronousQueue q = new SynchronousQueue();
+        try {
+            q.remove();
+            shouldThrow();
+        } catch (NoSuchElementException success){
+	}   
+    }
+
+    /**
+     * remove(x) returns false
+     */
+    public void testRemoveElement() {
+        SynchronousQueue q = new SynchronousQueue();
+        assertFalse(q.remove(zero));
+        assertTrue(q.isEmpty());
+    }
+	
+    /**
+     * contains returns false
+     */
+    public void testContains() {
+        SynchronousQueue q = new SynchronousQueue();
+        assertFalse(q.contains(zero));
+    }
+
+    /**
+     * clear ensures isEmpty
+     */
+    public void testClear() {
+        SynchronousQueue q = new SynchronousQueue();
+        q.clear();
+        assertTrue(q.isEmpty());
+    }
+
+    /**
+     * containsAll returns false unless empty
+     */
+    public void testContainsAll() {
+        SynchronousQueue q = new SynchronousQueue();
+        Integer[] empty = new Integer[0];
+        assertTrue(q.containsAll(Arrays.asList(empty)));
+        Integer[] ints = new Integer[1]; ints[0] = zero;
+        assertFalse(q.containsAll(Arrays.asList(ints)));
+    }
+
+    /**
+     * retainAll returns false
+     */
+    public void testRetainAll() {
+        SynchronousQueue q = new SynchronousQueue();
+        Integer[] empty = new Integer[0];
+        assertFalse(q.retainAll(Arrays.asList(empty)));
+        Integer[] ints = new Integer[1]; ints[0] = zero;
+        assertFalse(q.retainAll(Arrays.asList(ints)));
+    }
+
+    /**
+     * removeAll returns false
+     */
+    public void testRemoveAll() {
+        SynchronousQueue q = new SynchronousQueue();
+        Integer[] empty = new Integer[0];
+        assertFalse(q.removeAll(Arrays.asList(empty)));
+        Integer[] ints = new Integer[1]; ints[0] = zero;
+        assertFalse(q.containsAll(Arrays.asList(ints)));
+    }
+
+
+    /**
+     * toArray is empty
+     */
+    public void testToArray() {
+        SynchronousQueue q = new SynchronousQueue();
+	Object[] o = q.toArray();
+        assertEquals(o.length, 0);
+    }
+
+    /**
+     * toArray(a) is nulled at position 0
+     */
+    public void testToArray2() {
+        SynchronousQueue q = new SynchronousQueue();
+	Integer[] ints = new Integer[1];
+        assertNull(ints[0]);
+    }
+    
+    /**
+     * toArray(null) throws NPE
+     */
+    public void testToArray_BadArg() {
+	try {
+            SynchronousQueue q = new SynchronousQueue();
+	    Object o[] = q.toArray(null);
+	    shouldThrow();
+	} catch(NullPointerException success){}
+    }
+
+
+    /**
+     * iterator does not traverse any elements
+     */
+    public void testIterator() {
+        SynchronousQueue q = new SynchronousQueue();
+	Iterator it = q.iterator();
+        assertFalse(it.hasNext());
+        try {
+            Object x = it.next();
+            shouldThrow();
+        }
+        catch (NoSuchElementException success) {}
+    }
+
+    /**
+     * iterator remove throws ISE
+     */
+    public void testIteratorRemove() {
+        SynchronousQueue q = new SynchronousQueue();
+	Iterator it = q.iterator();
+        try {
+            it.remove();
+            shouldThrow();
+        }
+        catch (IllegalStateException success) {}
+    }
+
+    /**
+     * toString returns a non-null string
+     */
+    public void testToString() {
+        SynchronousQueue q = new SynchronousQueue();
+        String s = q.toString();
+        assertNotNull(s);
+    }        
+
+
+    /**
+     * offer transfers elements across Executor tasks
+     */
+    public void testOfferInExecutor() {
+        final SynchronousQueue q = new SynchronousQueue();
+        ExecutorService executor = Executors.newFixedThreadPool(2);
+        final Integer one = new Integer(1);
+
+        executor.execute(new Runnable() {
+            public void run() {
+                threadAssertFalse(q.offer(one));
+                try {
+                    threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
+                    threadAssertEquals(0, q.remainingCapacity());
+                }
+                catch (InterruptedException e) {
+                    threadUnexpectedException();
+                }
+            }
+        });
+
+        executor.execute(new Runnable() {
+            public void run() {
+                try {
+                    Thread.sleep(SMALL_DELAY_MS);
+                    threadAssertEquals(one, q.take());
+                }
+                catch (InterruptedException e) {
+                    threadUnexpectedException();
+                }
+            }
+        });
+        
+        joinPool(executor);
+
+    }
+
+    /**
+     * poll retrieves elements across Executor threads
+     */
+    public void testPollInExecutor() {
+        final SynchronousQueue q = new SynchronousQueue();
+        ExecutorService executor = Executors.newFixedThreadPool(2);
+        executor.execute(new Runnable() {
+            public void run() {
+                threadAssertNull(q.poll());
+                try {
+                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
+                    threadAssertTrue(q.isEmpty());
+                }
+                catch (InterruptedException e) {
+                    threadUnexpectedException();
+                }
+            }
+        });
+
+        executor.execute(new Runnable() {
+            public void run() {
+                try {
+                    Thread.sleep(SMALL_DELAY_MS);
+                    q.put(new Integer(1));
+                }
+                catch (InterruptedException e) {
+                    threadUnexpectedException();
+                }
+            }
+        });
+        
+        joinPool(executor);
+    }
+
+    /**
+     * a deserialized serialized queue is usable
+     */
+    public void testSerialization() {
+        SynchronousQueue q = new SynchronousQueue();
+        try {
+            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
+            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
+            out.writeObject(q);
+            out.close();
+
+            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
+            SynchronousQueue r = (SynchronousQueue)in.readObject();
+            assertEquals(q.size(), r.size());
+            while (!q.isEmpty()) 
+                assertEquals(q.remove(), r.remove());
+        } catch(Exception e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * drainTo(null) throws NPE
+     */ 
+    public void testDrainToNull() {
+        SynchronousQueue q = new SynchronousQueue();
+        try {
+            q.drainTo(null);
+            shouldThrow();
+        } catch(NullPointerException success) {
+        }
+    }
+
+    /**
+     * drainTo(this) throws IAE
+     */ 
+    public void testDrainToSelf() {
+        SynchronousQueue q = new SynchronousQueue();
+        try {
+            q.drainTo(q);
+            shouldThrow();
+        } catch(IllegalArgumentException success) {
+        }
+    }
+
+    /**
+     * drainTo(c) of empty queue doesn't transfer elements
+     */ 
+    public void testDrainTo() {
+        SynchronousQueue q = new SynchronousQueue();
+        ArrayList l = new ArrayList();
+        q.drainTo(l);
+        assertEquals(q.size(), 0);
+        assertEquals(l.size(), 0);
+    }
+
+    /**
+     * drainTo empties queue, unblocking a waiting put.
+     */ 
+    public void testDrainToWithActivePut() {
+        final SynchronousQueue q = new SynchronousQueue();
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        q.put(new Integer(1));
+                    } catch (InterruptedException ie){ 
+                        threadUnexpectedException();
+                    }
+                }
+            });
+        try {
+            t.start();
+            ArrayList l = new ArrayList();
+            Thread.sleep(SHORT_DELAY_MS);
+            q.drainTo(l);
+            assertTrue(l.size() <= 1);
+            if (l.size() > 0)
+                assertEquals(l.get(0), new Integer(1));
+            t.join();
+            assertTrue(l.size() <= 1);
+        } catch(Exception e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * drainTo(null, n) throws NPE
+     */ 
+    public void testDrainToNullN() {
+        SynchronousQueue q = new SynchronousQueue();
+        try {
+            q.drainTo(null, 0);
+            shouldThrow();
+        } catch(NullPointerException success) {
+        }
+    }
+
+    /**
+     * drainTo(this, n) throws IAE
+     */ 
+    public void testDrainToSelfN() {
+        SynchronousQueue q = new SynchronousQueue();
+        try {
+            q.drainTo(q, 0);
+            shouldThrow();
+        } catch(IllegalArgumentException success) {
+        }
+    }
+
+    /**
+     * drainTo(c, n) empties up to n elements of queue into c
+     */ 
+    public void testDrainToN() {
+        final SynchronousQueue q = new SynchronousQueue();
+        Thread t1 = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        q.put(one);
+                    } catch (InterruptedException ie){ 
+                        threadUnexpectedException();
+                    }
+                }
+            });
+        Thread t2 = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        q.put(two);
+                    } catch (InterruptedException ie){ 
+                        threadUnexpectedException();
+                    }
+                }
+            });
+
+        try {
+            t1.start();
+            t2.start();
+            ArrayList l = new ArrayList();
+            Thread.sleep(SHORT_DELAY_MS);
+            q.drainTo(l, 1);
+            assertTrue(l.size() == 1);
+            q.drainTo(l, 1);
+            assertTrue(l.size() == 2);
+            assertTrue(l.contains(one));
+            assertTrue(l.contains(two));
+            t1.join();
+            t2.join();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    }
+
+
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SynchronousQueueTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SystemTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SystemTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SystemTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SystemTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,79 @@
+/*
+ * 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. 
+ */
+
+import junit.framework.*;
+
+public class SystemTest extends JSR166TestCase {
+    public static void main(String[] args) {
+        junit.textui.TestRunner.run(suite());   
+    }
+    
+    public static Test suite() {
+        return new TestSuite(SystemTest.class);
+    }
+
+    /** 
+     * Worst case rounding for millisecs; set for 60 cycle millis clock.
+     * This value might need to be changed os JVMs with coarser
+     *  System.currentTimeMillis clocks.
+     */
+    static final long MILLIS_ROUND = 17;
+
+    /**
+     * Nanos between readings of millis is no longer than millis (plus
+     * possible rounding).
+     * This shows only that nano timing not (much) worse than milli.
+     */
+    public void testNanoTime1() {
+        try {
+            long m1 = System.currentTimeMillis();
+            Thread.sleep(1);
+            long n1 = System.nanoTime();
+            Thread.sleep(SHORT_DELAY_MS);
+            long n2 = System.nanoTime();
+            Thread.sleep(1);
+            long m2 = System.currentTimeMillis();
+            long millis = m2 - m1;
+            long nanos = n2 - n1;
+            assertTrue(nanos >= 0);
+            long nanosAsMillis = nanos / 1000000;
+            assertTrue(nanosAsMillis <= millis + MILLIS_ROUND);
+        }
+        catch(InterruptedException ie) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * Millis between readings of nanos is less than nanos, adjusting
+     * for rounding.
+     * This shows only that nano timing not (much) worse than milli.
+     */
+    public void testNanoTime2() {
+        try {
+            long n1 = System.nanoTime();
+            Thread.sleep(1);
+            long m1 = System.currentTimeMillis();
+            Thread.sleep(SHORT_DELAY_MS);
+            long m2 = System.currentTimeMillis();
+            Thread.sleep(1);
+            long n2 = System.nanoTime();
+            long millis = m2 - m1;
+            long nanos = n2 - n1;
+            
+            assertTrue(nanos >= 0);
+            long nanosAsMillis = nanos / 1000000;
+            assertTrue(millis <= nanosAsMillis + MILLIS_ROUND);
+        }
+        catch(InterruptedException ie) {
+            unexpectedException();
+        }
+    }
+
+}
+

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SystemTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ThreadLocalTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ThreadLocalTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ThreadLocalTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ThreadLocalTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,106 @@
+/*
+ * 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. 
+ */
+
+import junit.framework.*;
+import java.util.concurrent.Semaphore;
+
+public class ThreadLocalTest extends JSR166TestCase {
+    public static void main(String[] args) {
+	junit.textui.TestRunner.run(suite());	
+    }
+    
+    public static Test suite() {
+	return new TestSuite(ThreadLocalTest.class);
+    }
+
+    static ThreadLocal<Integer> tl = new ThreadLocal<Integer>() {
+            public Integer initialValue() {
+                return one;
+            }
+        };
+
+    static InheritableThreadLocal<Integer> itl =
+        new InheritableThreadLocal<Integer>() {
+            protected Integer initialValue() {
+                return zero;
+            }
+            
+            protected Integer childValue(Integer parentValue) {
+                return new Integer(parentValue.intValue() + 1);
+            }
+        };
+
+    /**
+     * remove causes next access to return initial value
+     */
+    public void testRemove() {
+        assertEquals(tl.get(), one);
+        tl.set(two);
+        assertEquals(tl.get(), two);
+        tl.remove();
+        assertEquals(tl.get(), one);
+    }
+
+    /**
+     * remove in InheritableThreadLocal causes next access to return
+     * initial value
+     */
+    public void testRemoveITL() {
+        assertEquals(itl.get(), zero);
+        itl.set(two);
+        assertEquals(itl.get(), two);
+        itl.remove();
+        assertEquals(itl.get(), zero);
+    }
+
+    private class ITLThread extends Thread {
+        final int[] x;
+        ITLThread(int[] array) { x = array; }
+        public void run() {
+            Thread child = null;
+            if (itl.get().intValue() < x.length - 1) {
+                child = new ITLThread(x);
+                child.start();
+            }
+            Thread.currentThread().yield();
+            
+            int threadId = itl.get().intValue();
+            for (int j = 0; j < threadId; j++) {
+                x[threadId]++;
+                Thread.currentThread().yield();
+            }
+            
+            if (child != null) { // Wait for child (if any)
+                try {
+                    child.join();
+                } catch(InterruptedException e) {
+                    threadUnexpectedException();
+                }
+            }
+        }
+    }
+
+    /**
+     * InheritableThreadLocal propagates generic values.
+     */
+    public void testGenericITL() {
+        final int threadCount = 10;
+        final int x[] = new int[threadCount];
+        Thread progenitor = new ITLThread(x);
+        try {
+            progenitor.start();
+            progenitor.join();
+            for(int i = 0; i < threadCount; i++) {
+                assertEquals(i, x[i]);
+            }
+        } catch(InterruptedException e) {
+            unexpectedException();
+        }
+    }
+}
+

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ThreadLocalTest.java
------------------------------------------------------------------------------
    svn:eol-style = native