11 |
|
import java.io.ByteArrayOutputStream; |
12 |
|
import java.io.ObjectInputStream; |
13 |
|
import java.io.ObjectOutputStream; |
14 |
+ |
import java.util.ArrayList; |
15 |
|
import java.util.Arrays; |
16 |
|
import java.util.Date; |
17 |
+ |
import java.util.Enumeration; |
18 |
+ |
import java.util.List; |
19 |
|
import java.util.NoSuchElementException; |
20 |
|
import java.util.PropertyPermission; |
21 |
|
import java.util.concurrent.*; |
72 |
|
* |
73 |
|
* </ol> |
74 |
|
* |
75 |
< |
* <p> <b>Other notes</b> |
75 |
> |
* <p><b>Other notes</b> |
76 |
|
* <ul> |
77 |
|
* |
78 |
|
* <li> Usually, there is one testcase method per JSR166 method |
263 |
|
return 50; |
264 |
|
} |
265 |
|
|
263 |
– |
|
266 |
|
/** |
267 |
|
* Sets delays as multiples of SHORT_DELAY. |
268 |
|
*/ |
286 |
|
* milliseconds in the future. |
287 |
|
*/ |
288 |
|
Date delayedDate(long delayMillis) { |
289 |
< |
return new Date(new Date().getTime() + delayMillis); |
289 |
> |
return new Date(System.currentTimeMillis() + delayMillis); |
290 |
|
} |
291 |
|
|
292 |
|
/** |
310 |
|
} |
311 |
|
|
312 |
|
/** |
313 |
+ |
* Extra checks that get done for all test cases. |
314 |
+ |
* |
315 |
|
* Triggers test case failure if any thread assertions have failed, |
316 |
|
* by rethrowing, in the test harness thread, any exception recorded |
317 |
|
* earlier by threadRecordFailure. |
318 |
+ |
* |
319 |
+ |
* Triggers test case failure if interrupt status is set in the main thread. |
320 |
|
*/ |
321 |
|
public void tearDown() throws Exception { |
322 |
|
Throwable t = threadFailure.getAndSet(null); |
334 |
|
throw afe; |
335 |
|
} |
336 |
|
} |
337 |
+ |
|
338 |
+ |
if (Thread.interrupted()) |
339 |
+ |
throw new AssertionFailedError("interrupt status set in main thread"); |
340 |
|
} |
341 |
|
|
342 |
|
/** |
531 |
|
} |
532 |
|
|
533 |
|
/** |
534 |
+ |
* Checks that the threads do not terminate within the default |
535 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
536 |
+ |
*/ |
537 |
+ |
void assertThreadsStayAlive(Thread... threads) { |
538 |
+ |
assertThreadsStayAlive(timeoutMillis(), threads); |
539 |
+ |
} |
540 |
+ |
|
541 |
+ |
/** |
542 |
+ |
* Checks that the threads do not terminate within the given millisecond delay. |
543 |
+ |
*/ |
544 |
+ |
void assertThreadsStayAlive(long millis, Thread... threads) { |
545 |
+ |
try { |
546 |
+ |
// No need to optimize the failing case via Thread.join. |
547 |
+ |
delay(millis); |
548 |
+ |
for (Thread thread : threads) |
549 |
+ |
assertTrue(thread.isAlive()); |
550 |
+ |
} catch (InterruptedException ie) { |
551 |
+ |
fail("Unexpected InterruptedException"); |
552 |
+ |
} |
553 |
+ |
} |
554 |
+ |
|
555 |
+ |
/** |
556 |
+ |
* Checks that future.get times out, with the default timeout of |
557 |
+ |
* {@code timeoutMillis()}. |
558 |
+ |
*/ |
559 |
+ |
void assertFutureTimesOut(Future future) { |
560 |
+ |
assertFutureTimesOut(future, timeoutMillis()); |
561 |
+ |
} |
562 |
+ |
|
563 |
+ |
/** |
564 |
+ |
* Checks that future.get times out, with the given millisecond timeout. |
565 |
+ |
*/ |
566 |
+ |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
567 |
+ |
long startTime = System.nanoTime(); |
568 |
+ |
try { |
569 |
+ |
future.get(timeoutMillis, MILLISECONDS); |
570 |
+ |
shouldThrow(); |
571 |
+ |
} catch (TimeoutException success) { |
572 |
+ |
} catch (Exception e) { |
573 |
+ |
threadUnexpectedException(e); |
574 |
+ |
} finally { future.cancel(true); } |
575 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
576 |
+ |
} |
577 |
+ |
|
578 |
+ |
/** |
579 |
|
* Fails with message "should throw exception". |
580 |
|
*/ |
581 |
|
public void shouldThrow() { |
626 |
|
SecurityManager sm = System.getSecurityManager(); |
627 |
|
if (sm == null) { |
628 |
|
r.run(); |
629 |
+ |
} |
630 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
631 |
+ |
} |
632 |
+ |
|
633 |
+ |
/** |
634 |
+ |
* Runs Runnable r with a security policy that permits precisely |
635 |
+ |
* the specified permissions. If there is no current security |
636 |
+ |
* manager, a temporary one is set for the duration of the |
637 |
+ |
* Runnable. We require that any security manager permit |
638 |
+ |
* getPolicy/setPolicy. |
639 |
+ |
*/ |
640 |
+ |
public void runWithSecurityManagerWithPermissions(Runnable r, |
641 |
+ |
Permission... permissions) { |
642 |
+ |
SecurityManager sm = System.getSecurityManager(); |
643 |
+ |
if (sm == null) { |
644 |
|
Policy savedPolicy = Policy.getPolicy(); |
645 |
|
try { |
646 |
|
Policy.setPolicy(permissivePolicy()); |
647 |
|
System.setSecurityManager(new SecurityManager()); |
648 |
< |
runWithPermissions(r, permissions); |
648 |
> |
runWithSecurityManagerWithPermissions(r, permissions); |
649 |
|
} finally { |
650 |
|
System.setSecurityManager(null); |
651 |
|
Policy.setPolicy(savedPolicy); |
693 |
|
return perms.implies(p); |
694 |
|
} |
695 |
|
public void refresh() {} |
696 |
+ |
public String toString() { |
697 |
+ |
List<Permission> ps = new ArrayList<Permission>(); |
698 |
+ |
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
699 |
+ |
ps.add(e.nextElement()); |
700 |
+ |
return "AdjustablePolicy with permissions " + ps; |
701 |
+ |
} |
702 |
|
} |
703 |
|
|
704 |
|
/** |
736 |
|
} |
737 |
|
|
738 |
|
/** |
739 |
< |
* Waits up to the specified number of milliseconds for the given |
739 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
740 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
741 |
|
*/ |
742 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
743 |
< |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
669 |
< |
long t0 = System.nanoTime(); |
743 |
> |
long startTime = System.nanoTime(); |
744 |
|
for (;;) { |
745 |
|
Thread.State s = thread.getState(); |
746 |
|
if (s == Thread.State.BLOCKED || |
749 |
|
return; |
750 |
|
else if (s == Thread.State.TERMINATED) |
751 |
|
fail("Unexpected thread termination"); |
752 |
< |
else if (System.nanoTime() - t0 > timeoutNanos) { |
752 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
753 |
|
threadAssertTrue(thread.isAlive()); |
754 |
|
return; |
755 |
|
} |
795 |
|
} catch (InterruptedException ie) { |
796 |
|
threadUnexpectedException(ie); |
797 |
|
} finally { |
798 |
< |
if (t.isAlive()) { |
798 |
> |
if (t.getState() != Thread.State.TERMINATED) { |
799 |
|
t.interrupt(); |
800 |
|
fail("Test timed out"); |
801 |
|
} |
950 |
|
} |
951 |
|
} |
952 |
|
|
953 |
+ |
public void await(Semaphore semaphore) { |
954 |
+ |
try { |
955 |
+ |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
956 |
+ |
} catch (Throwable t) { |
957 |
+ |
threadUnexpectedException(t); |
958 |
+ |
} |
959 |
+ |
} |
960 |
+ |
|
961 |
|
// /** |
962 |
|
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
963 |
|
// */ |
1179 |
|
} |
1180 |
|
|
1181 |
|
/** |
1182 |
< |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
1183 |
< |
* of throwing checked exceptions. |
1182 |
> |
* A CyclicBarrier that uses timed await and fails with |
1183 |
> |
* AssertionFailedErrors instead of throwing checked exceptions. |
1184 |
|
*/ |
1185 |
|
public class CheckedBarrier extends CyclicBarrier { |
1186 |
|
public CheckedBarrier(int parties) { super(parties); } |
1187 |
|
|
1188 |
|
public int await() { |
1189 |
|
try { |
1190 |
< |
return super.await(); |
1190 |
> |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1191 |
> |
} catch (TimeoutException e) { |
1192 |
> |
throw new AssertionFailedError("timed out"); |
1193 |
|
} catch (Exception e) { |
1194 |
|
AssertionFailedError afe = |
1195 |
|
new AssertionFailedError("Unexpected exception: " + e); |
1226 |
|
} |
1227 |
|
} |
1228 |
|
|
1229 |
< |
@SuppressWarnings("unchecked") |
1230 |
< |
<T> T serialClone(T o) { |
1229 |
> |
void assertSerialEquals(Object x, Object y) { |
1230 |
> |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1231 |
> |
} |
1232 |
> |
|
1233 |
> |
void assertNotSerialEquals(Object x, Object y) { |
1234 |
> |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1235 |
> |
} |
1236 |
> |
|
1237 |
> |
byte[] serialBytes(Object o) { |
1238 |
|
try { |
1239 |
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1240 |
|
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1241 |
|
oos.writeObject(o); |
1242 |
|
oos.flush(); |
1243 |
|
oos.close(); |
1244 |
< |
ByteArrayInputStream bin = |
1245 |
< |
new ByteArrayInputStream(bos.toByteArray()); |
1246 |
< |
ObjectInputStream ois = new ObjectInputStream(bin); |
1247 |
< |
return (T) ois.readObject(); |
1244 |
> |
return bos.toByteArray(); |
1245 |
> |
} catch (Throwable t) { |
1246 |
> |
threadUnexpectedException(t); |
1247 |
> |
return new byte[0]; |
1248 |
> |
} |
1249 |
> |
} |
1250 |
> |
|
1251 |
> |
@SuppressWarnings("unchecked") |
1252 |
> |
<T> T serialClone(T o) { |
1253 |
> |
try { |
1254 |
> |
ObjectInputStream ois = new ObjectInputStream |
1255 |
> |
(new ByteArrayInputStream(serialBytes(o))); |
1256 |
> |
T clone = (T) ois.readObject(); |
1257 |
> |
assertSame(o.getClass(), clone.getClass()); |
1258 |
> |
return clone; |
1259 |
|
} catch (Throwable t) { |
1260 |
|
threadUnexpectedException(t); |
1261 |
|
return null; |