1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
+ |
import java.util.Arrays; |
11 |
+ |
import java.util.NoSuchElementException; |
12 |
|
import java.util.PropertyPermission; |
13 |
|
import java.util.concurrent.*; |
14 |
|
import java.util.concurrent.atomic.AtomicReference; |
15 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
16 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
17 |
|
import java.security.CodeSource; |
18 |
|
import java.security.Permission; |
19 |
|
import java.security.PermissionCollection; |
262 |
|
SHORT_DELAY_MS = getShortDelay(); |
263 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
264 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
265 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
265 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
266 |
|
} |
267 |
|
|
268 |
|
/** |
291 |
|
* earlier by threadRecordFailure. |
292 |
|
*/ |
293 |
|
public void tearDown() throws Exception { |
294 |
< |
Throwable t = threadFailure.get(); |
294 |
> |
Throwable t = threadFailure.getAndSet(null); |
295 |
|
if (t != null) { |
296 |
|
if (t instanceof Error) |
297 |
|
throw (Error) t; |
449 |
|
try { |
450 |
|
exec.shutdown(); |
451 |
|
assertTrue("ExecutorService did not terminate in a timely manner", |
452 |
< |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
452 |
> |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
453 |
|
} catch (SecurityException ok) { |
454 |
|
// Allowed in case test doesn't have privs |
455 |
|
} catch (InterruptedException ie) { |
618 |
|
Thread.State s = thread.getState(); |
619 |
|
if (s == Thread.State.BLOCKED || |
620 |
|
s == Thread.State.WAITING || |
621 |
< |
s == Thread.State.TIMED_WAITING || |
619 |
< |
System.nanoTime() - t0 > timeoutNanos) |
621 |
> |
s == Thread.State.TIMED_WAITING) |
622 |
|
return; |
623 |
+ |
else if (s == Thread.State.TERMINATED) |
624 |
+ |
fail("Unexpected thread termination"); |
625 |
+ |
else if (System.nanoTime() - t0 > timeoutNanos) { |
626 |
+ |
threadAssertTrue(thread.isAlive()); |
627 |
+ |
return; |
628 |
+ |
} |
629 |
|
Thread.yield(); |
630 |
|
} |
631 |
|
} |
632 |
|
|
633 |
|
/** |
634 |
+ |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
635 |
+ |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
636 |
+ |
*/ |
637 |
+ |
void waitForThreadToEnterWaitState(Thread thread) { |
638 |
+ |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
639 |
+ |
} |
640 |
+ |
|
641 |
+ |
/** |
642 |
+ |
* Returns the number of milliseconds since time given by |
643 |
+ |
* startNanoTime, which must have been previously returned from a |
644 |
+ |
* call to {@link System.nanoTime()}. |
645 |
+ |
*/ |
646 |
+ |
long millisElapsedSince(long startNanoTime) { |
647 |
+ |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
648 |
+ |
} |
649 |
+ |
|
650 |
+ |
/** |
651 |
|
* Returns a new started daemon Thread running the given runnable. |
652 |
|
*/ |
653 |
|
Thread newStartedThread(Runnable runnable) { |
675 |
|
} |
676 |
|
} |
677 |
|
|
678 |
+ |
/** |
679 |
+ |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
680 |
+ |
* terminate (using {@link Thread#join(long)}), else interrupts |
681 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
682 |
+ |
*/ |
683 |
+ |
void awaitTermination(Thread t) { |
684 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
685 |
+ |
} |
686 |
+ |
|
687 |
|
// Some convenient Runnable classes |
688 |
|
|
689 |
|
public abstract class CheckedRunnable implements Runnable { |
806 |
|
}}; |
807 |
|
} |
808 |
|
|
809 |
+ |
public Runnable awaiter(final CountDownLatch latch) { |
810 |
+ |
return new CheckedRunnable() { |
811 |
+ |
public void realRun() throws InterruptedException { |
812 |
+ |
latch.await(); |
813 |
+ |
}}; |
814 |
+ |
} |
815 |
+ |
|
816 |
|
public static class NPETask implements Callable<String> { |
817 |
|
public String call() { throw new NullPointerException(); } |
818 |
|
} |
1033 |
|
} |
1034 |
|
} |
1035 |
|
|
1036 |
+ |
public void checkEmpty(BlockingQueue q) { |
1037 |
+ |
try { |
1038 |
+ |
assertTrue(q.isEmpty()); |
1039 |
+ |
assertEquals(0, q.size()); |
1040 |
+ |
assertNull(q.peek()); |
1041 |
+ |
assertNull(q.poll()); |
1042 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
1043 |
+ |
assertEquals(q.toString(), "[]"); |
1044 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1045 |
+ |
assertFalse(q.iterator().hasNext()); |
1046 |
+ |
try { |
1047 |
+ |
q.element(); |
1048 |
+ |
shouldThrow(); |
1049 |
+ |
} catch (NoSuchElementException success) {} |
1050 |
+ |
try { |
1051 |
+ |
q.iterator().next(); |
1052 |
+ |
shouldThrow(); |
1053 |
+ |
} catch (NoSuchElementException success) {} |
1054 |
+ |
try { |
1055 |
+ |
q.remove(); |
1056 |
+ |
shouldThrow(); |
1057 |
+ |
} catch (NoSuchElementException success) {} |
1058 |
+ |
} catch (InterruptedException ie) { |
1059 |
+ |
threadUnexpectedException(ie); |
1060 |
+ |
} |
1061 |
+ |
} |
1062 |
+ |
|
1063 |
|
} |