ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExecutorsTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ExecutorsTest.java (file contents):
Revision 1.6 by dl, Fri Sep 26 15:33:13 2003 UTC vs.
Revision 1.10 by tim, Wed Dec 10 01:51:12 2003 UTC

# Line 10 | Line 10 | import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12   import java.math.BigInteger;
13 + import java.security.*;
14  
15   public class ExecutorsTest extends JSR166TestCase{
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());  
17 >        junit.textui.TestRunner.run (suite());  
18      }
19      public static Test suite() {
20 <        return new TestSuite(ExecutorsTest.class);
20 <    }
21 <
22 <    private static final String TEST_STRING = "a test string";
23 <
24 <    private static class StringTask implements Callable<String> {
25 <        public String call() { return TEST_STRING; }
26 <    }
27 <
28 <    static class DirectExecutor implements Executor {
29 <        public void execute(Runnable r) {
30 <            r.run();
31 <        }
20 >        return new TestSuite(ExecutorsTest.class);
21      }
22  
23      static class TimedCallable<T> implements Callable<T> {
24 <        private final Executor exec;
24 >        private final ExecutorService exec;
25          private final Callable<T> func;
26          private final long msecs;
27          
28 <        TimedCallable(Executor exec, Callable<T> func, long msecs) {
28 >        TimedCallable(ExecutorService exec, Callable<T> func, long msecs) {
29              this.exec = exec;
30              this.func = func;
31              this.msecs = msecs;
32          }
33          
34          public T call() throws Exception {
35 <            Future<T> ftask = Executors.execute(exec, func);
35 >            Future<T> ftask = exec.submit(func);
36              try {
37                  return ftask.get(msecs, TimeUnit.MILLISECONDS);
38              } finally {
# Line 186 | Line 175 | public class ExecutorsTest extends JSR16
175          }
176      }
177  
189    /**
190     * execute of runnable runs it to completion
191     */
192    public void testExecuteRunnable () {
193        try {
194            Executor e = new DirectExecutor();
195            TrackedRunnable task = new TrackedRunnable();
196            assertFalse(task.done);
197            Future<String> future = Executors.execute(e, task, TEST_STRING);
198            String result = future.get();
199            assertTrue(task.done);
200            assertSame(TEST_STRING, result);
201        }
202        catch (ExecutionException ex) {
203            unexpectedException();
204        }
205        catch (InterruptedException ex) {
206            unexpectedException();
207        }
208    }
209
210    /**
211     * invoke of a runnable runs it to completion
212     */
213    public void testInvokeRunnable () {
214        try {
215            Executor e = new DirectExecutor();
216            TrackedRunnable task = new TrackedRunnable();
217            assertFalse(task.done);
218            Executors.invoke(e, task);
219            assertTrue(task.done);
220        }
221        catch (ExecutionException ex) {
222            unexpectedException();
223        }
224        catch (InterruptedException ex) {
225            unexpectedException();
226        }
227    }
228
229    /**
230     * execute of a callable runs it to completion
231     */
232    public void testExecuteCallable () {
233        try {
234            Executor e = new DirectExecutor();
235            Future<String> future = Executors.execute(e, new StringTask());
236            String result = future.get();
237            assertSame(TEST_STRING, result);
238        }
239        catch (ExecutionException ex) {
240            unexpectedException();
241        }
242        catch (InterruptedException ex) {
243            unexpectedException();
244        }
245    }
246
247    /**
248     * invoke of a collable runs it to completion
249     */
250    public void testInvokeCallable () {
251        try {
252            Executor e = new DirectExecutor();
253            String result = Executors.invoke(e, new StringTask());
254
255            assertSame(TEST_STRING, result);
256        }
257        catch (ExecutionException ex) {
258            unexpectedException();
259        }
260        catch (InterruptedException ex) {
261            unexpectedException();
262        }
263    }
264
265    /**
266     * execute with null executor throws NPE
267     */
268    public void testNullExecuteRunnable () {
269        try {
270            TrackedRunnable task = new TrackedRunnable();
271            assertFalse(task.done);
272            Future<String> future = Executors.execute(null, task, TEST_STRING);
273            shouldThrow();
274        }
275        catch (NullPointerException success) {
276        }
277        catch (Exception ex) {
278            unexpectedException();
279        }
280    }
281
282    /**
283     * execute with a null runnable throws NPE
284     */
285    public void testExecuteNullRunnable() {
286        try {
287            Executor e = new DirectExecutor();
288            TrackedRunnable task = null;
289            Future<String> future = Executors.execute(e, task, TEST_STRING);
290            shouldThrow();
291        }
292        catch (NullPointerException success) {
293        }
294        catch (Exception ex) {
295            unexpectedException();
296        }
297    }
298
299    /**
300     * invoke of a null runnable throws NPE
301     */
302    public void testInvokeNullRunnable () {
303        try {
304            Executor e = new DirectExecutor();
305            TrackedRunnable task = null;
306            Executors.invoke(e, task);
307            shouldThrow();
308        }
309        catch (NullPointerException success) {
310        }
311        catch (Exception ex) {
312            unexpectedException();
313        }
314    }
315
316    /**
317     * execute of a null callable throws NPE
318     */
319    public void testExecuteNullCallable () {
320        try {
321            Executor e = new DirectExecutor();
322            StringTask t = null;
323            Future<String> future = Executors.execute(e, t);
324            shouldThrow();
325        }
326        catch (NullPointerException success) {
327        }
328        catch (Exception ex) {
329            unexpectedException();
330        }
331    }
332
333    /**
334     * invoke of a null callable throws NPE
335     */
336    public void testInvokeNullCallable () {
337        try {
338            Executor e = new DirectExecutor();
339            StringTask t = null;
340            String result = Executors.invoke(e, t);
341            shouldThrow();
342        }
343        catch (NullPointerException success) {
344        }
345        catch (Exception ex) {
346            unexpectedException();
347        }
348    }
349
350    /**
351     *  execute(Executor, Runnable) throws RejectedExecutionException
352     *  if saturated.
353     */
354    public void testExecute1() {
355        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
356        try {
357            
358            for(int i = 0; i < 5; ++i){
359                Executors.execute(p, new MediumRunnable(), Boolean.TRUE);
360            }
361            shouldThrow();
362        } catch(RejectedExecutionException success){}
363        joinPool(p);
364    }
365
366    /**
367     *  execute(Executor, Callable)throws RejectedExecutionException
368     *  if saturated.
369     */
370    public void testExecute2() {
371         ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
372        try {
373            for(int i = 0; i < 5; ++i) {
374                Executors.execute(p, new SmallCallable());
375            }
376            shouldThrow();
377        } catch(RejectedExecutionException e){}
378        joinPool(p);
379    }
380
381
382    /**
383     *  invoke(Executor, Runnable) throws InterruptedException if
384     *  caller interrupted.
385     */
386    public void testInterruptedInvoke() {
387        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
388        Thread t = new Thread(new Runnable() {
389                public void run() {
390                    try {
391                        Executors.invoke(p,new Runnable() {
392                                public void run() {
393                                    try {
394                                        Thread.sleep(MEDIUM_DELAY_MS);
395                                        shouldThrow();
396                                    } catch(InterruptedException e){
397                                    }
398                                }
399                            });
400                    } catch(InterruptedException success){
401                    } catch(Exception e) {
402                        unexpectedException();
403                    }
404                    
405                }
406            });
407        try {
408            t.start();
409            Thread.sleep(SHORT_DELAY_MS);
410            t.interrupt();
411        } catch(Exception e){
412            unexpectedException();
413        }
414        joinPool(p);
415    }
416
417    /**
418     *  invoke(Executor, Runnable) throws ExecutionException if
419     *  runnable throws exception.
420     */
421    public void testInvoke3() {
422        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
423        try {
424            Runnable r = new Runnable() {
425                    public void run() {
426                        int i = 5/0;
427                    }
428                };
429            
430            for(int i =0; i < 5; i++){
431                Executors.invoke(p,r);
432            }
433            
434            shouldThrow();
435        } catch(ExecutionException success){
436        } catch(Exception e){
437            unexpectedException();
438        }
439        joinPool(p);
440    }
441
442
443
444    /**
445     *  invoke(Executor, Callable) throws InterruptedException if
446     *  callable throws exception
447     */
448    public void testInvoke5() {
449        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
450        
451        final Callable c = new Callable() {
452                public Object call() {
453                    try {
454                        Executors.invoke(p, new SmallCallable());
455                        shouldThrow();
456                    } catch(InterruptedException e){}
457                    catch(RejectedExecutionException e2){}
458                    catch(ExecutionException e3){}
459                    return Boolean.TRUE;
460                }
461            };
462
463
464        
465        Thread t = new Thread(new Runnable() {
466                public void run() {
467                    try {
468                        c.call();
469                    } catch(Exception e){}
470                }
471          });
472        try {
473            t.start();
474            Thread.sleep(SHORT_DELAY_MS);
475            t.interrupt();
476            t.join();
477        } catch(InterruptedException e){
478            unexpectedException();
479        }
480        
481        joinPool(p);
482    }
483
484    /**
485     *  invoke(Executor, Callable) will throw ExecutionException
486     *  if callable throws exception
487     */
488    public void testInvoke6() {
489        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
490
491        try {
492            Callable c = new Callable() {
493                    public Object call() {
494                        int i = 5/0;
495                        return Boolean.TRUE;
496                    }
497                };
498            
499            for(int i =0; i < 5; i++){
500                Executors.invoke(p,c);
501            }
502            
503            shouldThrow();
504        }
505        catch(ExecutionException success){
506        } catch(Exception e) {
507            unexpectedException();
508        }
509        joinPool(p);
510    }
511
512
178  
179      /**
180       *  timeouts from execute will time out if they compute too long.
# Line 552 | Line 217 | public class ExecutorsTest extends JSR16
217      }
218  
219      
220 +    /**
221 +     * ThreadPoolExecutor using defaultThreadFactory has
222 +     * specified group, priority, daemon status, and name
223 +     */
224 +    public void testDefaultThreadFactory() {
225 +        final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
226 +        Runnable r = new Runnable() {
227 +                public void run() {
228 +                    Thread current = Thread.currentThread();
229 +                    threadAssertTrue(!current.isDaemon());
230 +                    threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY);
231 +                    ThreadGroup g = current.getThreadGroup();
232 +                    SecurityManager s = System.getSecurityManager();
233 +                    if (s != null)
234 +                        threadAssertTrue(g == s.getThreadGroup());
235 +                    else
236 +                        threadAssertTrue(g == egroup);
237 +                    String name = current.getName();
238 +                    threadAssertTrue(name.endsWith("thread-1"));
239 +                }
240 +            };
241 +        ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
242 +        
243 +        e.execute(r);
244 +        e.shutdown();
245 +        try {
246 +            Thread.sleep(SHORT_DELAY_MS);
247 +        } catch (Exception eX) {
248 +            unexpectedException();
249 +        } finally {
250 +            joinPool(e);
251 +        }
252 +    }
253  
254 +    /**
255 +     * ThreadPoolExecutor using privilegedThreadFactory has
256 +     * specified group, priority, daemon status, name,
257 +     * access control context and context class loader
258 +     */
259 +    public void testPrivilegedThreadFactory() {
260 +        Policy savedPolicy = Policy.getPolicy();
261 +        AdjustablePolicy policy = new AdjustablePolicy();
262 +        policy.addPermission(new RuntimePermission("getContextClassLoader"));
263 +        policy.addPermission(new RuntimePermission("setContextClassLoader"));
264 +        Policy.setPolicy(policy);
265 +        final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
266 +        final ClassLoader thisccl = Thread.currentThread().getContextClassLoader();
267 +        final AccessControlContext thisacc = AccessController.getContext();
268 +        Runnable r = new Runnable() {
269 +                public void run() {
270 +                    Thread current = Thread.currentThread();
271 +                    threadAssertTrue(!current.isDaemon());
272 +                    threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY);
273 +                    ThreadGroup g = current.getThreadGroup();
274 +                    SecurityManager s = System.getSecurityManager();
275 +                    if (s != null)
276 +                        threadAssertTrue(g == s.getThreadGroup());
277 +                    else
278 +                        threadAssertTrue(g == egroup);
279 +                    String name = current.getName();
280 +                    threadAssertTrue(name.endsWith("thread-1"));
281 +                    threadAssertTrue(thisccl == current.getContextClassLoader());
282 +                    threadAssertTrue(thisacc.equals(AccessController.getContext()));
283 +                }
284 +            };
285 +        ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
286 +        
287 +        Policy.setPolicy(savedPolicy);
288 +        e.execute(r);
289 +        e.shutdown();
290 +        try {
291 +            Thread.sleep(SHORT_DELAY_MS);
292 +        } catch (Exception ex) {
293 +            unexpectedException();
294 +        } finally {
295 +            joinPool(e);
296 +        }
297 +
298 +    }
299  
300   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines