ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantLockTest.java (file contents):
Revision 1.18 by dl, Sat Jan 10 01:41:59 2004 UTC vs.
Revision 1.24 by dl, Fri Feb 24 00:03:16 2006 UTC

# Line 205 | Line 205 | public class ReentrantLockTest extends J
205      }
206  
207      /**
208 +     * hasQueuedThread(null) throws NPE
209 +     */
210 +    public void testHasQueuedThreadNPE() {
211 +        final ReentrantLock sync = new ReentrantLock();
212 +        try {
213 +            sync.hasQueuedThread(null);
214 +            shouldThrow();
215 +        } catch (NullPointerException success) {
216 +        }
217 +    }
218 +
219 +    /**
220 +     * hasQueuedThread reports whether a thread is queued.
221 +     */
222 +    public void testHasQueuedThread() {
223 +        final ReentrantLock sync = new ReentrantLock();
224 +        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
225 +        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
226 +        try {
227 +            assertFalse(sync.hasQueuedThread(t1));
228 +            assertFalse(sync.hasQueuedThread(t2));
229 +            sync.lock();
230 +            t1.start();
231 +            Thread.sleep(SHORT_DELAY_MS);
232 +            assertTrue(sync.hasQueuedThread(t1));
233 +            t2.start();
234 +            Thread.sleep(SHORT_DELAY_MS);
235 +            assertTrue(sync.hasQueuedThread(t1));
236 +            assertTrue(sync.hasQueuedThread(t2));
237 +            t1.interrupt();
238 +            Thread.sleep(SHORT_DELAY_MS);
239 +            assertFalse(sync.hasQueuedThread(t1));
240 +            assertTrue(sync.hasQueuedThread(t2));
241 +            sync.unlock();
242 +            Thread.sleep(SHORT_DELAY_MS);
243 +            assertFalse(sync.hasQueuedThread(t1));
244 +            Thread.sleep(SHORT_DELAY_MS);
245 +            assertFalse(sync.hasQueuedThread(t2));
246 +            t1.join();
247 +            t2.join();
248 +        } catch(Exception e){
249 +            unexpectedException();
250 +        }
251 +    }
252 +
253 +
254 +    /**
255       * getQueuedThreads includes waiting threads
256       */
257      public void testGetQueuedThreads() {
# Line 362 | Line 409 | public class ReentrantLockTest extends J
409          Thread t = new Thread(new InterruptedLockRunnable(lock));
410          try {
411              t.start();
412 +            Thread.sleep(SHORT_DELAY_MS);
413              t.interrupt();
414              lock.unlock();
415              t.join();
# Line 451 | Line 499 | public class ReentrantLockTest extends J
499          final Condition c = lock.newCondition();
500          try {
501              lock.lock();
502 <            assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
502 >            c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
503              lock.unlock();
504          }
505          catch (Exception ex) {
# Line 468 | Line 516 | public class ReentrantLockTest extends J
516          try {
517              lock.lock();
518              java.util.Date d = new java.util.Date();
519 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
519 >            c.awaitUntil(new java.util.Date(d.getTime() + 10));
520              lock.unlock();
521          }
522          catch (Exception ex) {
# Line 816 | Line 864 | public class ReentrantLockTest extends J
864          }
865      }
866  
867 <
867 >    /** A helper class for uninterruptible wait tests */
868 >    class UninterruptableThread extends Thread {
869 >        private ReentrantLock lock;
870 >        private Condition c;
871 >        
872 >        public volatile boolean canAwake = false;
873 >        public volatile boolean interrupted = false;
874 >        public volatile boolean lockStarted = false;
875 >        
876 >        public UninterruptableThread(ReentrantLock lock, Condition c) {
877 >            this.lock = lock;
878 >            this.c = c;
879 >        }
880 >        
881 >        public synchronized void run() {
882 >            lock.lock();
883 >            lockStarted = true;
884 >            
885 >            while (!canAwake) {
886 >                c.awaitUninterruptibly();
887 >            }
888 >            
889 >            interrupted = isInterrupted();
890 >            lock.unlock();
891 >        }
892 >    }
893  
894      /**
895       * awaitUninterruptibly doesn't abort on interrupt
896       */
897      public void testAwaitUninterruptibly() {
898 <        final ReentrantLock lock = new ReentrantLock();
898 >        final ReentrantLock lock = new ReentrantLock();
899          final Condition c = lock.newCondition();
900 <        Thread t = new Thread(new Runnable() {
828 <                public void run() {
829 <                    lock.lock();
830 <                    c.awaitUninterruptibly();
831 <                    lock.unlock();
832 <                }
833 <            });
900 >        UninterruptableThread thread = new UninterruptableThread(lock, c);
901  
902          try {
903 <            t.start();
904 <            Thread.sleep(SHORT_DELAY_MS);
905 <            t.interrupt();
903 >            thread.start();
904 >
905 >            while (!thread.lockStarted) {
906 >                Thread.sleep(100);
907 >            }
908 >
909              lock.lock();
910 <            c.signal();
911 <            lock.unlock();
912 <            assert(t.isInterrupted());
913 <            t.join(SHORT_DELAY_MS);
914 <            assertFalse(t.isAlive());
915 <        }
916 <        catch (Exception ex) {
910 >            try {
911 >                thread.interrupt();
912 >                thread.canAwake = true;
913 >                c.signal();
914 >            } finally {
915 >                lock.unlock();
916 >            }
917 >
918 >            thread.join();
919 >            assertTrue(thread.interrupted);
920 >            assertFalse(thread.isAlive());
921 >        } catch (Exception ex) {
922              unexpectedException();
923          }
924      }
# Line 969 | Line 1044 | public class ReentrantLockTest extends J
1044                          lock.unlock();
1045                      }
1046                      catch(InterruptedException e) {
1047 +                        threadUnexpectedException();
1048 +                    }
1049 +                }
1050 +            });
1051 +
1052 +        try {
1053 +            t1.start();
1054 +            t2.start();
1055 +            Thread.sleep(SHORT_DELAY_MS);
1056 +            lock.lock();
1057 +            c.signalAll();
1058 +            lock.unlock();
1059 +            t1.join(SHORT_DELAY_MS);
1060 +            t2.join(SHORT_DELAY_MS);
1061 +            assertFalse(t1.isAlive());
1062 +            assertFalse(t2.isAlive());
1063 +        }
1064 +        catch (Exception ex) {
1065 +            unexpectedException();
1066 +        }
1067 +    }
1068 +
1069 +    /**
1070 +     * await after multiple reentrant locking preserves lock count
1071 +     */
1072 +    public void testAwaitLockCount() {
1073 +        final ReentrantLock lock = new ReentrantLock();
1074 +        final Condition c = lock.newCondition();
1075 +        Thread t1 = new Thread(new Runnable() {
1076 +                public void run() {
1077 +                    try {
1078 +                        lock.lock();
1079 +                        threadAssertEquals(1, lock.getHoldCount());
1080 +                        c.await();
1081 +                        threadAssertEquals(1, lock.getHoldCount());
1082 +                        lock.unlock();
1083 +                    }
1084 +                    catch(InterruptedException e) {
1085 +                        threadUnexpectedException();
1086 +                    }
1087 +                }
1088 +            });
1089 +
1090 +        Thread t2 = new Thread(new Runnable() {
1091 +                public void run() {
1092 +                    try {
1093 +                        lock.lock();
1094 +                        lock.lock();
1095 +                        threadAssertEquals(2, lock.getHoldCount());
1096 +                        c.await();
1097 +                        threadAssertEquals(2, lock.getHoldCount());
1098 +                        lock.unlock();
1099 +                        lock.unlock();
1100 +                    }
1101 +                    catch(InterruptedException e) {
1102                          threadUnexpectedException();
1103                      }
1104                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines