16 |
|
|
17 |
|
public class ExecutorsTest 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(ExecutorsTest.class); |
181 |
|
* a newSingleThreadScheduledExecutor successfully runs delayed task |
182 |
|
*/ |
183 |
|
public void testNewSingleThreadScheduledExecutor() throws Exception { |
184 |
< |
TrackedCallable callable = new TrackedCallable(); |
185 |
< |
ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor(); |
186 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
187 |
< |
assertFalse(callable.done); |
188 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
189 |
< |
assertTrue(callable.done); |
190 |
< |
assertEquals(Boolean.TRUE, f.get()); |
191 |
< |
joinPool(p1); |
184 |
> |
ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor(); |
185 |
> |
try { |
186 |
> |
final CountDownLatch done = new CountDownLatch(1); |
187 |
> |
final Runnable task = new CheckedRunnable() { |
188 |
> |
public void realRun() { |
189 |
> |
done.countDown(); |
190 |
> |
}}; |
191 |
> |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
192 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
193 |
> |
assertFalse(f.isDone()); |
194 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
195 |
> |
assertTrue(f.isDone()); |
196 |
> |
assertEquals(Boolean.TRUE, f.get()); |
197 |
> |
} finally { |
198 |
> |
joinPool(p); |
199 |
> |
} |
200 |
|
} |
201 |
|
|
202 |
|
/** |
203 |
|
* a newScheduledThreadPool successfully runs delayed task |
204 |
|
*/ |
205 |
|
public void testnewScheduledThreadPool() throws Exception { |
206 |
< |
TrackedCallable callable = new TrackedCallable(); |
207 |
< |
ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2); |
208 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
209 |
< |
assertFalse(callable.done); |
210 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
211 |
< |
assertTrue(callable.done); |
212 |
< |
assertEquals(Boolean.TRUE, f.get()); |
213 |
< |
joinPool(p1); |
206 |
> |
ScheduledExecutorService p = Executors.newScheduledThreadPool(2); |
207 |
> |
try { |
208 |
> |
final CountDownLatch done = new CountDownLatch(1); |
209 |
> |
final Runnable task = new CheckedRunnable() { |
210 |
> |
public void realRun() { |
211 |
> |
done.countDown(); |
212 |
> |
}}; |
213 |
> |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
214 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
215 |
> |
assertFalse(f.isDone()); |
216 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
217 |
> |
assertTrue(f.isDone()); |
218 |
> |
assertEquals(Boolean.TRUE, f.get()); |
219 |
> |
} finally { |
220 |
> |
joinPool(p); |
221 |
> |
} |
222 |
|
} |
223 |
|
|
224 |
|
/** |
225 |
|
* an unconfigurable newScheduledThreadPool successfully runs delayed task |
226 |
|
*/ |
227 |
|
public void testunconfigurableScheduledExecutorService() throws Exception { |
228 |
< |
TrackedCallable callable = new TrackedCallable(); |
229 |
< |
ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2)); |
230 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
231 |
< |
assertFalse(callable.done); |
232 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
233 |
< |
assertTrue(callable.done); |
234 |
< |
assertEquals(Boolean.TRUE, f.get()); |
235 |
< |
joinPool(p1); |
228 |
> |
ScheduledExecutorService p = |
229 |
> |
Executors.unconfigurableScheduledExecutorService |
230 |
> |
(Executors.newScheduledThreadPool(2)); |
231 |
> |
try { |
232 |
> |
final CountDownLatch done = new CountDownLatch(1); |
233 |
> |
final Runnable task = new CheckedRunnable() { |
234 |
> |
public void realRun() { |
235 |
> |
done.countDown(); |
236 |
> |
}}; |
237 |
> |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
238 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
239 |
> |
assertFalse(f.isDone()); |
240 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
241 |
> |
assertTrue(f.isDone()); |
242 |
> |
assertEquals(Boolean.TRUE, f.get()); |
243 |
> |
} finally { |
244 |
> |
joinPool(p); |
245 |
> |
} |
246 |
|
} |
247 |
|
|
248 |
|
/** |
249 |
< |
* Future.get on submitted tasks will time out if they compute too long. |
249 |
> |
* Future.get on submitted tasks will time out if they compute too long. |
250 |
|
*/ |
251 |
|
public void testTimedCallable() throws Exception { |
252 |
< |
final Runnable sleeper = |
253 |
< |
new RunnableShouldThrow(InterruptedException.class) { |
254 |
< |
public void realRun() throws InterruptedException { |
255 |
< |
Thread.sleep(LONG_DELAY_MS); |
230 |
< |
}}; |
252 |
> |
final Runnable sleeper = new CheckedInterruptedRunnable() { |
253 |
> |
public void realRun() throws InterruptedException { |
254 |
> |
Thread.sleep(LONG_DELAY_MS); |
255 |
> |
}}; |
256 |
|
for (ExecutorService executor : |
257 |
|
new ExecutorService[] { |
258 |
|
Executors.newSingleThreadExecutor(), |
283 |
|
*/ |
284 |
|
public void testDefaultThreadFactory() throws Exception { |
285 |
|
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
286 |
< |
Runnable r = new Runnable() { |
287 |
< |
public void run() { |
288 |
< |
try { |
289 |
< |
Thread current = Thread.currentThread(); |
290 |
< |
threadAssertTrue(!current.isDaemon()); |
291 |
< |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
292 |
< |
ThreadGroup g = current.getThreadGroup(); |
293 |
< |
SecurityManager s = System.getSecurityManager(); |
294 |
< |
if (s != null) |
295 |
< |
threadAssertTrue(g == s.getThreadGroup()); |
296 |
< |
else |
297 |
< |
threadAssertTrue(g == egroup); |
298 |
< |
String name = current.getName(); |
299 |
< |
threadAssertTrue(name.endsWith("thread-1")); |
300 |
< |
} catch (SecurityException ok) { |
301 |
< |
// Also pass if not allowed to change setting |
277 |
< |
} |
286 |
> |
Runnable r = new CheckedRunnable() { |
287 |
> |
public void realRun() { |
288 |
> |
try { |
289 |
> |
Thread current = Thread.currentThread(); |
290 |
> |
assertTrue(!current.isDaemon()); |
291 |
> |
assertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
292 |
> |
ThreadGroup g = current.getThreadGroup(); |
293 |
> |
SecurityManager s = System.getSecurityManager(); |
294 |
> |
if (s != null) |
295 |
> |
assertTrue(g == s.getThreadGroup()); |
296 |
> |
else |
297 |
> |
assertTrue(g == egroup); |
298 |
> |
String name = current.getName(); |
299 |
> |
assertTrue(name.endsWith("thread-1")); |
300 |
> |
} catch (SecurityException ok) { |
301 |
> |
// Also pass if not allowed to change setting |
302 |
|
} |
303 |
< |
}; |
303 |
> |
}}; |
304 |
|
ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); |
305 |
|
|
306 |
|
e.execute(r); |
340 |
|
assertTrue(g == egroup); |
341 |
|
String name = current.getName(); |
342 |
|
assertTrue(name.endsWith("thread-1")); |
343 |
< |
assertTrue(thisccl == current.getContextClassLoader()); |
344 |
< |
assertTrue(thisacc.equals(AccessController.getContext())); |
343 |
> |
assertSame(thisccl, current.getContextClassLoader()); |
344 |
> |
assertEquals(thisacc, AccessController.getContext()); |
345 |
|
}}; |
346 |
|
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
347 |
|
e.execute(r); |
424 |
|
* Without permissions, calling privilegedCallable throws ACE |
425 |
|
*/ |
426 |
|
public void testprivilegedCallableWithNoPrivs() throws Exception { |
427 |
+ |
// Avoid classloader-related SecurityExceptions in swingui.TestRunner |
428 |
+ |
Executors.privilegedCallable(new CheckCCL()); |
429 |
+ |
|
430 |
|
Runnable r = new CheckedRunnable() { |
431 |
|
public void realRun() throws Exception { |
432 |
|
if (System.getSecurityManager() == null) |