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.15 by dl, Mon Dec 29 19:05:40 2003 UTC vs.
Revision 1.21 by dl, Mon May 2 19:20:00 2005 UTC

# Line 176 | Line 176 | public class ReentrantLockTest extends J
176      }
177  
178      /**
179 +     * getQueueLength reports number of waiting threads
180 +     */
181 +    public void testGetQueueLength_fair() {
182 +        final ReentrantLock lock = new ReentrantLock(true);
183 +        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
184 +        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
185 +        try {
186 +            assertEquals(0, lock.getQueueLength());
187 +            lock.lock();
188 +            t1.start();
189 +            Thread.sleep(SHORT_DELAY_MS);
190 +            assertEquals(1, lock.getQueueLength());
191 +            t2.start();
192 +            Thread.sleep(SHORT_DELAY_MS);
193 +            assertEquals(2, lock.getQueueLength());
194 +            t1.interrupt();
195 +            Thread.sleep(SHORT_DELAY_MS);
196 +            assertEquals(1, lock.getQueueLength());
197 +            lock.unlock();
198 +            Thread.sleep(SHORT_DELAY_MS);
199 +            assertEquals(0, lock.getQueueLength());
200 +            t1.join();
201 +            t2.join();
202 +        } catch(Exception e){
203 +            unexpectedException();
204 +        }
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 422 | Line 498 | public class ReentrantLockTest extends J
498          final Condition c = lock.newCondition();
499          try {
500              lock.lock();
501 <            assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
501 >            c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
502              lock.unlock();
503          }
504          catch (Exception ex) {
# Line 439 | Line 515 | public class ReentrantLockTest extends J
515          try {
516              lock.lock();
517              java.util.Date d = new java.util.Date();
518 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
518 >            c.awaitUntil(new java.util.Date(d.getTime() + 10));
519              lock.unlock();
520          }
521          catch (Exception ex) {
# Line 810 | Line 886 | public class ReentrantLockTest extends J
886              lock.lock();
887              c.signal();
888              lock.unlock();
889 <            assert(t.isInterrupted());
889 >            assertTrue(t.isInterrupted());
890              t.join(SHORT_DELAY_MS);
891              assertFalse(t.isAlive());
892          }
# Line 860 | Line 936 | public class ReentrantLockTest extends J
936                  public void run() {
937                      try {
938                          lock.lock();
939 <                        c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
939 >                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
940                          lock.unlock();
941                          threadShouldThrow();
942                      }
# Line 987 | Line 1063 | public class ReentrantLockTest extends J
1063          }
1064      }
1065  
1066 +    /**
1067 +     * toString indicates current lock state
1068 +     */
1069 +    public void testToString() {
1070 +        ReentrantLock lock = new ReentrantLock();
1071 +        String us = lock.toString();
1072 +        assertTrue(us.indexOf("Unlocked") >= 0);
1073 +        lock.lock();
1074 +        String ls = lock.toString();
1075 +        assertTrue(ls.indexOf("Locked") >= 0);
1076 +    }
1077 +
1078   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines