ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExecutorsTest.java
Revision: 1.10
Committed: Wed Dec 10 01:51:12 2003 UTC (20 years, 4 months ago) by tim
Branch: MAIN
CVS Tags: JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.9: +3 -428 lines
Log Message:
Move and rename static Executors.execute/invoke to ExecutorService.submit/invoke,
providing implementations in AbstractExecutorService (which TPE extends).

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by members of JCP JSR-166 Expert Group and released to the
3     * public domain. Use, modify, and redistribute this code in any way
4     * without acknowledgement. Other contributors include Andrew Wright,
5     * Jeffrey Hayes, Pat Fischer, Mike Judd.
6     */
7    
8    
9     import junit.framework.*;
10     import java.util.*;
11     import java.util.concurrent.*;
12 dl 1.2 import java.math.BigInteger;
13 dl 1.8 import java.security.*;
14 dl 1.1
15 dl 1.3 public class ExecutorsTest extends JSR166TestCase{
16 dl 1.1 public static void main(String[] args) {
17 tim 1.9 junit.textui.TestRunner.run (suite());
18 dl 1.1 }
19     public static Test suite() {
20 tim 1.9 return new TestSuite(ExecutorsTest.class);
21 dl 1.1 }
22    
23 dl 1.4 static class TimedCallable<T> implements Callable<T> {
24 tim 1.10 private final ExecutorService exec;
25 dl 1.4 private final Callable<T> func;
26     private final long msecs;
27    
28 tim 1.10 TimedCallable(ExecutorService exec, Callable<T> func, long msecs) {
29 dl 1.4 this.exec = exec;
30     this.func = func;
31     this.msecs = msecs;
32     }
33    
34     public T call() throws Exception {
35 tim 1.10 Future<T> ftask = exec.submit(func);
36 dl 1.4 try {
37     return ftask.get(msecs, TimeUnit.MILLISECONDS);
38     } finally {
39     ftask.cancel(true);
40     }
41     }
42     }
43    
44    
45     private static class Fib implements Callable<BigInteger> {
46     private final BigInteger n;
47     Fib(long n) {
48     if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n);
49     this.n = BigInteger.valueOf(n);
50     }
51     public BigInteger call() {
52     BigInteger f1 = BigInteger.ONE;
53     BigInteger f2 = f1;
54     for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) {
55     BigInteger t = f1.add(f2);
56     f1 = f2;
57     f2 = t;
58     }
59     return f1;
60     }
61     };
62    
63 dl 1.5 /**
64     * A newCachedThreadPool can execute runnables
65     */
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     * A newCachedThreadPool with given ThreadFactory can execute runnables
76     */
77     public void testNewCachedThreadPool2() {
78 dl 1.6 ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory());
79 dl 1.5 e.execute(new NoOpRunnable());
80     e.execute(new NoOpRunnable());
81     e.execute(new NoOpRunnable());
82     e.shutdown();
83     }
84    
85     /**
86     * A newCachedThreadPool with null ThreadFactory throws NPE
87     */
88     public void testNewCachedThreadPool3() {
89     try {
90     ExecutorService e = Executors.newCachedThreadPool(null);
91     shouldThrow();
92     }
93     catch(NullPointerException success) {
94     }
95     }
96    
97    
98     /**
99     * A new SingleThreadExecutor can execute runnables
100     */
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    
109     /**
110     * A new SingleThreadExecutor with given ThreadFactory can execute runnables
111     */
112     public void testNewSingleThreadExecutor2() {
113 dl 1.6 ExecutorService e = Executors.newSingleThreadExecutor(new SimpleThreadFactory());
114 dl 1.5 e.execute(new NoOpRunnable());
115     e.execute(new NoOpRunnable());
116     e.execute(new NoOpRunnable());
117     e.shutdown();
118     }
119    
120     /**
121     * A new SingleThreadExecutor with null ThreadFactory throws NPE
122     */
123     public void testNewSingleThreadExecutor3() {
124     try {
125     ExecutorService e = Executors.newSingleThreadExecutor(null);
126     shouldThrow();
127     }
128     catch(NullPointerException success) {
129     }
130     }
131    
132     /**
133     * A new newFixedThreadPool can execute runnables
134     */
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     * A new newFixedThreadPool with given ThreadFactory can execute runnables
145     */
146     public void testNewFixedThreadPool2() {
147 dl 1.6 ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory());
148 dl 1.5 e.execute(new NoOpRunnable());
149     e.execute(new NoOpRunnable());
150     e.execute(new NoOpRunnable());
151     e.shutdown();
152     }
153    
154     /**
155     * A new newFixedThreadPool with null ThreadFactory throws NPE
156     */
157     public void testNewFixedThreadPool3() {
158     try {
159     ExecutorService e = Executors.newFixedThreadPool(2, null);
160     shouldThrow();
161     }
162     catch(NullPointerException success) {
163     }
164     }
165    
166     /**
167     * A new newFixedThreadPool with 0 threads throws IAE
168     */
169     public void testNewFixedThreadPool4() {
170     try {
171     ExecutorService e = Executors.newFixedThreadPool(0);
172     shouldThrow();
173     }
174     catch(IllegalArgumentException success) {
175     }
176     }
177 dl 1.1
178 dl 1.2
179     /**
180 dl 1.4 * timeouts from execute will time out if they compute too long.
181 dl 1.2 */
182     public void testTimedCallable() {
183     int N = 10000;
184     ExecutorService executor = Executors.newSingleThreadExecutor();
185     List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N);
186     try {
187     long startTime = System.currentTimeMillis();
188    
189     long i = 0;
190     while (tasks.size() < N) {
191     tasks.add(new TimedCallable<BigInteger>(executor, new Fib(i), 1));
192     i += 10;
193     }
194    
195     int iters = 0;
196     BigInteger sum = BigInteger.ZERO;
197     for (Iterator<Callable<BigInteger>> it = tasks.iterator(); it.hasNext();) {
198     try {
199     ++iters;
200     sum = sum.add(it.next().call());
201     }
202     catch (TimeoutException success) {
203     assertTrue(iters > 0);
204     return;
205     }
206     catch (Exception e) {
207 dl 1.4 unexpectedException();
208 dl 1.2 }
209     }
210     // if by chance we didn't ever time out, total time must be small
211     long elapsed = System.currentTimeMillis() - startTime;
212     assertTrue(elapsed < N);
213     }
214     finally {
215 dl 1.3 joinPool(executor);
216 dl 1.2 }
217     }
218    
219    
220 dl 1.8 /**
221     * ThreadPoolExecutor using defaultThreadFactory has
222     * specified group, priority, daemon status, and name
223     */
224     public void testDefaultThreadFactory() {
225 tim 1.9 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 dl 1.8 }
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 tim 1.9 Policy savedPolicy = Policy.getPolicy();
261 dl 1.8 AdjustablePolicy policy = new AdjustablePolicy();
262     policy.addPermission(new RuntimePermission("getContextClassLoader"));
263     policy.addPermission(new RuntimePermission("setContextClassLoader"));
264 tim 1.9 Policy.setPolicy(policy);
265     final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
266     final ClassLoader thisccl = Thread.currentThread().getContextClassLoader();
267 dl 1.8 final AccessControlContext thisacc = AccessController.getContext();
268 tim 1.9 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 dl 1.1
298 dl 1.8 }
299 dl 1.1
300     }