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. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
|
10 |
|
import junit.framework.*; |
11 |
|
import java.util.*; |
12 |
|
import java.util.concurrent.*; |
13 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
|
import java.math.BigInteger; |
15 |
+ |
import java.security.*; |
16 |
|
|
17 |
< |
public class ExecutorsTest extends TestCase{ |
15 |
< |
|
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 |
|
} |
19 |
– |
|
20 |
– |
|
21 |
|
public static Test suite() { |
22 |
< |
return new TestSuite(ExecutorsTest.class); |
22 |
> |
return new TestSuite(ExecutorsTest.class); |
23 |
|
} |
24 |
|
|
25 |
< |
private static long SHORT_DELAY_MS = 100; |
26 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
27 |
< |
private static long LONG_DELAY_MS = 10000; |
28 |
< |
|
29 |
< |
class SleepRun implements Runnable { |
30 |
< |
public void run() { |
31 |
< |
try{ |
32 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
33 |
< |
} catch(InterruptedException e){ |
34 |
< |
fail("unexpected exception"); |
35 |
< |
} |
25 |
> |
static class TimedCallable<T> implements Callable<T> { |
26 |
> |
private final ExecutorService exec; |
27 |
> |
private final Callable<T> func; |
28 |
> |
private final long msecs; |
29 |
> |
|
30 |
> |
TimedCallable(ExecutorService exec, Callable<T> func, long msecs) { |
31 |
> |
this.exec = exec; |
32 |
> |
this.func = func; |
33 |
> |
this.msecs = msecs; |
34 |
|
} |
37 |
– |
} |
38 |
– |
|
35 |
|
|
36 |
< |
class SleepCall implements Callable { |
37 |
< |
public Object call(){ |
38 |
< |
try{ |
39 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
40 |
< |
}catch(InterruptedException e){ |
41 |
< |
fail("unexpected exception"); |
36 |
> |
public T call() throws Exception { |
37 |
> |
Future<T> ftask = exec.submit(func); |
38 |
> |
try { |
39 |
> |
return ftask.get(msecs, MILLISECONDS); |
40 |
> |
} finally { |
41 |
> |
ftask.cancel(true); |
42 |
|
} |
47 |
– |
return Boolean.TRUE; |
43 |
|
} |
44 |
|
} |
45 |
|
|
46 |
|
|
47 |
+ |
private static class Fib implements Callable<BigInteger> { |
48 |
+ |
private final BigInteger n; |
49 |
+ |
Fib(long n) { |
50 |
+ |
if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n); |
51 |
+ |
this.n = BigInteger.valueOf(n); |
52 |
+ |
} |
53 |
+ |
public BigInteger call() { |
54 |
+ |
BigInteger f1 = BigInteger.ONE; |
55 |
+ |
BigInteger f2 = f1; |
56 |
+ |
for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) { |
57 |
+ |
BigInteger t = f1.add(f2); |
58 |
+ |
f1 = f2; |
59 |
+ |
f2 = t; |
60 |
+ |
} |
61 |
+ |
return f1; |
62 |
+ |
} |
63 |
+ |
}; |
64 |
|
|
65 |
|
/** |
66 |
< |
* Test to verify execute(Executor, Runnable) will throw |
55 |
< |
* RejectedExecutionException Attempting to execute a runnable on |
56 |
< |
* a full ThreadPool will cause such an exception here, up to 5 |
57 |
< |
* runnables are attempted on a pool capable on handling one |
58 |
< |
* until it throws an exception |
66 |
> |
* A newCachedThreadPool can execute runnables |
67 |
|
*/ |
68 |
< |
public void testExecute1(){ |
69 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
70 |
< |
try{ |
71 |
< |
|
72 |
< |
for(int i = 0; i < 5; ++i){ |
73 |
< |
Executors.execute(p, new SleepRun(), Boolean.TRUE); |
66 |
< |
} |
67 |
< |
fail("should throw"); |
68 |
< |
} catch(RejectedExecutionException success){} |
69 |
< |
p.shutdownNow(); |
68 |
> |
public void testNewCachedThreadPool1() { |
69 |
> |
ExecutorService e = Executors.newCachedThreadPool(); |
70 |
> |
e.execute(new NoOpRunnable()); |
71 |
> |
e.execute(new NoOpRunnable()); |
72 |
> |
e.execute(new NoOpRunnable()); |
73 |
> |
joinPool(e); |
74 |
|
} |
75 |
|
|
76 |
|
/** |
77 |
< |
* Test to verify execute(Executor, Callable) will throw |
78 |
< |
* RejectedExecutionException Attempting to execute a callable on |
79 |
< |
* a full ThreadPool will cause such an exception here, up to 5 |
80 |
< |
* runnables are attempted on a pool capable on handling one |
81 |
< |
* until it throws an exception |
82 |
< |
*/ |
83 |
< |
public void testExecute2(){ |
84 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
81 |
< |
try{ |
82 |
< |
for(int i = 0; i < 5; ++i) { |
83 |
< |
Executors.execute(p, new SleepCall()); |
84 |
< |
} |
85 |
< |
fail("should throw"); |
86 |
< |
}catch(RejectedExecutionException e){} |
87 |
< |
p.shutdownNow(); |
77 |
> |
* A newCachedThreadPool with given ThreadFactory can execute runnables |
78 |
> |
*/ |
79 |
> |
public void testNewCachedThreadPool2() { |
80 |
> |
ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory()); |
81 |
> |
e.execute(new NoOpRunnable()); |
82 |
> |
e.execute(new NoOpRunnable()); |
83 |
> |
e.execute(new NoOpRunnable()); |
84 |
> |
joinPool(e); |
85 |
|
} |
86 |
|
|
90 |
– |
|
87 |
|
/** |
88 |
< |
* Test to verify invoke(Executor, Runnable) throws InterruptedException |
89 |
< |
* A single use of invoke starts that will wait long enough |
90 |
< |
* for the invoking thread to be interrupted |
91 |
< |
*/ |
92 |
< |
public void testInvoke2(){ |
93 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
94 |
< |
Thread t = new Thread(new Runnable() { |
99 |
< |
public void run(){ |
100 |
< |
try{ |
101 |
< |
Executors.invoke(p,new Runnable(){ |
102 |
< |
public void run(){ |
103 |
< |
try{ |
104 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
105 |
< |
fail("should throw"); |
106 |
< |
}catch(InterruptedException e){ |
107 |
< |
} |
108 |
< |
} |
109 |
< |
}); |
110 |
< |
} catch(InterruptedException success){ |
111 |
< |
} catch(Exception e) { |
112 |
< |
fail("unexpected exception"); |
113 |
< |
} |
114 |
< |
|
115 |
< |
} |
116 |
< |
}); |
117 |
< |
try{ |
118 |
< |
t.start(); |
119 |
< |
Thread.sleep(SHORT_DELAY_MS); |
120 |
< |
t.interrupt(); |
121 |
< |
}catch(Exception e){ |
122 |
< |
fail("unexpected exception"); |
123 |
< |
} |
124 |
< |
p.shutdownNow(); |
88 |
> |
* A newCachedThreadPool with null ThreadFactory throws NPE |
89 |
> |
*/ |
90 |
> |
public void testNewCachedThreadPool3() { |
91 |
> |
try { |
92 |
> |
ExecutorService e = Executors.newCachedThreadPool(null); |
93 |
> |
shouldThrow(); |
94 |
> |
} catch (NullPointerException success) {} |
95 |
|
} |
96 |
|
|
97 |
+ |
|
98 |
|
/** |
99 |
< |
* Test to verify invoke(Executor, Runnable) will throw |
129 |
< |
* ExecutionException An ExecutionException occurs when the |
130 |
< |
* underlying Runnable throws an exception, here the |
131 |
< |
* DivideByZeroException will cause an ExecutionException |
99 |
> |
* A new SingleThreadExecutor can execute runnables |
100 |
|
*/ |
101 |
< |
public void testInvoke3(){ |
102 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
103 |
< |
try{ |
104 |
< |
Runnable r = new Runnable(){ |
105 |
< |
public void run(){ |
106 |
< |
int i = 5/0; |
139 |
< |
} |
140 |
< |
}; |
141 |
< |
|
142 |
< |
for(int i =0; i < 5; i++){ |
143 |
< |
Executors.invoke(p,r); |
144 |
< |
} |
145 |
< |
|
146 |
< |
fail("should throw"); |
147 |
< |
} catch(ExecutionException success){ |
148 |
< |
} catch(Exception e){ |
149 |
< |
fail("should throw EE"); |
150 |
< |
} |
151 |
< |
p.shutdownNow(); |
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 |
> |
joinPool(e); |
107 |
|
} |
108 |
|
|
154 |
– |
|
155 |
– |
|
109 |
|
/** |
110 |
< |
* Test to verify invoke(Executor, Callable) throws |
111 |
< |
* InterruptedException A single use of invoke starts that will |
112 |
< |
* wait long enough for the invoking thread to be interrupted |
113 |
< |
*/ |
114 |
< |
public void testInvoke5(){ |
115 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
116 |
< |
|
117 |
< |
final Callable c = new Callable(){ |
165 |
< |
public Object call(){ |
166 |
< |
try{ |
167 |
< |
Executors.invoke(p, new SleepCall()); |
168 |
< |
fail("should throw"); |
169 |
< |
}catch(InterruptedException e){} |
170 |
< |
catch(RejectedExecutionException e2){} |
171 |
< |
catch(ExecutionException e3){} |
172 |
< |
return Boolean.TRUE; |
173 |
< |
} |
174 |
< |
}; |
175 |
< |
|
176 |
< |
|
177 |
< |
|
178 |
< |
Thread t = new Thread(new Runnable(){ |
179 |
< |
public void run(){ |
180 |
< |
try{ |
181 |
< |
c.call(); |
182 |
< |
}catch(Exception e){} |
183 |
< |
} |
184 |
< |
}); |
185 |
< |
try{ |
186 |
< |
t.start(); |
187 |
< |
Thread.sleep(SHORT_DELAY_MS); |
188 |
< |
t.interrupt(); |
189 |
< |
t.join(); |
190 |
< |
}catch(InterruptedException e){ |
191 |
< |
fail("unexpected exception"); |
192 |
< |
} |
193 |
< |
|
194 |
< |
p.shutdownNow(); |
110 |
> |
* A new SingleThreadExecutor with given ThreadFactory can execute runnables |
111 |
> |
*/ |
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 |
> |
joinPool(e); |
118 |
|
} |
119 |
|
|
120 |
|
/** |
121 |
< |
* Test to verify invoke(Executor, Callable) will throw ExecutionException |
199 |
< |
* An ExecutionException occurs when the underlying Runnable throws |
200 |
< |
* an exception, here the DivideByZeroException will cause an ExecutionException |
121 |
> |
* A new SingleThreadExecutor with null ThreadFactory throws NPE |
122 |
|
*/ |
123 |
< |
public void testInvoke6(){ |
124 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
125 |
< |
|
126 |
< |
try{ |
127 |
< |
Callable c = new Callable(){ |
207 |
< |
public Object call(){ |
208 |
< |
int i = 5/0; |
209 |
< |
return Boolean.TRUE; |
210 |
< |
} |
211 |
< |
}; |
212 |
< |
|
213 |
< |
for(int i =0; i < 5; i++){ |
214 |
< |
Executors.invoke(p,c); |
215 |
< |
} |
216 |
< |
|
217 |
< |
fail("should throw"); |
218 |
< |
}catch(RejectedExecutionException e){} |
219 |
< |
catch(InterruptedException e2){} |
220 |
< |
catch(ExecutionException e3){} |
221 |
< |
p.shutdownNow(); |
123 |
> |
public void testNewSingleThreadExecutor3() { |
124 |
> |
try { |
125 |
> |
ExecutorService e = Executors.newSingleThreadExecutor(null); |
126 |
> |
shouldThrow(); |
127 |
> |
} catch (NullPointerException success) {} |
128 |
|
} |
129 |
|
|
130 |
< |
public void testExecuteRunnable () { |
130 |
> |
/** |
131 |
> |
* A new SingleThreadExecutor cannot be casted to concrete implementation |
132 |
> |
*/ |
133 |
> |
public void testCastNewSingleThreadExecutor() { |
134 |
> |
ExecutorService e = Executors.newSingleThreadExecutor(); |
135 |
|
try { |
136 |
< |
Executor e = new DirectExecutor(); |
137 |
< |
Task task = new Task(); |
136 |
> |
ThreadPoolExecutor tpe = (ThreadPoolExecutor)e; |
137 |
> |
shouldThrow(); |
138 |
> |
} catch (ClassCastException success) { |
139 |
> |
} finally { |
140 |
> |
joinPool(e); |
141 |
> |
} |
142 |
> |
} |
143 |
|
|
229 |
– |
assertFalse("task should not be complete", task.isCompleted()); |
144 |
|
|
145 |
< |
Future<String> future = Executors.execute(e, task, TEST_STRING); |
146 |
< |
String result = future.get(); |
145 |
> |
/** |
146 |
> |
* A new newFixedThreadPool can execute runnables |
147 |
> |
*/ |
148 |
> |
public void testNewFixedThreadPool1() { |
149 |
> |
ExecutorService e = Executors.newFixedThreadPool(2); |
150 |
> |
e.execute(new NoOpRunnable()); |
151 |
> |
e.execute(new NoOpRunnable()); |
152 |
> |
e.execute(new NoOpRunnable()); |
153 |
> |
joinPool(e); |
154 |
> |
} |
155 |
|
|
156 |
< |
assertTrue("task should be complete", task.isCompleted()); |
157 |
< |
assertSame("should return test string", TEST_STRING, result); |
158 |
< |
} |
159 |
< |
catch (ExecutionException ex) { |
160 |
< |
fail("Unexpected exception"); |
161 |
< |
} |
162 |
< |
catch (InterruptedException ex) { |
163 |
< |
fail("Unexpected exception"); |
164 |
< |
} |
156 |
> |
/** |
157 |
> |
* A new newFixedThreadPool with given ThreadFactory can execute runnables |
158 |
> |
*/ |
159 |
> |
public void testNewFixedThreadPool2() { |
160 |
> |
ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory()); |
161 |
> |
e.execute(new NoOpRunnable()); |
162 |
> |
e.execute(new NoOpRunnable()); |
163 |
> |
e.execute(new NoOpRunnable()); |
164 |
> |
joinPool(e); |
165 |
|
} |
166 |
|
|
167 |
< |
public void testInvokeRunnable () { |
167 |
> |
/** |
168 |
> |
* A new newFixedThreadPool with null ThreadFactory throws NPE |
169 |
> |
*/ |
170 |
> |
public void testNewFixedThreadPool3() { |
171 |
|
try { |
172 |
< |
Executor e = new DirectExecutor(); |
173 |
< |
Task task = new Task(); |
172 |
> |
ExecutorService e = Executors.newFixedThreadPool(2, null); |
173 |
> |
shouldThrow(); |
174 |
> |
} catch (NullPointerException success) {} |
175 |
> |
} |
176 |
|
|
177 |
< |
assertFalse("task should not be complete", task.isCompleted()); |
177 |
> |
/** |
178 |
> |
* A new newFixedThreadPool with 0 threads throws IAE |
179 |
> |
*/ |
180 |
> |
public void testNewFixedThreadPool4() { |
181 |
> |
try { |
182 |
> |
ExecutorService e = Executors.newFixedThreadPool(0); |
183 |
> |
shouldThrow(); |
184 |
> |
} catch (IllegalArgumentException success) {} |
185 |
> |
} |
186 |
|
|
252 |
– |
Executors.invoke(e, task); |
187 |
|
|
188 |
< |
assertTrue("task should be complete", task.isCompleted()); |
189 |
< |
} |
190 |
< |
catch (ExecutionException ex) { |
191 |
< |
fail("Unexpected exception"); |
192 |
< |
} |
193 |
< |
catch (InterruptedException ex) { |
194 |
< |
fail("Unexpected exception"); |
195 |
< |
} |
188 |
> |
/** |
189 |
> |
* An unconfigurable newFixedThreadPool can execute runnables |
190 |
> |
*/ |
191 |
> |
public void testunconfigurableExecutorService() { |
192 |
> |
ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2)); |
193 |
> |
e.execute(new NoOpRunnable()); |
194 |
> |
e.execute(new NoOpRunnable()); |
195 |
> |
e.execute(new NoOpRunnable()); |
196 |
> |
joinPool(e); |
197 |
|
} |
198 |
|
|
199 |
< |
public void testExecuteCallable () { |
199 |
> |
/** |
200 |
> |
* unconfigurableExecutorService(null) throws NPE |
201 |
> |
*/ |
202 |
> |
public void testunconfigurableExecutorServiceNPE() { |
203 |
|
try { |
204 |
< |
Executor e = new DirectExecutor(); |
205 |
< |
Future<String> future = Executors.execute(e, new StringTask()); |
206 |
< |
String result = future.get(); |
269 |
< |
|
270 |
< |
assertSame("should return test string", TEST_STRING, result); |
271 |
< |
} |
272 |
< |
catch (ExecutionException ex) { |
273 |
< |
fail("Unexpected exception"); |
274 |
< |
} |
275 |
< |
catch (InterruptedException ex) { |
276 |
< |
fail("Unexpected exception"); |
277 |
< |
} |
204 |
> |
ExecutorService e = Executors.unconfigurableExecutorService(null); |
205 |
> |
shouldThrow(); |
206 |
> |
} catch (NullPointerException success) {} |
207 |
|
} |
208 |
|
|
209 |
< |
public void testInvokeCallable () { |
209 |
> |
/** |
210 |
> |
* unconfigurableScheduledExecutorService(null) throws NPE |
211 |
> |
*/ |
212 |
> |
public void testunconfigurableScheduledExecutorServiceNPE() { |
213 |
|
try { |
214 |
< |
Executor e = new DirectExecutor(); |
215 |
< |
String result = Executors.invoke(e, new StringTask()); |
216 |
< |
|
285 |
< |
assertSame("should return test string", TEST_STRING, result); |
286 |
< |
} |
287 |
< |
catch (ExecutionException ex) { |
288 |
< |
fail("Unexpected exception" ); |
289 |
< |
} |
290 |
< |
catch (InterruptedException ex) { |
291 |
< |
fail("Unexpected exception"); |
292 |
< |
} |
214 |
> |
ExecutorService e = Executors.unconfigurableScheduledExecutorService(null); |
215 |
> |
shouldThrow(); |
216 |
> |
} catch (NullPointerException success) {} |
217 |
|
} |
218 |
|
|
295 |
– |
private static final String TEST_STRING = "a test string"; |
219 |
|
|
220 |
< |
private static class Task implements Runnable { |
221 |
< |
public void run() { completed = true; } |
222 |
< |
public boolean isCompleted() { return completed; } |
223 |
< |
public void reset() { completed = false; } |
224 |
< |
private boolean completed = false; |
220 |
> |
/** |
221 |
> |
* a newSingleThreadScheduledExecutor successfully runs delayed task |
222 |
> |
*/ |
223 |
> |
public void testNewSingleThreadScheduledExecutor() throws Exception { |
224 |
> |
TrackedCallable callable = new TrackedCallable(); |
225 |
> |
ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor(); |
226 |
> |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
227 |
> |
assertFalse(callable.done); |
228 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
229 |
> |
assertTrue(callable.done); |
230 |
> |
assertEquals(Boolean.TRUE, f.get()); |
231 |
> |
joinPool(p1); |
232 |
|
} |
233 |
|
|
234 |
< |
private static class StringTask implements Callable<String> { |
235 |
< |
public String call() { return TEST_STRING; } |
234 |
> |
/** |
235 |
> |
* a newScheduledThreadPool successfully runs delayed task |
236 |
> |
*/ |
237 |
> |
public void testnewScheduledThreadPool() throws Exception { |
238 |
> |
TrackedCallable callable = new TrackedCallable(); |
239 |
> |
ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2); |
240 |
> |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
241 |
> |
assertFalse(callable.done); |
242 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
243 |
> |
assertTrue(callable.done); |
244 |
> |
assertEquals(Boolean.TRUE, f.get()); |
245 |
> |
joinPool(p1); |
246 |
|
} |
247 |
|
|
248 |
< |
static class DirectExecutor implements Executor { |
249 |
< |
public void execute(Runnable r) { |
250 |
< |
r.run(); |
251 |
< |
} |
248 |
> |
/** |
249 |
> |
* an unconfigurable newScheduledThreadPool successfully runs delayed task |
250 |
> |
*/ |
251 |
> |
public void testunconfigurableScheduledExecutorService() throws Exception { |
252 |
> |
TrackedCallable callable = new TrackedCallable(); |
253 |
> |
ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2)); |
254 |
> |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
255 |
> |
assertFalse(callable.done); |
256 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
257 |
> |
assertTrue(callable.done); |
258 |
> |
assertEquals(Boolean.TRUE, f.get()); |
259 |
> |
joinPool(p1); |
260 |
|
} |
261 |
|
|
262 |
|
/** |
263 |
< |
* Check that timeouts from execute will time out if they compute |
316 |
< |
* too long. |
263 |
> |
* timeouts from execute will time out if they compute too long. |
264 |
|
*/ |
265 |
< |
|
319 |
< |
public void testTimedCallable() { |
265 |
> |
public void testTimedCallable() throws Exception { |
266 |
|
int N = 10000; |
267 |
|
ExecutorService executor = Executors.newSingleThreadExecutor(); |
268 |
|
List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N); |
269 |
|
try { |
270 |
|
long startTime = System.currentTimeMillis(); |
271 |
< |
|
271 |
> |
|
272 |
|
long i = 0; |
273 |
|
while (tasks.size() < N) { |
274 |
|
tasks.add(new TimedCallable<BigInteger>(executor, new Fib(i), 1)); |
275 |
|
i += 10; |
276 |
|
} |
277 |
< |
|
277 |
> |
|
278 |
|
int iters = 0; |
279 |
|
BigInteger sum = BigInteger.ZERO; |
280 |
|
for (Iterator<Callable<BigInteger>> it = tasks.iterator(); it.hasNext();) { |
286 |
|
assertTrue(iters > 0); |
287 |
|
return; |
288 |
|
} |
343 |
– |
catch (Exception e) { |
344 |
– |
fail("unexpected exception: " + e); |
345 |
– |
} |
289 |
|
} |
290 |
|
// if by chance we didn't ever time out, total time must be small |
291 |
|
long elapsed = System.currentTimeMillis() - startTime; |
292 |
|
assertTrue(elapsed < N); |
293 |
|
} |
294 |
|
finally { |
295 |
< |
executor.shutdownNow(); |
295 |
> |
joinPool(executor); |
296 |
|
} |
297 |
|
} |
298 |
|
|
299 |
< |
|
300 |
< |
static class TimedCallable<T> implements Callable<T> { |
301 |
< |
private final Executor exec; |
302 |
< |
private final Callable<T> func; |
303 |
< |
private final long msecs; |
304 |
< |
|
305 |
< |
TimedCallable(Executor exec, Callable<T> func, long msecs) { |
306 |
< |
this.exec = exec; |
307 |
< |
this.func = func; |
308 |
< |
this.msecs = msecs; |
299 |
> |
|
300 |
> |
/** |
301 |
> |
* ThreadPoolExecutor using defaultThreadFactory has |
302 |
> |
* specified group, priority, daemon status, and name |
303 |
> |
*/ |
304 |
> |
public void testDefaultThreadFactory() throws Exception { |
305 |
> |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
306 |
> |
Runnable r = new Runnable() { |
307 |
> |
public void run() { |
308 |
> |
try { |
309 |
> |
Thread current = Thread.currentThread(); |
310 |
> |
threadAssertTrue(!current.isDaemon()); |
311 |
> |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
312 |
> |
ThreadGroup g = current.getThreadGroup(); |
313 |
> |
SecurityManager s = System.getSecurityManager(); |
314 |
> |
if (s != null) |
315 |
> |
threadAssertTrue(g == s.getThreadGroup()); |
316 |
> |
else |
317 |
> |
threadAssertTrue(g == egroup); |
318 |
> |
String name = current.getName(); |
319 |
> |
threadAssertTrue(name.endsWith("thread-1")); |
320 |
> |
} catch (SecurityException ok) { |
321 |
> |
// Also pass if not allowed to change setting |
322 |
> |
} |
323 |
> |
} |
324 |
> |
}; |
325 |
> |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); |
326 |
> |
|
327 |
> |
e.execute(r); |
328 |
> |
try { |
329 |
> |
e.shutdown(); |
330 |
> |
} catch (SecurityException ok) { |
331 |
|
} |
332 |
< |
|
333 |
< |
public T call() throws Exception { |
334 |
< |
Future<T> ftask = Executors.execute(exec, func); |
335 |
< |
try { |
336 |
< |
return ftask.get(msecs, TimeUnit.MILLISECONDS); |
372 |
< |
} finally { |
373 |
< |
ftask.cancel(true); |
374 |
< |
} |
332 |
> |
|
333 |
> |
try { |
334 |
> |
Thread.sleep(SHORT_DELAY_MS); |
335 |
> |
} finally { |
336 |
> |
joinPool(e); |
337 |
|
} |
338 |
|
} |
339 |
|
|
340 |
+ |
/** |
341 |
+ |
* ThreadPoolExecutor using privilegedThreadFactory has |
342 |
+ |
* specified group, priority, daemon status, name, |
343 |
+ |
* access control context and context class loader |
344 |
+ |
*/ |
345 |
+ |
public void testPrivilegedThreadFactory() throws Exception { |
346 |
+ |
Policy savedPolicy = null; |
347 |
+ |
try { |
348 |
+ |
savedPolicy = Policy.getPolicy(); |
349 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(); |
350 |
+ |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
351 |
+ |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
352 |
+ |
Policy.setPolicy(policy); |
353 |
+ |
} catch (AccessControlException ok) { |
354 |
+ |
return; |
355 |
+ |
} |
356 |
+ |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
357 |
+ |
final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); |
358 |
+ |
final AccessControlContext thisacc = AccessController.getContext(); |
359 |
+ |
Runnable r = new Runnable() { |
360 |
+ |
public void run() { |
361 |
+ |
try { |
362 |
+ |
Thread current = Thread.currentThread(); |
363 |
+ |
threadAssertTrue(!current.isDaemon()); |
364 |
+ |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
365 |
+ |
ThreadGroup g = current.getThreadGroup(); |
366 |
+ |
SecurityManager s = System.getSecurityManager(); |
367 |
+ |
if (s != null) |
368 |
+ |
threadAssertTrue(g == s.getThreadGroup()); |
369 |
+ |
else |
370 |
+ |
threadAssertTrue(g == egroup); |
371 |
+ |
String name = current.getName(); |
372 |
+ |
threadAssertTrue(name.endsWith("thread-1")); |
373 |
+ |
threadAssertTrue(thisccl == current.getContextClassLoader()); |
374 |
+ |
threadAssertTrue(thisacc.equals(AccessController.getContext())); |
375 |
+ |
} catch (SecurityException ok) { |
376 |
+ |
// Also pass if not allowed to change settings |
377 |
+ |
} |
378 |
+ |
} |
379 |
+ |
}; |
380 |
+ |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
381 |
|
|
382 |
< |
private static class Fib implements Callable<BigInteger> { |
383 |
< |
private final BigInteger n; |
384 |
< |
Fib(long n) { |
385 |
< |
if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n); |
386 |
< |
this.n = BigInteger.valueOf(n); |
382 |
> |
Policy.setPolicy(savedPolicy); |
383 |
> |
e.execute(r); |
384 |
> |
try { |
385 |
> |
e.shutdown(); |
386 |
> |
} catch (SecurityException ok) { |
387 |
|
} |
388 |
< |
public BigInteger call() { |
389 |
< |
BigInteger f1 = BigInteger.ONE; |
390 |
< |
BigInteger f2 = f1; |
391 |
< |
for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) { |
392 |
< |
BigInteger t = f1.add(f2); |
393 |
< |
f1 = f2; |
394 |
< |
f2 = t; |
395 |
< |
} |
396 |
< |
return f1; |
388 |
> |
try { |
389 |
> |
Thread.sleep(SHORT_DELAY_MS); |
390 |
> |
} finally { |
391 |
> |
joinPool(e); |
392 |
> |
} |
393 |
> |
} |
394 |
> |
|
395 |
> |
void checkCCL() { |
396 |
> |
SecurityManager sm = System.getSecurityManager(); |
397 |
> |
if (sm != null) { |
398 |
> |
sm.checkPermission(new RuntimePermission("setContextClassLoader")); |
399 |
> |
sm.checkPermission(new RuntimePermission("getClassLoader")); |
400 |
> |
} |
401 |
> |
} |
402 |
> |
|
403 |
> |
class CheckCCL implements Callable<Object> { |
404 |
> |
public Object call() { |
405 |
> |
checkCCL(); |
406 |
> |
return null; |
407 |
> |
} |
408 |
> |
} |
409 |
> |
|
410 |
> |
|
411 |
> |
/** |
412 |
> |
* Without class loader permissions, creating |
413 |
> |
* privilegedCallableUsingCurrentClassLoader throws ACE |
414 |
> |
*/ |
415 |
> |
public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() { |
416 |
> |
Policy savedPolicy = null; |
417 |
> |
try { |
418 |
> |
savedPolicy = Policy.getPolicy(); |
419 |
> |
AdjustablePolicy policy = new AdjustablePolicy(); |
420 |
> |
Policy.setPolicy(policy); |
421 |
> |
} catch (AccessControlException ok) { |
422 |
> |
return; |
423 |
|
} |
395 |
– |
}; |
424 |
|
|
425 |
+ |
// Check if program still has too many permissions to run test |
426 |
+ |
try { |
427 |
+ |
checkCCL(); |
428 |
+ |
// too many privileges to test; so return |
429 |
+ |
Policy.setPolicy(savedPolicy); |
430 |
+ |
return; |
431 |
+ |
} catch (AccessControlException ok) { |
432 |
+ |
} |
433 |
+ |
|
434 |
+ |
try { |
435 |
+ |
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
436 |
+ |
shouldThrow(); |
437 |
+ |
} catch (AccessControlException success) { |
438 |
+ |
} finally { |
439 |
+ |
Policy.setPolicy(savedPolicy); |
440 |
+ |
} |
441 |
+ |
} |
442 |
+ |
|
443 |
+ |
/** |
444 |
+ |
* With class loader permissions, calling |
445 |
+ |
* privilegedCallableUsingCurrentClassLoader does not throw ACE |
446 |
+ |
*/ |
447 |
+ |
public void testprivilegedCallableUsingCCLWithPrivs() throws Exception { |
448 |
+ |
Policy savedPolicy = null; |
449 |
+ |
try { |
450 |
+ |
savedPolicy = Policy.getPolicy(); |
451 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(); |
452 |
+ |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
453 |
+ |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
454 |
+ |
Policy.setPolicy(policy); |
455 |
+ |
} catch (AccessControlException ok) { |
456 |
+ |
return; |
457 |
+ |
} |
458 |
+ |
|
459 |
+ |
try { |
460 |
+ |
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
461 |
+ |
task.call(); |
462 |
+ |
} |
463 |
+ |
finally { |
464 |
+ |
Policy.setPolicy(savedPolicy); |
465 |
+ |
} |
466 |
+ |
} |
467 |
+ |
|
468 |
+ |
/** |
469 |
+ |
* Without permissions, calling privilegedCallable throws ACE |
470 |
+ |
*/ |
471 |
+ |
public void testprivilegedCallableWithNoPrivs() throws Exception { |
472 |
+ |
Callable task; |
473 |
+ |
Policy savedPolicy = null; |
474 |
+ |
AdjustablePolicy policy = null; |
475 |
+ |
AccessControlContext noprivAcc = null; |
476 |
+ |
try { |
477 |
+ |
savedPolicy = Policy.getPolicy(); |
478 |
+ |
policy = new AdjustablePolicy(); |
479 |
+ |
Policy.setPolicy(policy); |
480 |
+ |
noprivAcc = AccessController.getContext(); |
481 |
+ |
task = Executors.privilegedCallable(new CheckCCL()); |
482 |
+ |
Policy.setPolicy(savedPolicy); |
483 |
+ |
} catch (AccessControlException ok) { |
484 |
+ |
return; // program has too few permissions to set up test |
485 |
+ |
} |
486 |
+ |
|
487 |
+ |
// Make sure that program doesn't have too many permissions |
488 |
+ |
try { |
489 |
+ |
AccessController.doPrivileged(new PrivilegedAction() { |
490 |
+ |
public Object run() { |
491 |
+ |
checkCCL(); |
492 |
+ |
return null; |
493 |
+ |
}}, noprivAcc); |
494 |
+ |
// too many permssions; skip test |
495 |
+ |
return; |
496 |
+ |
} catch (AccessControlException ok) { |
497 |
+ |
} |
498 |
+ |
|
499 |
+ |
try { |
500 |
+ |
task.call(); |
501 |
+ |
shouldThrow(); |
502 |
+ |
} catch (AccessControlException success) {} |
503 |
+ |
} |
504 |
+ |
|
505 |
+ |
/** |
506 |
+ |
* With permissions, calling privilegedCallable succeeds |
507 |
+ |
*/ |
508 |
+ |
public void testprivilegedCallableWithPrivs() throws Exception { |
509 |
+ |
Policy savedPolicy = null; |
510 |
+ |
try { |
511 |
+ |
savedPolicy = Policy.getPolicy(); |
512 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(); |
513 |
+ |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
514 |
+ |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
515 |
+ |
Policy.setPolicy(policy); |
516 |
+ |
} catch (AccessControlException ok) { |
517 |
+ |
return; |
518 |
+ |
} |
519 |
+ |
|
520 |
+ |
Callable task = Executors.privilegedCallable(new CheckCCL()); |
521 |
+ |
try { |
522 |
+ |
task.call(); |
523 |
+ |
} finally { |
524 |
+ |
Policy.setPolicy(savedPolicy); |
525 |
+ |
} |
526 |
+ |
} |
527 |
+ |
|
528 |
+ |
/** |
529 |
+ |
* callable(Runnable) returns null when called |
530 |
+ |
*/ |
531 |
+ |
public void testCallable1() throws Exception { |
532 |
+ |
Callable c = Executors.callable(new NoOpRunnable()); |
533 |
+ |
assertNull(c.call()); |
534 |
+ |
} |
535 |
+ |
|
536 |
+ |
/** |
537 |
+ |
* callable(Runnable, result) returns result when called |
538 |
+ |
*/ |
539 |
+ |
public void testCallable2() throws Exception { |
540 |
+ |
Callable c = Executors.callable(new NoOpRunnable(), one); |
541 |
+ |
assertSame(one, c.call()); |
542 |
+ |
} |
543 |
+ |
|
544 |
+ |
/** |
545 |
+ |
* callable(PrivilegedAction) returns its result when called |
546 |
+ |
*/ |
547 |
+ |
public void testCallable3() throws Exception { |
548 |
+ |
Callable c = Executors.callable(new PrivilegedAction() { |
549 |
+ |
public Object run() { return one; }}); |
550 |
+ |
assertSame(one, c.call()); |
551 |
+ |
} |
552 |
+ |
|
553 |
+ |
/** |
554 |
+ |
* callable(PrivilegedExceptionAction) returns its result when called |
555 |
+ |
*/ |
556 |
+ |
public void testCallable4() throws Exception { |
557 |
+ |
Callable c = Executors.callable(new PrivilegedExceptionAction() { |
558 |
+ |
public Object run() { return one; }}); |
559 |
+ |
assertSame(one, c.call()); |
560 |
+ |
} |
561 |
+ |
|
562 |
+ |
|
563 |
+ |
/** |
564 |
+ |
* callable(null Runnable) throws NPE |
565 |
+ |
*/ |
566 |
+ |
public void testCallableNPE1() { |
567 |
+ |
try { |
568 |
+ |
Callable c = Executors.callable((Runnable) null); |
569 |
+ |
shouldThrow(); |
570 |
+ |
} catch (NullPointerException success) {} |
571 |
+ |
} |
572 |
+ |
|
573 |
+ |
/** |
574 |
+ |
* callable(null, result) throws NPE |
575 |
+ |
*/ |
576 |
+ |
public void testCallableNPE2() { |
577 |
+ |
try { |
578 |
+ |
Callable c = Executors.callable((Runnable) null, one); |
579 |
+ |
shouldThrow(); |
580 |
+ |
} catch (NullPointerException success) {} |
581 |
+ |
} |
582 |
+ |
|
583 |
+ |
/** |
584 |
+ |
* callable(null PrivilegedAction) throws NPE |
585 |
+ |
*/ |
586 |
+ |
public void testCallableNPE3() { |
587 |
+ |
try { |
588 |
+ |
Callable c = Executors.callable((PrivilegedAction) null); |
589 |
+ |
shouldThrow(); |
590 |
+ |
} catch (NullPointerException success) {} |
591 |
+ |
} |
592 |
+ |
|
593 |
+ |
/** |
594 |
+ |
* callable(null PrivilegedExceptionAction) throws NPE |
595 |
+ |
*/ |
596 |
+ |
public void testCallableNPE4() { |
597 |
+ |
try { |
598 |
+ |
Callable c = Executors.callable((PrivilegedExceptionAction) null); |
599 |
+ |
shouldThrow(); |
600 |
+ |
} catch (NullPointerException success) {} |
601 |
+ |
} |
602 |
|
|
603 |
|
|
604 |
|
} |