43 |
|
*/ |
44 |
|
public void testSchedule1() throws Exception { |
45 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
46 |
< |
final long t0 = System.nanoTime(); |
47 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
46 |
> |
final long startTime = System.nanoTime(); |
47 |
|
final CountDownLatch done = new CountDownLatch(1); |
48 |
|
try { |
49 |
|
Callable task = new CheckedCallable<Boolean>() { |
50 |
|
public Boolean realCall() { |
51 |
|
done.countDown(); |
52 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
52 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
53 |
|
return Boolean.TRUE; |
54 |
|
}}; |
55 |
< |
Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS); |
56 |
< |
assertEquals(Boolean.TRUE, f.get()); |
57 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
55 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
56 |
> |
assertSame(Boolean.TRUE, f.get()); |
57 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
58 |
|
assertTrue(done.await(0L, MILLISECONDS)); |
59 |
|
} finally { |
60 |
|
joinPool(p); |
66 |
|
*/ |
67 |
|
public void testSchedule3() throws Exception { |
68 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
69 |
< |
final long t0 = System.nanoTime(); |
71 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
69 |
> |
final long startTime = System.nanoTime(); |
70 |
|
final CountDownLatch done = new CountDownLatch(1); |
71 |
|
try { |
72 |
|
Runnable task = new CheckedRunnable() { |
73 |
|
public void realRun() { |
74 |
|
done.countDown(); |
75 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
75 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
76 |
|
}}; |
77 |
< |
Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS); |
78 |
< |
assertNull(f.get()); |
79 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
80 |
< |
assertTrue(done.await(0L, MILLISECONDS)); |
77 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
78 |
> |
await(done); |
79 |
> |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
80 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
81 |
|
} finally { |
82 |
|
joinPool(p); |
83 |
|
} |
88 |
|
*/ |
89 |
|
public void testSchedule4() throws Exception { |
90 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
91 |
< |
final long t0 = System.nanoTime(); |
94 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
91 |
> |
final long startTime = System.nanoTime(); |
92 |
|
final CountDownLatch done = new CountDownLatch(1); |
93 |
|
try { |
94 |
|
Runnable task = new CheckedRunnable() { |
95 |
|
public void realRun() { |
96 |
|
done.countDown(); |
97 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
97 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
98 |
|
}}; |
99 |
|
ScheduledFuture f = |
100 |
< |
p.scheduleAtFixedRate(task, SHORT_DELAY_MS, |
101 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
102 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
103 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
100 |
> |
p.scheduleAtFixedRate(task, timeoutMillis(), |
101 |
> |
LONG_DELAY_MS, MILLISECONDS); |
102 |
> |
await(done); |
103 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
104 |
|
f.cancel(true); |
105 |
|
} finally { |
106 |
|
joinPool(p); |
110 |
|
/** |
111 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
112 |
|
*/ |
113 |
< |
public void testSchedule5() throws InterruptedException { |
113 |
> |
public void testSchedule5() throws Exception { |
114 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
115 |
< |
final long t0 = System.nanoTime(); |
119 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
115 |
> |
final long startTime = System.nanoTime(); |
116 |
|
final CountDownLatch done = new CountDownLatch(1); |
117 |
|
try { |
118 |
|
Runnable task = new CheckedRunnable() { |
119 |
|
public void realRun() { |
120 |
|
done.countDown(); |
121 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
121 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
122 |
|
}}; |
123 |
|
ScheduledFuture f = |
124 |
< |
p.scheduleWithFixedDelay(task, SHORT_DELAY_MS, |
125 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
126 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
127 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
124 |
> |
p.scheduleWithFixedDelay(task, timeoutMillis(), |
125 |
> |
LONG_DELAY_MS, MILLISECONDS); |
126 |
> |
await(done); |
127 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
128 |
|
f.cancel(true); |
129 |
|
} finally { |
130 |
|
joinPool(p); |
324 |
|
assertEquals(0, p.getCompletedTaskCount()); |
325 |
|
threadProceed.countDown(); |
326 |
|
threadDone.await(); |
327 |
< |
delay(SHORT_DELAY_MS); |
328 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
327 |
> |
long startTime = System.nanoTime(); |
328 |
> |
while (p.getCompletedTaskCount() != 1) { |
329 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
330 |
> |
fail("timed out"); |
331 |
> |
Thread.yield(); |
332 |
> |
} |
333 |
|
} finally { |
334 |
|
joinPool(p); |
335 |
|
} |