10 |
|
import junit.framework.*; |
11 |
|
import java.util.*; |
12 |
|
import java.util.concurrent.*; |
13 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
|
import java.util.concurrent.locks.*; |
15 |
|
import java.io.*; |
16 |
|
|
17 |
|
public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { |
18 |
|
public static void main(String[] args) { |
19 |
< |
junit.textui.TestRunner.run (suite()); |
19 |
> |
junit.textui.TestRunner.run(suite()); |
20 |
|
} |
21 |
|
public static Test suite() { |
22 |
|
return new TestSuite(AbstractQueuedLongSynchronizerTest.class); |
47 |
|
return true; |
48 |
|
} |
49 |
|
|
50 |
< |
public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { return new AbstractQueuedLongSynchronizer.ConditionObject(); } |
50 |
> |
public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { |
51 |
> |
return new AbstractQueuedLongSynchronizer.ConditionObject(); |
52 |
> |
} |
53 |
|
|
54 |
|
} |
55 |
|
|
332 |
|
sync.acquire(1); |
333 |
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
334 |
|
public void realRun() throws InterruptedException { |
335 |
< |
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
335 |
> |
sync.tryAcquireNanos(1, MILLISECONDS.toNanos(MEDIUM_DELAY_MS)); |
336 |
|
}}); |
337 |
|
|
338 |
|
t.start(); |
339 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
340 |
|
t.interrupt(); |
341 |
|
t.join(); |
342 |
|
} |
350 |
|
sync.acquire(1); |
351 |
|
Thread t = new Thread(new CheckedRunnable() { |
352 |
|
public void realRun() { |
353 |
< |
threadAssertFalse(sync.tryAcquire(1)); |
353 |
> |
assertFalse(sync.tryAcquire(1)); |
354 |
|
}}); |
355 |
|
|
356 |
|
t.start(); |
366 |
|
sync.acquire(1); |
367 |
|
Thread t = new Thread(new CheckedRunnable() { |
368 |
|
public void realRun() throws InterruptedException { |
369 |
< |
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
369 |
> |
long nanos = MILLISECONDS.toNanos(SHORT_DELAY_MS); |
370 |
> |
assertFalse(sync.tryAcquireNanos(1, nanos)); |
371 |
|
}}); |
372 |
|
|
373 |
|
t.start(); |
423 |
|
sync.acquireInterruptibly(1); |
424 |
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
425 |
|
t.start(); |
426 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
427 |
|
t.interrupt(); |
428 |
|
assertTrue(sync.isHeldExclusively()); |
429 |
|
t.join(); |
477 |
|
} |
478 |
|
|
479 |
|
/** |
480 |
< |
* Timed await without a signal times out |
480 |
> |
* Timed await without a signal times out |
481 |
|
*/ |
482 |
|
public void testAwait_Timeout() throws InterruptedException { |
483 |
|
final Mutex sync = new Mutex(); |
484 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
485 |
|
sync.acquire(1); |
486 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
486 |
> |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
487 |
|
sync.release(1); |
488 |
|
} |
489 |
|
|
646 |
|
Thread t = new Thread(new CheckedRunnable() { |
647 |
|
public void realRun() throws InterruptedException { |
648 |
|
sync.acquire(1); |
649 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
650 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
649 |
> |
assertFalse(sync.hasWaiters(c)); |
650 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
651 |
|
c.await(); |
652 |
|
sync.release(1); |
653 |
|
}}); |
677 |
|
Thread t1 = new Thread(new CheckedRunnable() { |
678 |
|
public void realRun() throws InterruptedException { |
679 |
|
sync.acquire(1); |
680 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
681 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
680 |
> |
assertFalse(sync.hasWaiters(c)); |
681 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
682 |
|
c.await(); |
683 |
|
sync.release(1); |
684 |
|
}}); |
686 |
|
Thread t2 = new Thread(new CheckedRunnable() { |
687 |
|
public void realRun() throws InterruptedException { |
688 |
|
sync.acquire(1); |
689 |
< |
threadAssertTrue(sync.hasWaiters(c)); |
690 |
< |
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
689 |
> |
assertTrue(sync.hasWaiters(c)); |
690 |
> |
assertEquals(1, sync.getWaitQueueLength(c)); |
691 |
|
c.await(); |
692 |
|
sync.release(1); |
693 |
|
}}); |
721 |
|
Thread t1 = new Thread(new CheckedRunnable() { |
722 |
|
public void realRun() throws InterruptedException { |
723 |
|
sync.acquire(1); |
724 |
< |
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
724 |
> |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
725 |
|
c.await(); |
726 |
|
sync.release(1); |
727 |
|
}}); |
729 |
|
Thread t2 = new Thread(new CheckedRunnable() { |
730 |
|
public void realRun() throws InterruptedException { |
731 |
|
sync.acquire(1); |
732 |
< |
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
732 |
> |
assertFalse(sync.getWaitingThreads(c).isEmpty()); |
733 |
|
c.await(); |
734 |
|
sync.release(1); |
735 |
|
}}); |
793 |
|
public void realRun() throws InterruptedException { |
794 |
|
sync.acquire(1); |
795 |
|
c.await(); |
790 |
– |
sync.release(1); |
796 |
|
}}); |
797 |
|
|
798 |
|
t.start(); |
811 |
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
812 |
|
public void realRun() throws InterruptedException { |
813 |
|
sync.acquire(1); |
814 |
< |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
810 |
< |
sync.release(1); |
814 |
> |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
815 |
|
}}); |
816 |
|
|
817 |
|
t.start(); |
832 |
|
sync.acquire(1); |
833 |
|
java.util.Date d = new java.util.Date(); |
834 |
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
831 |
– |
sync.release(1); |
835 |
|
}}); |
836 |
|
|
837 |
|
t.start(); |
936 |
|
|
937 |
|
Thread t = new Thread(new CheckedRunnable() { |
938 |
|
public void realRun() throws InterruptedException { |
939 |
< |
threadAssertFalse(l.isSignalled()); |
939 |
> |
assertFalse(l.isSignalled()); |
940 |
|
l.acquireSharedInterruptibly(0); |
941 |
< |
threadAssertTrue(l.isSignalled()); |
941 |
> |
assertTrue(l.isSignalled()); |
942 |
|
}}); |
943 |
|
|
944 |
|
t.start(); |
958 |
|
|
959 |
|
Thread t = new Thread(new CheckedRunnable() { |
960 |
|
public void realRun() throws InterruptedException { |
961 |
< |
threadAssertFalse(l.isSignalled()); |
962 |
< |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
963 |
< |
threadAssertTrue(l.isSignalled()); |
961 |
> |
assertFalse(l.isSignalled()); |
962 |
> |
long nanos = MILLISECONDS.toNanos(MEDIUM_DELAY_MS); |
963 |
> |
assertTrue(l.tryAcquireSharedNanos(0, nanos)); |
964 |
> |
assertTrue(l.isSignalled()); |
965 |
|
}}); |
966 |
|
|
967 |
|
t.start(); |
975 |
|
/** |
976 |
|
* acquireSharedInterruptibly throws IE if interrupted before released |
977 |
|
*/ |
978 |
< |
public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { |
978 |
> |
public void testAcquireSharedInterruptibly_InterruptedException() |
979 |
> |
throws InterruptedException { |
980 |
|
final BooleanLatch l = new BooleanLatch(); |
981 |
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
982 |
|
public void realRun() throws InterruptedException { |
983 |
< |
threadAssertFalse(l.isSignalled()); |
983 |
> |
assertFalse(l.isSignalled()); |
984 |
|
l.acquireSharedInterruptibly(0); |
985 |
|
}}); |
986 |
|
|
997 |
|
final BooleanLatch l = new BooleanLatch(); |
998 |
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
999 |
|
public void realRun() throws InterruptedException { |
1000 |
< |
threadAssertFalse(l.isSignalled()); |
1001 |
< |
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
1000 |
> |
assertFalse(l.isSignalled()); |
1001 |
> |
long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); |
1002 |
> |
l.tryAcquireSharedNanos(0, nanos); |
1003 |
|
}}); |
1004 |
|
|
1005 |
|
t.start(); |
1016 |
|
final BooleanLatch l = new BooleanLatch(); |
1017 |
|
Thread t = new Thread(new CheckedRunnable() { |
1018 |
|
public void realRun() throws InterruptedException { |
1019 |
< |
threadAssertFalse(l.isSignalled()); |
1020 |
< |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1019 |
> |
assertFalse(l.isSignalled()); |
1020 |
> |
long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); |
1021 |
> |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1022 |
|
}}); |
1023 |
|
|
1024 |
|
t.start(); |