143 |
|
/** |
144 |
|
* An unconfigurable newFixedThreadPool can execute runnables |
145 |
|
*/ |
146 |
< |
public void testunconfigurableExecutorService() { |
146 |
> |
public void testUnconfigurableExecutorService() { |
147 |
|
ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2)); |
148 |
|
e.execute(new NoOpRunnable()); |
149 |
|
e.execute(new NoOpRunnable()); |
154 |
|
/** |
155 |
|
* unconfigurableExecutorService(null) throws NPE |
156 |
|
*/ |
157 |
< |
public void testunconfigurableExecutorServiceNPE() { |
157 |
> |
public void testUnconfigurableExecutorServiceNPE() { |
158 |
|
try { |
159 |
|
ExecutorService e = Executors.unconfigurableExecutorService(null); |
160 |
|
shouldThrow(); |
164 |
|
/** |
165 |
|
* unconfigurableScheduledExecutorService(null) throws NPE |
166 |
|
*/ |
167 |
< |
public void testunconfigurableScheduledExecutorServiceNPE() { |
167 |
> |
public void testUnconfigurableScheduledExecutorServiceNPE() { |
168 |
|
try { |
169 |
|
ExecutorService e = Executors.unconfigurableScheduledExecutorService(null); |
170 |
|
shouldThrow(); |
177 |
|
public void testNewSingleThreadScheduledExecutor() throws Exception { |
178 |
|
ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor(); |
179 |
|
try { |
180 |
< |
final CountDownLatch done = new CountDownLatch(1); |
180 |
> |
final CountDownLatch proceed = new CountDownLatch(1); |
181 |
|
final Runnable task = new CheckedRunnable() { |
182 |
|
public void realRun() { |
183 |
< |
done.countDown(); |
183 |
> |
await(proceed); |
184 |
|
}}; |
185 |
+ |
long startTime = System.nanoTime(); |
186 |
|
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
187 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
187 |
> |
timeoutMillis(), MILLISECONDS); |
188 |
|
assertFalse(f.isDone()); |
189 |
< |
assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
190 |
< |
assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS)); |
189 |
> |
proceed.countDown(); |
190 |
> |
assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS)); |
191 |
|
assertSame(Boolean.TRUE, f.get()); |
192 |
|
assertTrue(f.isDone()); |
193 |
+ |
assertFalse(f.isCancelled()); |
194 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
195 |
|
} finally { |
196 |
|
joinPool(p); |
197 |
|
} |
200 |
|
/** |
201 |
|
* a newScheduledThreadPool successfully runs delayed task |
202 |
|
*/ |
203 |
< |
public void testnewScheduledThreadPool() throws Exception { |
203 |
> |
public void testNewScheduledThreadPool() throws Exception { |
204 |
|
ScheduledExecutorService p = Executors.newScheduledThreadPool(2); |
205 |
|
try { |
206 |
< |
final CountDownLatch done = new CountDownLatch(1); |
206 |
> |
final CountDownLatch proceed = new CountDownLatch(1); |
207 |
|
final Runnable task = new CheckedRunnable() { |
208 |
|
public void realRun() { |
209 |
< |
done.countDown(); |
209 |
> |
await(proceed); |
210 |
|
}}; |
211 |
+ |
long startTime = System.nanoTime(); |
212 |
|
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
213 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
213 |
> |
timeoutMillis(), MILLISECONDS); |
214 |
|
assertFalse(f.isDone()); |
215 |
< |
assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
216 |
< |
assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS)); |
215 |
> |
proceed.countDown(); |
216 |
> |
assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS)); |
217 |
|
assertSame(Boolean.TRUE, f.get()); |
218 |
|
assertTrue(f.isDone()); |
219 |
+ |
assertFalse(f.isCancelled()); |
220 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
221 |
|
} finally { |
222 |
|
joinPool(p); |
223 |
|
} |
226 |
|
/** |
227 |
|
* an unconfigurable newScheduledThreadPool successfully runs delayed task |
228 |
|
*/ |
229 |
< |
public void testunconfigurableScheduledExecutorService() throws Exception { |
229 |
> |
public void testUnconfigurableScheduledExecutorService() throws Exception { |
230 |
|
ScheduledExecutorService p = |
231 |
|
Executors.unconfigurableScheduledExecutorService |
232 |
|
(Executors.newScheduledThreadPool(2)); |
233 |
|
try { |
234 |
< |
final CountDownLatch done = new CountDownLatch(1); |
234 |
> |
final CountDownLatch proceed = new CountDownLatch(1); |
235 |
|
final Runnable task = new CheckedRunnable() { |
236 |
|
public void realRun() { |
237 |
< |
done.countDown(); |
237 |
> |
await(proceed); |
238 |
|
}}; |
239 |
+ |
long startTime = System.nanoTime(); |
240 |
|
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
241 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
241 |
> |
timeoutMillis(), MILLISECONDS); |
242 |
|
assertFalse(f.isDone()); |
243 |
< |
assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
244 |
< |
assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS)); |
243 |
> |
proceed.countDown(); |
244 |
> |
assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS)); |
245 |
|
assertSame(Boolean.TRUE, f.get()); |
246 |
|
assertTrue(f.isDone()); |
247 |
+ |
assertFalse(f.isCancelled()); |
248 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
249 |
|
} finally { |
250 |
|
joinPool(p); |
251 |
|
} |
288 |
|
*/ |
289 |
|
public void testDefaultThreadFactory() throws Exception { |
290 |
|
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
291 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
292 |
|
Runnable r = new CheckedRunnable() { |
293 |
|
public void realRun() { |
294 |
|
try { |
306 |
|
} catch (SecurityException ok) { |
307 |
|
// Also pass if not allowed to change setting |
308 |
|
} |
309 |
+ |
done.countDown(); |
310 |
|
}}; |
311 |
|
ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); |
312 |
|
|
313 |
|
e.execute(r); |
314 |
+ |
await(done); |
315 |
+ |
|
316 |
|
try { |
317 |
|
e.shutdown(); |
318 |
|
} catch (SecurityException ok) { |
319 |
|
} |
320 |
|
|
321 |
< |
try { |
309 |
< |
delay(SHORT_DELAY_MS); |
310 |
< |
} finally { |
311 |
< |
joinPool(e); |
312 |
< |
} |
321 |
> |
joinPool(e); |
322 |
|
} |
323 |
|
|
324 |
|
/** |
327 |
|
* access control context and context class loader |
328 |
|
*/ |
329 |
|
public void testPrivilegedThreadFactory() throws Exception { |
330 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
331 |
|
Runnable r = new CheckedRunnable() { |
332 |
|
public void realRun() throws Exception { |
333 |
|
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
348 |
|
assertTrue(name.endsWith("thread-1")); |
349 |
|
assertSame(thisccl, current.getContextClassLoader()); |
350 |
|
assertEquals(thisacc, AccessController.getContext()); |
351 |
+ |
done.countDown(); |
352 |
|
}}; |
353 |
|
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
354 |
|
e.execute(r); |
355 |
+ |
await(done); |
356 |
|
e.shutdown(); |
345 |
– |
delay(SHORT_DELAY_MS); |
357 |
|
joinPool(e); |
358 |
|
}}; |
359 |
|
|
413 |
|
* With class loader permissions, calling |
414 |
|
* privilegedCallableUsingCurrentClassLoader does not throw ACE |
415 |
|
*/ |
416 |
< |
public void testprivilegedCallableUsingCCLWithPrivs() throws Exception { |
416 |
> |
public void testPrivilegedCallableUsingCCLWithPrivs() throws Exception { |
417 |
|
Runnable r = new CheckedRunnable() { |
418 |
|
public void realRun() throws Exception { |
419 |
|
Executors.privilegedCallableUsingCurrentClassLoader |
429 |
|
/** |
430 |
|
* Without permissions, calling privilegedCallable throws ACE |
431 |
|
*/ |
432 |
< |
public void testprivilegedCallableWithNoPrivs() throws Exception { |
432 |
> |
public void testPrivilegedCallableWithNoPrivs() throws Exception { |
433 |
|
// Avoid classloader-related SecurityExceptions in swingui.TestRunner |
434 |
|
Executors.privilegedCallable(new CheckCCL()); |
435 |
|
|
501 |
|
/** |
502 |
|
* With permissions, calling privilegedCallable succeeds |
503 |
|
*/ |
504 |
< |
public void testprivilegedCallableWithPrivs() throws Exception { |
504 |
> |
public void testPrivilegedCallableWithPrivs() throws Exception { |
505 |
|
Runnable r = new CheckedRunnable() { |
506 |
|
public void realRun() throws Exception { |
507 |
|
Executors.privilegedCallable(new CheckCCL()).call(); |