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.4 by dl, Sat Sep 20 18:20:07 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{
15  
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);
21 <    }
22 <
23 <    private static final String TEST_STRING = "a test string";
24 <
25 <    private static class MyTask implements Runnable {
26 <        public void run() { completed = true; }
27 <        public boolean isCompleted() { return completed; }
28 <        public void reset() { completed = false; }
29 <        private boolean completed = false;
30 <    }
31 <
32 <    private static class StringTask implements Callable<String> {
33 <        public String call() { return TEST_STRING; }
34 <    }
35 <
36 <    static class DirectExecutor implements Executor {
37 <        public void execute(Runnable r) {
38 <            r.run();
39 <        }
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 79 | Line 60 | public class ExecutorsTest extends JSR16
60          }
61      };
62  
82
83
63      /**
64 <     *   execute(Executor, Runnable) will throw
86 <     *  RejectedExecutionException Attempting to execute a runnable on
87 <     *  a full ThreadPool will cause such an exception here, up to 5
88 <     *  runnables are attempted on a pool capable on handling one
89 <     *  until it throws an exception
64 >     * A newCachedThreadPool can execute runnables
65       */
66 <    public void testExecute1() {
67 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
68 <        try {
69 <            
70 <            for(int i = 0; i < 5; ++i){
71 <                Executors.execute(p, new MediumRunnable(), Boolean.TRUE);
97 <            }
98 <            shouldThrow();
99 <        } catch(RejectedExecutionException success){}
100 <        joinPool(p);
66 >    public void testNewCachedThreadPool1() {
67 >        ExecutorService e = Executors.newCachedThreadPool();
68 >        e.execute(new NoOpRunnable());
69 >        e.execute(new NoOpRunnable());
70 >        e.execute(new NoOpRunnable());
71 >        e.shutdown();
72      }
73  
74      /**
75 <     *   execute(Executor, Callable) will throw
105 <     *  RejectedExecutionException Attempting to execute a callable on
106 <     *  a full ThreadPool will cause such an exception here, up to 5
107 <     *  runnables are attempted on a pool capable on handling one
108 <     *  until it throws an exception
75 >     * A newCachedThreadPool with given ThreadFactory can execute runnables
76       */
77 <    public void testExecute2() {
78 <         ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
79 <        try {
80 <            for(int i = 0; i < 5; ++i) {
81 <                Executors.execute(p, new SmallCallable());
82 <            }
116 <            shouldThrow();
117 <        } catch(RejectedExecutionException e){}
118 <        joinPool(p);
77 >    public void testNewCachedThreadPool2() {
78 >        ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory());
79 >        e.execute(new NoOpRunnable());
80 >        e.execute(new NoOpRunnable());
81 >        e.execute(new NoOpRunnable());
82 >        e.shutdown();
83      }
84  
121
85      /**
86 <     *   invoke(Executor, Runnable) throws InterruptedException
124 <     *  A single use of invoke starts that will wait long enough
125 <     *  for the invoking thread to be interrupted
86 >     * A newCachedThreadPool with null ThreadFactory throws NPE
87       */
88 <    public void testInvoke2() {
128 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
129 <        Thread t = new Thread(new Runnable() {
130 <                public void run() {
131 <                    try {
132 <                        Executors.invoke(p,new Runnable() {
133 <                                public void run() {
134 <                                    try {
135 <                                        Thread.sleep(MEDIUM_DELAY_MS);
136 <                                        shouldThrow();
137 <                                    } catch(InterruptedException e){
138 <                                    }
139 <                                }
140 <                            });
141 <                    } catch(InterruptedException success){
142 <                    } catch(Exception e) {
143 <                        unexpectedException();
144 <                    }
145 <                    
146 <                }
147 <            });
88 >    public void testNewCachedThreadPool3() {
89          try {
90 <            t.start();
91 <            Thread.sleep(SHORT_DELAY_MS);
92 <            t.interrupt();
93 <        } catch(Exception e){
153 <            unexpectedException();
90 >            ExecutorService e = Executors.newCachedThreadPool(null);
91 >            shouldThrow();
92 >        }
93 >        catch(NullPointerException success) {
94          }
155        joinPool(p);
95      }
96  
97 +
98      /**
99 <     *   invoke(Executor, Runnable) will throw
160 <     *  ExecutionException An ExecutionException occurs when the
161 <     *  underlying Runnable throws an exception, here the
162 <     *  DivideByZeroException will cause an ExecutionException
99 >     * A new SingleThreadExecutor can execute runnables
100       */
101 <    public void testInvoke3() {
102 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
103 <        try {
104 <            Runnable r = new Runnable() {
105 <                    public void run() {
106 <                        int i = 5/0;
170 <                    }
171 <                };
172 <            
173 <            for(int i =0; i < 5; i++){
174 <                Executors.invoke(p,r);
175 <            }
176 <            
177 <            shouldThrow();
178 <        } catch(ExecutionException success){
179 <        } catch(Exception e){
180 <            unexpectedException();
181 <        }
182 <        joinPool(p);
101 >    public void testNewSingleThreadExecutor1() {
102 >        ExecutorService e = Executors.newSingleThreadExecutor();
103 >        e.execute(new NoOpRunnable());
104 >        e.execute(new NoOpRunnable());
105 >        e.execute(new NoOpRunnable());
106 >        e.shutdown();
107      }
108  
185
186
109      /**
110 <     *   invoke(Executor, Callable) throws
189 <     *  InterruptedException A single use of invoke starts that will
190 <     *  wait long enough for the invoking thread to be interrupted
110 >     * A new SingleThreadExecutor with given ThreadFactory can execute runnables
111       */
112 <    public void testInvoke5() {
113 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
114 <        
115 <        final Callable c = new Callable() {
116 <                public Object call() {
117 <                    try {
198 <                        Executors.invoke(p, new SmallCallable());
199 <                        shouldThrow();
200 <                    } catch(InterruptedException e){}
201 <                    catch(RejectedExecutionException e2){}
202 <                    catch(ExecutionException e3){}
203 <                    return Boolean.TRUE;
204 <                }
205 <            };
206 <
207 <
208 <        
209 <        Thread t = new Thread(new Runnable() {
210 <                public void run() {
211 <                    try {
212 <                        c.call();
213 <                    } catch(Exception e){}
214 <                }
215 <          });
216 <        try {
217 <            t.start();
218 <            Thread.sleep(SHORT_DELAY_MS);
219 <            t.interrupt();
220 <            t.join();
221 <        } catch(InterruptedException e){
222 <            unexpectedException();
223 <        }
224 <        
225 <        joinPool(p);
112 >    public void testNewSingleThreadExecutor2() {
113 >        ExecutorService e = Executors.newSingleThreadExecutor(new SimpleThreadFactory());
114 >        e.execute(new NoOpRunnable());
115 >        e.execute(new NoOpRunnable());
116 >        e.execute(new NoOpRunnable());
117 >        e.shutdown();
118      }
119  
120      /**
121 <     *   invoke(Executor, Callable) will throw ExecutionException
230 <     *  An ExecutionException occurs when the underlying Runnable throws
231 <     *  an exception, here the DivideByZeroException will cause an ExecutionException
121 >     * A new SingleThreadExecutor with null ThreadFactory throws NPE
122       */
123 <    public void testInvoke6() {
234 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
235 <
123 >    public void testNewSingleThreadExecutor3() {
124          try {
125 <            Callable c = new Callable() {
238 <                    public Object call() {
239 <                        int i = 5/0;
240 <                        return Boolean.TRUE;
241 <                    }
242 <                };
243 <            
244 <            for(int i =0; i < 5; i++){
245 <                Executors.invoke(p,c);
246 <            }
247 <            
125 >            ExecutorService e = Executors.newSingleThreadExecutor(null);
126              shouldThrow();
127 <        } catch(RejectedExecutionException e){}
128 <        catch(InterruptedException e2){}
129 <        catch(ExecutionException e3){}
252 <        joinPool(p);
127 >        }
128 >        catch(NullPointerException success) {
129 >        }
130      }
131  
132      /**
133 <     *
133 >     * A new newFixedThreadPool can execute runnables
134       */
135 <    public void testExecuteRunnable () {
136 <        try {
137 <            Executor e = new DirectExecutor();
138 <            MyTask task = new MyTask();
139 <            assertFalse(task.isCompleted());
140 <            Future<String> future = Executors.execute(e, task, TEST_STRING);
264 <            String result = future.get();
265 <            assertTrue(task.isCompleted());
266 <            assertSame(TEST_STRING, result);
267 <        }
268 <        catch (ExecutionException ex) {
269 <            unexpectedException();
270 <        }
271 <        catch (InterruptedException ex) {
272 <            unexpectedException();
273 <        }
135 >    public void testNewFixedThreadPool1() {
136 >        ExecutorService e = Executors.newFixedThreadPool(2);
137 >        e.execute(new NoOpRunnable());
138 >        e.execute(new NoOpRunnable());
139 >        e.execute(new NoOpRunnable());
140 >        e.shutdown();
141      }
142  
143      /**
144 <     *
144 >     * A new newFixedThreadPool with given ThreadFactory can execute runnables
145       */
146 <    public void testInvokeRunnable () {
147 <        try {
148 <            Executor e = new DirectExecutor();
149 <            MyTask task = new MyTask();
150 <            assertFalse(task.isCompleted());
151 <            Executors.invoke(e, task);
285 <            assertTrue(task.isCompleted());
286 <        }
287 <        catch (ExecutionException ex) {
288 <            unexpectedException();
289 <        }
290 <        catch (InterruptedException ex) {
291 <            unexpectedException();
292 <        }
146 >    public void testNewFixedThreadPool2() {
147 >        ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory());
148 >        e.execute(new NoOpRunnable());
149 >        e.execute(new NoOpRunnable());
150 >        e.execute(new NoOpRunnable());
151 >        e.shutdown();
152      }
153  
154      /**
155 <     *
155 >     * A new newFixedThreadPool with null ThreadFactory throws NPE
156       */
157 <    public void testExecuteCallable () {
157 >    public void testNewFixedThreadPool3() {
158          try {
159 <            Executor e = new DirectExecutor();
160 <            Future<String> future = Executors.execute(e, new StringTask());
302 <            String result = future.get();
303 <            assertSame(TEST_STRING, result);
304 <        }
305 <        catch (ExecutionException ex) {
306 <            unexpectedException();
159 >            ExecutorService e = Executors.newFixedThreadPool(2, null);
160 >            shouldThrow();
161          }
162 <        catch (InterruptedException ex) {
309 <            unexpectedException();
162 >        catch(NullPointerException success) {
163          }
164      }
165  
166      /**
167 <     *
167 >     * A new newFixedThreadPool with 0 threads throws IAE
168       */
169 <    public void testInvokeCallable () {
169 >    public void testNewFixedThreadPool4() {
170          try {
171 <            Executor e = new DirectExecutor();
172 <            String result = Executors.invoke(e, new StringTask());
320 <
321 <            assertSame(TEST_STRING, result);
322 <        }
323 <        catch (ExecutionException ex) {
324 <            unexpectedException();
171 >            ExecutorService e = Executors.newFixedThreadPool(0);
172 >            shouldThrow();
173          }
174 <        catch (InterruptedException ex) {
327 <            unexpectedException();
174 >        catch(IllegalArgumentException success) {
175          }
176      }
177  
178 +
179      /**
180       *  timeouts from execute will time out if they compute too long.
181       */
# Line 369 | 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